KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > SelfClosingPopupMenu


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
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 import java.awt.event.ActionEvent JavaDoc;
19 import java.awt.event.ActionListener JavaDoc;
20
21 import javax.swing.JPopupMenu JavaDoc;
22 import javax.swing.MenuSelectionManager JavaDoc;
23 import javax.swing.Timer JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26
27 /**
28  * Extension of the JPopupMenu so that if nothing is selected, the menu is closed after 1 second.
29  *
30  * @author tstich
31  */

32 public class SelfClosingPopupMenu implements ActionListener JavaDoc, ChangeListener JavaDoc {
33     
34     protected Timer JavaDoc timer;
35     private JPopupMenu JavaDoc popupMenu;
36     
37     public SelfClosingPopupMenu(JPopupMenu JavaDoc popupMenu) {
38         super();
39         
40         this.popupMenu = popupMenu;
41         
42         timer = new Timer JavaDoc(1000, this);
43         timer.setRepeats(false);
44         
45         MenuSelectionManager.defaultManager().addChangeListener(this);
46         
47     }
48     
49     /**
50      * @see javax.swing.JPopupMenu#show(java.awt.Component, int, int)
51      */

52 // public void show(Component invoker, int x, int y) {
53
// super.show(invoker, x, y);
54
// }
55

56     /**
57      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
58      */

59     public void actionPerformed(ActionEvent JavaDoc e) {
60         popupMenu.setVisible(false);
61     }
62
63     /**
64      * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
65      */

66     public void stateChanged(ChangeEvent JavaDoc e) {
67         // The length is 1 if no item is selected
68
if( MenuSelectionManager.defaultManager().getSelectedPath().length==1 ) {
69             timer.start();
70         } else {
71             timer.stop();
72         }
73     }
74 }
75
Popular Tags