001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.mru;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030:
031: import java.io.*;
032:
033: import javax.swing.*;
034: import javax.swing.event.*;
035:
036: /**
037: *
038: *
039: * @author $author$
040: * @version $Revision: 1.13 $
041: */
042: public class MRUMenu extends JMenu implements ListDataListener,
043: ActionListener {
044: private MRUListModel model;
045:
046: /**
047: * Creates a new MRUMenu object.
048: *
049: * @param action
050: * @param model
051: */
052: protected MRUMenu(Action action, MRUListModel model) {
053: super (action);
054: init(model);
055: }
056:
057: /**
058: * Creates a new MRUMenu object.
059: *
060: * @param text
061: * @param model
062: */
063: protected MRUMenu(String text, MRUListModel model) {
064: super (text);
065: init(model);
066: }
067:
068: private void init(MRUListModel model) {
069: this .model = model;
070: rebuildMenu();
071: model.addListDataListener(this );
072: }
073:
074: /**
075: *
076: */
077: public void cleanUp() {
078: model.removeListDataListener(this );
079: }
080:
081: /**
082: *
083: *
084: * @param e
085: */
086: public void intervalAdded(ListDataEvent e) {
087: rebuildMenu();
088: }
089:
090: /**
091: *
092: *
093: * @param e
094: */
095: public void intervalRemoved(ListDataEvent e) {
096: rebuildMenu();
097: }
098:
099: /**
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: }
|