KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JMenu


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: JMenu.java,v $
11    Revision 1.16 2004/05/05 12:43:21 bobintetley
12    Patches/new files from Laurent Martell
13
14    Revision 1.15 2004/04/30 16:52:17 bobintetley
15    MenuListener support, JViewport support, TreeSelectionModel stubs, additional JTree methods
16
17    Revision 1.14 2004/01/26 08:11:00 bobintetley
18    Many bugfixes and addition of SwingSet
19
20    Revision 1.13 2004/01/23 08:05:07 bobintetley
21    JComboBox fixes and better Action implementation
22
23    Revision 1.12 2004/01/20 09:17:15 bobintetley
24    Menu class overhaul for compatibility, Action support and thread safety
25
26    Revision 1.11 2003/12/16 18:49:07 bobintetley
27    removeAll() now works
28
29    Revision 1.10 2003/12/15 18:29:57 bobintetley
30    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
31
32    Revision 1.9 2003/12/15 16:40:04 bobintetley
33    Core methods + skeleton JTableHeader/JScrollBar support
34
35    Revision 1.8 2003/12/14 09:13:38 bobintetley
36    Added CVS log to source headers
37
38 */

39
40
41 package swingwtx.swing;
42
43 import org.eclipse.swt.widgets.*;
44 import org.eclipse.swt.*;
45
46 import swingwt.awt.Component;
47
48 import java.util.*;
49
50 public class JMenu extends JSWTMenuComponent {
51     
52     /** Menu container for this item if it has one */
53     private Menu container = null;
54     /** The shell this menu is living on, or null if
55      * it is a popup menu */

56     private Shell shell = null;
57
58     /** Stores children until we're ready */
59     private Vector components = new Vector();
60     
61     public JMenu() {}
62     public JMenu(Action a) { setAction(a); }
63     public JMenu(String JavaDoc text) { this(text, false); }
64     public JMenu(String JavaDoc text, boolean isTearOff) { pText = text; }
65     
66     /** Adds a component to the container */
67     public JSWTMenuComponent add(final JSWTMenuComponent c) {
68         components.add(c);
69         SwingUtilities.invokeSync(new Runnable JavaDoc() {
70             public void run() {
71                 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) {
72                     try {
73                         c.setSwingWTParent(container, shell);
74                         c.setCachedProperties();
75                         c.registerEvents();
76                     }
77                     catch (Exception JavaDoc e) {
78                         e.printStackTrace();
79                     }
80                 }
81             }
82         });
83         return c;
84     }
85     
86     public JSWTMenuComponent add(JSWTMenuComponent c, String JavaDoc name) {
87         return add(c);
88     }
89     
90     public JMenu add(JMenu c) {
91         return (JMenu) add((JSWTMenuComponent) c);
92     }
93     
94     public JMenu add(JMenu c, String JavaDoc name) {
95         return (JMenu) add((JSWTMenuComponent) c, name);
96     }
97     
98     public JMenuItem add(JMenuItem c) {
99         return (JMenuItem) add((JSWTMenuComponent) c);
100     }
101     
102     public JMenuItem add(JMenuItem c, String JavaDoc name) {
103         return (JMenuItem) add((JSWTMenuComponent) c, name);
104     }
105     
106     /** Adds an action to the container */
107     public JMenuItem add(Action a) {
108         JMenuItem jmenu = new JMenuItem(a);
109         add(jmenu);
110         return jmenu;
111     }
112     
113     /** Adds a new menu item with the specified text */
114     public JMenuItem add(String JavaDoc text) {
115         JMenuItem jmenu = new JMenuItem(text);
116         add(jmenu);
117         return jmenu;
118     }
119     
120     /** Adds a component to the menu.
121      * @throws IllegalArgumentException - SwingWT cannot support this.
122      */

123     public Component add(Component c) {
124         throw new IllegalArgumentException JavaDoc("Sorry, SwingWT cannot support components in menus");
125     }
126     
127     /** Adds a component to the menu at the index specified.
128      * @throws IllegalArgumentException - SwingWT cannot support this.
129      */

130     public Component add(Component c, int index) {
131         throw new IllegalArgumentException JavaDoc("Sorry, SwingWT cannot support components in menus");
132     }
133     
134     /** Removes a component from the menu
135      * @throws IllegalArgumentException - SwingWT cannot support this.
136      */

137     public void remove(Component c) {
138         throw new IllegalArgumentException JavaDoc("Sorry, SwingWT cannot support components in menus");
139     }
140     
141     public void remove(JSWTMenuComponent c) {
142         c.dispose();
143     }
144     
145     /** Removes the menu item with the specified index from the list */
146     public void remove(int index) {
147         JSWTMenuComponent c = (JSWTMenuComponent) components.get(index);
148         components.remove(index);
149         c.dispose();
150     }
151     
152     public void removeAll() {
153         Iterator i = components.iterator();
154         while (i.hasNext()) {
155             JSWTMenuComponent c = (JSWTMenuComponent) i.next();
156             c.dispose();
157         }
158         
159         SwingUtilities.invokeSync(new Runnable JavaDoc() {
160             public void run() {
161                 if (peer != null && container != null) {
162                     // Kill the old container, destroying the children
163
container.dispose();
164                     // Recreate it
165
container = new Menu(shell, SWT.DROP_DOWN);
166                     peer.setMenu(container);
167                 }
168             }
169         });
170     }
171
172     public void addSeparator() {
173         add(new JSeparator());
174     }
175     
176     /** Generates a new JPopupMenu with references to the
177      * same child components as this JMenu. Should work just
178      * like Swing in practice (and doesn't waste objects - aside
179      * from the JPopupMenu itself, the existing objects are reused
180      */

181     public JPopupMenu getPopupMenu() {
182         return new JPopupMenu(components);
183     }
184     
185     public void setSwingWTParent(Menu parent, Shell shell) throws Exception JavaDoc {
186         this.shell = shell;
187         peer = new MenuItem(parent, SWT.CASCADE);
188         container = new Menu(shell, SWT.DROP_DOWN);
189         peer.setMenu(container);
190         
191         // If we have some children waiting to be added, do it now
192
if (components.size() > 0) {
193              Object JavaDoc[] o = components.toArray();
194              for (int i = 0; i < o.length; i++)
195                   add((JSWTMenuComponent) o[i]);
196         }
197         
198     }
199     
200     public boolean isSelected() {
201         return false;
202     }
203     
204     public void setSelected(boolean b) {
205     }
206     
207 }
208
Popular Tags