KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JRadioButton


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: JRadioButton.java,v $
11    Revision 1.24 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.23 2004/04/30 13:20:43 bobintetley
15    Fix to unrealised peer preferred sizes, forwarding window events to
16    content panes and fix for mouse drag events.
17
18    Revision 1.22 2004/04/28 08:38:11 bobintetley
19    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
20
21    Revision 1.21 2004/03/30 10:42:46 bobintetley
22    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
23
24    Revision 1.20 2004/02/13 15:09:23 bobintetley
25    JComboBox/Abstract button non-peer selection and JTable threading fixed
26
27    Revision 1.19 2004/01/26 08:11:00 bobintetley
28    Many bugfixes and addition of SwingSet
29
30    Revision 1.18 2004/01/20 07:38:05 bobintetley
31    Bug fixes and compatibility methods
32
33    Revision 1.17 2004/01/16 15:53:32 bobintetley
34    Many compatibility methods added to Container, Component, JInternalFrame,
35       UIManager, SwingUtilities, JTabbedPane, JPasswordField, JCheckBox
36       and JRadioButton.
37
38    Revision 1.16 2003/12/17 16:30:42 bobintetley
39    Flowlayout fix, vertical toolbar support and cleaned up text alignment
40    hierarchy.
41
42    Revision 1.15 2003/12/16 18:04:10 bobintetley
43    Fixes to handling of mnemonics
44
45    Revision 1.14 2003/12/16 17:46:17 bobintetley
46    Additional thread safety methods
47
48    Revision 1.13 2003/12/16 13:14:33 bobintetley
49    Use of SwingWTUtils.isSWTControlAvailable instead of null test
50
51    Revision 1.12 2003/12/15 18:29:57 bobintetley
52    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
53
54    Revision 1.11 2003/12/15 15:54:25 bobintetley
55    Additional core methods
56
57    Revision 1.10 2003/12/14 09:37:39 bobintetley
58    Cached selection support
59
60    Revision 1.9 2003/12/14 09:13:38 bobintetley
61    Added CVS log to source headers
62
63 */

64
65
66 package swingwtx.swing;
67
68 import org.eclipse.swt.widgets.*;
69 import org.eclipse.swt.*;
70
71 import swingwt.awt.event.*;
72
73 import java.util.*;
74
75 public class JRadioButton extends swingwtx.swing.AbstractButton implements ButtonModel, SwingConstants {
76     
77     /** boolean thread safe accessor */
78     private boolean bRetVal;
79     
80     public JRadioButton() {this(""); }
81     public JRadioButton(String JavaDoc text) { this(text, false); }
82     public JRadioButton(String JavaDoc text, Icon icon) { this(text, false); }
83     public JRadioButton(String JavaDoc text, boolean selected) { pSelection = selected; pText = text; setModel(this); showMnemonic(); }
84     
85     /**
86      * Sends mouse events to component listeners
87      * Overriden from Component to handle ItemEvents
88      */

89     public void processMouseEvent(MouseEvent e) {
90         Iterator i = mouseListeners.iterator();
91         while (i.hasNext()) {
92             MouseListener ml = (MouseListener) i.next();
93             if (e.eventID == MouseEvent.CLICKED) { ml.mouseClicked(e); processItemEvent(); }
94             if (e.eventID == MouseEvent.ENTERED) ml.mouseEntered(e);
95             if (e.eventID == MouseEvent.EXITED) ml.mouseExited(e);
96             if (e.eventID == MouseEvent.PRESSED) ml.mousePressed(e);
97             if (e.eventID == MouseEvent.RELEASED) ml.mouseReleased(e);
98         }
99     }
100     
101     /**
102      * Sends KeyEvents to component listeners
103      * Overriden from Component to handle ItemEvents
104      */

105     public void processKeyEvent(KeyEvent e) {
106         Iterator i = keyListeners.iterator();
107         while (i.hasNext()) {
108             KeyListener ml = (KeyListener) i.next();
109             if (e.eventID == KeyEvent.PRESSED) { ml.keyTyped(e); processItemEvent(); }
110             if (e.eventID == KeyEvent.RELEASED) ml.keyReleased(e);
111             if (e.eventID == KeyEvent.PRESSED) ml.keyPressed(e);
112         }
113     }
114
115     /** Overriden to calculate non-realised
116      * preferred size.
117      */

118     protected swingwt.awt.Dimension calculatePreferredSize() {
119         // Use the text height/width + 2 pixels for
120
// borders and an extra 8 pixels for the radio itself.
121
swingwt.awt.Dimension size = new swingwt.awt.Dimension(
122             SwingWTUtils.getRenderStringWidth(pText) + 10,
123             SwingWTUtils.getRenderStringHeight(pText) + 6);
124         setSize(size);
125         return size;
126     }
127     
128     
129     /**
130      * Once a parent component receives an "add" call for a child, this being
131      * the child, this should be called to tell us to instantiate the peer
132      * and load in any cached properties.
133      */

134     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
135         descendantHasPeer = true;
136         ppeer = new Button(parent.getComposite(), SWT.RADIO);
137         ppeer.setText(pText);
138         ppeer.setSelection(pSelection);
139         ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(pVAlign) |
140             SwingWTUtils.translateSwingAlignmentConstant(pHAlign));
141         peer = ppeer;
142         this.parent = parent;
143     }
144     
145     public boolean isSelected() {
146         bRetVal = false;
147         SwingUtilities.invokeSync(new Runnable JavaDoc() {
148             public void run() {
149                 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) bRetVal = pSelection; else bRetVal = ppeer.getSelection();
150             }
151         });
152         return bRetVal;
153     }
154         
155     public void setSelected(final boolean b) {
156         SwingUtilities.invokeSync(new Runnable JavaDoc() {
157             public void run() {
158                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setSelection(b); else pSelection = b;
159             }
160         });
161         processItemEvent();
162     }
163
164     public Object JavaDoc[] getSelectedObjects() {
165         return null;
166     }
167     
168     public boolean isArmed() {
169         return false;
170     }
171     
172     public boolean isPressed() {
173         return false;
174     }
175     
176     public boolean isRollover() {
177         return false;
178     }
179     
180     public void setArmed(boolean b) {
181     }
182     
183     public void setPressed(boolean b) {
184     }
185     
186     public void setRollover(boolean b) {
187     }
188     
189 }
190
Popular Tags