KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JMenuBar


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    Contact me by electronic mail: bobintetley@users.sourceforge.net
8
9    $Log: JMenuBar.java,v $
10    Revision 1.17 2004/05/05 12:43:21 bobintetley
11    Patches/new files from Laurent Martell
12
13    Revision 1.16 2004/05/01 09:33:39 bobintetley
14    Removed broken impor
15
16    Revision 1.15 2004/04/30 23:18:26 dannaab
17    List selection support, misc bug fixes
18
19    Revision 1.14 2004/04/30 16:52:17 bobintetley
20    MenuListener support, JViewport support, TreeSelectionModel stubs, additional JTree methods
21
22    Revision 1.13 2004/02/19 09:58:44 bobintetley
23    Various small bug fixes and JTextArea should be much faster/lighter
24
25    Revision 1.12 2004/01/26 14:05:23 bobintetley
26    Compatibility methods
27
28    Revision 1.11 2004/01/26 08:11:00 bobintetley
29    Many bugfixes and addition of SwingSet
30
31    Revision 1.10 2004/01/20 09:17:15 bobintetley
32    Menu class overhaul for compatibility, Action support and thread safety
33
34    Revision 1.9 2003/12/15 18:29:57 bobintetley
35    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
36
37    Revision 1.8 2003/12/14 09:13:38 bobintetley
38    Added CVS log to source headers
39
40 */

41
42
43 package swingwtx.swing;
44
45 import swingwt.awt.Component;
46
47 import org.eclipse.swt.widgets.*;
48 import org.eclipse.swt.*;
49
50 import java.util.*;
51
52 public class JMenuBar extends JComponent {
53     
54     /** The SWT menu this container represents */
55     protected Menu menu = null;
56     /** Stores children until we're ready */
57     protected Vector components = new Vector();
58     /** The SWT shell this MenuBar is attached to */
59     protected Shell shell = null;
60     private Object JavaDoc retval;
61     
62     public JMenuBar() {
63     }
64
65     /** Adds a component to the container */
66     public JSWTMenuComponent add(final JSWTMenuComponent c) {
67         SwingUtilities.invokeSync(new Runnable JavaDoc() {
68             public void run() {
69                 try {
70                     if (menu == null)
71                         components.add(c);
72                     else {
73                         c.setSwingWTParent(menu, shell);
74                         c.setCachedProperties();
75                         c.registerEvents();
76                     }
77                 }
78                 catch (Exception JavaDoc e) {
79                     e.printStackTrace();
80                 }
81             }
82         });
83         return c;
84     }
85     
86     public JMenu add(JMenu c) {
87         return (JMenu) add((JSWTMenuComponent) c);
88     }
89     
90     public Component add(Component c) {
91         if (c instanceof JSWTMenuComponent)
92             return add(((JSWTMenuComponent) c));
93         else
94             return c;
95     }
96     
97         
98     public void remove(JSWTMenuComponent c) {
99         c.dispose();
100     }
101     
102     public void setVisible(final boolean b) {
103         SwingUtilities.invokeSync(new Runnable JavaDoc() {
104             public void run() {
105                 if (menu != null)
106                     menu.setVisible(b);
107             }
108         });
109     }
110     
111     public boolean isVisible() {
112         SwingUtilities.invokeSync(new Runnable JavaDoc() {
113             public void run() {
114                 if (menu != null)
115                     retval = new Boolean JavaDoc(menu.isVisible());
116                 else
117                     retval = new Boolean JavaDoc(false);
118             }
119         });
120         return ((Boolean JavaDoc) retval).booleanValue();
121     }
122     
123     public swingwt.awt.Dimension getPreferredSize() {
124         return new swingwt.awt.Dimension(0, 0);
125     }
126     
127     public void addSeparator() {
128         add(new JSeparator());
129     }
130     
131     public Menu getSWTMenu() {
132         return menu;
133     }
134     
135     public void setSwingWTParent(Shell parent) {
136         shell = parent;
137         menu = new Menu(parent, SWT.BAR);
138         parent.setMenuBar(menu);
139         
140         // If we have some children waiting to be added, do it now
141
if (components.size() > 0) {
142              Iterator i = components.iterator();
143              while (i.hasNext()) {
144                   add((JSWTMenuComponent) i.next());
145              }
146         }
147     }
148 }
149
Popular Tags