/*
* $Header: /home/cvs/jakarta-slide/src/contrib/webdavgui/src/java/org/apache/webdav/ui/LoginFrame.java,v 1.2 2001/07/17 04:00:45 msmith Exp $
* $Revision: 1.2 $
* $Date: 2001/07/17 04:00:45 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.webdav.ui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.Keymap;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.webdav.lib.*;
import org.apache.webdav.ui.SPWebdavClient;
import org.apache.webdav.ui.lib.methods.SPHeadMethod;
import org.apache.webdav.ui.filechooser.SPFileChooser;
import org.apache.commons.httpclient.Credentials;
/**
* Title: LoginFrame.java
* Description: A Login Dialog box
*
* Company: SpeedLegal Holdings Inc.
* @author Jojada J. Tirtowidjojo
* @version 1.0
*/
public class LoginFrame extends JDialog {
JPanel contentPane;
BorderLayout contentPaneLayout = new BorderLayout();
JPanel SessionPane = new JPanel();
GridBagLayout sessionPaneLayout = new GridBagLayout();
JPanel emptyPanel1 = new JPanel();
JLabel hostLabel = new JLabel();
JTextField hostField = new JTextField();
JLabel portLabel = new JLabel();
JTextField portField = new JTextField();
JLabel pathLabel = new JLabel();
JTextField pathField = new JTextField();
JPanel emptyPanel2 = new JPanel();
JLabel userLabel = new JLabel();
JTextField userField = new JTextField();
JLabel passwdLabel = new JLabel();
JPasswordField passwdField = new JPasswordField();
JButton resetButton = new JButton();
JButton okButton = new JButton();
JPanel emptyPanel4 = new JPanel();
JPanel emptyPanel3 = new JPanel();
private boolean focusIsSet = false;
private boolean pathFieldVisible = false;
private ResourceBundle props;
private String rootPath;
protected SPWebdavClient webdavClient=null;
protected String proxyServer=null;
public LoginFrame(Frame owner, String webdavServer, String rootPath,
String userName, String userPasswd) {
super(owner, true);
this.rootPath = rootPath;
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try{
props = ResourceBundle.getBundle(
"org.apache.webdav.ui.resources."+"login",
Locale.getDefault());
} catch (Exception ex) {
System.err.println("Property loading error:");
ex.printStackTrace();
}
try {
createWebdavClient(webdavServer, userName, userPasswd);
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void showAtCentre() {
this.validate();
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
this.pathField.setVisible(isPathFieldVisible());
this.pathLabel.setVisible(isPathFieldVisible());
this.setVisible(true);
}
public SPWebdavClient getWebdavClient() {
return webdavClient;
}
public SPFileChooser getWebFileChooser() {
SPFileChooser fileChooser = null;
try {
fileChooser = SPFileChooser.createWebFileChooser(getWebdavClient(),
pathField.getText());
fileChooser.addFileFilter("XML Files (.xml)");
fileChooser.addFileFilter("DTD Files (.dtd)");
} catch (Exception e) {
e.printStackTrace();
}
return fileChooser;
}
public String getWebdavServer() {
return getHostName()+":"+getHostPort();
}
public String getHostName() {
if (webdavClient == null) {
return null;
}
return webdavClient.getHost();
}
public String getHostPort() {
if (webdavClient == null) {
return null;
}
return Integer.toString(webdavClient.getPort());
}
public String getCurrentPath() {
return pathField.getText();
}
public String getUserName() {
if (webdavClient == null) {
return null;
}
return webdavClient.getCredentials().getUserName();
}
public String getUserPasswd() {
if (webdavClient == null) {
return null;
}
return webdavClient.getCredentials().getPassword();
}
public String getProxyServer() {
return proxyServer;
}
public void setProxyServer(String server) {
if (server == null) {
return;
}
this.proxyServer = server;
int colonIndex = proxyServer.indexOf(':');
int proxyPort = Integer.parseInt(proxyServer.substring(colonIndex+1));
String proxyServerName = proxyServer.substring(0,colonIndex);
SPWebdavClient client = new SPWebdavClient();
client.startSession(getHostName(), Integer.parseInt(getHostPort()),
proxyServerName, proxyPort);
Credentials credentials = new Credentials(getUserName(), getUserPasswd());
client.setCredentials(credentials);
this.webdavClient = client;
}
public void setPathFieldVisible(boolean b) {
this.pathFieldVisible = b;
}
public boolean isPathFieldVisible() {
return pathFieldVisible;
}
protected void resetTextFields() {
hostField.setText("");
portField.setText("");
if (isPathFieldVisible()) {
pathField.setText("");
}
userField.setText("");
passwdField.setText("");
focusIsSet = false;
setFocus();
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_ACTIVATED) {
this.setFocus();
} else if (e.getID() == WindowEvent.WINDOW_CLOSING) {
this.webdavClient = null;
this.proxyServer = null;
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.
// getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(contentPaneLayout);
this.setSize(new Dimension(400, 300));
this.setTitle(getProperty("label.hostconnection"));
SessionPane.setBorder(BorderFactory.createRaisedBevelBorder());
SessionPane.setLayout(sessionPaneLayout);
emptyPanel1.setLayout(null);
hostLabel.setMaximumSize(new Dimension(100, 17));
hostLabel.setMinimumSize(new Dimension(100, 17));
hostLabel.setPreferredSize(new Dimension(100, 17));
hostLabel.setRequestFocusEnabled(false);
hostLabel.setHorizontalAlignment(SwingConstants.TRAILING);
hostLabel.setText(getProperty("label.hostname"));
hostField.setMinimumSize(new Dimension(220, 21));
hostField.setPreferredSize(new Dimension(220, 21));
hostField.setColumns(50);
hostField.setText(getHostName());
emptyPanel1.setMinimumSize(new Dimension(100, 34));
emptyPanel1.setPreferredSize(new Dimension(100, 34));
emptyPanel1.setRequestFocusEnabled(false);
portLabel.setMaximumSize(new Dimension(100, 17));
portLabel.setMinimumSize(new Dimension(100, 17));
portLabel.setPreferredSize(new Dimension(100, 17));
portLabel.setRequestFocusEnabled(false);
portLabel.setHorizontalAlignment(SwingConstants.TRAILING);
portLabel.setText(getProperty("label.port"));
portField.setMinimumSize(new Dimension(60, 21));
portField.setPreferredSize(new Dimension(60, 21));
portField.setText(getHostPort());
pathLabel.setMaximumSize(new Dimension(100, 17));
pathLabel.setMinimumSize(new Dimension(100, 17));
pathLabel.setPreferredSize(new Dimension(100, 17));
pathLabel.setRequestFocusEnabled(false);
pathLabel.setHorizontalAlignment(SwingConstants.TRAILING);
pathLabel.setText(getProperty("label.rootpath"));
pathField.setMinimumSize(new Dimension(200, 21));
pathField.setPreferredSize(new Dimension(200, 21));
pathField.setText(rootPath);
emptyPanel2.setMinimumSize(new Dimension(100, 34));
emptyPanel2.setPreferredSize(new Dimension(100, 34));
emptyPanel2.setRequestFocusEnabled(false);
emptyPanel2.setLayout(null);
userLabel.setMaximumSize(new Dimension(100, 17));
userLabel.setMinimumSize(new Dimension(100, 17));
userLabel.setPreferredSize(new Dimension(100, 17));
userLabel.setRequestFocusEnabled(false);
userLabel.setHorizontalAlignment(SwingConstants.TRAILING);
userLabel.setText(getProperty("label.username"));
passwdLabel.setMaximumSize(new Dimension(100, 17));
passwdLabel.setMinimumSize(new Dimension(100, 17));
passwdLabel.setPreferredSize(new Dimension(100, 17));
passwdLabel.setRequestFocusEnabled(false);
passwdLabel.setHorizontalAlignment(SwingConstants.TRAILING);
passwdLabel.setText(getProperty("label.password"));
resetButton.setRequestFocusEnabled(false);
resetButton.setText(getProperty("button.reset"));
resetButton.addActionListener(new LoginFrame_resetButton_actionAdapter(this));
okButton.setRequestFocusEnabled(false);
okButton.setText(getProperty("button.ok"));
okButton.addActionListener(new LoginFrame_okButton_actionAdapter(this));
emptyPanel3.setLayout(null);
emptyPanel3.setRequestFocusEnabled(false);
emptyPanel3.setPreferredSize(new Dimension(100, 34));
emptyPanel3.setMinimumSize(new Dimension(100, 34));
userField.setMinimumSize(new Dimension(80, 21));
userField.setPreferredSize(new Dimension(80, 21));
userField.setText(getUserName());
passwdField.setPreferredSize(new Dimension(80, 21));
passwdField.setMinimumSize(new Dimension(80, 21));
passwdField.setText(getUserPasswd());
contentPane.add(SessionPane, BorderLayout.CENTER);
SessionPane.add(emptyPanel1, new GridBagConstraints(0, 0, 4, 1, 0, 0,
GridBagConstraints.SOUTH, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(hostLabel, new GridBagConstraints(0, 1, 1, 1, 0, 0,
GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(portLabel, new GridBagConstraints(0, 2, 1, 1, 0, 0,
GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(pathLabel, new GridBagConstraints(0, 3, 1, 1, 0, 0,
GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(emptyPanel2, new GridBagConstraints(0, 4, 4, 1, 0, 0,
GridBagConstraints.SOUTH, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(userLabel, new GridBagConstraints(0, 5, 1, 1, 0, 0,
GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(passwdLabel, new GridBagConstraints(0, 6, 1, 1, 0, 0,
GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(emptyPanel3, new GridBagConstraints(0, 7, 4, 1, 0, 0,
GridBagConstraints.SOUTH, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(resetButton, new GridBagConstraints(0, 8, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(okButton, new GridBagConstraints(3, 8,
GridBagConstraints.REMAINDER, 1, 0, 0,
GridBagConstraints.EAST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(hostField, new GridBagConstraints(1, 1, 3, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(portField, new GridBagConstraints(1, 2, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 20), 0, 0));
SessionPane.add(pathField, new GridBagConstraints(1, 3, 3, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(userField, new GridBagConstraints(1, 5, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
SessionPane.add(passwdField, new GridBagConstraints(1, 6, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
/* Remove the RETURN key from the shared map of JTextFields */
Keymap map = hostField.getKeymap();
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
map.removeKeyStrokeBinding(key);
/* Make the OK button the default button */
this.getRootPane().setDefaultButton(okButton);
} //private jbInit()
private void setFocus() {
if (!focusIsSet) {
passwdField.requestFocus();
focusIsSet = true;
}
}
protected void createWebdavClient(String webdavServer,
String userName, String userPasswd) {
int colonIndex = webdavServer.lastIndexOf(":");
String host = webdavServer.substring(0, colonIndex);
String port = webdavServer.substring(colonIndex+1);
webdavClient = new SPWebdavClient();
webdavClient.setDebug(10);
webdavClient.startSession(host, Integer.parseInt(port));
Credentials credentials = new Credentials(userName, userPasswd);
webdavClient.setCredentials(credentials);
}
protected void validateClient() throws Exception {
SPHeadMethod headMethod = new SPHeadMethod(getWebdavClient(), rootPath);
headMethod.execute();
}
protected void resetButton_actionPerformed(ActionEvent e) {
resetTextFields();
}
protected void okButton_actionPerformed(ActionEvent e) {
try {
this.rootPath = pathField.getText();
createWebdavClient(hostField.getText()+":"+portField.getText(),
userField.getText(), new String(passwdField.getPassword()));
setProxyServer(getProxyServer());
validateClient();
setVisible(false);
} catch (Exception exception) {
final String title = "Connection Failure";
final String msg;
if (getProxyServer() == null) {
msg = "Unable to connect to "+getWebdavServer()
+"\n"+exception.getMessage();
} else {
msg = "Unable to connect to "+getWebdavServer()+".\n"
+"Proxy Server = "+getProxyServer()+".\n"
+exception.getMessage();
}
JOptionPane.showMessageDialog(null, msg,
title, JOptionPane.ERROR_MESSAGE);
webdavClient = null;
exception.printStackTrace();
}
}//okButton_actionPerformed()
/**
* Gets a String property defined in the property file
*
* @param name name of property
* @return The Property value
*/
public String getProperty(String name) {
//return props.getProperty(name);
return props.getString(name);
}
} // End of LoginFrame class
class LoginFrame_resetButton_actionAdapter implements ActionListener {
LoginFrame adaptee;
LoginFrame_resetButton_actionAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.resetButton_actionPerformed(e);
}
} // End of LoginFrame_resetButton_actionAdapter class
class LoginFrame_okButton_actionAdapter implements ActionListener {
LoginFrame adaptee;
LoginFrame_okButton_actionAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.okButton_actionPerformed(e);
}
} // End of LoginFrame_okButton_actionAdapter class