KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JPopupMenu


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: JPopupMenu.java,v $
11    Revision 1.12 2004/05/05 12:43:21 bobintetley
12    Patches/new files from Laurent Martell
13
14    Revision 1.11 2004/04/18 12:45:59 bobintetley
15    Popup menu fix to recreate peers when the peer's parent is destroyed
16
17    Revision 1.10 2004/01/26 08:11:00 bobintetley
18    Many bugfixes and addition of SwingSet
19
20    Revision 1.9 2004/01/15 10:11:15 bobintetley
21    Fixed AWT constructors/hierarchy
22
23    Revision 1.8 2003/12/15 18:29:57 bobintetley
24    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
25
26    Revision 1.7 2003/12/14 09:13:38 bobintetley
27    Added CVS log to source headers
28
29 */

30
31
32 package swingwtx.swing;
33
34 import swingwtx.swing.event.*;
35
36 import org.eclipse.swt.widgets.*;
37 import org.eclipse.swt.*;
38
39 import java.util.*;
40
41 public class JPopupMenu extends JMenuBar {
42     
43     protected Vector popupListeners = new Vector();
44     
45     protected final static int CANCELED = 0;
46     protected final static int INVISIBLE = 1;
47     protected final static int VISIBLE = 2;
48     
49     public JPopupMenu() { }
50     public JPopupMenu(String JavaDoc label) { }
51     
52     /** Only to be used by JMenu on demand for getPopupMenu() */
53     protected JPopupMenu(Vector childComponents) { this.components = childComponents; }
54     
55     public void addPopupMenuListener(PopupMenuListener l) {
56         popupListeners.add(l);
57     }
58     
59     public void removePopupMenuListener(PopupMenuListener l) {
60         popupListeners.remove(l);
61     }
62     
63     public void processPopupMenuEvent(int id) {
64         Iterator i = popupListeners.iterator();
65         PopupMenuEvent e = new PopupMenuEvent(this);
66         while (i.hasNext()) {
67             PopupMenuListener l = (PopupMenuListener) i.next();
68             switch(id) {
69                 case CANCELED: l.popupMenuCanceled(e); break;
70                 case INVISIBLE: l.popupMenuWillBecomeInvisible(e); break;
71                 case VISIBLE: l.popupMenuWillBecomeVisible(e); break;
72             }
73         }
74     }
75     
76     public void setVisible(boolean b) {
77         super.setVisible(b);
78         if (b)
79             processPopupMenuEvent(VISIBLE);
80         else {
81             processPopupMenuEvent(INVISIBLE);
82             processPopupMenuEvent(CANCELED);
83         }
84     }
85     
86     public void show(swingwt.awt.Component c, int x, int y) {
87         
88         // Create the actual menu if it hasn't been already
89
// and tie it to the component
90
boolean needToCreatePeer = menu == null;
91     if (!needToCreatePeer)
92         needToCreatePeer = menu.isDisposed();
93     
94         if (needToCreatePeer) {
95             setSwingWTParent(c.getPeer().getShell());
96             c.getPeer().setMenu(menu);
97         }
98         
99         // activate it's popup menu:
100
menu.setVisible(true);
101         
102     }
103
104     public void setSwingWTParent(Shell parent) {
105         shell = parent;
106         menu = new Menu(parent, SWT.POP_UP);
107         // If we have some children waiting to be added, do it now
108
if (components.size() > 0) {
109              Iterator i = components.iterator();
110              while (i.hasNext()) {
111                   add((JSWTMenuComponent) i.next());
112              }
113         }
114     }
115     
116 }
117
Popular Tags