incubator-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ran...@apache.org
Subject cvs commit: incubator-ftpserver/src/java/org/apache/ftpserver/gui MessagePanel.java PluginPanel.java PluginPanelContainer.java RootPanel.java ServerFrame.java SpyPanel.java SpyPanelContainer.java StatisticsPanel.java TreePluginPanelContainer.java UserManagerPanel.java
Date Wed, 07 Sep 2005 05:04:45 GMT
rana_b      2005/09/06 22:04:45

  Added:       src/java/org/apache/ftpserver/gui MessagePanel.java
                        PluginPanel.java PluginPanelContainer.java
                        RootPanel.java ServerFrame.java SpyPanel.java
                        SpyPanelContainer.java StatisticsPanel.java
                        TreePluginPanelContainer.java UserManagerPanel.java
  Log:
  Refactored - removed avalon entirely
  
  Revision  Changes    Path
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/gui/MessagePanel.java
  
  Index: MessagePanel.java
  ===================================================================
  // $Id: MessagePanel.java,v 1.1 2005/09/07 05:04:45 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.gui;
  
  import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Enumeration;
  import java.util.Iterator;
  import java.util.Properties;
  
  import javax.swing.JButton;
  import javax.swing.JComboBox;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  
  import org.apache.ftpserver.ftplet.FtpException;
  import org.apache.ftpserver.interfaces.IFtpConfig;
  import org.apache.ftpserver.interfaces.IMessageResource;
  
  /**
   * This is FTP server response panel. User can customize server responses.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class MessagePanel extends PluginPanel implements ActionListener {
  
      private static final long serialVersionUID = -68038181884794057L;
      
      private IFtpConfig m_fconfig;
      private JTextArea m_txtArea;
      private JComboBox m_comboBox;
      
      private ArrayList m_messageKeys = new ArrayList();
      private Properties m_messageProps = new Properties();
      private int m_oldSelIndex = -1;
      
      /**
       * Constructor - set the container.
       */
      public MessagePanel(PluginPanelContainer container) {
          super(container);
          initComponents();
      }
      
      /**
       * Initialize UI components.
       */
      private void initComponents() {
          
          setLayout(new BorderLayout());
          
          JPanel comboPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
          add(comboPanel, BorderLayout.NORTH);
          
          // all replies
          m_comboBox = new JComboBox();
          m_comboBox.setPreferredSize(new Dimension(250, 22));
          m_comboBox.addActionListener(this);
          comboPanel.add(m_comboBox);
          
          // message text
          m_txtArea = new JTextArea();
          JScrollPane txtPane = new JScrollPane(m_txtArea, 
                      JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          add(txtPane, BorderLayout.CENTER);
          
          JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
          add(buttonPanel, BorderLayout.SOUTH);
          
          // save button
          JButton saveBtn = new JButton("Save");
          saveBtn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  save();
              }
          });
          buttonPanel.add(saveBtn);
          
          // reload button
          JButton reloadBtn = new JButton("Reload");
          reloadBtn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  refresh(m_fconfig);
              }
          });
          buttonPanel.add(reloadBtn);
      }
      
      /**
       * List selection changed. 
       */
      public void actionPerformed(ActionEvent e) {
          int selIdx = m_comboBox.getSelectedIndex();
          if(selIdx != -1) {
              Object selVal = m_messageKeys.get(selIdx);
              String val = m_messageProps.getProperty((String)selVal);
              if(val == null) {
                  val = "";
              }
              
              // save the last text area value
              if(m_oldSelIndex != -1) {
                  String oldKey = (String)m_messageKeys.get(m_oldSelIndex);
                  String oldTxt = m_txtArea.getText();
                  m_messageProps.setProperty(oldKey, oldTxt);
              }
              
              // update text area
              m_txtArea.setText(val);
          }
      } 
  
      /**
       * Save entered properties 
       */
      private void save() {
          
          try {
              // save the text value
              String key = (String)m_comboBox.getSelectedItem();
              String val = m_txtArea.getText();
              m_messageProps.setProperty(key, val);
              
              // save messages
              getContainer().getFtpConfig().getMessageResource().save(m_messageProps, null);
          }
          catch(FtpException ex) {
              GuiUtils.showErrorMessage(this, "Cannot save messages.");
          }
      }
      
      /** 
       * Refresh the ftp configuration
       */
      public void refresh(IFtpConfig ftpConfig) {
          m_fconfig = ftpConfig;
          m_comboBox.removeAllItems();
          m_txtArea.setText("");
          m_messageKeys.clear();
          m_oldSelIndex = -1;
          
          if(m_fconfig != null) {
              IMessageResource msgRes = m_fconfig.getMessageResource();
              m_messageProps = msgRes.getMessages(null);
              Enumeration props = m_messageProps.propertyNames();
              while(props.hasMoreElements()) {
                  String key = (String)props.nextElement();
                  m_messageKeys.add(key);
              }
              
              Collections.sort(m_messageKeys);
              Iterator it = m_messageKeys.iterator();
              while(it.hasNext()) {
                  m_comboBox.addItem(it.next());
              }
              if(!m_messageKeys.isEmpty()) {
                  m_comboBox.setSelectedIndex(0);
              }
          }
      }
  
      /** 
       * This can be displayed only when the server is running.
       */
      public boolean canBeDisplayed() {
          return (m_fconfig != null);
      }
      
      /**
       * Get the string representation.
       */
      public String toString() {
          return "Messages";
      }
  }
  
  
  
  1.4       +26 -33    incubator-ftpserver/src/java/org/apache/ftpserver/gui/PluginPanel.java
  
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/gui/PluginPanelContainer.java
  
  Index: PluginPanelContainer.java
  ===================================================================
  // $Id: PluginPanelContainer.java,v 1.1 2005/09/07 05:04:45 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.gui;
  
  import org.apache.ftpserver.interfaces.IFtpConfig;
  
  import javax.swing.JComponent;
  
  /**
   * This is the plugin panel container.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  interface PluginPanelContainer {
  
      public final static int ROOT_INDEX = 0;
      public final static int USER_INDEX = 1;
      public final static int IP_INDEX   = 2;
      public final static int MSG_INDEX  = 3;
      public final static int CON_INDEX  = 4;
      public final static int SPY_INDEX  = 5;
      public final static int FILE_INDEX = 6;
      public final static int DIR_INDEX  = 7;
      public final static int LOG_INDEX  = 8;
      public final static int STAT_INDEX = 9;
      
      /**
       * Add plugin panel.
       */
      void add(PluginPanel panel);
      
      /**
       * Get selected panel index.
       */
      int getSelectedIndex();
      
      /**
       * Set selected panel index.
       */
      void setSelectedIndex(int index);
      
      /**
       * Get the plugin panel at the specified index.
       */
      PluginPanel getPluginPanel(int index);
      
      /**
       * Get component.
       */
      JComponent getComponent();
      
      /**
       * Refresh with new ftp configuration object.
       */
      void refresh(IFtpConfig config);
      
      /**
       * Get current ftp config
       */
      IFtpConfig getFtpConfig();
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/gui/RootPanel.java
  
  Index: RootPanel.java
  ===================================================================
  // $Id: RootPanel.java,v 1.1 2005/09/07 05:04:45 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.gui;
  
  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.FlowLayout;
  import java.awt.Font;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  
  import javax.swing.ButtonGroup;
  import javax.swing.JButton;
  import javax.swing.JFileChooser;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JRadioButton;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  import javax.swing.JTextField;
  import javax.swing.JToggleButton;
  
  import org.apache.ftpserver.FtpConfigImpl;
  import org.apache.ftpserver.FtpServer;
  import org.apache.ftpserver.config.PropertiesConfiguration;
  import org.apache.ftpserver.config.XmlConfigurationHandler;
  import org.apache.ftpserver.ftplet.Configuration;
  import org.apache.ftpserver.interfaces.IFtpConfig;
  import org.apache.ftpserver.util.IoUtils;
  
  /**
   * This is the tree root panel. It is responsible to load 
   * configuration file and start/stop the server. 
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class RootPanel extends PluginPanel {
      
      private static final long serialVersionUID = 6048031185986894615L;
  
      private JTextField m_cfgFileTxt;
      
      private JRadioButton m_cfgPropRadio;
      private JRadioButton m_cfgXmlRadio;
      
      private JRadioButton m_cfgFileRadio;
      private JRadioButton m_cfgClasspathRadio;
      
      private JButton m_browseButton;
      private JButton m_startButton;
      private JButton m_stopButton;
      private JToggleButton m_suspendButton; 
      
      private FtpServer m_server;
      
      
      /**
       * Constructor - create all UI components.
       */
      public RootPanel(PluginPanelContainer container) {
          super(container);
          initComponents();
      }
      
      
      /**
       * Initialize UI components.
       */
      public void initComponents() {
          
          setLayout(new BorderLayout());
          
          // top panel
          JPanel topPnl = new JPanel(new GridBagLayout());
          add(topPnl, BorderLayout.NORTH);
          GridBagConstraints gc = new GridBagConstraints();
          int yindex = -1; 
          gc.insets = new Insets(0, 0, 0, 10);
          
          // configuration label
          JLabel cfgLab = new JLabel("Configuration", JLabel.CENTER);
          cfgLab.setForeground(Color.darkGray);
          cfgLab.setFont(new Font(null, Font.BOLD, 14));
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 4;
          gc.fill = GridBagConstraints.HORIZONTAL;
          topPnl.add(cfgLab, gc);
          
          // file label
          JLabel cfgFileLab = new JLabel("File :: ", JLabel.RIGHT);
          cfgFileLab.setFont(new Font(null, Font.BOLD, 12));
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.fill = GridBagConstraints.HORIZONTAL;
          topPnl.add(cfgFileLab, gc);
          
          // file text field
          m_cfgFileTxt = new JTextField();
          m_cfgFileTxt.setText("./res/conf/ftpd.properties");
          gc.fill = GridBagConstraints.HORIZONTAL;
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          topPnl.add(m_cfgFileTxt, gc);
          
          // browse button
          m_browseButton = new JButton("Browse");
          m_browseButton.setPreferredSize(new Dimension(80, 22));
          m_browseButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  File currFile = new File(m_cfgFileTxt.getText());
                  JFileChooser fileChoose = new JFileChooser(currFile.getParentFile());
                  fileChoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
                  int returnVal = fileChoose.showOpenDialog(RootPanel.this);
                  if (returnVal == JFileChooser.APPROVE_OPTION) {
                      String path = fileChoose.getSelectedFile().getAbsolutePath();
                      m_cfgFileTxt.setText(path);
                  }
              }
          });
          gc.fill = GridBagConstraints.HORIZONTAL;
          gc.gridx = 3;
          gc.gridy = yindex;
          gc.gridwidth = 1;
          topPnl.add(m_browseButton, gc);
          
          // source label
          JLabel cfgSrcLab = new JLabel("Source :: ", JLabel.RIGHT);
          cfgSrcLab.setFont(new Font(null, Font.BOLD, 12));
          gc.fill = GridBagConstraints.HORIZONTAL;
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          topPnl.add(cfgSrcLab, gc);
          
          // file system radio button
          m_cfgFileRadio = new JRadioButton("File System");
          m_cfgFileRadio.setFont(new Font(null, Font.BOLD, 12));
          m_cfgFileRadio.setSelected(true);
          m_cfgFileRadio.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  m_browseButton.setEnabled(m_cfgFileRadio.isSelected());
              }
          });
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 1;
          topPnl.add(m_cfgFileRadio, gc);
          
          // classpath radio button
          m_cfgClasspathRadio = new JRadioButton("Classpath");
          m_cfgClasspathRadio.setFont(new Font(null, Font.BOLD, 12));
          m_cfgClasspathRadio.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  m_browseButton.setEnabled(!m_cfgClasspathRadio.isSelected());
              }
          });
          gc.gridx = 2;
          gc.gridy = yindex;
          gc.gridwidth = 1;
          topPnl.add(m_cfgClasspathRadio, gc);
          
          // type label
          JLabel cfgTypLab = new JLabel("Type :: ", JLabel.RIGHT);
          cfgTypLab.setFont(new Font(null, Font.BOLD, 12));
          gc.fill = GridBagConstraints.HORIZONTAL;
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          topPnl.add(cfgTypLab, gc);
          
          // properties radio button
          m_cfgPropRadio = new JRadioButton("Properties");
          m_cfgPropRadio.setFont(new Font(null, Font.BOLD, 12));
          m_cfgPropRadio.setSelected(true);
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 1;
          topPnl.add(m_cfgPropRadio, gc);
          
          // xml radio button
          m_cfgXmlRadio = new JRadioButton("XML");
          m_cfgXmlRadio.setFont(new Font(null, Font.BOLD, 12));
          gc.gridx = 2;
          gc.gridy = yindex;
          gc.gridwidth = 1;
          topPnl.add(m_cfgXmlRadio, gc);
          
          // button panel
          JPanel btnPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 4;
          topPnl.add(btnPnl, gc);
          
          // start button
          m_startButton = new JButton("Start");
          m_startButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  startServer();
              }
          });
          btnPnl.add(m_startButton);
          
          // stop button
          m_stopButton = new JButton("Stop");
          m_stopButton.setEnabled(false);
          m_stopButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  stopServer();
              }
          });
          btnPnl.add(m_stopButton);
          
          // suspend button
          m_suspendButton = new JToggleButton("Suspend");
          m_suspendButton.setEnabled(false);
          m_suspendButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  suspendServer();
              }
          });
          btnPnl.add(m_suspendButton);
          
          // bottom license text area
          JTextArea txtArea = new JTextArea();
          txtArea.setFont(new Font("Monospaced", Font.PLAIN, 10));
          txtArea.setEditable(false);
          
          // load start.html
          InputStream is = null;
          InputStreamReader rd = null;
          try {
              is = getClass().getClassLoader().getResourceAsStream("org/apache/ftpserver/gui/LICENSE.txt");
              if (is != null) {
                  rd = new InputStreamReader(is);
                  txtArea.read(rd, null);
              }
          }
          catch(IOException ex) {
          }
          finally {
              IoUtils.close(rd);
              IoUtils.close(is);
          }
          
          // editor scroll pane
          JScrollPane editorScrollPane = new JScrollPane(txtArea);
          add(editorScrollPane, BorderLayout.CENTER);
          
          // type button group 
          ButtonGroup typeButtonGroup = new ButtonGroup();
          typeButtonGroup.add(m_cfgPropRadio);
          typeButtonGroup.add(m_cfgXmlRadio);
          
          // source button group
          ButtonGroup sourceButtonGroup = new ButtonGroup();
          sourceButtonGroup.add(m_cfgFileRadio);
          sourceButtonGroup.add(m_cfgClasspathRadio);
      }
      
      
      /**
       * Refresh the panel with the new ftp config.
       */
      public void refresh(IFtpConfig ftpCOnfig) {
      }
      
      
      /**
       * This panel can always be displayed.
       */
      public boolean canBeDisplayed() {
          return true;
      }
      
      
      /**
       * Start the server.
       */
      public void startServer() {
          
          // already started - no need to do it again
          if(m_server != null) {
              return;
          }
          
          InputStream in = null;
          try {
              
              // get appropriate stream
              String path = m_cfgFileTxt.getText();
              if(m_cfgFileRadio.isSelected()) {
                  in = new FileInputStream(path);
              }
              else {
                  // strip the first slash (/) if exists
                  if( (path.length() > 0) && (path.charAt(0) == '/') ) {
                      path = path.substring(1);
                  }
                  in = getClass().getClassLoader().getResourceAsStream(path);
              }
              
              // create configuration object
              Configuration config = null;
              if(m_cfgPropRadio.isSelected()) {
                  config = new PropertiesConfiguration(in);
              }
              else {
                  XmlConfigurationHandler xmlHandler = new XmlConfigurationHandler(in);
                  config = xmlHandler.parse();
              }
              
              // create ftp configuration object
              IFtpConfig fconfig = new FtpConfigImpl(config);
              
              // start server
              m_server = new FtpServer(fconfig);
              m_server.start();
              
              // enabled/disable components
              m_cfgFileTxt.setEnabled(false);
              m_cfgPropRadio.setEnabled(false);
              m_cfgXmlRadio.setEnabled(false);
              m_cfgFileRadio.setEnabled(false);
              m_cfgClasspathRadio.setEnabled(false);
              m_browseButton.setEnabled(false);
              m_startButton.setEnabled(false);
              m_stopButton.setEnabled(true);
              m_suspendButton.setEnabled(true);
              m_suspendButton.setSelected(false);
              
              // refresh the container
              getContainer().refresh(m_server.getFtpConfig());
          }
          catch(Exception ex) {
              m_server = null;
              ex.printStackTrace();
              GuiUtils.showErrorMessage(RootPanel.this, "Cannot start FTP server.");
          }
          finally {
              IoUtils.close(in);
          }
      }
      
      /**
       * Stop the server.
       */
      public void stopServer() {
          
          // not running - no need to stop it.
          if(m_server == null) {
              return;
          }
          
          m_server.stop();
          m_server = null;
      
          // enable/disable components
          m_cfgFileTxt.setEnabled(true);
          m_cfgPropRadio.setEnabled(true);
          m_cfgXmlRadio.setEnabled(true);
          m_cfgFileRadio.setEnabled(true);
          m_cfgClasspathRadio.setEnabled(true);
          m_browseButton.setEnabled(true);
          m_startButton.setEnabled(true);
          m_stopButton.setEnabled(false);
          m_suspendButton.setEnabled(false);
          m_suspendButton.setSelected(false);
          
          // refresh the container
          getContainer().refresh(null);
      }
      
      
      /**
       * Suspend server.
       */
      private void suspendServer() {
          
          // not running - no need to do anything.
          if(m_server == null) {
              return;
          }
          
          if(m_suspendButton.isSelected()) {
              m_server.suspend();
          }
          else {
              m_server.resume();
          }
      }
      
      
      /**
       * Get the panel name.
       */
      public String toString() {
          return "FTP";
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/gui/ServerFrame.java
  
  Index: ServerFrame.java
  ===================================================================
  // $Id: ServerFrame.java,v 1.1 2005/09/07 05:04:45 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.gui;
  
  import java.awt.Dimension;
  import java.awt.event.WindowEvent;
  
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JTabbedPane;
  
  /**
   * This is the FTP server UI starting point.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class ServerFrame extends JFrame {
      
      private static final long serialVersionUID = 8399655106217258507L;
      
      private PluginPanelContainer m_pluginContainer;
      
      /**
       * Constructor - create tree and show the root panel.
       */
      public ServerFrame() {
          JTabbedPane tabPane = new JTabbedPane(); 
          getContentPane().add(tabPane);
          
          // add all plugin panels
          m_pluginContainer = new TreePluginPanelContainer();
          m_pluginContainer.add(new RootPanel(m_pluginContainer));
          m_pluginContainer.add(new UserManagerPanel(m_pluginContainer));
          m_pluginContainer.add(new IPRestrictorPanel(m_pluginContainer));
          m_pluginContainer.add(new MessagePanel(m_pluginContainer));
          m_pluginContainer.add(new ConnectionPanel(m_pluginContainer));
          m_pluginContainer.add(new SpyPanelContainer(m_pluginContainer));
          m_pluginContainer.add(new FilePanel(m_pluginContainer));
          m_pluginContainer.add(new DirectoryPanel(m_pluginContainer));
          m_pluginContainer.add(new LoggerPanel(m_pluginContainer));
          m_pluginContainer.add(new StatisticsPanel(m_pluginContainer));
          
          tabPane.addTab("FTP", m_pluginContainer.getComponent());
          m_pluginContainer.setSelectedIndex(PluginPanelContainer.ROOT_INDEX);
          
          // show frame
          setTitle("Apache FTP Server");
          ImageIcon icon = GuiUtils.createImageIcon("org/apache/ftpserver/gui/logo.gif");
          if (icon != null) {
              setIconImage(icon.getImage());
          }
          setSize(new Dimension(620, 420));
          GuiUtils.setLocation(this);
          setVisible(true);
          toFront();
      }
      
      /*
       * Handle window closing event.
       */ 
      protected void processWindowEvent(WindowEvent e) {
          int id = e.getID();
          if (id == WindowEvent.WINDOW_CLOSING) {
              if ( !GuiUtils.getConfirmation(this, "Do you really want to exit?") ) {
                  return;
              }
              super.processWindowEvent(e);
              RootPanel root = (RootPanel)m_pluginContainer.getPluginPanel(0);
              root.stopServer();
              dispose();
              System.exit(0);
          } 
          else {
              super.processWindowEvent(e);
          }    
      }
  
      /**
       * Server GUI starting point.
       */
       public static void main (String args[]) {
          System.out.println("Opening UI window, please wait...");            
          new ServerFrame();
       }
  }
  
  
  
  1.5       +133 -87   incubator-ftpserver/src/java/org/apache/ftpserver/gui/SpyPanel.java
  
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/gui/SpyPanelContainer.java
  
  Index: SpyPanelContainer.java
  ===================================================================
  // $Id: SpyPanelContainer.java,v 1.1 2005/09/07 05:04:45 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.gui;
  
  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Component;
  import java.awt.Dimension;
  import java.awt.Font;
  
  import javax.swing.BorderFactory;
  import javax.swing.JLabel;
  import javax.swing.JTabbedPane;
  
  import org.apache.ftpserver.ftplet.User;
  import org.apache.ftpserver.interfaces.IConnection;
  import org.apache.ftpserver.interfaces.IFtpConfig;
  
  /**
   * This panel monitors user request/replies.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class SpyPanelContainer extends PluginPanel {
      
      private static final long serialVersionUID = 7426681776615720958L;
      
      private JTabbedPane m_tabbedPane;
      private JLabel m_defaultComp;
      
      private IFtpConfig m_fconfig;
      
      
      /**
       * Constructor - create empty tabbed frame
       */
      public SpyPanelContainer(PluginPanelContainer container) {
          super(container);
          initComponents();
      }
      
      /**
       * Initialize UI components.
       */
      private void initComponents() {
          setLayout(new BorderLayout());
          m_tabbedPane = new JTabbedPane();
          m_tabbedPane.setPreferredSize(new Dimension(470, 340));
          add(m_tabbedPane, BorderLayout.CENTER);
          
          // initialize component to be displayed if 
          // there is no currently monitored connection
          m_defaultComp = new JLabel("Please, select a connection.", JLabel.CENTER);
          m_defaultComp.setFont(new Font(null, Font.BOLD, 17));
          m_defaultComp.setForeground(Color.blue);
          m_defaultComp.setBackground(Color.white);
          m_defaultComp.setBorder(BorderFactory.createEtchedBorder());
          m_defaultComp.setOpaque(true);
          
          m_tabbedPane.addTab("Spy", m_defaultComp);
      }
  
      /**
       * Monitor connection.
       */
      public void monitorConnection(IConnection con) {    
          String userName = getCaption(con); 
          
          // don't add another tab if already being monitored
          int tabCount = m_tabbedPane.getTabCount();
          for(int i=0; i<tabCount; i++) {
              Component selComp = m_tabbedPane.getComponentAt(i);
              if ( (selComp != null) && (selComp != m_defaultComp) ) {
                  IConnection tabcon = ((SpyPanel)selComp).getConnection();
                  if (tabcon == con) {
                      m_tabbedPane.setTitleAt(i, userName);
                      m_tabbedPane.setSelectedIndex(i);
                      return;
                  }
              }
          }
          
          // add new tab
          SpyPanel spyPane = new SpyPanel(m_fconfig, con, m_tabbedPane, m_defaultComp);
          m_tabbedPane.remove(m_defaultComp);
          m_tabbedPane.add(userName, spyPane);
          m_tabbedPane.setSelectedComponent(spyPane);
      } 
      
      /**
       * Get tab caption.
       */
      private String getCaption(IConnection con) {
          User user = con.getRequest().getUser();
          String name = "UNKNOWN";
          if(user != null) {
              String tmp = user.getName();
              if(tmp != null) {
                  name = tmp;
              }
          }
          return name;
      } 
          
      /** 
       * Refresh the ftp configuration
       */
      public void refresh(IFtpConfig ftpConfig) {
          m_fconfig = ftpConfig;
          int tabCount = m_tabbedPane.getTabCount();
          for(int i=0; i<tabCount; i++) {
              Component tabComp = m_tabbedPane.getComponentAt(i);
              if ( (tabComp != null) && (tabComp != m_defaultComp) ) {
                  ((SpyPanel)tabComp).closePane();
                  m_tabbedPane.remove(tabComp);
              }
          }
          
          m_tabbedPane.addTab("Spy", m_defaultComp);
      }
  
      
      /** 
       * This can be displayed only when the server is running.
       */
      public boolean canBeDisplayed() {
          return (m_fconfig != null);
      }
  
      
      /**
       * Get the string representation.
       */
      public String toString() {
          return "Spy";
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/gui/StatisticsPanel.java
  
  Index: StatisticsPanel.java
  ===================================================================
  // $Id: StatisticsPanel.java,v 1.1 2005/09/07 05:04:45 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.gui;
  
  import org.apache.ftpserver.ftplet.FileObject;
  import org.apache.ftpserver.interfaces.FileObserver;
  import org.apache.ftpserver.interfaces.IConnection;
  import org.apache.ftpserver.interfaces.IFtpConfig;
  import org.apache.ftpserver.interfaces.IFtpStatistics;
  import org.apache.ftpserver.interfaces.StatisticsObserver;
  import org.apache.ftpserver.util.DateUtils;
  
  import java.awt.BorderLayout;
  import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.JButton;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTable;
  import javax.swing.SwingUtilities;
  import javax.swing.event.EventListenerList;
  import javax.swing.event.TableModelEvent;
  import javax.swing.event.TableModelListener;
  import javax.swing.table.TableModel;
  
  /**
   * This panel displays all the global statistics information.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class StatisticsPanel extends PluginPanel 
                        implements StatisticsObserver, FileObserver, TableModel {
  
      private static final long serialVersionUID = -8316984131627702453L;
      
      private final static int I_START_TIME        = 0;
      private final static int I_DIR_CREATED       = 1;
      private final static int I_DIR_REMOVED       = 2;
      private final static int I_FILE_UPLOAD       = 3;
      private final static int I_FILE_DOWNLOAD     = 4;
      private final static int I_FILE_REMOVED      = 5;
      private final static int I_UPLOAD_BYTES      = 6;
      private final static int I_DOWNLOAD_BYTES    = 7;
      private final static int I_CURR_LOGINS       = 8;
      private final static int I_TOTAL_LOGINS      = 9;
      private final static int I_CURR_ANON_LOGINS  = 10;
      private final static int I_TOTAL_ANON_LOGINS = 11;
      private final static int I_CURR_CONS         = 12;
      private final static int I_TOTAL_CONS        = 13;
      
      private final static String[] COL_NAMES = {
              "Name",  
              "Value"
      };
  
      private final static String[] STAT_NAMES = {
              "Server start time",
              "Number of directories created",
              "Number of directories removed",
              "Number of file uploaded",
              "Number of file downloaded",
              "Number of file deleted",
              "Uploaded bytes",
              "Downloaded bytes",
              "Current logins",
              "Total logins",
              "Current anonymous logins",
              "Total anonymous logins",
              "Current connections",
              "Total connections"
      };
  
      private IFtpConfig m_ftpConfig;
      private IFtpStatistics m_statistics;
      private String m_data[] = new String[STAT_NAMES.length];
      private EventListenerList m_listeners = new EventListenerList();
      
      /**
       * Constructor - create all UI components.
       */
      public StatisticsPanel(PluginPanelContainer container) {
          super(container);
          
          // initialize string array
          for(int i=0; i<m_data.length; ++i) {
              m_data[i] = "";
          }
          
          initComponents();
      } 
      
      
      /**
       * Initialize all UI components.
       */
      private void initComponents() {
          
          setLayout(new BorderLayout());
          
          // top table
          JTable dataTable = new JTable(this); 
          JScrollPane scrollPane = new JScrollPane(dataTable, 
                                                   JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                                   JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          add(scrollPane, BorderLayout.CENTER);
          
          // reload statistics
          JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
          add(btnPanel, BorderLayout.SOUTH);
          
          JButton reloadButton = new JButton("Reload");
          btnPanel.add(reloadButton);
          reloadButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  refresh(m_ftpConfig);
              }
          });
      }
      
      /**
       * Get column class - always string
       */
      public Class getColumnClass(int index) {
          return String.class;
      }
      
      /**
       * Get column count.
       */
      public int getColumnCount() {
          return COL_NAMES.length;
      }
      
      /**
       * Get column name.
       */
      public String getColumnName(int index) {
          return COL_NAMES[index];
      } 
      
      /**
       * Get row count.
       */
      public int getRowCount() {
          return STAT_NAMES.length;
      }
      
      /**
       * Is cell editable - currently false.
       */
      public boolean isCellEditable(int row, int col) {
          return false;
      }
  
      /**
       * Get table cell value.
       */
      public Object getValueAt(int rowIndex, int columnIndex) {
          if(columnIndex == 0) {
              return STAT_NAMES[rowIndex];
          }
          else {
              return m_data[rowIndex];
          }
      }
      
      /**
       * Set table cell value.
       */
      public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
      }
      
      /**
       * Add table model listener.
       */
      public void addTableModelListener(TableModelListener l) {
          m_listeners.add(TableModelListener.class, l);
      } 
      
      /**
       * Remove table model listener.
       */
      public void removeTableModelListener(TableModelListener l) {
          m_listeners.remove(TableModelListener.class, l);
      }
  
      /**
       * Set value.
       */
      private void setValue(int index, String val) {
                  
          // set string data
          if(val == null) {
              val = "";
          }
          m_data[index] = val;
          
          // notify table listeners
          TableModelEvent e = new TableModelEvent(StatisticsPanel.this, index, index, 1);
          Object[] listeners = m_listeners.getListenerList();
          for (int i = listeners.length-2; i>=0; i-=2) {
              if (listeners[i]==TableModelListener.class) {
                  ((TableModelListener)listeners[i+1]).tableChanged(e);
              }
          }
      }
      
      /** 
       * Refresh the ftp configuration
       */
      public void refresh(IFtpConfig ftpConfig) {
          m_ftpConfig = ftpConfig;
          if (m_ftpConfig != null) {
              m_statistics = (IFtpStatistics)m_ftpConfig.getFtpStatistics();
              m_statistics.setObserver(this);
              m_statistics.setFileObserver(this);
              
              // reset component values
              String startTime = DateUtils.getISO8601Date(m_statistics.getStartTime().getTime());
              setValue(I_START_TIME, startTime);
              notifyMkdir();
              notifyRmdir();
              notifyUpload();
              notifyDownload();
              notifyDelete();
              notifyLogin(true);
              notifyOpenConnection();
          }
          else {
              if(m_statistics != null) {
                  m_statistics.setObserver(null);
                  m_statistics.setFileObserver(null);
              }
              m_statistics = null;
          }
      }
  
      /**
       * Upload notification.
       */
      public void notifyUpload() {
          Runnable runnable = new Runnable() {
              public void run() { 
                  IFtpStatistics stat = m_statistics;
                  if(stat != null) {
                      int totalUpload = stat.getTotalUploadNumber();
                      setValue(I_FILE_UPLOAD, String.valueOf(totalUpload));
                  
                      long totalUploadSz = stat.getTotalUploadSize();
                      setValue(I_UPLOAD_BYTES, String.valueOf(totalUploadSz));
                  }
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
      
      /**
       * File upload notification.
       */
      public void notifyUpload(final IConnection connection, final FileObject file, final long size) {
          Runnable runnable = new Runnable() {
              public void run() { 
                  FilePanel filePanel = (FilePanel)getContainer().getPluginPanel(PluginPanelContainer.FILE_INDEX);
                  filePanel.notifyUpload(connection, file, size);        
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
      
      /**
       * Download notification.
       */
      public void notifyDownload() {
          Runnable runnable = new Runnable() {
              public void run() {
                  IFtpStatistics stat = m_statistics;
                  if(stat != null) {
                      int totalDownload = stat.getTotalDownloadNumber();
                      setValue(I_FILE_DOWNLOAD, String.valueOf(totalDownload));
                      
                      long totalDownloadSz = stat.getTotalDownloadSize();
                      setValue(I_DOWNLOAD_BYTES, String.valueOf(totalDownloadSz));
                  }
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
  
      /**
       * File download notification.
       */
      public void notifyDownload(final IConnection connection, final FileObject file, final long size) {
          Runnable runnable = new Runnable() {
              public void run() { 
                  FilePanel filePanel = (FilePanel)getContainer().getPluginPanel(PluginPanelContainer.FILE_INDEX);
                  filePanel.notifyDownload(connection, file, size);        
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
      
      /**
       * Delete notification.
       */
      public void notifyDelete() {
          Runnable runnable = new Runnable() {
              public void run() { 
                  IFtpStatistics stat = m_statistics;
                  if(stat != null) {
                      int totalDelete = stat.getTotalDeleteNumber();
                      setValue(I_FILE_REMOVED, String.valueOf(totalDelete));
                  }
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
      
      /**
       * File delete notification.
       */
      public void notifyDelete(final IConnection connection, final FileObject file) {
          Runnable runnable = new Runnable() {
              public void run() { 
                  FilePanel filePanel = (FilePanel)getContainer().getPluginPanel(PluginPanelContainer.FILE_INDEX);
                  filePanel.notifyDelete(connection, file);        
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
      
      /**
       * User login notification.
       */
      public void notifyLogin(final boolean anonymous) {
          Runnable runnable = new Runnable() {
              public void run() { 
                  IFtpStatistics stat = m_statistics;
                  if(stat != null) {
                      int loginNbr = stat.getCurrentLoginNumber();
                      setValue(I_CURR_LOGINS, String.valueOf(loginNbr));
                      
                      int totalLoginNbr = stat.getTotalLoginNumber();
                      setValue(I_TOTAL_LOGINS, String.valueOf(totalLoginNbr));
                      
                      if(anonymous) {
                          int anonLoginNbr = stat.getCurrentAnonymousLoginNumber();
                          setValue(I_CURR_ANON_LOGINS, String.valueOf(anonLoginNbr));
                          
                          int totalAnonLoginNbr = stat.getTotalAnonymousLoginNumber();
                          setValue(I_TOTAL_ANON_LOGINS, String.valueOf(totalAnonLoginNbr));
                      }
                  }
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
      
      /**
       * User logout notification.
       */
      public void notifyLogout(boolean anonymous) {
          notifyLogin(anonymous);
      } 
       
      /**
       * Notify open connection
       */ 
      public void notifyOpenConnection() {
          Runnable runnable = new Runnable() {
              public void run() { 
                  IFtpStatistics stat = m_statistics;
                  if(stat != null) {
                      int currCon = stat.getCurrentConnectionNumber();
                      setValue(I_CURR_CONS, String.valueOf(currCon));
                  
                      int totalCon = stat.getTotalConnectionNumber();
                      setValue(I_TOTAL_CONS, String.valueOf(totalCon));
                  }
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
      
      /**
       * Notify close connection.
       */
      public void notifyCloseConnection() {
          notifyOpenConnection();
      }
      
      /**
       * Make directory notification.
       */
      public void notifyMkdir() {
          Runnable runnable = new Runnable() {
              public void run() { 
                  IFtpStatistics stat = m_statistics;
                  if(stat != null) {
                      int totalMkdir = stat.getTotalDirectoryCreated();
                      setValue(I_DIR_CREATED, String.valueOf(totalMkdir));
                  }
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
  
      /**
       * Make directry notification.
       */
      public void notifyMkdir(final IConnection connection, final FileObject file) {
          Runnable runnable = new Runnable() {
              public void run() { 
                  DirectoryPanel dirPanel = (DirectoryPanel)getContainer().getPluginPanel(PluginPanelContainer.DIR_INDEX);
                  dirPanel.notifyMkdir(connection, file);        
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
      
      /**
       * Directory removal notification.
       */
      public void notifyRmdir() {
          Runnable runnable = new Runnable() {
              public void run() { 
                  IFtpStatistics stat = m_statistics;
                  if(stat != null) {
                      int totalRmdir = stat.getTotalDirectoryRemoved();
                      setValue(I_DIR_REMOVED, String.valueOf(totalRmdir));
                  }
              }
          };
          SwingUtilities.invokeLater(runnable);
      }   
      
      /**
       * Remove directry notification.
       */
      public void notifyRmdir(final IConnection connection, final FileObject file) {
          Runnable runnable = new Runnable() {
              public void run() { 
                  DirectoryPanel dirPanel = (DirectoryPanel)getContainer().getPluginPanel(PluginPanelContainer.DIR_INDEX);
                  dirPanel.notifyRmdir(connection, file);        
              }
          };
          SwingUtilities.invokeLater(runnable);
      }
          
      /** 
       * This can be displayed only when the server is running.
       */
      public boolean canBeDisplayed() {
          return (m_ftpConfig != null);
      }
  
      /**
       * Get the string representation.
       */
      public String toString() {
          return "Statistics";
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/gui/TreePluginPanelContainer.java
  
  Index: TreePluginPanelContainer.java
  ===================================================================
  // $Id: TreePluginPanelContainer.java,v 1.1 2005/09/07 05:04:45 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.gui;
  
  import org.apache.ftpserver.interfaces.IFtpConfig;
  
  import java.awt.BorderLayout;
  import java.util.ArrayList;
  import java.util.Vector;
  
  import javax.swing.JComponent;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JSplitPane;
  import javax.swing.JTree;
  import javax.swing.event.TreeModelListener;
  import javax.swing.event.TreeSelectionEvent;
  import javax.swing.event.TreeSelectionListener;
  import javax.swing.tree.DefaultTreeCellRenderer;
  import javax.swing.tree.TreeModel;
  import javax.swing.tree.TreePath;
  
  /**
   * Tree plugin panel container.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class TreePluginPanelContainer extends JSplitPane 
                                 implements PluginPanelContainer, 
                                            TreeModel, 
                                            TreeSelectionListener {
      
      private static final long serialVersionUID = -6807863406907626635L;
      
      private ArrayList m_pluginPanels = new ArrayList();
      private Vector m_treeListenrs = new Vector();
      
      private IFtpConfig m_fconfig;
      
      private JPanel m_rightPane;
      private JTree m_tree;
      
      /**
       * Add plugin panel.
       */
      public void add(PluginPanel panel) {
          m_pluginPanels.add(panel);
      }
      
      /**
       * Get the plugin panel at the specifid index.
       */
      public PluginPanel getPluginPanel(int index) {
          return (PluginPanel)m_pluginPanels.get(index);
      }
      
      /**
       * Get container panel.
       */
      public JComponent getComponent() {
          setDividerSize(2);
          setDividerLocation(110);
          
          m_tree = new JTree(this);
          putClientProperty("JTree.lineStyle", "Angled");
          
          DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
          renderer.setLeafIcon(null);
          renderer.setOpenIcon(null);
          renderer.setClosedIcon(null);
          m_tree.setCellRenderer(renderer);
          m_tree.addTreeSelectionListener(this);
          
          JScrollPane custPane = new JScrollPane(m_tree, 
                  JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          setLeftComponent(custPane);
          
          m_rightPane = new JPanel(new BorderLayout());
          setRightComponent(m_rightPane);
          return this;
      }
      
      /**
       * Get the selected index.
       */
      public int getSelectedIndex() {
          Object node = m_tree.getSelectionPath().getLastPathComponent();
          return m_pluginPanels.indexOf(node);
      }
      
      /**
       * Set the selected index.
       */
      public void setSelectedIndex(int index) {
          m_tree.setSelectionRow(index);
      }
      
      /**
       * Handle tree selection
       */
      public void valueChanged(TreeSelectionEvent e) {
          
          // check selection value
          int rows[] = m_tree.getSelectionRows();
          if( (rows == null) || (rows.length == 0) ) {
              return;
          }
          
          // return the selected plugin panel
          PluginPanel panel = (PluginPanel)m_pluginPanels.get(rows[rows.length - 1]);
          if(panel.canBeDisplayed()) {
              GuiUtils.showNewPanel(m_rightPane, panel);
          }
      }
      
      /**
       * get root object
       */
      public Object getRoot() {
          return m_pluginPanels.get(0);
      }
      
      /**
       * get child count
       */
      public int getChildCount(Object parent) {
          if(parent == m_pluginPanels.get(0)) {
              return m_pluginPanels.size() - 1;
          }
          return 0;
      }
      
      /**
       * is a leaf or node
       */
      public boolean isLeaf(Object node) {
         return (node != m_pluginPanels.get(0));
      }
      
      /**
       * get child index
       */
      public int getIndexOfChild(Object parent, Object child) {
          return m_pluginPanels.indexOf(child) - 1;
      }
      
      /**
       * add a listener
       */
      public void addTreeModelListener(TreeModelListener l) {
          m_treeListenrs.add(l);
      }
  
      /**
       * Object changed. In our case it is not possible - so igmore it.
       */
      public void valueForPathChanged(TreePath path, Object newValue) {
      }
      
      /**
       * remove a listener
       */
      public void removeTreeModelListener(TreeModelListener l) {
          m_treeListenrs.remove(l);
      }
      
      /** 
       * get child object
       */
      public Object getChild(Object parent, int index) {
          return m_pluginPanels.get(index + 1);
      }
      
      /**
       * Get ftp config.
       */
      public void refresh(IFtpConfig ftpConfig) {
          m_fconfig = ftpConfig;
          for(int i=0; i<m_pluginPanels.size(); ++i) {
              PluginPanel ppanel = (PluginPanel)m_pluginPanels.get(i);
              ppanel.refresh(m_fconfig);
          }
      }
      
      /**
       * Get ftp config.
       */
      public IFtpConfig getFtpConfig() {
          return m_fconfig;
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/gui/UserManagerPanel.java
  
  Index: UserManagerPanel.java
  ===================================================================
  // $Id: UserManagerPanel.java,v 1.1 2005/09/07 05:04:45 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.ftpserver.gui;
  
  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.FlowLayout;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.File;
  import java.util.Iterator;
  import java.util.Random;
  
  import javax.swing.BorderFactory;
  import javax.swing.JButton;
  import javax.swing.JCheckBox;
  import javax.swing.JComboBox;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JPasswordField;
  import javax.swing.JTextField;
  import javax.swing.SwingConstants;
  
  import org.apache.ftpserver.ftplet.FtpException;
  import org.apache.ftpserver.ftplet.User;
  import org.apache.ftpserver.ftplet.UserManager;
  import org.apache.ftpserver.interfaces.IFtpConfig;
  import org.apache.ftpserver.usermanager.BaseUser;
  
  /**
   * User management panel.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class UserManagerPanel extends PluginPanel implements ActionListener {
  
      private static final long serialVersionUID = 2496923918460548623L;
  
      private final static Random PASS_GEN = new Random(System.currentTimeMillis()); 
      
      private final static Object[] BYTE_RATES = {
          "No limit",
          new Integer(1200),
          new Integer(2400),
          new Integer(4800),
          new Integer(9600),
          new Integer(14400),
          new Integer(28800),    
          new Integer(57600),
          new Integer(115200)    
      };                   
      
      private final static Object[] IDLE_SECONDS = {
          "No limit",
          new Integer(60),
          new Integer(300),
          new Integer(900),
          new Integer(1800),
          new Integer(3600)    
      };
      
      private IFtpConfig m_fconfig;                  
      
      private JComboBox m_userLst;
      private JTextField m_nameTxt;
      
      private JPasswordField m_passwordTxt;
      private JPasswordField m_retypePasswordTxt;
      private JCheckBox m_passwordChkBox;
      
      private JTextField m_directoryTxt;
      private JCheckBox m_enabledChkBox;
      private JCheckBox m_writeChkBox;
      private JComboBox m_idleLst;
      private JComboBox m_uploadLst;
      private JComboBox m_downloadLst;
      
      /**
       * Constructor - create all UI components.
       */
      public UserManagerPanel(PluginPanelContainer container) {
          super(container);
          initComponents();
      }
      
      /**
       * Initial all UI components.
       */
      private void initComponents() {
          
          setLayout(new BorderLayout());
          
          JPanel topPanel = new JPanel(new GridBagLayout()); 
          topPanel.setBorder(BorderFactory.createEtchedBorder());
          GridBagConstraints gc = new GridBagConstraints();
          gc.insets = new Insets(4, 0, 0, 5);
          add(topPanel, BorderLayout.CENTER);
          int yindex = -1;        
  
          // user list
          m_userLst = new JComboBox();
          m_userLst.addActionListener(this);
          m_userLst.setPreferredSize(new Dimension(120, 22));
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 3;
          gc.anchor = GridBagConstraints.CENTER;
          topPanel.add(m_userLst, gc);
          
          // user name
          JLabel nameLab = new JLabel("Name :: ");
          nameLab.setHorizontalAlignment(JLabel.RIGHT);
          nameLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(nameLab, gc);
           
          m_nameTxt = new JTextField();
          m_nameTxt.setColumns(12);
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_nameTxt, gc);
          
          // password
          JLabel passwordLab = new JLabel("Password :: ");
          passwordLab.setHorizontalAlignment(JLabel.RIGHT);
          passwordLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(passwordLab, gc);        
          
          m_passwordTxt = new JPasswordField();
          m_passwordTxt.setColumns(12);
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_passwordTxt, gc);
  
          JButton generatePassBtn = new JButton("Generate");
          generatePassBtn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  generatePassword();
              }
           });
          generatePassBtn.setPreferredSize(new Dimension(90, 22));
          gc.gridx = 2;
          gc.gridy = yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(generatePassBtn, gc);
          
          // retype password
          JLabel retypePasswordLab = new JLabel("Retype Password :: ");
          retypePasswordLab.setHorizontalAlignment(JLabel.RIGHT);
          retypePasswordLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(retypePasswordLab, gc);
          
          m_retypePasswordTxt = new JPasswordField();
          m_retypePasswordTxt.setColumns(12);
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_retypePasswordTxt, gc);
          
          // set password
          JLabel setPasswordLab = new JLabel("Set Password :: ");
          setPasswordLab.setHorizontalAlignment(JLabel.RIGHT);
          setPasswordLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(setPasswordLab, gc);
          
          m_passwordChkBox = new JCheckBox();
          m_passwordChkBox.setHorizontalTextPosition(SwingConstants.LEFT);
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_passwordChkBox, gc);
  
          // root directory
          JLabel directoryLab = new JLabel("Root Directory :: ");
          directoryLab.setHorizontalAlignment(JLabel.RIGHT);
          directoryLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(directoryLab, gc);
          
          m_directoryTxt = new JTextField("./res/home");
          m_directoryTxt.setColumns(12);
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_directoryTxt, gc); 
          
          JButton directoryBtn = new JButton("Browse");
          directoryBtn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  String dir = DirChooser.showDialog(UserManagerPanel.this, "Select User Home", null);
                  if(dir != null) {
                      m_directoryTxt.setText(dir);
                  }
              }
           });
          directoryBtn.setPreferredSize(new Dimension(90, 22));
          gc.gridx = 2;
          gc.gridy = yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(directoryBtn, gc);
          
          // enable/disable
          JLabel enabledLab = new JLabel("Enabled :: ");
          enabledLab.setHorizontalAlignment(JLabel.RIGHT);
          enabledLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(enabledLab, gc);
  
          m_enabledChkBox = new JCheckBox();
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_enabledChkBox, gc);
          
          // write permission
          JLabel writePermLab = new JLabel("Write Permission :: ");
          writePermLab.setHorizontalAlignment(JLabel.RIGHT);
          writePermLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(writePermLab, gc);
          
          m_writeChkBox = new JCheckBox();
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_writeChkBox, gc);
                  
          // idle time
          JLabel idleLab = new JLabel("Max. Idle Time (seconds) :: ");
          idleLab.setHorizontalAlignment(JLabel.RIGHT);
          idleLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(idleLab, gc);
          
          m_idleLst = new JComboBox(IDLE_SECONDS);
          m_idleLst.setPreferredSize(new Dimension(130, 22));
          m_idleLst.setEditable(true);
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_idleLst, gc);
          
          // user upload limit
          JLabel uploadLab = new JLabel("Max. Upload (bytes/sec) :: ");
          uploadLab.setHorizontalAlignment(JLabel.RIGHT);
          uploadLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(uploadLab, gc);
          
          m_uploadLst = new JComboBox(BYTE_RATES);
          m_uploadLst.setPreferredSize(new Dimension(130, 22));
          m_uploadLst.setEditable(true);
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_uploadLst, gc);
          
          // user download limit
          JLabel jDownloadLab = new JLabel("Max. Download (bytes/sec) :: ");
          jDownloadLab.setHorizontalAlignment(JLabel.RIGHT);
          jDownloadLab.setForeground(Color.black);
          gc.gridx = 0;
          gc.gridy = ++yindex;
          gc.gridwidth = 1;
          gc.anchor = GridBagConstraints.EAST;
          topPanel.add(jDownloadLab, gc);
          
          m_downloadLst = new JComboBox(BYTE_RATES);
          m_downloadLst.setPreferredSize(new Dimension(130, 22));
          m_downloadLst.setEditable(true);
          gc.gridx = 1;
          gc.gridy = yindex;
          gc.gridwidth = 2;
          gc.anchor = GridBagConstraints.WEST;
          topPanel.add(m_downloadLst, gc);
          
          JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
          add(btnPanel, BorderLayout.SOUTH);
          
          // save user
          JButton saveBtn = new JButton("Save");
          saveBtn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  save();
              }
          });
          btnPanel.add(saveBtn);
          
          // delete user
          JButton deleteBtn = new JButton("Delete");
          deleteBtn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  delete();
              }
          });
          btnPanel.add(deleteBtn);
          
          // reload user data
          JButton reloadBtn = new JButton("Reload");
          reloadBtn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  refresh(m_fconfig);
              }
          });
          btnPanel.add(reloadBtn);
      }
      
      /**
       * Generate random password.
       */
      private void generatePassword() {
          StringBuffer sb = new StringBuffer(8);
          for(int i=0; i<8; i++) {
              int charType = PASS_GEN.nextInt(3);
              switch (charType) {
              
                  // number
                  case 0:
                      sb.append( (char)('0' + PASS_GEN.nextInt(10)) );
                      break;
                  
                  // uppercase character    
                  case 1:    
                      sb.append( (char)('A' + PASS_GEN.nextInt(26)) );
                      break;
                      
                  // lowercase character
                  case 2:
                      sb.append( (char)('a' + PASS_GEN.nextInt(26)) );
                      break;
              }
          }
          String password = sb.toString();
          int option = JOptionPane.showConfirmDialog(this, 
                                                     "Generated password: " + password, 
                                                     "Password Generation", 
                                                     JOptionPane.OK_CANCEL_OPTION,
                                                     JOptionPane.INFORMATION_MESSAGE);
          if(option == JOptionPane.OK_OPTION) {
              m_passwordTxt.setText(password);
              m_retypePasswordTxt.setText(password);
              m_passwordChkBox.setSelected(true);
          }
      }
  
      
      /**
       * Refresh the panel - set the ftp config.
       */
      public void refresh(IFtpConfig config) {
          m_fconfig = config;
          m_userLst.removeAllItems();
          if(m_fconfig == null) {
              return;
          }
          
          UserManager userManager = m_fconfig.getUserManager();
          try {
              Iterator userIt = userManager.getAllUserNames().iterator();
              boolean hasUser = false;
              while(userIt.hasNext()) {
                  hasUser = true;
                  m_userLst.addItem(userIt.next());
              }
              if(hasUser) {
                  m_userLst.setSelectedIndex(0);
              }
          }
          catch(Exception ex) {
              ex.printStackTrace();
              GuiUtils.showErrorMessage(this, "Cannot load user list.");
          }
      }
      
      /**
       * List selection changed. 
       */
      public void actionPerformed(ActionEvent e) {
          Object selVal = m_userLst.getSelectedItem();
          try {      
              if(selVal != null) {
                  String userName = selVal.toString();
                  User user = m_fconfig.getUserManager().getUserByName(userName);
                  if (user == null) {
                      GuiUtils.showErrorMessage(this, userName + " : does not exist.");
                      refresh(m_fconfig);
                  }
                  else {
                      
                      // populate UI components
                      m_nameTxt.setText(user.getName());
                      m_passwordTxt.setText("");
                      m_retypePasswordTxt.setText("");
                      m_passwordChkBox.setSelected(false);
                      m_directoryTxt.setText(user.getHomeDirectory());
                      m_enabledChkBox.setSelected(user.getEnabled());
                      m_writeChkBox.setSelected(user.getWritePermission());
                      setIdleTimeCombo(m_idleLst, user.getMaxIdleTime());
                      setByteRateCombo(m_uploadLst, user.getMaxUploadRate());
                      setByteRateCombo(m_downloadLst, user.getMaxDownloadRate());
                  }
              }
          }
          catch(FtpException ex) {
              GuiUtils.showErrorMessage(this, "Cannot fetch user information : " + selVal);
          }
      } 
  
          
      /**
       * Save user.
       */
      private void save() {
          // check user name field
          String userName = m_nameTxt.getText().trim();
          if(userName.equals("")) {
              GuiUtils.showErrorMessage(this, "Please enter an user name");
              return;
          }
          
          try {
              BaseUser user = new BaseUser();
              user.setName(userName);
              if(setPassword(user)) {
                  user.setHomeDirectory(new File(m_directoryTxt.getText()).getCanonicalPath());
                  user.setEnabled(m_enabledChkBox.isSelected());
                  user.setWritePermission(m_writeChkBox.isSelected());
                  user.setMaxIdleTime(getMaxIdleTime(m_uploadLst));
                  user.setMaxUploadRate(getBytesTransferRate(m_uploadLst));
                  user.setMaxDownloadRate(getBytesTransferRate(m_downloadLst));
                  m_fconfig.getUserManager().save(user);
                  refresh(m_fconfig);
                  GuiUtils.showInformationMessage(this, "Saved user - " + user.getName());
              }
          }
          catch(Exception ex) {
              GuiUtils.showErrorMessage(this, ex.getMessage());
          }
      }
      
      
      /**
       * Set password if necessary.
       */
      private boolean setPassword(BaseUser usr) throws FtpException {
          
          String userName = usr.getName();
          boolean bNewUser = !m_fconfig.getUserManager().doesExist(userName);
          boolean bPassSet = m_passwordChkBox.isSelected();
          String password = new String(m_passwordTxt.getPassword());
          String repassword = new String(m_retypePasswordTxt.getPassword()); 
          boolean bAnonymous = userName.equals("anonymous");
          
          // new user
          if( bNewUser && (!bPassSet) && (!bAnonymous) ) {
              GuiUtils.showErrorMessage(this, "New user - password required");
              return false;
          }
          
          // password set 
          if( bPassSet && (!password.equals(repassword)) && (!bAnonymous) ) {
              GuiUtils.showErrorMessage(this, "Password entries are not equal");
              return false;
          }
          
          // set password if necessary
          if(bPassSet && (!bAnonymous)) {
              usr.setPassword(password);
          }
          else {
              usr.setPassword(null);
          }
          return true;
      } 
  
      /**
       * Get max bytes/sec.
       */
      private int getBytesTransferRate(JComboBox combo) {
          int rate = 0;
          Object selObj = combo.getSelectedItem();
          if (!selObj.equals(BYTE_RATES[0])) {
              try {
                  rate = Integer.parseInt(selObj.toString());
              }
              catch(NumberFormatException ex) {
                  GuiUtils.showErrorMessage(this, ex.getMessage());
              }
          }
          
          return rate;
      }
       
      /**
       * Get max idle time in sec.
       */
      private int getMaxIdleTime(JComboBox combo) {
          int sec = 0;
          Object selObj = combo.getSelectedItem();
          if (!selObj.equals(IDLE_SECONDS[0])) {
              try {
                  sec = Integer.parseInt(selObj.toString());
              }
              catch(NumberFormatException ex) {
                  GuiUtils.showErrorMessage(this, ex.getMessage());
              }
          }
          
          return sec;
      }    
      
      /**
       * Set byte transfer rate combo.
       */
      private void setByteRateCombo(JComboBox combo, int rate) {
          Object selItem = new Integer(rate);
          if (rate == 0) {
              selItem = BYTE_RATES[0];
          }
          combo.setSelectedItem(selItem);
      }
      
      /**
       * Set idle time combo.
       */
      private void setIdleTimeCombo(JComboBox combo, int idle) {
          Object selItem = new Integer(idle);
          if (idle == 0){
              selItem = IDLE_SECONDS[0];
          }
          combo.setSelectedItem(selItem);
      }
  
      
      /**
       * Delete user.
       */
      private void delete() {
          Object selVal = m_userLst.getSelectedItem();
          if(selVal == null) {
              return;
          }
          
          String userName = selVal.toString();
          if(!GuiUtils.getConfirmation(this, "Do you really want to delete user " + userName + "?")) {
              return;
          }
          
          try {
              m_fconfig.getUserManager().delete(userName);
              refresh(m_fconfig);
              GuiUtils.showInformationMessage(this, "Deleted user - " + userName);
          }
          catch(Exception ex) {
              GuiUtils.showErrorMessage(this, "User delete error : " + userName);
          }
      }
      
      
      /**
       * Can this panel be displayed.
       */
      public boolean canBeDisplayed() {
          return (m_fconfig != null);
      }
      
      
      /**
       * String representation of this panel.
       */
      public String toString() {
          return "Users";
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@incubator.apache.org
For additional commands, e-mail: cvs-help@incubator.apache.org


Mime
View raw message