KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JButton


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: JButton.java,v $
11    Revision 1.36 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.35 2004/05/05 01:15:07 dannaab
15    Remove duplicate setTooltipText() calls (Laurent Martelli)
16
17    Revision 1.34 2004/04/30 23:18:26 dannaab
18    List selection support, misc bug fixes
19
20    Revision 1.33 2004/04/30 13:20:43 bobintetley
21    Fix to unrealised peer preferred sizes, forwarding window events to
22    content panes and fix for mouse drag events.
23
24    Revision 1.32 2004/04/06 13:38:23 bobintetley
25    (James Moger) patch to allow JButton.renderText() to support other fonts
26
27    Revision 1.31 2004/04/06 12:51:39 bobintetley
28    JButton text defaults to center alignment correctly again
29
30    Revision 1.30 2004/03/30 10:42:46 bobintetley
31    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
32
33    Revision 1.29 2004/03/01 15:58:47 bobintetley
34    Various little bug fixes
35
36    Revision 1.28 2004/01/26 08:11:00 bobintetley
37    Many bugfixes and addition of SwingSet
38
39    Revision 1.27 2004/01/23 08:04:56 bobintetley
40    JComboBox fixes and better Action implementation
41
42    Revision 1.26 2004/01/20 07:38:05 bobintetley
43    Bug fixes and compatibility methods
44
45    Revision 1.25 2004/01/09 11:47:27 bobintetley
46    Automatic JButton mapping!
47
48    Revision 1.24 2004/01/09 10:33:57 bobintetley
49    Changes for JToolBar to allow platform ToolBars, mixed with other components
50
51    Revision 1.23 2004/01/06 15:38:30 bobintetley
52    Adjusted render width calculations. Fixed horrible button border under Win32
53
54    Revision 1.22 2004/01/02 10:50:50 bobintetley
55    Button mnemonic/tooltip fixes
56
57    Revision 1.21 2003/12/22 20:48:48 bobintetley
58    Text and image support together for JButton
59
60    Revision 1.20 2003/12/22 09:18:40 bobintetley
61    Icon support for JToggleButton
62
63    Revision 1.19 2003/12/17 16:30:37 bobintetley
64    Flowlayout fix, vertical toolbar support and cleaned up text alignment
65    hierarchy.
66
67    Revision 1.18 2003/12/17 09:03:14 bobintetley
68    Closely matches Swing behaviour + JScrollPane support for Panels
69
70    Revision 1.17 2003/12/16 18:04:10 bobintetley
71    Fixes to handling of mnemonics
72
73    Revision 1.16 2003/12/16 17:46:17 bobintetley
74    Additional thread safety methods
75
76    Revision 1.15 2003/12/16 14:08:05 bobintetley
77    Corrected event hierarchy for Button ActionEvents
78
79    Revision 1.14 2003/12/16 13:51:45 bobintetley
80    JButton fires ActionEvents when enter is pressed
81
82    Revision 1.13 2003/12/16 13:14:33 bobintetley
83    Use of SwingWTUtils.isSWTControlAvailable instead of null test
84
85    Revision 1.12 2003/12/16 11:01:02 bobintetley
86    Additional Swing compatibility
87
88    Revision 1.11 2003/12/15 18:29:57 bobintetley
89    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
90
91    Revision 1.10 2003/12/15 15:53:06 bobintetley
92    defaultCapable methods and alignment
93
94    Revision 1.9 2003/12/14 09:13:38 bobintetley
95    Added CVS log to source headers
96
97 */

98
99
100 package swingwtx.swing;
101
102 import org.eclipse.swt.SWT;
103 import org.eclipse.swt.widgets.Button;
104 import org.eclipse.swt.widgets.Shell;
105 import org.eclipse.swt.widgets.ToolItem;
106
107 public class JButton extends AbstractButton implements SwingConstants, ButtonModel {
108
109     protected Icon pImage = null;
110     protected boolean pDefaultCapable = true;
111     protected swingwt.awt.Container pDefaultButtonParent = null;
112     /** If this JButton is representing an SWT tool button, a reference to it */
113     protected ToolItem pSWTToolButton = null;
114     
115     public JButton() {this(""); }
116     public JButton(String JavaDoc text) { pText = text; setModel(this); showMnemonic(); pHAlign = CENTER; }
117     public JButton(Action a) { setAction(a); pHAlign = CENTER; }
118     public JButton(Icon icon) { pImage = icon; pHAlign = CENTER; }
119     public JButton(String JavaDoc text, Icon icon) { pImage = icon; pText = text; pHAlign = CENTER; setModel(this); showMnemonic(); }
120     
121     /** @see AbstractButton.setAction(Action, bool) */
122     protected JButton(Action a, boolean addAsListener) { setAction(a, addAsListener); }
123     
124     /**
125      * If this JButton is part of a JButtonMappedAction, update it
126      */

127     private void updateMappedAction() {
128         if (pAction instanceof JButtonMappedAction) {
129             pAction.putValue(Action.NAME, getText());
130             pAction.putValue(Action.SHORT_DESCRIPTION, getToolTipText());
131             pAction.putValue(Action.SMALL_ICON, getIcon());
132             pAction.putValue(Action.DISABLED_ICON, getIcon());
133             pAction.putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(getMnemonic()));
134         }
135     }
136     
137     /**
138      * If this JButton is representing an SWT ToolItem, then update it's properties
139      * from this
140      */

141     private void updateToolButton() {
142         final JButton pthis = this;
143         if (pSWTToolButton == null) return;
144         SwingUtilities.invokeSync(new Runnable JavaDoc() {
145             public void run() {
146                 pSWTToolButton.setText(getText());
147                 pSWTToolButton.setToolTipText(getToolTipText());
148                 pSWTToolButton.setEnabled(isEnabled());
149                 pSWTToolButton.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, getIcon()));
150             }
151         });
152     }
153     
154     public Icon getIcon() { return pImage; }
155     
156     public void setIcon(Icon icon) {
157         pImage = icon;
158         final JButton pthis = this;
159         SwingUtilities.invokeSync(new Runnable JavaDoc() {
160             public void run() {
161                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, pImage));
162                 renderText();
163                 updateToolButton();
164                 updateMappedAction();
165             }
166         });
167     }
168     
169     /** Overrides superclass so we can update any ToolItem */
170     public void setToolTipText(String JavaDoc text) {
171         super.setToolTipText(text);
172         updateToolButton();
173         updateMappedAction();
174     }
175     
176     /** Overrides superclass as we need to read cached text for
177      * tool button purposes */

178     public String JavaDoc getText() {
179         return pText;
180     }
181     
182     /** Overrides superclass so we can render it where necessary */
183     public void setText(String JavaDoc text) {
184         super.setText(text);
185         renderText();
186         updateMappedAction();
187         updateToolButton();
188     }
189     
190     public void setEnabled(boolean b) {
191         super.setEnabled(b);
192         updateToolButton();
193     }
194     
195     public boolean getDefaultCapable() { return pDefaultCapable; }
196     public void setDefaultCapable(boolean b) { pDefaultCapable = b; }
197     
198     /** This makes up for deficiencies in being able to set
199       * images and text on a button. Call it after setting either the
200      * text or image.
201      *
202      * @author Robin Rawson-Tetley
203       */

204      protected void renderText() {
205          
206         if (pText == null)
207             return;
208         if (!SwingWTUtils.isSWTControlAvailable(ppeer))
209             return;
210         if (pText.length() == 2 && pText.startsWith("&"))
211             return;
212         if (pText.equals("") || pImage == null)
213             return;
214
215         final JButton pthis = this;
216         SwingUtilities.invokeSync(new Runnable JavaDoc() {
217             public void run() {
218                 org.eclipse.swt.graphics.GC gc = new org.eclipse.swt.graphics.GC(ppeer.getImage());
219                 gc.setFont(ppeer.getFont()); // Required to calculate correct string width
220

221                 // Calculate the text width
222
org.eclipse.swt.graphics.Point p = gc.textExtent(pText);
223
224                 // Create a new image accounting for a two pixel margin and
225
// the text width
226
int width = ppeer.getImage().getBounds().width + 2;
227                 int height = ppeer.getImage().getBounds().height;
228                 org.eclipse.swt.graphics.Image im = new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(), width + p.x, height);
229
230                 // Draw the old image on it
231
org.eclipse.swt.graphics.GC ngc = new org.eclipse.swt.graphics.GC(im);
232                 ngc.setFont(ppeer.getFont()); // Required to properly render font on image-text button
233

234                  // Here, I set the background and foreground to the composite
235
// background colour, so if your image is a PNG, you get
236
// the right alpha channel
237
ngc.setBackground(ppeer.getShell().getBackground());
238                  ngc.setForeground(ppeer.getShell().getBackground());
239                  ngc.fillRectangle(0, 0, width + p.x, height);
240
241                  // Change foreground back to the regular colour for text rendering
242
ngc.setForeground(ppeer.getShell().getForeground());
243                  ngc.drawImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, pImage), 0, 0);
244
245                  // We have to take & out of the text, because I'm too lazy
246
// to support mnemonics (yes, I could draw a line, whatever - I'll
247
// revisit this)
248
String JavaDoc text = pText;
249                  int mnPos = text.indexOf("&");
250                  if (mnPos != -1)
251                      text = text.substring(0, mnPos) + text.substring(mnPos + 1, text.length());
252
253                  // Draw the text
254
ngc.drawText(text, width, (height - p.y) / 2, true);
255
256                  // Drop the drawing contexts
257
ngc.dispose();
258                  gc.dispose();
259
260                  // Update the image
261
ppeer.setImage(im);
262              }
263          });
264      }
265      
266     /** Overriden to calculate non-realised
267      * preferred size.
268      */

269     protected swingwt.awt.Dimension calculatePreferredSize() {
270         // Use the text height/width + 6 pixels for width
271
// 10 for height button borders.
272
swingwt.awt.Dimension size = new swingwt.awt.Dimension(
273             SwingWTUtils.getRenderStringWidth(pText) + 6,
274             SwingWTUtils.getRenderStringHeight(pText) + 12);
275         setSize(size);
276         return size;
277     }
278     
279     /**
280      * Once a parent component receives an "add" call for a child, this being
281      * the child, this should be called to tell us to instantiate the peer
282      * and load in any cached properties.
283      */

284     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
285         
286         // If the Button's parent is a JToolbar, make it flat instead of
287
// a push button
288
ppeer = new Button(parent.getComposite(),
289             (parent instanceof JToolBar ? SWT.FLAT : SWT.PUSH));
290         peer = ppeer;
291         
292         if (pText != null)
293             ppeer.setText(pText);
294
295         if (pMnemonic != ' ') showMnemonic();
296         if (pImage != null) ppeer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(this, pImage));
297         if (pFont != null) ppeer.setFont(pFont.getSWTFont());
298         ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(pVAlign) |
299             SwingWTUtils.translateSwingAlignmentConstant(pHAlign));
300         renderText();
301         
302         this.parent = parent;
303         
304         // If this button is the default for a given container,
305
// make sure it knows about it
306
if (pDefaultButtonParent != null) {
307             
308             // Is the peer a shell?
309
if (pDefaultButtonParent.getComposite() instanceof Shell) {
310                 ((Shell) pDefaultButtonParent.getComposite()).setDefaultButton(ppeer);
311             }
312             // Some other type of container
313
else {
314                 pDefaultButtonParent.getComposite().getShell().setDefaultButton(ppeer);
315             }
316         }
317     }
318     
319     public void setDefaultButtonParent(swingwt.awt.Container window) {
320         pDefaultButtonParent = window;
321     }
322     
323     public boolean isSelected() { return false; }
324     public void setSelected(boolean b) {}
325     
326     public void addItemListener(swingwt.awt.event.ItemListener l) {
327     }
328     
329     public Object JavaDoc[] getSelectedObjects() {
330         return null;
331     }
332     
333     public void removeItemListener(swingwt.awt.event.ItemListener l) {
334     }
335     
336     public boolean isArmed() {
337         return false;
338     }
339     
340     public boolean isPressed() {
341         return false;
342     }
343     
344     public boolean isRollover() {
345         return false;
346     }
347     
348     public void setArmed(boolean b) {
349     }
350     
351     public void setPressed(boolean b) {
352     }
353     
354     public void setRollover(boolean b) {
355     }
356     
357 }
358
Popular Tags