KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > AbstractButton


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: AbstractButton.java,v $
11    Revision 1.28 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.27 2004/05/05 12:43:21 bobintetley
15    Patches/new files from Laurent Martell
16
17    Revision 1.26 2004/04/30 23:18:26 dannaab
18    List selection support, misc bug fixes
19
20    Revision 1.25 2004/04/06 12:46:45 bobintetley
21    ButtonDemo/ListDemo re-enabled for SwingSet2
22
23    Revision 1.24 2004/03/30 10:42:46 bobintetley
24    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
25
26    Revision 1.23 2004/03/12 14:21:47 bobintetley
27    Fix to threading issue and removal of debug messages
28
29    Revision 1.22 2004/03/01 15:58:47 bobintetley
30    Various little bug fixes
31
32    Revision 1.21 2004/02/19 09:58:44 bobintetley
33    Various small bug fixes and JTextArea should be much faster/lighter
34
35    Revision 1.20 2004/02/13 15:09:23 bobintetley
36    JComboBox/Abstract button non-peer selection and JTable threading fixed
37
38    Revision 1.19 2004/01/26 08:11:00 bobintetley
39    Many bugfixes and addition of SwingSet
40
41    Revision 1.18 2004/01/23 08:04:56 bobintetley
42    JComboBox fixes and better Action implementation
43
44    Revision 1.17 2004/01/20 07:38:05 bobintetley
45    Bug fixes and compatibility methods
46
47    Revision 1.16 2004/01/05 02:50:39 djspiewak
48    Added JToolBar peer functionality and commenced AWT layout manager support
49
50    Revision 1.15 2004/01/02 10:50:50 bobintetley
51    Button mnemonic/tooltip fixes
52
53    Revision 1.14 2003/12/22 09:57:23 bobintetley
54    Fixed broken ItemEvent calls in AbstractButton
55
56    Revision 1.13 2003/12/17 16:35:52 bobintetley
57    Mnemonics are no longer case sensitive
58
59    Revision 1.12 2003/12/17 16:30:35 bobintetley
60    Flowlayout fix, vertical toolbar support and cleaned up text alignment
61    hierarchy.
62
63    Revision 1.11 2003/12/16 19:04:38 bobintetley
64    Fix to broken mnemonics
65
66    Revision 1.10 2003/12/16 18:04:10 bobintetley
67    Fixes to handling of mnemonics
68
69    Revision 1.9 2003/12/16 17:46:17 bobintetley
70    Additional thread safety methods
71
72    Revision 1.8 2003/12/16 14:08:05 bobintetley
73    Corrected event hierarchy for Button ActionEvents
74
75    Revision 1.7 2003/12/16 13:14:33 bobintetley
76    Use of SwingWTUtils.isSWTControlAvailable instead of null test
77
78    Revision 1.6 2003/12/16 12:23:31 bobintetley
79    Corrected handling of table selection + keyboard action events
80
81    Revision 1.5 2003/12/14 09:13:38 bobintetley
82    Added CVS log to source headers
83
84 */

85
86 package swingwtx.swing;
87
88 import java.util.Vector JavaDoc;
89
90 import org.eclipse.swt.widgets.Button;
91
92 import swingwt.awt.event.ActionEvent;
93 import swingwt.awt.event.ActionListener;
94 import swingwt.awt.event.ItemEvent;
95 import swingwt.awt.event.ItemListener;
96 import swingwtx.swing.event.ChangeEvent;
97 import swingwtx.swing.event.ChangeListener;
98
99 /**
100  * Swing Abstract Button superclass for improved
101  * swingy-ness (and correct ActionEvent command)
102  * Written by Diane Trout, few changes and fixes up by me
103  * to provide a better hierarchy and prevent ActionEvents
104  * being fired twice.
105  *
106  * @author Diane Trout
107  * @author Robin Rawson-Tetley
108  * @author Brian Sant
109  */

110  public abstract class AbstractButton extends JComponent implements SwingConstants {
111      
112      protected Button ppeer = null;
113      protected String JavaDoc pText = null;
114      protected ButtonModel pModel;
115      protected ButtonGroup pGroup;
116      protected char pMnemonic = ' ';
117      protected boolean pSelection = false;
118      protected int pHTextPosition = LEFT;
119      protected int pVTextPosition = TOP;
120      protected int pHAlign = LEFT;
121      protected int pVAlign = TOP;
122      
123      protected Icon pIcon = null;
124      protected Action pAction = null;
125      
126      protected Vector JavaDoc changeListeners = new Vector JavaDoc();
127      protected Vector JavaDoc itemListeners = new Vector JavaDoc();
128      
129      public void setAction(Action a) {
130         if (a == null || a.equals(pAction)) return;
131       
132         if (pAction != null) { removeActionListener(pAction); }
133         if (a instanceof ActionListener) { addActionListener(a); }
134       
135         if (a.getValue(Action.SMALL_ICON) != null)
136            setIcon((Icon) a.getValue(Action.SMALL_ICON));
137         if (a.getValue(Action.NAME) != null)
138            setText((String JavaDoc) a.getValue(Action.NAME));
139         if (a.getValue(Action.SHORT_DESCRIPTION) != null)
140            setToolTipText((String JavaDoc) a.getValue(Action.SHORT_DESCRIPTION));
141         if (a.getValue(Action.MNEMONIC_KEY) != null)
142            setMnemonic(((Integer JavaDoc) a.getValue(Action.MNEMONIC_KEY)).intValue());
143       
144         // LONG_DESCRIPTION is not used in the orginal Swing implementation.
145
// The javadoc mentions that the LONG_DESCRIPTION may be used in
146
// an application's "context sensitive help", but Swing components
147
// don't use it directly.
148

149         setEnabled(a.isEnabled());
150         pAction = a;
151      }
152      
153      /**
154       * Works like setAction.
155       * @param a The action to set
156       * @param addAsListener Whether or not to add the action as a listener to the button.
157       * This method is used by JToolBar with wrappers and prevents running out of
158       * stack space by getting into an endless loop of actions that fire events.
159       */

160      protected void setAction(Action a, boolean addAsListener) {
161           setAction(a);
162           removeActionListener(a);
163      }
164      
165      public Action getAction() { return pAction; }
166      
167      public void setIcon(Icon icon) {}
168      public void setPressedIcon(Icon icon) {}
169      public void setRolloverIcon(Icon icon) {}
170      public void setRolloverSelectedIcon(Icon icon) {}
171      public void setDisabledIcon(Icon icon) {}
172      public void setSelectedIcon(Icon icon) {}
173      
174     public void setBorderPainted(boolean b) {}
175     public void setFocusPainted(boolean b) {}
176     public void setContentAreaFilled(boolean b) {}
177      
178      public void addChangeListener(ChangeListener l) {
179         changeListeners.add(l);
180      }
181      
182      public void removeChangeListener(ChangeListener l) {
183         changeListeners.remove(l);
184      }
185      
186     
187     public void addItemListener(ItemListener l) {
188         itemListeners.add(l);
189     }
190     
191     public void removeItemListener(ItemListener l) {
192         itemListeners.remove(l);
193     }
194     
195     
196     /** Overridden as we use the Button selection for action events */
197     protected void registerActionEvents() {
198         // Handle selection as ActionEvent
199
ppeer.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
200             public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
201                 processActionEvent(0);
202             }
203             public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {}
204         });
205     }
206     
207     public void setHorizontalAlignment(final int align) { pHAlign = align; SwingUtilities.invokeSync(new Runnable JavaDoc() { public void run() { if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(align) | SwingWTUtils.translateSwingAlignmentConstant(pVAlign));}}); }
208     public void setVerticalAlignment(final int align) { pVAlign = align; SwingUtilities.invokeSync(new Runnable JavaDoc() { public void run() {if (SwingWTUtils.isSWTControlAvailable(ppeer))ppeer.setAlignment(SwingWTUtils.translateSwingAlignmentConstant(align) | SwingWTUtils.translateSwingAlignmentConstant(pHAlign));}});}
209     public int getHorizontalAlignment() { return pHAlign; }
210     public int getVerticalAlignment() { return pVAlign; }
211     public void setHorizontalTextPosition(int textpos) { setHorizontalAlignment(textpos); }
212     public void setVerticalTextPosition(int textpos) { setVerticalAlignment(textpos); }
213     public int getHorizontalTextPosition() { return getHorizontalAlignment(); }
214     public int getVerticalTextPosition() { return getVerticalAlignment(); }
215     
216    /**
217     * Sends action events to listeners.
218     */

219      public void processActionEvent(int id) {
220          
221          ActionEvent ae = new ActionEvent(this, id, this.getActionCommand());
222          for (int i = 0; i < actionListeners.size(); i++) {
223              ActionListener al = (ActionListener) actionListeners.get(i);
224              al.actionPerformed(ae);
225          }
226          
227          // Send the actions as changes
228
ChangeEvent ce = new ChangeEvent(this);
229          for (int i = 0; i < changeListeners.size(); i++) {
230             ChangeListener cl = (ChangeListener) changeListeners.get(i);
231             cl.stateChanged(ce);
232          }
233          
234          // Send item events too
235
processItemEvent();
236      }
237      
238     /**
239      * Handles firing of Item events for when selection changes
240      */

241     public void processItemEvent() {
242         if (itemListeners.size() == 0) return;
243         boolean isSelected = pSelection;
244         if (SwingWTUtils.isSWTControlAvailable(ppeer)) isSelected = ppeer.getSelection();
245         ItemEvent e = new ItemEvent(this, 0, this, (isSelected ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
246         
247         for (int i = 0; i < itemListeners.size(); i++) {
248             ItemListener il = (ItemListener) itemListeners.get(i);
249             il.itemStateChanged(e);
250         }
251         // If the item is selected, and it is part of a group, tell the group
252
if (isSelected && pGroup != null) {
253             pGroup.setSelected(this.getModel(), true);
254         }
255     }
256
257      public String JavaDoc getText() { return pText; }
258      public void setText(String JavaDoc text) {
259          pText = text;
260          if (pText == null) pText = "";
261          showMnemonic();
262          if (SwingWTUtils.isSWTControlAvailable(ppeer))
263              SwingUtilities.invokeSync(new Runnable JavaDoc() {
264                  public void run() {
265                      ppeer.setText(pText);
266                  }
267              });
268      }
269      
270     public int getMnemonic() { return pMnemonic; };
271     public void setMnemonic(char mnemonic) { pMnemonic = mnemonic; showMnemonic(); }
272     public void setMnemonic(int mnemonic) { pMnemonic = (char) mnemonic; showMnemonic(); }
273     
274     protected void showMnemonic() {
275         final Object JavaDoc me = this;
276         SwingUtilities.invokeSync(new Runnable JavaDoc() {
277             public void run() {
278                 
279                 // Stop if we don't have any cached text
280
if (pText == null) return;
281                 
282                 // Strip out any HTML
283
if (pText.indexOf("<") != -1)
284                     pText = SwingWTUtils.removeHTML(pText);
285                 
286                 // If we don't have a mnemonic, don't do anything
287
if (pMnemonic == ' ') return;
288                 String JavaDoc text = ( (!SwingWTUtils.isSWTControlAvailable(ppeer)) ? pText : ppeer.getText());
289                 
290                 // Sort out the tooltip for the mnemonic if this is a push button
291
// of some type (check/radio is more obviously visible)
292
if (me instanceof JToggleButton || me instanceof JButton)
293                     if (pToolTipText.indexOf("(ALT+") == -1) {
294                         pToolTipText += " (ALT+" + new String JavaDoc(new char[] { pMnemonic }).toUpperCase()+ ")";
295                         SwingUtilities.invokeSync(new Runnable JavaDoc() {
296                             public void run() {
297                                 if (SwingWTUtils.isSWTControlAvailable(peer))
298                                     peer.setToolTipText(pToolTipText);
299                             }
300                         });
301                     }
302                 
303                 // If there is already a mnemonic, remove it
304
if (text.indexOf("&") != -1) {
305                     text = SwingWTUtils.replace(text, "&", "");
306                 }
307                 
308                 // If there is no text, then set the text to just be the mnemonic
309
if (text.equals("")) {
310                     pText = "&" + new String JavaDoc(new char[] {pMnemonic}).toLowerCase();
311                     if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setText(text);
312                 }
313                 else {
314                     // Convert the mnemonic and text to a lower case string to make
315
// the match case insensitive
316
String JavaDoc lower = text.toLowerCase();
317                     String JavaDoc mn = new String JavaDoc(new char[] { pMnemonic }).toLowerCase();
318                     int pos = lower.indexOf(mn);
319
320                     if (pos != -1) {
321                         text = text.substring(0, pos) + "&" + text.substring(pos, text.length());
322                         pText = text;
323                         if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setText(text);
324                     }
325                 }
326             }
327         });
328     }
329
330      public abstract boolean isSelected();
331      public abstract void setSelected(boolean b);
332
333     /** Sets the tooltip text used on the component - overridden here,
334       * so that if we have a mnemonic, we can tack it on the end. Only applies
335       * to JButton and JToggleButton */

336     public void setToolTipText(final String JavaDoc text) {
337         pToolTipText = text;
338         
339         if (this instanceof JToggleButton || this instanceof JButton)
340             if ( pMnemonic != ' ')
341                 if (pToolTipText.indexOf("(ALT+") == -1)
342                     pToolTipText += " (ALT+" + new String JavaDoc(new char[] { pMnemonic }).toUpperCase()+ ")";
343                 
344         SwingUtilities.invokeSync(new Runnable JavaDoc() {
345             public void run() {
346                 if (SwingWTUtils.isSWTControlAvailable(peer))
347                     peer.setToolTipText(pToolTipText);
348             }
349         });
350         
351     }
352      
353      public void setModel(ButtonModel m) { pModel = m; }
354      public ButtonModel getModel() { return pModel; }
355      public void setGroup(ButtonGroup g) { pGroup = g; }
356  }
Popular Tags