01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
10: //The Original Code is "The Columba Project"
11: //
12: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14: //
15: //All Rights Reserved.
16:
17: package org.columba.core.gui.base;
18:
19: import java.awt.event.ActionEvent;
20: import java.awt.event.ActionListener;
21:
22: import javax.swing.JPopupMenu;
23: import javax.swing.MenuSelectionManager;
24: import javax.swing.Timer;
25: import javax.swing.event.ChangeEvent;
26: import javax.swing.event.ChangeListener;
27:
28: /**
29: * Extension of the JPopupMenu so that if nothing is selected, the menu is closed after 1 second.
30: *
31: * @author tstich
32: */
33: public class SelfClosingPopupMenu implements ActionListener,
34: ChangeListener {
35:
36: protected Timer timer;
37: private JPopupMenu popupMenu;
38:
39: public SelfClosingPopupMenu(JPopupMenu popupMenu) {
40: super ();
41:
42: this .popupMenu = popupMenu;
43:
44: timer = new Timer(1000, this );
45: timer.setRepeats(false);
46:
47: MenuSelectionManager.defaultManager().addChangeListener(this );
48:
49: }
50:
51: /**
52: * @see javax.swing.JPopupMenu#show(java.awt.Component, int, int)
53: */
54: // public void show(Component invoker, int x, int y) {
55: // super.show(invoker, x, y);
56: // }
57: /**
58: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
59: */
60: public void actionPerformed(ActionEvent e) {
61: popupMenu.setVisible(false);
62: }
63:
64: /**
65: * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
66: */
67: public void stateChanged(ChangeEvent e) {
68: // The length is 1 if no item is selected
69: if (MenuSelectionManager.defaultManager().getSelectedPath().length == 1) {
70: timer.start();
71: } else {
72: timer.stop();
73: }
74: }
75: }
|