KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JCheckBoxMenuItem


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: JCheckBoxMenuItem.java,v $
11    Revision 1.12 2004/03/30 10:42:46 bobintetley
12    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
13
14    Revision 1.11 2004/01/26 08:11:00 bobintetley
15    Many bugfixes and addition of SwingSet
16
17    Revision 1.10 2004/01/23 08:04:56 bobintetley
18    JComboBox fixes and better Action implementation
19
20    Revision 1.9 2004/01/20 09:17:15 bobintetley
21    Menu class overhaul for compatibility, Action support and thread safety
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/15 15:54:25 bobintetley
27    Additional core methods
28
29    Revision 1.6 2003/12/14 09:13:38 bobintetley
30    Added CVS log to source headers
31
32 */

33
34
35 package swingwtx.swing;
36
37 import org.eclipse.swt.widgets.*;
38 import org.eclipse.swt.*;
39
40 import swingwt.awt.event.*;
41
42 import java.util.*;
43
44 public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants, ButtonModel {
45     
46     protected boolean pState = false;
47     private Shell shell = null;
48     protected Vector itemListeners = new Vector();
49     private Object JavaDoc retval;
50     
51     public JCheckBoxMenuItem() {setModel(this);}
52     public JCheckBoxMenuItem(Action a) { setAction(a); setModel(this);}
53     public JCheckBoxMenuItem(Icon icon) { this("", icon); }
54     public JCheckBoxMenuItem(Icon icon, boolean b) { this("", icon, b); }
55     public JCheckBoxMenuItem(String JavaDoc text) { this(text, null, false); }
56     public JCheckBoxMenuItem(String JavaDoc text, boolean b) { this(text, null, b); }
57     public JCheckBoxMenuItem(String JavaDoc text, Icon icon) { this(text, null, false); }
58     public JCheckBoxMenuItem(String JavaDoc text, Icon icon, boolean b) { pText = text; pImage = icon; pState = b; setModel(this);}
59     public JCheckBoxMenuItem(String JavaDoc text, int mnemonic) { pText = text; setMnemonic(mnemonic); setModel(this);}
60     public JCheckBoxMenuItem(String JavaDoc text, int mnemonic, boolean b) { pText = text; setMnemonic(mnemonic); pState = b; setModel(this); }
61     
62     public void addItemListener(ItemListener l) {
63         itemListeners.add(l);
64     }
65     
66     public void removeItemListener(ItemListener l) {
67         itemListeners.remove(l);
68     }
69     
70     /**
71      * Sends action events to listeners
72      * Overriden from JSWTMenuComponent to handle selection events
73      */

74     public void processActionEvent(int id) {
75         super.processActionEvent(id);
76     processItemEvent();
77     }
78     
79     /**
80      * Handles firing of Item events for when selection changes
81      */

82     public void processItemEvent() {
83         if (itemListeners.size() == 0) return;
84         Iterator i = itemListeners.iterator();
85         ItemEvent e = new ItemEvent(this, 0, this, (ppeer.getSelection() ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
86         while (i.hasNext()) {
87             ItemListener il = (ItemListener) i.next();
88             il.itemStateChanged(e);
89         }
90     }
91     
92     public Object JavaDoc[] getSelectedObjects() { return new Object JavaDoc[] { getText() };}
93     public boolean getState() {
94          SwingUtilities.invokeSync(new Runnable JavaDoc() {
95             public void run() {
96                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
97                     retval = new Boolean JavaDoc(ppeer.getSelection());
98                 else
99                     retval = new Boolean JavaDoc(pState);
100             }
101          });
102          return ((Boolean JavaDoc) retval).booleanValue();
103     }
104         
105     public void setState(boolean b) {
106          pState = b;
107          SwingUtilities.invokeSync(new Runnable JavaDoc() {
108             public void run() {
109                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
110                     ppeer.setSelection(pState);
111             }
112          });
113     }
114     
115     public boolean isSelected() { return getState(); }
116     public void setSelected(boolean b) { setState(b); }
117     
118     public void setSwingWTParent(Menu parent, Shell shell) throws Exception JavaDoc {
119         this.shell = shell;
120         peer = new MenuItem(parent, SWT.CHECK);
121         peer.setSelection(pState);
122     }
123     
124 }
125
Popular Tags