KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JToggleButton


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: JToggleButton.java,v $
11    Revision 1.7 2004/04/28 08:38:12 bobintetley
12    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
13
14    Revision 1.6 2004/03/30 10:42:46 bobintetley
15    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
16
17    Revision 1.5 2004/02/13 15:09:23 bobintetley
18    JComboBox/Abstract button non-peer selection and JTable threading fixed
19
20    Revision 1.4 2004/01/26 08:11:00 bobintetley
21    Many bugfixes and addition of SwingSet
22
23    Revision 1.3 2004/01/20 07:38:05 bobintetley
24    Bug fixes and compatibility methods
25
26    Revision 1.2 2003/12/22 09:18:40 bobintetley
27    Icon support for JToggleButton
28
29    Revision 1.1 2003/12/22 09:17:21 bobintetley
30    Added JToggleButton support
31
32    
33 */

34
35
36 package swingwtx.swing;
37
38 import org.eclipse.swt.widgets.*;
39 import org.eclipse.swt.*;
40
41 import swingwt.awt.event.*;
42
43 import java.util.*;
44
45 public class JToggleButton extends swingwtx.swing.AbstractButton implements ButtonModel, SwingConstants {
46     
47     protected Icon pImage = null;
48     
49     /** boolean thread safe accessor */
50     private boolean bRetVal;
51     
52     public JToggleButton() {this(""); }
53     public JToggleButton(String JavaDoc text) { pText = text; setModel(this); showMnemonic(); }
54     public JToggleButton(String JavaDoc text, Icon icon) { pText = text; pImage = icon; setModel(this); showMnemonic(); }
55     
56     public void setIcon(Icon icon) {
57         pImage = icon;
58         final JToggleButton pthis = this;
59         SwingUtilities.invokeSync(new Runnable JavaDoc() {
60             public void run() {
61                 if (SwingWTUtils.isSWTControlAvailable(ppeer))
62                     ppeer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, pImage));
63             }
64         });
65     }
66     
67     /**
68      * Sends mouse events to component listeners
69      * Overriden from Component to handle ItemEvents
70      */

71     public void processMouseEvent(MouseEvent e) {
72         Iterator i = mouseListeners.iterator();
73         while (i.hasNext()) {
74             MouseListener ml = (MouseListener) i.next();
75             if (e.eventID == MouseEvent.CLICKED) { ml.mouseClicked(e); processItemEvent(); }
76             if (e.eventID == MouseEvent.ENTERED) ml.mouseEntered(e);
77             if (e.eventID == MouseEvent.EXITED) ml.mouseExited(e);
78             if (e.eventID == MouseEvent.PRESSED) ml.mousePressed(e);
79             if (e.eventID == MouseEvent.RELEASED) ml.mouseReleased(e);
80         }
81     }
82     
83     /**
84      * Sends KeyEvents to component listeners
85      * Overriden from Component to handle ItemEvents
86      */

87     public void processKeyEvent(KeyEvent e) {
88         Iterator i = keyListeners.iterator();
89         while (i.hasNext()) {
90             KeyListener ml = (KeyListener) i.next();
91             if (e.eventID == KeyEvent.PRESSED) { ml.keyTyped(e); processItemEvent(); }
92             if (e.eventID == KeyEvent.RELEASED) ml.keyReleased(e);
93             if (e.eventID == KeyEvent.PRESSED) ml.keyPressed(e);
94         }
95     }
96     
97     /**
98      * Once a parent component receives an "add" call for a child, this being
99      * the child, this should be called to tell us to instantiate the peer
100      * and load in any cached properties.
101      */

102     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
103         descendantHasPeer = true;
104         ppeer = new Button(parent.getComposite(), SWT.TOGGLE);
105         if (pText != null) ppeer.setText(pText);
106         ppeer.setSelection(pSelection);
107         ppeer.setAlignment(pVAlign | pHAlign);
108         peer = ppeer;
109         this.parent = parent;
110     }
111     
112     public boolean isSelected() {
113         bRetVal = false;
114         SwingUtilities.invokeSync(new Runnable JavaDoc() {
115             public void run() {
116                 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) bRetVal = false; else bRetVal = ppeer.getSelection();
117             }
118         });
119         return bRetVal;
120     }
121         
122     public void setSelected(final boolean b) {
123         SwingUtilities.invokeSync(new Runnable JavaDoc() {
124             public void run() {
125                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setSelection(b); else pSelection = b;
126             }
127         });
128     }
129
130     public Object JavaDoc[] getSelectedObjects() {
131         return null;
132     }
133     
134     public boolean isArmed() {
135         return false;
136     }
137     
138     public boolean isPressed() {
139         return false;
140     }
141     
142     public boolean isRollover() {
143         return false;
144     }
145     
146     public void setArmed(boolean b) {
147     }
148     
149     public void setPressed(boolean b) {
150     }
151     
152     public void setRollover(boolean b) {
153     }
154     
155 }
156
Popular Tags