KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JCheckBox


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: JCheckBox.java,v $
11    Revision 1.27 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.26 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.25 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.24 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.23 2004/03/12 16:35:28 bobintetley
25    AWT CheckboxGroup support and fix for incorrectly named AWT Scrollbar
26
27    Revision 1.22 2004/02/13 15:09:23 bobintetley
28    JComboBox/Abstract button non-peer selection and JTable threading fixed
29
30    Revision 1.21 2004/01/26 08:11:00 bobintetley
31    Many bugfixes and addition of SwingSet
32
33    Revision 1.20 2004/01/20 07:38:05 bobintetley
34    Bug fixes and compatibility methods
35
36    Revision 1.19 2004/01/16 15:53:32 bobintetley
37    Many compatibility methods added to Container, Component, JInternalFrame,
38       UIManager, SwingUtilities, JTabbedPane, JPasswordField, JCheckBox
39       and JRadioButton.
40
41    Revision 1.18 2003/12/17 16:30:40 bobintetley
42    Flowlayout fix, vertical toolbar support and cleaned up text alignment
43    hierarchy.
44
45    Revision 1.17 2003/12/17 15:24:33 bobintetley
46    Threading fixes
47
48    Revision 1.16 2003/12/16 18:04:10 bobintetley
49    Fixes to handling of mnemonics
50
51    Revision 1.15 2003/12/16 17:46:17 bobintetley
52    Additional thread safety methods
53
54    Revision 1.14 2003/12/16 15:47:45 bobintetley
55    Thread safety added to common methods
56
57    Revision 1.13 2003/12/16 13:14:33 bobintetley
58    Use of SwingWTUtils.isSWTControlAvailable instead of null test
59
60    Revision 1.12 2003/12/15 18:29:57 bobintetley
61    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
62
63    Revision 1.11 2003/12/15 15:54:25 bobintetley
64    Additional core methods
65
66    Revision 1.10 2003/12/14 09:37:39 bobintetley
67    Cached selection support
68
69    Revision 1.9 2003/12/14 09:13:38 bobintetley
70    Added CVS log to source headers
71
72 */

73
74
75 package swingwtx.swing;
76
77 import org.eclipse.swt.widgets.*;
78 import org.eclipse.swt.*;
79
80 import swingwt.awt.event.*;
81
82 import java.util.*;
83
84 public class JCheckBox extends swingwtx.swing.AbstractButton implements ButtonModel, SwingConstants {
85     
86     /** boolean thread safe accessor */
87     private boolean bRetVal;
88
89     /** AWT uses Checkboxes for radio buttons as well **/
90     private boolean isAWTRadio = false;
91     
92     public JCheckBox() {this(""); }
93     public JCheckBox(String JavaDoc text) { this(text, false); }
94     public JCheckBox(String JavaDoc text, Icon icon) { this(text, false); }
95     public JCheckBox(String JavaDoc text, boolean selected) { pText = text; pSelection = selected; setModel(this); showMnemonic();}
96     /** This constructor is used for AWT CheckboxGroup support **/
97     public JCheckBox(String JavaDoc text, boolean selected, ButtonGroup bg) { pText = text; pSelection = selected; isAWTRadio = true; setModel(this); showMnemonic();}
98     
99     
100     /**
101      * Sends mouse events to component listeners
102      * Overriden from Component to handle ItemEvents
103      */

104     public void processMouseEvent(MouseEvent e) {
105         Iterator i = mouseListeners.iterator();
106         while (i.hasNext()) {
107             MouseListener ml = (MouseListener) i.next();
108             if (e.eventID == MouseEvent.CLICKED) { ml.mouseClicked(e); processItemEvent(); }
109             if (e.eventID == MouseEvent.ENTERED) ml.mouseEntered(e);
110             if (e.eventID == MouseEvent.EXITED) ml.mouseExited(e);
111             if (e.eventID == MouseEvent.PRESSED) ml.mousePressed(e);
112             if (e.eventID == MouseEvent.RELEASED) ml.mouseReleased(e);
113         }
114     }
115     
116     /**
117      * Sends KeyEvents to component listeners
118      * Overriden from Component to handle ItemEvents
119      */

120     public void processKeyEvent(KeyEvent e) {
121         Iterator i = keyListeners.iterator();
122         while (i.hasNext()) {
123             KeyListener ml = (KeyListener) i.next();
124             if (e.eventID == KeyEvent.PRESSED) { ml.keyTyped(e); processItemEvent(); }
125             if (e.eventID == KeyEvent.RELEASED) ml.keyReleased(e);
126             if (e.eventID == KeyEvent.PRESSED) ml.keyPressed(e);
127         }
128     }
129
130     /** Overriden to calculate non-realised
131      * preferred size.
132      */

133     protected swingwt.awt.Dimension calculatePreferredSize() {
134         // Use the text height/width + 6 pixels for
135
// borders and an extra 8 pixels for the checkbox itself.
136
swingwt.awt.Dimension size = new swingwt.awt.Dimension(
137             SwingWTUtils.getRenderStringWidth(pText) + 6,
138             SwingWTUtils.getRenderStringHeight(pText) + 6);
139         setSize(size);
140         return size;
141     }
142     
143     /**
144      * Once a parent component receives an "add" call for a child, this being
145      * the child, this should be called to tell us to instantiate the peer
146      * and load in any cached properties.
147      */

148     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
149         descendantHasPeer = true;
150         if (isAWTRadio) {
151             ppeer = new Button(parent.getComposite(), SWT.RADIO);
152         } else {
153             ppeer = new Button(parent.getComposite(), SWT.CHECK);
154         }
155         ppeer.setText(pText);
156         ppeer.setSelection(pSelection);
157         ppeer.setAlignment( SwingWTUtils.translateSwingAlignmentConstant(pVAlign) |
158             SwingWTUtils.translateSwingAlignmentConstant(pHAlign) );
159         peer = ppeer;
160         this.parent = parent;
161     }
162     
163     public boolean isSelected() {
164         bRetVal = false;
165         SwingUtilities.invokeSync(new Runnable JavaDoc() {
166             public void run() {
167                 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) bRetVal = pSelection; else bRetVal = ppeer.getSelection();
168             }
169         });
170         return bRetVal;
171     }
172         
173     public void setSelected(final boolean b) {
174         SwingUtilities.invokeSync(new Runnable JavaDoc() {
175             public void run() {
176                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setSelection(b); else pSelection = b;
177             }
178         });
179     }
180     
181     public Object JavaDoc[] getSelectedObjects() {
182         return null;
183     }
184     
185     public int getMnemonic() {
186         return 0;
187     }
188     
189     public boolean isArmed() {
190         return false;
191     }
192     
193     public boolean isPressed() {
194         return false;
195     }
196     
197     public boolean isRollover() {
198         return false;
199     }
200     
201     public void setArmed(boolean b) {
202     }
203     
204     public void setPressed(boolean b) {
205     }
206     
207     public void setRollover(boolean b) {
208     }
209     
210 }
211
Popular Tags