KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JSWTMenuComponent


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: JSWTMenuComponent.java,v $
11    Revision 1.26 2004/05/06 13:10:27 bobintetley
12    Fix to menu accelerator key display
13
14    Revision 1.25 2004/05/06 12:35:22 bobintetley
15    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
16
17    Revision 1.24 2004/04/30 16:52:17 bobintetley
18    MenuListener support, JViewport support, TreeSelectionModel stubs, additional JTree methods
19
20    Revision 1.23 2004/03/30 10:42:46 bobintetley
21    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
22
23    Revision 1.22 2004/02/13 22:55:29 djspiewak
24    Improved the JToolBar class and added two new methods
25
26    Revision 1.21 2004/01/23 08:05:13 bobintetley
27    JComboBox fixes and better Action implementation
28
29    Revision 1.20 2004/01/20 09:17:15 bobintetley
30    Menu class overhaul for compatibility, Action support and thread safety
31
32    Revision 1.19 2004/01/20 07:38:05 bobintetley
33    Bug fixes and compatibility methods
34
35    Revision 1.18 2004/01/16 15:53:32 bobintetley
36    Many compatibility methods added to Container, Component, JInternalFrame,
37       UIManager, SwingUtilities, JTabbedPane, JPasswordField, JCheckBox
38       and JRadioButton.
39
40    Revision 1.17 2003/12/17 16:35:52 bobintetley
41    Mnemonics are no longer case sensitive
42
43    Revision 1.16 2003/12/16 19:04:38 bobintetley
44    Fix to broken mnemonics
45
46    Revision 1.15 2003/12/16 18:04:10 bobintetley
47    Fixes to handling of mnemonics
48
49    Revision 1.14 2003/12/15 18:46:56 bobintetley
50    Menu tooltip text skeleton
51
52    Revision 1.13 2003/12/15 18:29:57 bobintetley
53    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
54
55    Revision 1.12 2003/12/15 16:40:04 bobintetley
56    Core methods + skeleton JTableHeader/JScrollBar support
57
58    Revision 1.11 2003/12/14 09:13:38 bobintetley
59    Added CVS log to source headers
60
61 */

62
63
64 package swingwtx.swing;
65
66 import java.util.Vector JavaDoc;
67
68 import org.eclipse.swt.widgets.Widget;
69
70 import swingwt.awt.event.ActionEvent;
71 import swingwt.awt.event.ActionListener;
72 import swingwt.awt.event.InputEvent;
73 import swingwt.awt.event.KeyEvent;
74 import swingwtx.swing.event.MenuEvent;
75 import swingwtx.swing.event.MenuListener;
76
77 public abstract class JSWTMenuComponent extends AbstractButton {
78     
79     /** The SWT peer this component is representing */
80     protected org.eclipse.swt.widgets.MenuItem peer = null;
81     
82     /** Cache value */
83     protected boolean pEnabled = true;
84     /** Cache value */
85     protected Icon pImage = null;
86     /** Cache value */
87     protected char pMnemonic = ' ';
88     /** Cache value */
89     protected int pAccelerator = 0;
90     protected String JavaDoc pNamedAccelerator = "";
91     /** Cache value */
92     protected String JavaDoc pText = "";
93     /** Action command */
94     protected String JavaDoc actionCommand = "";
95     /** Swing action used to generate the component */
96     protected Action menuAction = null;
97     
98     /** Thread safe return value */
99     private Object JavaDoc retval;
100     
101     /** Action Listener objects */
102     protected Vector JavaDoc actionListeners = new Vector JavaDoc();
103     
104     /** Menu Listener objects */
105     protected Vector JavaDoc menuListeners = new Vector JavaDoc();
106     
107     /* Menu event constants */
108     protected final static int SELECTED = 0;
109     protected final static int DESELECTED = 1;
110     protected final static int CANCELED = 2;
111     
112     public void setIcon(final Icon icon) {
113         pImage = icon;
114         final JSWTMenuComponent pthis = this;
115         SwingUtilities.invokeSync(new Runnable JavaDoc() {
116             public void run() {
117                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
118                     peer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, icon));
119             }
120         });
121     }
122             
123     public void addMenuListener(MenuListener l) {
124         menuListeners.add(l);
125     }
126     
127     public void removeMenuListener(MenuListener l) {
128         menuListeners.remove(l);
129     }
130     
131     public int getMnemonic() { return (int) pMnemonic; };
132     public void setMnemonic(char mnemonic) { pMnemonic = mnemonic; showMnemonic(); }
133     public void setMnemonic(int mnemonic) { pMnemonic = (char) mnemonic; showMnemonic(); }
134     
135     /** @deprecated - Replaced by <code>getText</code> */
136     public String JavaDoc getLabel() { return getText(); }
137     /** @deprecated - Replaced by <code>setText</code> */
138     public void setLabel(String JavaDoc label) { setText(label); }
139     
140     public String JavaDoc getText() {
141         retval = "";
142         SwingUtilities.invokeSync(new Runnable JavaDoc() {
143             public void run() {
144                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
145                     retval = peer.getText();
146                 else
147                     retval = pText;
148             }
149         });
150         return retval.toString();
151     }
152     
153     public void setText(String JavaDoc text) {
154         if (text == null) text = "";
155         pText = text;
156         SwingUtilities.invokeSync(new Runnable JavaDoc() {
157             public void run() {
158                 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) {
159                     peer.setText(pText); showMnemonic(); showAccelerator();
160                 }
161             }
162         });
163     }
164     
165     public String JavaDoc getToolTipText() { return ""; }
166     public void setToolTipText(String JavaDoc tip) { }
167     
168     public String JavaDoc getActionCommand() { return actionCommand; }
169     public void setActionCommand(String JavaDoc command) { actionCommand = command; }
170
171     protected void showMnemonic() {
172         // If we don't have a mnemonic, don't show it
173
if (pMnemonic == ' ') return;
174         String JavaDoc text = ( peer == null ? pText : peer.getText());
175         // If there is already a mnemonic - don't do anything
176
if (text.indexOf("&") != -1) return;
177         
178         // Convert the mnemonic and text to a lower case string to make
179
// the match case insensitive
180
String JavaDoc lower = text.toLowerCase();
181         String JavaDoc mn = new String JavaDoc(new char[] { pMnemonic }).toLowerCase();
182         int pos = lower.indexOf(mn);
183         
184         if (pos != -1) {
185             text = text.substring(0, pos) + "&" + text.substring(pos, text.length());
186             setText(text);
187         }
188     }
189     
190     private void showAccelerator() {
191         // Already accelerator shown - don't do anything
192
if (pText.indexOf("\t") != -1) return;
193     if (pNamedAccelerator == null) return;
194     if (pNamedAccelerator.equals("")) return;
195         pText += "\t" + pNamedAccelerator;
196         setText(pText);
197     }
198     
199     public void setSwingWTParent(org.eclipse.swt.widgets.Menu parent, org.eclipse.swt.widgets.Shell shell) throws Exception JavaDoc {
200         throw new Exception JavaDoc("Override in subclass");
201     }
202     
203         
204     /** Handles registering of accelerator keys for menu items. */
205     public void setAccelerator(final KeyStroke k) {
206         
207         // Translate the keystroke to an equivalent SWT
208
SwingUtilities.invokeSync(new Runnable JavaDoc() {
209             public void run() {
210                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
211                     peer.setAccelerator( swingwt.awt.event.KeyEvent.translateAWTKey(k.getKeyCode())
212                         + swingwt.awt.event.InputEvent.translateAWTModifiers(k.getModifiers()));
213                 else
214                     pAccelerator = swingwt.awt.event.KeyEvent.translateAWTKey(k.getKeyCode())
215                         + swingwt.awt.event.InputEvent.translateAWTModifiers(k.getModifiers());
216             }
217         });
218         
219         // Make a string to represent the accelerator
220
pNamedAccelerator = "";
221         if ((k.getModifiers() & InputEvent.CTRL_MASK) > 0) pNamedAccelerator += "Ctrl+";
222         if ((k.getModifiers() & InputEvent.ALT_MASK) > 0)
223             pNamedAccelerator += "Alt+";
224         if ((k.getModifiers() & InputEvent.SHIFT_DOWN_MASK) > 0 || (k.getModifiers() & InputEvent.SHIFT_MASK) > 0)
225             pNamedAccelerator += "Shift+";
226         if (k.getKeyCode() == KeyEvent.VK_SPACE) pNamedAccelerator += "Space";
227         else if (k.getKeyCode() == KeyEvent.VK_F1) pNamedAccelerator += "F1";
228         else if (k.getKeyCode() == KeyEvent.VK_F2) pNamedAccelerator += "F2";
229         else if (k.getKeyCode() == KeyEvent.VK_F3) pNamedAccelerator += "F3";
230         else if (k.getKeyCode() == KeyEvent.VK_F4) pNamedAccelerator += "F4";
231         else if (k.getKeyCode() == KeyEvent.VK_F5) pNamedAccelerator += "F5";
232         else if (k.getKeyCode() == KeyEvent.VK_F6) pNamedAccelerator += "F6";
233         else if (k.getKeyCode() == KeyEvent.VK_F7) pNamedAccelerator += "F7";
234         else if (k.getKeyCode() == KeyEvent.VK_F8) pNamedAccelerator += "F8";
235         else if (k.getKeyCode() == KeyEvent.VK_F9) pNamedAccelerator += "F9";
236         else if (k.getKeyCode() == KeyEvent.VK_F10) pNamedAccelerator += "F10";
237         else if (k.getKeyCode() == KeyEvent.VK_F11) pNamedAccelerator += "F11";
238         else if (k.getKeyCode() == KeyEvent.VK_F12) pNamedAccelerator += "F12";
239         else
240             pNamedAccelerator += new String JavaDoc(new char[] {(char) swingwt.awt.event.KeyEvent.translateAWTKey(k.getKeyCode())} );
241         
242     // Set it
243
showAccelerator();
244     }
245     
246     /** Returns the SWT Menu peer */
247     public Widget getMenuPeer() { return peer; }
248
249     /** Change the enabled state of the component */
250     public void setEnabled(final boolean b) {
251         pEnabled = b;
252         SwingUtilities.invokeSync(new Runnable JavaDoc() {
253             public void run() {
254                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
255                     peer.setEnabled(b);
256             }
257         });
258     }
259     
260     /** Returns true if the component is enabled */
261     public boolean isEnabled() {
262         SwingUtilities.invokeSync(new Runnable JavaDoc() {
263             public void run() {
264                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
265                     retval = new Boolean JavaDoc(peer.isEnabled());
266                 else
267                     retval = new Boolean JavaDoc(pEnabled);
268             }
269         });
270         return ((Boolean JavaDoc) retval).booleanValue();
271     }
272     
273     /** Destroys the component */
274     public void dispose() {
275          SwingUtilities.invokeSync(new Runnable JavaDoc() {
276             public void run() {
277                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
278                     { peer.dispose(); peer = null; }
279             }
280          });
281     }
282     
283     /** Unworkable on native platforms */
284     public void requestFocus() {}
285     
286     
287     /** Add an <code>ActionListener</code> to this component */
288     public void addActionListener(ActionListener l) {
289         actionListeners.add(l);
290     }
291     
292     /** Remove an <code>ActionListener</code> from the component */
293     public void removeActionListener(ActionListener l) {
294         actionListeners.remove(l);
295     }
296
297     /**
298      * Sends action events to listeners
299      */

300     public void processActionEvent(int id) {
301         ActionEvent ae = new ActionEvent(this, id, this.getActionCommand());
302         for (int i = 0; i < actionListeners.size(); i++) {
303             ActionListener al = (ActionListener) actionListeners.get(i);
304             al.actionPerformed(ae);
305         }
306     }
307     
308     /**
309      * Sends menu events to listeners
310      */

311     public void processMenuEvent(int id) {
312         MenuEvent me = new MenuEvent(this);
313         for (int i = 0; i < menuListeners.size(); i++) {
314             MenuListener ml = (MenuListener) menuListeners.get(i);
315             switch (id) {
316                 case SELECTED: ml.menuSelected(me); break;
317                 case DESELECTED: ml.menuDeselected(me); break;
318                 case CANCELED: ml.menuCanceled(me); break;
319             }
320         }
321     }
322
323     
324     /**
325      * When the component has been added to a container, this
326      * routine gets called to tell it to set up SWT listeners
327      * for all its events. We can map these then to the
328      * AWT-style events and Swing events
329      */

330     public void registerEvents() {
331         
332          SwingUtilities.invokeSync(new Runnable JavaDoc() {
333              
334              public void run() {
335
336                 // Action Events
337
// ========================
338
peer.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
339                     public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
340                         processActionEvent(0);
341                     }
342                     public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
343                         //processActionEvent(0);
344
}
345                 });
346                 
347                 // Menu Events
348
peer.addArmListener(new org.eclipse.swt.events.ArmListener() {
349                     public void widgetArmed(org.eclipse.swt.events.ArmEvent e) {
350                         processMenuEvent(SELECTED);
351                     }
352                 });
353                 
354              }
355          });
356          
357     }
358     
359     /**
360      * If the component has just been added to a container, but
361      * properties were set in the meantime, this routine gets
362      * called by JMenu/JMenuBar to set the cached properties.
363      */

364     public void setCachedProperties() {
365         final JSWTMenuComponent pthis = this;
366          SwingUtilities.invokeSync(new Runnable JavaDoc() {
367             public void run() {
368                 peer.setEnabled(pEnabled);
369                 if (pImage != null) peer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, pImage));
370                 if (pMnemonic != ' ') showMnemonic();
371                 if (pAccelerator != 0) { peer.setAccelerator(pAccelerator); showAccelerator(); }
372                 peer.setText(pText);
373             }
374          });
375     }
376     
377 }
378
Popular Tags