Package org.apache.webdav.ui.lab

Source Code of org.apache.webdav.ui.lab.MainFrame_resetButton_actionAdapter

/*
* $Header: /home/cvs/jakarta-slide/src/contrib/webdavgui/src/java/org/apache/webdav/ui/lab/MainFrame.java,v 1.2 2001/07/17 04:00:48 msmith Exp $
* $Revision: 1.2 $
* $Date: 2001/07/17 04:00:48 $
*
* ====================================================================
*
* 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.lab;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.Keymap;

import org.apache.webdav.lib.*;
import org.apache.webdav.ui.lib.methods.SPHeadMethod;
import org.apache.webdav.ui.SPWebdavClient;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;

/**
* Title:        MainFrame.java
* Description:  The main window frame for ui lab experiments.
*
* Company:      SpeedLegal Holdings Inc.
* @author       Jojada J. Tirtowidjojo
* @version 1.0
*/

public class MainFrame extends JFrame {
  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 = true;

  private String currentPath;
  protected SPWebdavClient webdavClient=null;
  protected String proxyServer=null;

  public MainFrame(String webdavServer, String rootPath,
              String userName, String userPasswd) {

    this.currentPath = rootPath;

    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    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 String getWebdavServer()  {
    return getHostName()+":"+getHostPort();
  }

  public String getHostName()  {
    if (webdavClient == null)  {
  return null;
    }
    return webdavClient.getSessionHost();
  }

  public String getHostPort()  {
    if (webdavClient == null)  {
  return null;
    }
    return webdavClient.getSessionPort();
  }

  public String getCurrentPath()  {
    return currentPath;
  }

  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_CLOSING) {
      System.exit(0);
    }

    if (e.getID() == WindowEvent.WINDOW_ACTIVATED) {
      //Whenever window gets the focus, let this
      //MainFrame set the initial focus.
      this.setFocus();
    }

  }


  /**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("Host Connection");
    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("Host Name:");
    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("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("Root Path:");
    pathField.setMinimumSize(new Dimension(200, 21));
    pathField.setPreferredSize(new Dimension(200, 21));
    pathField.setText(currentPath);

    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("User Name:");
    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("Password:");
    resetButton.setRequestFocusEnabled(false);
    resetButton.setText("RESET");
    resetButton.addActionListener(new MainFrame_resetButton_actionAdapter(this));
    okButton.setRequestFocusEnabled(false);
    okButton.setText("OK");
    okButton.addActionListener(new MainFrame_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 launch_HostWindow()  {

    HostFrame newHost = new HostFrame(getWebdavClient(), getCurrentPath());

    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = newHost.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    newHost.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    newHost.setVisible(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.startSession(host, Integer.parseInt(port));

    Credentials credentials = new Credentials(userName, userPasswd);
    webdavClient.setCredentials(credentials);
  }

  protected void validateClient() throws Exception {
    SPHeadMethod headMethod = new SPHeadMethod(getWebdavClient(),
  getCurrentPath());
    headMethod.execute();
  }

  protected void resetButton_actionPerformed(ActionEvent e) {
    resetTextFields();
  }

  protected void okButton_actionPerformed(ActionEvent e) {
    try  {
        this.currentPath = pathField.getText();
        createWebdavClient(hostField.getText()+":"+portField.getText(),
      userField.getText(), new String(passwdField.getPassword()));

        setProxyServer(getProxyServer());
        validateClient();
        launch_HostWindow();

    } 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()

} // End of MainFrame class

class MainFrame_resetButton_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_resetButton_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.resetButton_actionPerformed(e);
  }
} // End of MainFrame_resetButton_actionAdapter class

class MainFrame_okButton_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_okButton_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.okButton_actionPerformed(e);
  }
} // End of MainFrame_okButton_actionAdapter class





TOP

Related Classes of org.apache.webdav.ui.lab.MainFrame_resetButton_actionAdapter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.