KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JRadioButtonMenuItem


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: JRadioButtonMenuItem.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:05:11 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 JRadioButtonMenuItem extends JMenuItem implements ButtonModel {
45     
46     private Shell shell = null;
47     protected boolean pState = false;
48     protected Vector itemListeners = new Vector();
49     private Object JavaDoc retval;
50
51     public JRadioButtonMenuItem() {setModel(this);}
52     public JRadioButtonMenuItem(Action a) { setAction(a); setModel(this);}
53     public JRadioButtonMenuItem(Icon icon) { this("", icon); }
54     public JRadioButtonMenuItem(Icon icon, boolean b) { this("", icon, b); }
55     public JRadioButtonMenuItem(String JavaDoc text) { this(text, null, false); }
56     public JRadioButtonMenuItem(String JavaDoc text, boolean b) { this(text, null, b); }
57     public JRadioButtonMenuItem(String JavaDoc text, Icon icon) { this(text, null, false); }
58     public JRadioButtonMenuItem(String JavaDoc text, Icon icon, boolean b) { pText = text; pImage = icon; pState = b; setModel(this); }
59     public JRadioButtonMenuItem(String JavaDoc text, int mnemonic) { pText = text; setMnemonic(mnemonic); setModel(this);}
60     public JRadioButtonMenuItem(String JavaDoc text, int mnemonic, boolean b) { pText = text; setMnemonic(mnemonic); pState = b; setModel(this);}
61     
62     public Object JavaDoc[] getSelectedObjects() { return new Object JavaDoc[] { getText() };}
63     public boolean getState() {
64          SwingUtilities.invokeSync(new Runnable JavaDoc() {
65             public void run() {
66                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
67                     retval = new Boolean JavaDoc(ppeer.getSelection());
68                 else
69                     retval = new Boolean JavaDoc(pState);
70             }
71          });
72          return ((Boolean JavaDoc) retval).booleanValue();
73     }
74         
75     public void setState(boolean b) {
76          pState = b;
77          SwingUtilities.invokeSync(new Runnable JavaDoc() {
78             public void run() {
79                 if (SwingWTUtils.isSWTMenuControlAvailable(peer))
80                     ppeer.setSelection(pState);
81             }
82          });
83     }
84     public boolean isSelected() { return getState(); }
85     public void setSelected(boolean b) { setState(b); }
86     
87     public void addItemListener(ItemListener l) {
88         itemListeners.add(l);
89     }
90     
91     public void removeItemListener(ItemListener l) {
92         itemListeners.remove(l);
93     }
94     
95     /**
96      * Sends action events to listeners
97      * Overriden from JSWTMenuComponent to handle selection events
98      */

99     public void processActionEvent(int id) {
100         super.processActionEvent(id);
101     processItemEvent();
102     }
103     
104     /**
105      * Handles firing of Item events for when selection changes
106      */

107     public void processItemEvent() {
108         if (itemListeners.size() == 0) return;
109         Iterator i = itemListeners.iterator();
110         ItemEvent e = new ItemEvent(this, 0, this, (ppeer.getSelection() ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
111         while (i.hasNext()) {
112             ItemListener il = (ItemListener) i.next();
113             il.itemStateChanged(e);
114         }
115     }
116     
117     public void setSwingWTParent(Menu parent, Shell shell) throws Exception JavaDoc {
118         this.shell = shell;
119         peer = new MenuItem(parent, SWT.RADIO);
120         peer.setSelection(pState);
121     }
122     
123 }
124
Popular Tags