Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.common.mru » [javadoc | source]
    1   /*
    2    *  SSHTools - Java SSH2 API
    3    *
    4    *  Copyright (C) 2002-2003 Lee David Painter and Contributors.
    5    *
    6    *  Contributions made by:
    7    *
    8    *  Brett Smith
    9    *  Richard Pernavas
   10    *  Erwin Bolwidt
   11    *
   12    *  This program is free software; you can redistribute it and/or
   13    *  modify it under the terms of the GNU General Public License
   14    *  as published by the Free Software Foundation; either version 2
   15    *  of the License, or (at your option) any later version.
   16    *
   17    *  This program is distributed in the hope that it will be useful,
   18    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   19    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20    *  GNU General Public License for more details.
   21    *
   22    *  You should have received a copy of the GNU General Public License
   23    *  along with this program; if not, write to the Free Software
   24    *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   25    */
   26   package com.sshtools.common.mru;
   27   
   28   import java.awt;
   29   import java.awt.event;
   30   
   31   import java.io;
   32   
   33   import javax.swing;
   34   import javax.swing.event;
   35   
   36   
   37   /**
   38    *
   39    *
   40    * @author $author$
   41    * @version $Revision: 1.13 $
   42    */
   43   public class MRUMenu extends JMenu implements ListDataListener, ActionListener {
   44       private MRUListModel model;
   45   
   46       /**
   47   * Creates a new MRUMenu object.
   48   *
   49   * @param action
   50   * @param model
   51   */
   52       protected MRUMenu(Action action, MRUListModel model) {
   53           super(action);
   54           init(model);
   55       }
   56   
   57       /**
   58   * Creates a new MRUMenu object.
   59   *
   60   * @param text
   61   * @param model
   62   */
   63       protected MRUMenu(String text, MRUListModel model) {
   64           super(text);
   65           init(model);
   66       }
   67   
   68       private void init(MRUListModel model) {
   69           this.model = model;
   70           rebuildMenu();
   71           model.addListDataListener(this);
   72       }
   73   
   74       /**
   75   *
   76   */
   77       public void cleanUp() {
   78           model.removeListDataListener(this);
   79       }
   80   
   81       /**
   82   *
   83   *
   84   * @param e
   85   */
   86       public void intervalAdded(ListDataEvent e) {
   87           rebuildMenu();
   88       }
   89   
   90       /**
   91   *
   92   *
   93   * @param e
   94   */
   95       public void intervalRemoved(ListDataEvent e) {
   96           rebuildMenu();
   97       }
   98   
   99       /**
  100   *
  101   *
  102   * @param e
  103   */
  104       public void contentsChanged(ListDataEvent e) {
  105           rebuildMenu();
  106       }
  107   
  108       /**
  109   *
  110   *
  111   * @param evt
  112   */
  113       public void actionPerformed(ActionEvent evt) {
  114           fireActionPerformed(evt);
  115       }
  116   
  117       private void rebuildMenu() {
  118           Component[] c = getMenuComponents();
  119   
  120           for (int i = 0; (c != null) && (i < c.length); i++) {
  121               ((JMenuItem) c[i]).removeActionListener(this);
  122               remove(c[i]);
  123           }
  124   
  125           for (int i = 0; i < model.getSize(); i++) {
  126               File f = (File) model.getElementAt(i);
  127               JMenuItem m = new JMenuItem(f.getName());
  128               m.setActionCommand(f.getAbsolutePath());
  129               m.setToolTipText(f.getAbsolutePath());
  130               m.addActionListener(this);
  131               add(m);
  132           }
  133   
  134           setEnabled(model.getSize() > 0);
  135           validate();
  136       }
  137   }

Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.common.mru » [javadoc | source]