2
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package javax.swing;
|
|
26 |
|
|
27 |
import java.awt.*;
|
|
28 |
import java.awt.event.*;
|
|
29 |
import java.awt.image.*;
|
|
30 |
import java.text.*;
|
|
31 |
import java.awt.geom.*;
|
|
32 |
import java.beans.*;
|
|
33 |
import java.util.Enumeration;
|
|
34 |
import java.util.Vector;
|
|
35 |
import java.io.Serializable;
|
|
36 |
import javax.swing.event.*;
|
|
37 |
import javax.swing.border.*;
|
|
38 |
import javax.swing.plaf.*;
|
|
39 |
import javax.accessibility.*;
|
|
40 |
import javax.swing.text.*;
|
|
41 |
import javax.swing.text.html.*;
|
|
42 |
import javax.swing.plaf.basic.*;
|
|
43 |
import java.util.*;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Defines common behaviors for buttons and menu items.
|
|
47 |
* <p>
|
|
48 |
* Buttons can be configured, and to some degree controlled, by
|
|
49 |
* <code><a href="Action.html">Action</a></code>s. Using an
|
|
50 |
* <code>Action</code> with a button has many benefits beyond directly
|
|
51 |
* configuring a button. Refer to <a href="Action.html#buttonActions">
|
|
52 |
* Swing Components Supporting <code>Action</code></a> for more
|
|
53 |
* details, and you can find more information in <a
|
|
54 |
* href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
|
|
55 |
* to Use Actions</a>, a section in <em>The Java Tutorial</em>.
|
|
56 |
* <p>
|
|
57 |
* For further information see
|
|
58 |
* <a
|
|
59 |
href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>,
|
|
60 |
* a section in <em>The Java Tutorial</em>.
|
|
61 |
* <p>
|
|
62 |
* <strong>Warning:</strong>
|
|
63 |
* Serialized objects of this class will not be compatible with
|
|
64 |
* future Swing releases. The current serialization support is
|
|
65 |
* appropriate for short term storage or RMI between applications running
|
|
66 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
67 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
68 |
* has been added to the <code>java.beans</code> package.
|
|
69 |
* Please see {@link java.beans.XMLEncoder}.
|
|
70 |
*
|
|
71 |
* @author Jeff Dinkins
|
|
72 |
*/
|
|
73 |
public abstract class AbstractButton extends JComponent implements ItemSelectable, SwingConstants {
|
|
74 |
|
|
75 |
// *********************************
|
|
76 |
// ******* Button properties *******
|
|
77 |
// *********************************
|
|
78 |
|
|
79 |
/** Identifies a change in the button model. */
|
|
80 |
public static final String MODEL_CHANGED_PROPERTY = "model";
|
|
81 |
/** Identifies a change in the button's text. */
|
|
82 |
public static final String TEXT_CHANGED_PROPERTY = "text";
|
|
83 |
/** Identifies a change to the button's mnemonic. */
|
|
84 |
public static final String MNEMONIC_CHANGED_PROPERTY = "mnemonic";
|
|
85 |
|
|
86 |
// Text positioning and alignment
|
|
87 |
/** Identifies a change in the button's margins. */
|
|
88 |
public static final String MARGIN_CHANGED_PROPERTY = "margin";
|
|
89 |
/** Identifies a change in the button's vertical alignment. */
|
|
90 |
public static final String VERTICAL_ALIGNMENT_CHANGED_PROPERTY = "verticalAlignment";
|
|
91 |
/** Identifies a change in the button's horizontal alignment. */
|
|
92 |
public static final String HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = "horizontalAlignment";
|
|
93 |
|
|
94 |
/** Identifies a change in the button's vertical text position. */
|
|
95 |
public static final String VERTICAL_TEXT_POSITION_CHANGED_PROPERTY = "verticalTextPosition";
|
|
96 |
/** Identifies a change in the button's horizontal text position. */
|
|
97 |
public static final String HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = "horizontalTextPosition";
|
|
98 |
|
|
99 |
// Paint options
|
|
100 |
/**
|
|
101 |
* Identifies a change to having the border drawn,
|
|
102 |
* or having it not drawn.
|
|
103 |
*/
|
|
104 |
public static final String BORDER_PAINTED_CHANGED_PROPERTY = "borderPainted";
|
|
105 |
/**
|
|
106 |
* Identifies a change to having the border highlighted when focused,
|
|
107 |
* or not.
|
|
108 |
*/
|
|
109 |
public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";
|
|
110 |
/**
|
|
111 |
* Identifies a change from rollover enabled to disabled or back
|
|
112 |
* to enabled.
|
|
113 |
*/
|
|
114 |
public static final String ROLLOVER_ENABLED_CHANGED_PROPERTY = "rolloverEnabled";
|
|
115 |
/**
|
|
116 |
* Identifies a change to having the button paint the content area.
|
|
117 |
*/
|
|
118 |
public static final String CONTENT_AREA_FILLED_CHANGED_PROPERTY = "contentAreaFilled";
|
|
119 |
|
|
120 |
// Icons
|
|
121 |
/** Identifies a change to the icon that represents the button. */
|
|
122 |
public static final String ICON_CHANGED_PROPERTY = "icon";
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Identifies a change to the icon used when the button has been
|
|
126 |
* pressed.
|
|
127 |
*/
|
|
128 |
public static final String PRESSED_ICON_CHANGED_PROPERTY = "pressedIcon";
|
|
129 |
/**
|
|
130 |
* Identifies a change to the icon used when the button has
|
|
131 |
* been selected.
|
|
132 |
*/
|
|
133 |
public static final String SELECTED_ICON_CHANGED_PROPERTY = "selectedIcon";
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Identifies a change to the icon used when the cursor is over
|
|
137 |
* the button.
|
|
138 |
*/
|
|
139 |
public static final String ROLLOVER_ICON_CHANGED_PROPERTY = "rolloverIcon";
|
|
140 |
/**
|
|
141 |
* Identifies a change to the icon used when the cursor is
|
|
142 |
* over the button and it has been selected.
|
|
143 |
*/
|
|
144 |
public static final String ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY = "rolloverSelectedIcon";
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Identifies a change to the icon used when the button has
|
|
148 |
* been disabled.
|
|
149 |
*/
|
|
150 |
public static final String DISABLED_ICON_CHANGED_PROPERTY = "disabledIcon";
|
|
151 |
/**
|
|
152 |
* Identifies a change to the icon used when the button has been
|
|
153 |
* disabled and selected.
|
|
154 |
*/
|
|
155 |
public static final String DISABLED_SELECTED_ICON_CHANGED_PROPERTY = "disabledSelectedIcon";
|
|
156 |
|
|
157 |
|
|
158 |
/** The data model that determines the button's state. */
|
|
159 |
protected ButtonModel model = null;
|
|
160 |
|
|
161 |
private String text = ""; // for BeanBox
|
|
162 |
private Insets margin = null;
|
|
163 |
private Insets defaultMargin = null;
|
|
164 |
|
|
165 |
// Button icons
|
|
166 |
// PENDING(jeff) - hold icons in an array
|
|
167 |
private Icon defaultIcon = null;
|
|
168 |
private Icon pressedIcon = null;
|
|
169 |
private Icon disabledIcon = null;
|
|
170 |
|
|
171 |
private Icon selectedIcon = null;
|
|
172 |
private Icon disabledSelectedIcon = null;
|
|
173 |
|
|
174 |
private Icon rolloverIcon = null;
|
|
175 |
private Icon rolloverSelectedIcon = null;
|
|
176 |
|
|
177 |
// Display properties
|
|
178 |
private boolean paintBorder = true;
|
|
179 |
private boolean paintFocus = true;
|
|
180 |
private boolean rolloverEnabled = false;
|
|
181 |
private boolean contentAreaFilled = true;
|
|
182 |
|
|
183 |
// Icon/Label Alignment
|
|
184 |
private int verticalAlignment = CENTER;
|
|
185 |
private int horizontalAlignment = CENTER;
|
|
186 |
|
|
187 |
private int verticalTextPosition = CENTER;
|
|
188 |
private int horizontalTextPosition = TRAILING;
|
|
189 |
|
|
190 |
private int iconTextGap = 4;
|
|
191 |
|
|
192 |
private int mnemonic;
|
|
193 |
private int mnemonicIndex = -1;
|
|
194 |
|
|
195 |
private long multiClickThreshhold = 0;
|
|
196 |
|
|
197 |
private boolean borderPaintedSet = false;
|
|
198 |
private boolean rolloverEnabledSet = false;
|
|
199 |
private boolean iconTextGapSet = false;
|
|
200 |
private boolean contentAreaFilledSet = false;
|
|
201 |
|
|
202 |
// Whether or not we've set the LayoutManager.
|
|
203 |
private boolean setLayout = false;
|
|
204 |
|
|
205 |
// This is only used by JButton, promoted to avoid an extra
|
|
206 |
// boolean field in JButton
|
|
207 |
boolean defaultCapable = true;
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Combined listeners: ActionListener, ChangeListener, ItemListener.
|
|
211 |
*/
|
|
212 |
private Handler handler;
|
|
213 |
|
|
214 |
/**
|
|
215 |
* The button model's <code>changeListener</code>.
|
|
216 |
*/
|
|
217 |
protected ChangeListener changeListener = null;
|
|
218 |
/**
|
|
219 |
* The button model's <code>ActionListener</code>.
|
|
220 |
*/
|
|
221 |
protected ActionListener actionListener = null;
|
|
222 |
/**
|
|
223 |
* The button model's <code>ItemListener</code>.
|
|
224 |
*/
|
|
225 |
protected ItemListener itemListener = null;
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Only one <code>ChangeEvent</code> is needed per button
|
|
229 |
* instance since the
|
|
230 |
* event's only state is the source property. The source of events
|
|
231 |
* generated is always "this".
|
|
232 |
*/
|
|
233 |
protected transient ChangeEvent changeEvent;
|
|
234 |
|
|
235 |
private boolean hideActionText = false;
|
|
236 |
|
|
237 |
/**
|
|
238 |
* Sets the <code>hideActionText</code> property, which determines
|
|
239 |
* whether the button displays text from the <code>Action</code>.
|
|
240 |
* This is useful only if an <code>Action</code> has been
|
|
241 |
* installed on the button.
|
|
242 |
*
|
|
243 |
* @param hideActionText <code>true</code> if the button's
|
|
244 |
* <code>text</code> property should not reflect
|
|
245 |
* that of the <code>Action</code>; the default is
|
|
246 |
* <code>false</code>
|
|
247 |
* @see <a href="Action.html#buttonActions">Swing Components Supporting
|
|
248 |
* <code>Action</code></a>
|
|
249 |
* @since 1.6
|
|
250 |
* @beaninfo
|
|
251 |
* bound: true
|
|
252 |
* expert: true
|
|
253 |
* description: Whether the text of the button should come from
|
|
254 |
* the <code>Action</code>.
|
|
255 |
*/
|
|
256 |
public void setHideActionText(boolean hideActionText) {
|
|
257 |
if (hideActionText != this.hideActionText) {
|
|
258 |
this.hideActionText = hideActionText;
|
|
259 |
if (getAction() != null) {
|
|
260 |
setTextFromAction(getAction(), false);
|
|
261 |
}
|
|
262 |
firePropertyChange("hideActionText", !hideActionText,
|
|
263 |
hideActionText);
|
|
264 |
}
|
|
265 |
}
|
|
266 |
|
|
267 |
/**
|
|
268 |
* Returns the value of the <code>hideActionText</code> property, which
|
|
269 |
* determines whether the button displays text from the
|
|
270 |
* <code>Action</code>. This is useful only if an <code>Action</code>
|
|
271 |
* has been installed on the button.
|
|
272 |
*
|
|
273 |
* @return <code>true</code> if the button's <code>text</code>
|
|
274 |
* property should not reflect that of the
|
|
275 |
* <code>Action</code>; the default is <code>false</code>
|
|
276 |
* @since 1.6
|
|
277 |
*/
|
|
278 |
public boolean getHideActionText() {
|
|
279 |
return hideActionText;
|
|
280 |
}
|
|
281 |
|
|
282 |
/**
|
|
283 |
* Returns the button's text.
|
|
284 |
* @return the buttons text
|
|
285 |
* @see #setText
|
|
286 |
*/
|
|
287 |
public String getText() {
|
|
288 |
return text;
|
|
289 |
}
|
|
290 |
|
|
291 |
/**
|
|
292 |
* Sets the button's text.
|
|
293 |
* @param text the string used to set the text
|
|
294 |
* @see #getText
|
|
295 |
* @beaninfo
|
|
296 |
* bound: true
|
|
297 |
* preferred: true
|
|
298 |
* attribute: visualUpdate true
|
|
299 |
* description: The button's text.
|
|
300 |
*/
|
|
301 |
public void setText(String text) {
|
|
302 |
String oldValue = this.text;
|
|
303 |
this.text = text;
|
|
304 |
firePropertyChange(TEXT_CHANGED_PROPERTY, oldValue, text);
|
|
305 |
updateDisplayedMnemonicIndex(text, getMnemonic());
|
|
306 |
|
|
307 |
if (accessibleContext != null) {
|
|
308 |
accessibleContext.firePropertyChange(
|
|
309 |
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
|
|
310 |
oldValue, text);
|
|
311 |
}
|
|
312 |
if (text == null || oldValue == null || !text.equals(oldValue)) {
|
|
313 |
revalidate();
|
|
314 |
repaint();
|
|
315 |
}
|
|
316 |
}
|
|
317 |
|
|
318 |
|
|
319 |
/**
|
|
320 |
* Returns the state of the button. True if the
|
|
321 |
* toggle button is selected, false if it's not.
|
|
322 |
* @return true if the toggle button is selected, otherwise false
|
|
323 |
*/
|
|
324 |
public boolean isSelected() {
|
|
325 |
return model.isSelected();
|
|
326 |
}
|
|
327 |
|
|
328 |
/**
|
|
329 |
* Sets the state of the button. Note that this method does not
|
|
330 |
* trigger an <code>actionEvent</code>.
|
|
331 |
* Call <code>doClick</code> to perform a programatic action change.
|
|
332 |
*
|
|
333 |
* @param b true if the button is selected, otherwise false
|
|
334 |
*/
|
|
335 |
public void setSelected(boolean b) {
|
|
336 |
boolean oldValue = isSelected();
|
|
337 |
|
|
338 |
// TIGER - 4840653
|
|
339 |
// Removed code which fired an AccessibleState.SELECTED
|
|
340 |
// PropertyChangeEvent since this resulted in two
|
|
341 |
// identical events being fired since
|
|
342 |
// AbstractButton.fireItemStateChanged also fires the
|
|
343 |
// same event. This caused screen readers to speak the
|
|
344 |
// name of the item twice.
|
|
345 |
|
|
346 |
model.setSelected(b);
|
|
347 |
}
|
|
348 |
|
|
349 |
/**
|
|
350 |
* Programmatically perform a "click". This does the same
|
|
351 |
* thing as if the user had pressed and released the button.
|
|
352 |
*/
|
|
353 |
public void doClick() {
|
|
354 |
doClick(68);
|
|
355 |
}
|
|
356 |
|
|
357 |
/**
|
|
358 |
* Programmatically perform a "click". This does the same
|
|
359 |
* thing as if the user had pressed and released the button.
|
|
360 |
* The button stays visually "pressed" for <code>pressTime</code>
|
|
361 |
* milliseconds.
|
|
362 |
*
|
|
363 |
* @param pressTime the time to "hold down" the button, in milliseconds
|
|
364 |
*/
|
|
365 |
public void doClick(int pressTime) {
|
|
366 |
Dimension size = getSize();
|
|
367 |
model.setArmed(true);
|
|
368 |
model.setPressed(true);
|
|
369 |
paintImmediately(new Rectangle(0,0, size.width, size.height));
|
|
370 |
try {
|
|
371 |
Thread.currentThread().sleep(pressTime);
|
|
372 |
} catch(InterruptedException ie) {
|
|
373 |
}
|
|
374 |
model.setPressed(false);
|
|
375 |
model.setArmed(false);
|
|
376 |
}
|
|
377 |
|
|
378 |
/**
|
|
379 |
* Sets space for margin between the button's border and
|
|
380 |
* the label. Setting to <code>null</code> will cause the button to
|
|
381 |
* use the default margin. The button's default <code>Border</code>
|
|
382 |
* object will use this value to create the proper margin.
|
|
383 |
* However, if a non-default border is set on the button,
|
|
384 |
* it is that <code>Border</code> object's responsibility to create the
|
|
385 |
* appropriate margin space (else this property will
|
|
386 |
* effectively be ignored).
|
|
387 |
*
|
|
388 |
* @param m the space between the border and the label
|
|
389 |
*
|
|
390 |
* @beaninfo
|
|
391 |
* bound: true
|
|
392 |
* attribute: visualUpdate true
|
|
393 |
* description: The space between the button's border and the label.
|
|
394 |
*/
|
|
395 |
public void setMargin(Insets m) {
|
|
396 |
// Cache the old margin if it comes from the UI
|
|
397 |
if(m instanceof UIResource) {
|
|
398 |
defaultMargin = m;
|
|
399 |
} else if(margin instanceof UIResource) {
|
|
400 |
defaultMargin = margin;
|
|
401 |
}
|
|
402 |
|
|
403 |
// If the client passes in a null insets, restore the margin
|
|
404 |
// from the UI if possible
|
|
405 |
if(m == null && defaultMargin != null) {
|
|
406 |
m = defaultMargin;
|
|
407 |
}
|
|
408 |
|
|
409 |
Insets old = margin;
|
|
410 |
margin = m;
|
|
411 |
firePropertyChange(MARGIN_CHANGED_PROPERTY, old, m);
|
|
412 |
if (old == null || !old.equals(m)) {
|
|
413 |
revalidate();
|
|
414 |
repaint();
|
|
415 |
}
|
|
416 |
}
|
|
417 |
|
|
418 |
/**
|
|
419 |
* Returns the margin between the button's border and
|
|
420 |
* the label.
|
|
421 |
*
|
|
422 |
* @return an <code>Insets</code> object specifying the margin
|
|
423 |
* between the botton's border and the label
|
|
424 |
* @see #setMargin
|
|
425 |
*/
|
|
426 |
public Insets getMargin() {
|
|
427 |
return (margin == null) ? null : (Insets) margin.clone();
|
|
428 |
}
|
|
429 |
|
|
430 |
/**
|
|
431 |
* Returns the default icon.
|
|
432 |
* @return the default <code>Icon</code>
|
|
433 |
* @see #setIcon
|
|
434 |
*/
|
|
435 |
public Icon getIcon() {
|
|
436 |
return defaultIcon;
|
|
437 |
}
|
|
438 |
|
|
439 |
/**
|
|
440 |
* Sets the button's default icon. This icon is
|
|
441 |
* also used as the "pressed" and "disabled" icon if
|
|
442 |
* there is no explicitly set pressed icon.
|
|
443 |
*
|
|
444 |
* @param defaultIcon the icon used as the default image
|
|
445 |
* @see #getIcon
|
|
446 |
* @see #setPressedIcon
|
|
447 |
* @beaninfo
|
|
448 |
* bound: true
|
|
449 |
* attribute: visualUpdate true
|
|
450 |
* description: The button's default icon
|
|
451 |
*/
|
|
452 |
public void setIcon(Icon defaultIcon) {
|
|
453 |
Icon oldValue = this.defaultIcon;
|
|
454 |
this.defaultIcon = defaultIcon;
|
|
455 |
|
|
456 |
/* If the default icon has really changed and we had
|
|
457 |
* generated the disabled icon for this component,
|
|
458 |
* (i.e. setDisabledIcon() was never called) then
|
|
459 |
* clear the disabledIcon field.
|
|
460 |
*/
|
|
461 |
if (defaultIcon != oldValue && (disabledIcon instanceof UIResource)) {
|
|
462 |
disabledIcon = null;
|
|
463 |
}
|
|
464 |
|
|
465 |
firePropertyChange(ICON_CHANGED_PROPERTY, oldValue, defaultIcon);
|
|
466 |
if (accessibleContext != null) {
|
|
467 |
accessibleContext.firePropertyChange(
|
|
468 |
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
|
|
469 |
oldValue, defaultIcon);
|
|
470 |
}
|
|
471 |
if (defaultIcon != oldValue) {
|
|
472 |
if (defaultIcon == null || oldValue == null ||
|
|
473 |
defaultIcon.getIconWidth() != oldValue.getIconWidth() ||
|
|
474 |
defaultIcon.getIconHeight() != oldValue.getIconHeight()) {
|
|
475 |
revalidate();
|
|
476 |
}
|
|
477 |
repaint();
|
|
478 |
}
|
|
479 |
}
|
|
480 |
|
|
481 |
/**
|
|
482 |
* Returns the pressed icon for the button.
|
|
483 |
* @return the <code>pressedIcon</code> property
|
|
484 |
* @see #setPressedIcon
|
|
485 |
*/
|
|
486 |
public Icon getPressedIcon() {
|
|
487 |
return pressedIcon;
|
|
488 |
}
|
|
489 |
|
|
490 |
/**
|
|
491 |
* Sets the pressed icon for the button.
|
|
492 |
* @param pressedIcon the icon used as the "pressed" image
|
|
493 |
* @see #getPressedIcon
|
|
494 |
* @beaninfo
|
|
495 |
* bound: true
|
|
496 |
* attribute: visualUpdate true
|
|
497 |
* description: The pressed icon for the button.
|
|
498 |
*/
|
|
499 |
public void setPressedIcon(Icon pressedIcon) {
|
|
500 |
Icon oldValue = this.pressedIcon;
|
|
501 |
this.pressedIcon = pressedIcon;
|
|
502 |
firePropertyChange(PRESSED_ICON_CHANGED_PROPERTY, oldValue, pressedIcon);
|
|
503 |
if (accessibleContext != null) {
|
|
504 |
accessibleContext.firePropertyChange(
|
|
505 |
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
|
|
506 |
oldValue, pressedIcon);
|
|
507 |
}
|
|
508 |
if (pressedIcon != oldValue) {
|
|
509 |
if (getModel().isPressed()) {
|
|
510 |
repaint();
|
|
511 |
}
|
|
512 |
}
|
|
513 |
}
|
|
514 |
|
|
515 |
/**
|
|
516 |
* Returns the selected icon for the button.
|
|
517 |
* @return the <code>selectedIcon</code> property
|
|
518 |
* @see #setSelectedIcon
|
|
519 |
*/
|
|
520 |
public Icon getSelectedIcon() {
|
|
521 |
return selectedIcon;
|
|
522 |
}
|
|
523 |
|
|
524 |
/**
|
|
525 |
* Sets the selected icon for the button.
|
|
526 |
* @param selectedIcon the icon used as the "selected" image
|
|
527 |
* @see #getSelectedIcon
|
|
528 |
* @beaninfo
|
|
529 |
* bound: true
|
|
530 |
* attribute: visualUpdate true
|
|
531 |
* description: The selected icon for the button.
|
|
532 |
*/
|
|
533 |
public void setSelectedIcon(Icon selectedIcon) {
|
|
534 |
Icon oldValue = this.selectedIcon;
|
|
535 |
this.selectedIcon = selectedIcon;
|
|
536 |
|
|
537 |
/* If the default selected icon has really changed and we had
|
|
538 |
* generated the disabled selected icon for this component,
|
|
539 |
* (i.e. setDisabledSelectedIcon() was never called) then
|
|
540 |
* clear the disabledSelectedIcon field.
|
|
541 |
*/
|
|
542 |
if (selectedIcon != oldValue &&
|
|
543 |
disabledSelectedIcon instanceof UIResource) {
|
|
544 |
|
|
545 |
disabledSelectedIcon = null;
|
|
546 |
}
|
|
547 |
|
|
548 |
firePropertyChange(SELECTED_ICON_CHANGED_PROPERTY, oldValue, selectedIcon);
|
|
549 |
if (accessibleContext != null) {
|
|
550 |
accessibleContext.firePropertyChange(
|
|
551 |
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
|
|
552 |
oldValue, selectedIcon);
|
|
553 |
}
|
|
554 |
if (selectedIcon != oldValue) {
|
|
555 |
if (isSelected()) {
|
|
556 |
repaint();
|
|
557 |
}
|
|
558 |
}
|
|
559 |
}
|
|
560 |
|
|
561 |
/**
|
|
562 |
* Returns the rollover icon for the button.
|
|
563 |
* @return the <code>rolloverIcon</code> property
|
|
564 |
* @see #setRolloverIcon
|
|
565 |
*/
|
|
566 |
public Icon getRolloverIcon() {
|
|
567 |
return rolloverIcon;
|
|
568 |
}
|
|
569 |
|
|
570 |
/**
|
|
571 |
* Sets the rollover icon for the button.
|
|
572 |
* @param rolloverIcon the icon used as the "rollover" image
|
|
573 |
* @see #getRolloverIcon
|
|
574 |
* @beaninfo
|
|
575 |
* bound: true
|
|
576 |
* attribute: visualUpdate true
|
|
577 |
* description: The rollover icon for the button.
|
|
578 |
*/
|
|
579 |
public void setRolloverIcon(Icon rolloverIcon) {
|
|
580 |
Icon oldValue = this.rolloverIcon;
|
|
581 |
this.rolloverIcon = rolloverIcon;
|
|
582 |
firePropertyChange(ROLLOVER_ICON_CHANGED_PROPERTY, oldValue, rolloverIcon);
|
|
583 |
if (accessibleContext != null) {
|
|
584 |
accessibleContext.firePropertyChange(
|
|
585 |
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
|
|
586 |
oldValue, rolloverIcon);
|
|
587 |
}
|
|
588 |
setRolloverEnabled(true);
|
|
589 |
if (rolloverIcon != oldValue) {
|
|
590 |
// No way to determine whether we are currently in
|
|
591 |
// a rollover state, so repaint regardless
|
|
592 |
repaint();
|
|
593 |
}
|
|
594 |
|
|
595 |
}
|
|
596 |
|
|
597 |
/**
|
|
598 |
* Returns the rollover selection icon for the button.
|
|
599 |
* @return the <code>rolloverSelectedIcon</code> property
|
|
600 |
* @see #setRolloverSelectedIcon
|
|
601 |
*/
|
|
602 |
public Icon getRolloverSelectedIcon() {
|
|
603 |
return rolloverSelectedIcon;
|
|
604 |
}
|
|
605 |
|
|
606 |
/**
|
|
607 |
* Sets the rollover selected icon for the button.
|
|
608 |
* @param rolloverSelectedIcon the icon used as the
|
|
609 |
* "selected rollover" image
|
|
610 |
* @see #getRolloverSelectedIcon
|
|
611 |
* @beaninfo
|
|
612 |
* bound: true
|
|
613 |
* attribute: visualUpdate true
|
|
614 |
* description: The rollover selected icon for the button.
|
|
615 |
*/
|
|
616 |
public void setRolloverSelectedIcon(Icon rolloverSelectedIcon) {
|
|
617 |
Icon oldValue = this.rolloverSelectedIcon;
|
|
618 |
this.rolloverSelectedIcon = rolloverSelectedIcon;
|
|
619 |
firePropertyChange(ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, oldValue, rolloverSelectedIcon);
|
|
620 |
if (accessibleContext != null) {
|
|
621 |
accessibleContext.firePropertyChange(
|
|
622 |
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
|
|
623 |
oldValue, rolloverSelectedIcon);
|
|
624 |
}
|
|
625 |
setRolloverEnabled(true);
|
|
626 |
if (rolloverSelectedIcon != oldValue) {
|
|
627 |
// No way to determine whether we are currently in
|
|
628 |
// a rollover state, so repaint regardless
|
|
629 |
if (isSelected()) {
|
|
630 |
repaint();
|
|
631 |
}
|
|
632 |
}
|
|
633 |
}
|
|
634 |
|
|
635 |
/**
|
|
636 |
* Returns the icon used by the button when it's disabled.
|
|
637 |
* If no disabled icon has been set this will forward the call to
|
|
638 |
* the look and feel to construct an appropriate disabled Icon.
|
|
639 |
* <p>
|
|
640 |
* Some look and feels might not render the disabled Icon, in which
|
|
641 |
* case they will ignore this.
|
|
642 |
*
|
|
643 |
* @return the <code>disabledIcon</code> property
|
|
644 |
* @see #getPressedIcon
|
|
645 |
* @see #setDisabledIcon
|
|
646 |
* @see javax.swing.LookAndFeel#getDisabledIcon
|
|
647 |
*/
|
|
648 |
public Icon getDisabledIcon() {
|
|
649 |
if (disabledIcon == null) {
|
|
650 |
disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, getIcon());
|
|
651 |
if (disabledIcon != null) {
|
|
652 |
firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, null, disabledIcon);
|
|
653 |
}
|
|
654 |
}
|
|
655 |
return disabledIcon;
|
|
656 |
}
|
|
657 |
|
|
658 |
/**
|
|
659 |
* Sets the disabled icon for the button.
|
|
660 |
* @param disabledIcon the icon used as the disabled image
|
|
661 |
* @see #getDisabledIcon
|
|
662 |
* @beaninfo
|
|
663 |
* bound: true
|
|
664 |
* attribute: visualUpdate true
|
|
665 |
* description: The disabled icon for the button.
|
|
666 |
*/
|
|
667 |
public void setDisabledIcon(Icon disabledIcon) {
|
|
668 |
Icon oldValue = this.disabledIcon;
|
|
669 |
this.disabledIcon = disabledIcon;
|
|
670 |
firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, oldValue, disabledIcon);
|
|
671 |
if (accessibleContext != null) {
|
|
672 |
accessibleContext.firePropertyChange(
|
|
673 |
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
|
|
674 |
oldValue, disabledIcon);
|
|
675 |
}
|
|
676 |
if (disabledIcon != oldValue) {
|
|
677 |
if (!isEnabled()) {
|
|
678 |
repaint();
|
|
679 |
}
|
|
680 |
}
|
|
681 |
}
|
|
682 |
|
|
683 |
/**
|
|
684 |
* Returns the icon used by the button when it's disabled and selected.
|
|
685 |
* If no disabled selection icon has been set, this will forward
|
|
686 |
* the call to the LookAndFeel to construct an appropriate disabled
|
|
687 |
* Icon from the selection icon if it has been set and to
|
|
688 |
* <code>getDisabledIcon()</code> otherwise.
|
|
689 |
* <p>
|
|
690 |
* Some look and feels might not render the disabled selected Icon, in
|
|
691 |
* which case they will ignore this.
|
|
692 |
*
|
|
693 |
* @return the <code>disabledSelectedIcon</code> property
|
|
694 |
* @see #getDisabledIcon
|
|
695 |
* @see #setDisabledSelectedIcon
|
|
696 |
* @see javax.swing.LookAndFeel#getDisabledSelectedIcon
|
|
697 |
*/
|
|
698 |
public Icon getDisabledSelectedIcon() {
|
|
699 |
if (disabledSelectedIcon == null) {
|
|
700 |
if (selectedIcon != null) {
|
|
701 |
disabledSelectedIcon = UIManager.getLookAndFeel().
|
|
702 |
getDisabledSelectedIcon(this, getSelectedIcon());
|
|
703 |
} else {
|
|
704 |
return getDisabledIcon();
|
|
705 |
}
|
|
706 |
}
|
|
707 |
return disabledSelectedIcon;
|
|
708 |
}
|
|
709 |
|
|
710 |
/**
|
|
711 |
* Sets the disabled selection icon for the button.
|
|
712 |
* @param disabledSelectedIcon the icon used as the disabled
|
|
713 |
* selection image
|
|
714 |
* @see #getDisabledSelectedIcon
|
|
715 |
* @beaninfo
|
|
716 |
* bound: true
|
|
717 |
* attribute: visualUpdate true
|
|
718 |
* description: The disabled selection icon for the button.
|
|
719 |
*/
|
|
720 |
public void setDisabledSelectedIcon(Icon disabledSelectedIcon) {
|
|
721 |
Icon oldValue = this.disabledSelectedIcon;
|
|
722 |
this.disabledSelectedIcon = disabledSelectedIcon;
|
|
723 |
firePropertyChange(DISABLED_SELECTED_ICON_CHANGED_PROPERTY, oldValue, disabledSelectedIcon);
|
|
724 |
if (accessibleContext != null) {
|
|
725 |
accessibleContext.firePropertyChange(
|
|
726 |
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
|
|
727 |
oldValue, disabledSelectedIcon);
|
|
728 |
}
|
|
729 |
if (disabledSelectedIcon != oldValue) {
|
|
730 |
if (disabledSelectedIcon == null || oldValue == null ||
|
|
731 |
disabledSelectedIcon.getIconWidth() != oldValue.getIconWidth() ||
|
|
732 |
disabledSelectedIcon.getIconHeight() != oldValue.getIconHeight()) {
|
|
733 |
revalidate();
|
|
734 |
}
|
|
735 |
if (!isEnabled() && isSelected()) {
|
|
736 |
repaint();
|
|
737 |
}
|
|
738 |
}
|
|
739 |
}
|
|
740 |
|
|
741 |
/**
|
|
742 |
* Returns the vertical alignment of the text and icon.
|
|
743 |
*
|
|
744 |
* @return the <code>verticalAlignment</code> property, one of the
|
|
745 |
* following values:
|
|
746 |
* <ul>
|
|
747 |
* <li>{@code SwingConstants.CENTER} (the default)
|
|
748 |
* <li>{@code SwingConstants.TOP}
|
|
749 |
* <li>{@code SwingConstants.BOTTOM}
|
|
750 |
* </ul>
|
|
751 |
*/
|
|
752 |
public int getVerticalAlignment() {
|
|
753 |
return verticalAlignment;
|
|
754 |
}
|
|
755 |
|
|
756 |
/**
|
|
757 |
* Sets the vertical alignment of the icon and text.
|
|
758 |
* @param alignment one of the following values:
|
|
759 |
* <ul>
|
|
760 |
* <li>{@code SwingConstants.CENTER} (the default)
|
|
761 |
* <li>{@code SwingConstants.TOP}
|
|
762 |
* <li>{@code SwingConstants.BOTTOM}
|
|
763 |
* </ul>
|
|
764 |
* @throws IllegalArgumentException if the alignment is not one of the legal
|
|
765 |
* values listed above
|
|
766 |
* @beaninfo
|
|
767 |
* bound: true
|
|
768 |
* enum: TOP SwingConstants.TOP
|
|
769 |
* CENTER SwingConstants.CENTER
|
|
770 |
* BOTTOM SwingConstants.BOTTOM
|
|
771 |
* attribute: visualUpdate true
|
|
772 |
* description: The vertical alignment of the icon and text.
|
|
773 |
*/
|
|
774 |
public void setVerticalAlignment(int alignment) {
|
|
775 |
if (alignment == verticalAlignment) return;
|
|
776 |
int oldValue = verticalAlignment;
|
|
777 |
verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
|
|
778 |
firePropertyChange(VERTICAL_ALIGNMENT_CHANGED_PROPERTY, oldValue, verticalAlignment); repaint();
|
|
779 |
}
|
|
780 |
|
|
781 |
/**
|
|
782 |
* Returns the horizontal alignment of the icon and text.
|
|
783 |
* {@code AbstractButton}'s default is {@code SwingConstants.CENTER},
|
|
784 |
* but subclasses such as {@code JCheckBox} may use a different default.
|
|
785 |
*
|
|
786 |
* @return the <code>horizontalAlignment</code> property,
|
|
787 |
* one of the following values:
|
|
788 |
* <ul>
|
|
789 |
* <li>{@code SwingConstants.RIGHT}
|
|
790 |
* <li>{@code SwingConstants.LEFT}
|
|
791 |
* <li>{@code SwingConstants.CENTER}
|
|
792 |
* <li>{@code SwingConstants.LEADING}
|
|
793 |
* <li>{@code SwingConstants.TRAILING}
|
|
794 |
* </ul>
|
|
795 |
*/
|
|
796 |
public int getHorizontalAlignment() {
|
|
797 |
return horizontalAlignment;
|
|
798 |
}
|
|
799 |
|
|
800 |
/**
|
|
801 |
* Sets the horizontal alignment of the icon and text.
|
|
802 |
* {@code AbstractButton}'s default is {@code SwingConstants.CENTER},
|
|
803 |
* but subclasses such as {@code JCheckBox} may use a different default.
|
|
804 |
*
|
|
805 |
* @param alignment the alignment value, one of the following values:
|
|
806 |
* <ul>
|
|
807 |
* <li>{@code SwingConstants.RIGHT}
|
|
808 |
* <li>{@code SwingConstants.LEFT}
|
|
809 |
* <li>{@code SwingConstants.CENTER}
|
|
810 |
* <li>{@code SwingConstants.LEADING}
|
|
811 |
* <li>{@code SwingConstants.TRAILING}
|
|
812 |
* </ul>
|
|
813 |
* @throws IllegalArgumentException if the alignment is not one of the
|
|
814 |
* valid values
|
|
815 |
* @beaninfo
|
|
816 |
* bound: true
|
|
817 |
* enum: LEFT SwingConstants.LEFT
|
|
818 |
* CENTER SwingConstants.CENTER
|
|
819 |
* RIGHT SwingConstants.RIGHT
|
|
820 |
* LEADING SwingConstants.LEADING
|
|
821 |
* TRAILING SwingConstants.TRAILING
|
|
822 |
* attribute: visualUpdate true
|
|
823 |
* description: The horizontal alignment of the icon and text.
|
|
824 |
*/
|
|
825 |
public void setHorizontalAlignment(int alignment) {
|
|
826 |
if (alignment == horizontalAlignment) return;
|
|
827 |
int oldValue = horizontalAlignment;
|
|
828 |
horizontalAlignment = checkHorizontalKey(alignment,
|
|
829 |
"horizontalAlignment");
|
|
830 |
firePropertyChange(HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY,
|
|
831 |
oldValue, horizontalAlignment);
|
|
832 |
repaint();
|
|
833 |
}
|
|
834 |
|
|
835 |
|
|
836 |
/**
|
|
837 |
* Returns the vertical position of the text relative to the icon.
|
|
838 |
* @return the <code>verticalTextPosition</code> property,
|
|
839 |
* one of the following values:
|
|
840 |
* <ul>
|
|
841 |
* <li>{@code SwingConstants.CENTER} (the default)
|
|
842 |
* <li>{@code SwingConstants.TOP}
|
|
843 |
* <li>{@code SwingConstants.BOTTOM}
|
|
844 |
* </ul>
|
|
845 |
*/
|
|
846 |
public int getVerticalTextPosition() {
|
|
847 |
return verticalTextPosition;
|
|
848 |
}
|
|
849 |
|
|
850 |
/**
|
|
851 |
* Sets the vertical position of the text relative to the icon.
|
|
852 |
* @param textPosition one of the following values:
|
|
853 |
* <ul>
|
|
854 |
* <li>{@code SwingConstants.CENTER} (the default)
|
|
855 |
* <li>{@code SwingConstants.TOP}
|
|
856 |
* <li>{@code SwingConstants.BOTTOM}
|
|
857 |
* </ul>
|
|
858 |
* @beaninfo
|
|
859 |
* bound: true
|
|
860 |
* enum: TOP SwingConstants.TOP
|
|
861 |
* CENTER SwingConstants.CENTER
|
|
862 |
* BOTTOM SwingConstants.BOTTOM
|
|
863 |
* attribute: visualUpdate true
|
|
864 |
* description: The vertical position of the text relative to the icon.
|
|
865 |
*/
|
|
866 |
public void setVerticalTextPosition(int textPosition) {
|
|
867 |
if (textPosition == verticalTextPosition) return;
|
|
868 |
int oldValue = verticalTextPosition;
|
|
869 |
verticalTextPosition = checkVerticalKey(textPosition, "verticalTextPosition");
|
|
870 |
firePropertyChange(VERTICAL_TEXT_POSITION_CHANGED_PROPERTY, oldValue, verticalTextPosition);
|
|
871 |
revalidate();
|
|
872 |
repaint();
|
|
873 |
}
|
|
874 |
|
|
875 |
/**
|
|
876 |
* Returns the horizontal position of the text relative to the icon.
|
|
877 |
* @return the <code>horizontalTextPosition</code> property,
|
|
878 |
* one of the following values:
|
|
879 |
* <ul>
|
|
880 |
* <li>{@code SwingConstants.RIGHT}
|
|
881 |
* <li>{@code SwingConstants.LEFT}
|
|
882 |
* <li>{@code SwingConstants.CENTER}
|
|
883 |
* <li>{@code SwingConstants.LEADING}
|
|
884 |
* <li>{@code SwingConstants.TRAILING} (the default)
|
|
885 |
* </ul>
|
|
886 |
*/
|
|
887 |
public int getHorizontalTextPosition() {
|
|
888 |
return horizontalTextPosition;
|
|
889 |
}
|
|
890 |
|
|
891 |
/**
|
|
892 |
* Sets the horizontal position of the text relative to the icon.
|
|
893 |
* @param textPosition one of the following values:
|
|
894 |
* <ul>
|
|
895 |
* <li>{@code SwingConstants.RIGHT}
|
|
896 |
* <li>{@code SwingConstants.LEFT}
|
|
897 |
* <li>{@code SwingConstants.CENTER}
|
|
898 |
* <li>{@code SwingConstants.LEADING}
|
|
899 |
* <li>{@code SwingConstants.TRAILING} (the default)
|
|
900 |
* </ul>
|
|
901 |
* @exception IllegalArgumentException if <code>textPosition</code>
|
|
902 |
* is not one of the legal values listed above
|
|
903 |
* @beaninfo
|
|
904 |
* bound: true
|
|
905 |
* enum: LEFT SwingConstants.LEFT
|
|
906 |
* CENTER SwingConstants.CENTER
|
|
907 |
* RIGHT SwingConstants.RIGHT
|
|
908 |
* LEADING SwingConstants.LEADING
|
|
909 |
* TRAILING SwingConstants.TRAILING
|
|
910 |
* attribute: visualUpdate true
|
|
911 |
* description: The horizontal position of the text relative to the icon.
|
|
912 |
*/
|
|
913 |
public void setHorizontalTextPosition(int textPosition) {
|
|
914 |
if (textPosition == horizontalTextPosition) return;
|
|
915 |
int oldValue = horizontalTextPosition;
|
|
916 |
horizontalTextPosition = checkHorizontalKey(textPosition,
|
|
917 |
"horizontalTextPosition");
|
|
918 |
firePropertyChange(HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY,
|
|
919 |
oldValue,
|
|
920 |
horizontalTextPosition);
|
|
921 |
revalidate();
|
|
922 |
repaint();
|
|
923 |
}
|
|
924 |
|
|
925 |
/**
|
|
926 |
* Returns the amount of space between the text and the icon
|
|
927 |
* displayed in this button.
|
|
928 |
*
|
|
929 |
* @return an int equal to the number of pixels between the text
|
|
930 |
* and the icon.
|
|
931 |
* @since 1.4
|
|
932 |
* @see #setIconTextGap
|
|
933 |
*/
|
|
934 |
public int getIconTextGap() {
|
|
935 |
return iconTextGap;
|
|
936 |
}
|
|
937 |
|
|
938 |
/**
|
|
939 |
* If both the icon and text properties are set, this property
|
|
940 |
* defines the space between them.
|
|
941 |
* <p>
|
|
942 |
* The default value of this property is 4 pixels.
|
|
943 |
* <p>
|
|
944 |
* This is a JavaBeans bound property.
|
|
945 |
*
|
|
946 |
* @since 1.4
|
|
947 |
* @see #getIconTextGap
|
|
948 |
* @beaninfo
|
|
949 |
* bound: true
|
|
950 |
* attribute: visualUpdate true
|
|
951 |
* description: If both the icon and text properties are set, this
|
|
952 |
* property defines the space between them.
|
|
953 |
*/
|
|
954 |
public void setIconTextGap(int iconTextGap) {
|
|
955 |
int oldValue = this.iconTextGap;
|
|
956 |
this.iconTextGap = iconTextGap;
|
|
957 |
iconTextGapSet = true;
|
|
958 |
firePropertyChange("iconTextGap", oldValue, iconTextGap);
|
|
959 |
if (iconTextGap != oldValue) {
|
|
960 |
revalidate();
|
|
961 |
repaint();
|
|
962 |
}
|
|
963 |
}
|
|
964 |
|
|
965 |
/**
|
|
966 |
* Verify that the {@code key} argument is a legal value for the
|
|
967 |
* {@code horizontalAlignment} and {@code horizontalTextPosition}
|
|
968 |
* properties. Valid values are:
|
|
969 |
* <ul>
|
|
970 |
* <li>{@code SwingConstants.RIGHT}
|
|
971 |
* <li>{@code SwingConstants.LEFT}
|
|
972 |
* <li>{@code SwingConstants.CENTER}
|
|
973 |
* <li>{@code SwingConstants.LEADING}
|
|
974 |
* <li>{@code SwingConstants.TRAILING}
|
|
975 |
* </ul>
|
|
976 |
*
|
|
977 |
* @param key the property value to check
|
|
978 |
* @param exception the message to use in the
|
|
979 |
* {@code IllegalArgumentException} that is thrown for an invalid
|
|
980 |
* value
|
|
981 |
* @exception IllegalArgumentException if key is not one of the legal
|
|
982 |
* values listed above
|
|
983 |
* @see #setHorizontalTextPosition
|
|
984 |
* @see #setHorizontalAlignment
|
|
985 |
*/
|
|
986 |
protected int checkHorizontalKey(int key, String exception) {
|
|
987 |
if ((key == LEFT) ||
|
|
988 |
(key == CENTER) ||
|
|
989 |
(key == RIGHT) ||
|
|
990 |
(key == LEADING) ||
|
|
991 |
(key == TRAILING)) {
|
|
992 |
return key;
|
|
993 |
} else {
|
|
994 |
throw new IllegalArgumentException(exception);
|
|
995 |
}
|
|
996 |
}
|
|
997 |
|
|
998 |
/**
|
|
999 |
* Verify that the {@code key} argument is a legal value for the
|
|
1000 |
* vertical properties. Valid values are:
|
|
1001 |
* <ul>
|
|
1002 |
* <li>{@code SwingConstants.CENTER}
|
|
1003 |
* <li>{@code SwingConstants.TOP}
|
|
1004 |
* <li>{@code SwingConstants.BOTTOM}
|
|
1005 |
* </ul>
|
|
1006 |
*
|
|
1007 |
* @param key the property value to check
|
|
1008 |
* @param exception the message to use in the
|
|
1009 |
* {@code IllegalArgumentException} that is thrown for an invalid
|
|
1010 |
* value
|
|
1011 |
* @exception IllegalArgumentException if key is not one of the legal
|
|
1012 |
* values listed above
|
|
1013 |
*/
|
|
1014 |
protected int checkVerticalKey(int key, String exception) {
|
|
1015 |
if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
|
|
1016 |
return key;
|
|
1017 |
} else {
|
|
1018 |
throw new IllegalArgumentException(exception);
|
|
1019 |
}
|
|
1020 |
}
|
|
1021 |
|
|
1022 |
/**
|
|
1023 |
*{@inheritDoc}
|
|
1024 |
*
|
|
1025 |
* @since 1.6
|
|
1026 |
*/
|
|
1027 |
public void removeNotify() {
|
|
1028 |
super.removeNotify();
|
|
1029 |
if(isRolloverEnabled()) {
|
|
1030 |
getModel().setRollover(false);
|
|
1031 |
}
|
|
1032 |
}
|
|
1033 |
|
|
1034 |
/**
|
|
1035 |
* Sets the action command for this button.
|
|
1036 |
* @param actionCommand the action command for this button
|
|
1037 |
*/
|
|
1038 |
public void setActionCommand(String actionCommand) {
|
|
1039 |
getModel().setActionCommand(actionCommand);
|
|
1040 |
}
|
|
1041 |
|
|
1042 |
/**
|
|
1043 |
* Returns the action command for this button.
|
|
1044 |
* @return the action command for this button
|
|
1045 |
*/
|
|
1046 |
public String getActionCommand() {
|
|
1047 |
String ac = getModel().getActionCommand();
|
|
1048 |
if(ac == null) {
|
|
1049 |
ac = getText();
|
|
1050 |
}
|
|
1051 |
return ac;
|
|
1052 |
}
|
|
1053 |
|
|
1054 |
private Action action;
|
|
1055 |
private PropertyChangeListener actionPropertyChangeListener;
|
|
1056 |
|
|
1057 |
/**
|
|
1058 |
* Sets the <code>Action</code>.
|
|
1059 |
* The new <code>Action</code> replaces any previously set
|
|
1060 |
* <code>Action</code> but does not affect <code>ActionListeners</code>
|
|
1061 |
* independently added with <code>addActionListener</code>.
|
|
1062 |
* If the <code>Action</code> is already a registered
|
|
1063 |
* <code>ActionListener</code> for the button, it is not re-registered.
|
|
1064 |
* <p>
|
|
1065 |
* Setting the <code>Action</code> results in immediately changing
|
|
1066 |
* all the properties described in <a href="Action.html#buttonActions">
|
|
1067 |
* Swing Components Supporting <code>Action</code></a>.
|
|
1068 |
* Subsequently, the button's properties are automatically updated
|
|
1069 |
* as the <code>Action</code>'s properties change.
|
|
1070 |
* <p>
|
|
1071 |
* This method uses three other methods to set
|
|
1072 |
* and help track the <code>Action</code>'s property values.
|
|
1073 |
* It uses the <code>configurePropertiesFromAction</code> method
|
|
1074 |
* to immediately change the button's properties.
|
|
1075 |
* To track changes in the <code>Action</code>'s property values,
|
|
1076 |
* this method registers the <code>PropertyChangeListener</code>
|
|
1077 |
* returned by <code>createActionPropertyChangeListener</code>. The
|
|
1078 |
* default {@code PropertyChangeListener} invokes the
|
|
1079 |
* {@code actionPropertyChanged} method when a property in the
|
|
1080 |
* {@code Action} changes.
|
|
1081 |
*
|
|
1082 |
* @param a the <code>Action</code> for the <code>AbstractButton</code>,
|
|
1083 |
* or <code>null</code>
|
|
1084 |
* @since 1.3
|
|
1085 |
* @see Action
|
|
1086 |
* @see #getAction
|
|
1087 |
* @see #configurePropertiesFromAction
|
|
1088 |
* @see #createActionPropertyChangeListener
|
|
1089 |
* @see #actionPropertyChanged
|
|
1090 |
* @beaninfo
|
|
1091 |
* bound: true
|
|
1092 |
* attribute: visualUpdate true
|
|
1093 |
* description: the Action instance connected with this ActionEvent source
|
|
1094 |
*/
|
|
1095 |
public void setAction(Action a) {
|
|
1096 |
Action oldValue = getAction();
|
|
1097 |
if (action==null || !action.equals(a)) {
|
|
1098 |
action = a;
|
|
1099 |
if (oldValue!=null) {
|
|
1100 |
removeActionListener(oldValue);
|
|
1101 |
oldValue.removePropertyChangeListener(actionPropertyChangeListener);
|
|
1102 |
actionPropertyChangeListener = null;
|
|
1103 |
}
|
|
1104 |
configurePropertiesFromAction(action);
|
|
1105 |
if (action!=null) {
|
|
1106 |
// Don't add if it is already a listener
|
|
1107 |
if (!isListener(ActionListener.class, action)) {
|
|
1108 |
addActionListener(action);
|
|
1109 |
}
|
|
1110 |
// Reverse linkage:
|
|
1111 |
actionPropertyChangeListener = createActionPropertyChangeListener(action);
|
|
1112 |
action.addPropertyChangeListener(actionPropertyChangeListener);
|
|
1113 |
}
|
|
1114 |
firePropertyChange("action", oldValue, action);
|
|
1115 |
}
|
|
1116 |
}
|
|
1117 |
|
|
1118 |
private boolean isListener(Class c, ActionListener a) {
|
|
1119 |
boolean isListener = false;
|
|
1120 |
Object[] listeners = listenerList.getListenerList();
|
|
1121 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
1122 |
if (listeners[i]==c && listeners[i+1]==a) {
|
|
1123 |
isListener=true;
|
|
1124 |
}
|
|
1125 |
}
|
|
1126 |
return isListener;
|
|
1127 |
}
|
|
1128 |
|
|
1129 |
/**
|
|
1130 |
* Returns the currently set <code>Action</code> for this
|
|
1131 |
* <code>ActionEvent</code> source, or <code>null</code>
|
|
1132 |
* if no <code>Action</code> is set.
|
|
1133 |
*
|
|
1134 |
* @return the <code>Action</code> for this <code>ActionEvent</code>
|
|
1135 |
* source, or <code>null</code>
|
|
1136 |
* @since 1.3
|
|
1137 |
* @see Action
|
|
1138 |
* @see #setAction
|
|
1139 |
*/
|
|
1140 |
public Action getAction() {
|
|
1141 |
return action;
|
|
1142 |
}
|
|
1143 |
|
|
1144 |
/**
|
|
1145 |
* Sets the properties on this button to match those in the specified
|
|
1146 |
* <code>Action</code>. Refer to <a href="Action.html#buttonActions">
|
|
1147 |
* Swing Components Supporting <code>Action</code></a> for more
|
|
1148 |
* details as to which properties this sets.
|
|
1149 |
*
|
|
1150 |
* @param a the <code>Action</code> from which to get the properties,
|
|
1151 |
* or <code>null</code>
|
|
1152 |
* @since 1.3
|
|
1153 |
* @see Action
|
|
1154 |
* @see #setAction
|
|
1155 |
*/
|
|
1156 |
protected void configurePropertiesFromAction(Action a) {
|
|
1157 |
setMnemonicFromAction(a);
|
|
1158 |
setTextFromAction(a, false);
|
|
1159 |
AbstractAction.setToolTipTextFromAction(this, a);
|
|
1160 |
setIconFromAction(a);
|
|
1161 |
setActionCommandFromAction(a);
|
|
1162 |
AbstractAction.setEnabledFromAction(this, a);
|
|
1163 |
if (AbstractAction.hasSelectedKey(a) &&
|
|
1164 |
shouldUpdateSelectedStateFromAction()) {
|
|
1165 |
setSelectedFromAction(a);
|
|
1166 |
}
|
|
1167 |
setDisplayedMnemonicIndexFromAction(a, false);
|
|
1168 |
}
|
|
1169 |
|
|
1170 |
void clientPropertyChanged(Object key, Object oldValue,
|
|
1171 |
Object newValue) {
|
|
1172 |
if (key == "hideActionText") {
|
|
1173 |
boolean current = (newValue instanceof Boolean) ?
|
|
1174 |
(Boolean)newValue : false;
|
|
1175 |
if (getHideActionText() != current) {
|
|
1176 |
setHideActionText(current);
|
|
1177 |
}
|
|
1178 |
}
|
|
1179 |
}
|
|
1180 |
|
|
1181 |
/**
|
|
1182 |
* Button subclasses that support mirroring the selected state from
|
|
1183 |
* the action should override this to return true. AbstractButton's
|
|
1184 |
* implementation returns false.
|
|
1185 |
*/
|
|
1186 |
boolean shouldUpdateSelectedStateFromAction() {
|
|
1187 |
return false;
|
|
1188 |
}
|
|
1189 |
|
|
1190 |
/**
|
|
1191 |
* Updates the button's state in response to property changes in the
|
|
1192 |
* associated action. This method is invoked from the
|
|
1193 |
* {@code PropertyChangeListener} returned from
|
|
1194 |
* {@code createActionPropertyChangeListener}. Subclasses do not normally
|
|
1195 |
* need to invoke this. Subclasses that support additional {@code Action}
|
|
1196 |
* properties should override this and
|
|
1197 |
* {@code configurePropertiesFromAction}.
|
|
1198 |
* <p>
|
|
1199 |
* Refer to the table at <a href="Action.html#buttonActions">
|
|
1200 |
* Swing Components Supporting <code>Action</code></a> for a list of
|
|
1201 |
* the properties this method sets.
|
|
1202 |
*
|
|
1203 |
* @param action the <code>Action</code> associated with this button
|
|
1204 |
* @param propertyName the name of the property that changed
|
|
1205 |
* @since 1.6
|
|
1206 |
* @see Action
|
|
1207 |
* @see #configurePropertiesFromAction
|
|
1208 |
*/
|
|
1209 |
protected void actionPropertyChanged(Action action, String propertyName) {
|
|
1210 |
if (propertyName == Action.NAME) {
|
|
1211 |
setTextFromAction(action, true);
|
|
1212 |
} else if (propertyName == "enabled") {
|
|
1213 |
AbstractAction.setEnabledFromAction(this, action);
|
|
1214 |
} else if (propertyName == Action.SHORT_DESCRIPTION) {
|
|
1215 |
AbstractAction.setToolTipTextFromAction(this, action);
|
|
1216 |
} else if (propertyName == Action.SMALL_ICON) {
|
|
1217 |
smallIconChanged(action);
|
|
1218 |
} else if (propertyName == Action.MNEMONIC_KEY) {
|
|
1219 |
setMnemonicFromAction(action);
|
|
1220 |
} else if (propertyName == Action.ACTION_COMMAND_KEY) {
|
|
1221 |
setActionCommandFromAction(action);
|
|
1222 |
} else if (propertyName == Action.SELECTED_KEY &&
|
|
1223 |
AbstractAction.hasSelectedKey(action) &&
|
|
1224 |
shouldUpdateSelectedStateFromAction()) {
|
|
1225 |
setSelectedFromAction(action);
|
|
1226 |
} else if (propertyName == Action.DISPLAYED_MNEMONIC_INDEX_KEY) {
|
|
1227 |
setDisplayedMnemonicIndexFromAction(action, true);
|
|
1228 |
} else if (propertyName == Action.LARGE_ICON_KEY) {
|
|
1229 |
largeIconChanged(action);
|
|
1230 |
}
|
|
1231 |
}
|
|
1232 |
|
|
1233 |
private void setDisplayedMnemonicIndexFromAction(
|
|
1234 |
Action a, boolean fromPropertyChange) {
|
|
1235 |
Integer iValue = (a == null) ? null :
|
|
1236 |
(Integer)a.getValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY);
|
|
1237 |
if (fromPropertyChange || iValue != null) {
|
|
1238 |
int value;
|
|
1239 |
if (iValue == null) {
|
|
1240 |
value = -1;
|
|
1241 |
} else {
|
|
1242 |
value = iValue;
|
|
1243 |
String text = getText();
|
|
1244 |
if (text == null || value >= text.length()) {
|
|
1245 |
value = -1;
|
|
1246 |
}
|
|
1247 |
}
|
|
1248 |
setDisplayedMnemonicIndex(value);
|
|
1249 |
}
|
|
1250 |
}
|
|
1251 |
|
|
1252 |
private void setMnemonicFromAction(Action a) {
|
|
1253 |
Integer n = (a == null) ? null :
|
|
1254 |
(Integer)a.getValue(Action.MNEMONIC_KEY);
|
|
1255 |
setMnemonic((n == null) ? '\0' : n);
|
|
1256 |
}
|
|
1257 |
|
|
1258 |
private void setTextFromAction(Action a, boolean propertyChange) {
|
|
1259 |
boolean hideText = getHideActionText();
|
|
1260 |
if (!propertyChange) {
|
|
1261 |
setText((a != null && !hideText) ?
|
|
1262 |
(String)a.getValue(Action.NAME) : null);
|
|
1263 |
}
|
|
1264 |
else if (!hideText) {
|
|
1265 |
setText((String)a.getValue(Action.NAME));
|
|
1266 |
}
|
|
1267 |
}
|
|
1268 |
|
|
1269 |
void setIconFromAction(Action a) {
|
|
1270 |
Icon icon = null;
|
|
1271 |
if (a != null) {
|
|
1272 |
icon = (Icon)a.getValue(Action.LARGE_ICON_KEY);
|
|
1273 |
if (icon == null) {
|
|
1274 |
icon = (Icon)a.getValue(Action.SMALL_ICON);
|
|
1275 |
}
|
|
1276 |
}
|
|
1277 |
setIcon(icon);
|
|
1278 |
}
|
|
1279 |
|
|
1280 |
void smallIconChanged(Action a) {
|
|
1281 |
if (a.getValue(Action.LARGE_ICON_KEY) == null) {
|
|
1282 |
setIconFromAction(a);
|
|
1283 |
}
|
|
1284 |
}
|
|
1285 |
|
|
1286 |
void largeIconChanged(Action a) {
|
|
1287 |
setIconFromAction(a);
|
|
1288 |
}
|
|
1289 |
|
|
1290 |
private void setActionCommandFromAction(Action a) {
|
|
1291 |
setActionCommand((a != null) ?
|
|
1292 |
(String)a.getValue(Action.ACTION_COMMAND_KEY) :
|
|
1293 |
null);
|
|
1294 |
}
|
|
1295 |
|
|
1296 |
/**
|
|
1297 |
* Sets the seleted state of the button from the action. This is defined
|
|
1298 |
* here, but not wired up. Subclasses like JToggleButton and
|
|
1299 |
* JCheckBoxMenuItem make use of it.
|
|
1300 |
*
|
|
1301 |
* @param a the Action
|
|
1302 |
*/
|
|
1303 |
private void setSelectedFromAction(Action a) {
|
|
1304 |
boolean selected = false;
|
|
1305 |
if (a != null) {
|
|
1306 |
selected = AbstractAction.isSelected(a);
|
|
1307 |
}
|
|
1308 |
if (selected != isSelected()) {
|
|
1309 |
// This won't notify ActionListeners, but that should be
|
|
1310 |
// ok as the change is coming from the Action.
|
|
1311 |
setSelected(selected);
|
|
1312 |
// Make sure the change actually took effect
|
|
1313 |
if (!selected && isSelected()) {
|
|
1314 |
if (getModel() instanceof DefaultButtonModel) {
|
|
1315 |
ButtonGroup group = (ButtonGroup)
|
|
1316 |
((DefaultButtonModel)getModel()).getGroup();
|
|
1317 |
if (group != null) {
|
|
1318 |
group.clearSelection();
|
|
1319 |
}
|
|
1320 |
}
|
|
1321 |
}
|
|
1322 |
}
|
|
1323 |
}
|
|
1324 |
|
|
1325 |
/**
|
|
1326 |
* Creates and returns a <code>PropertyChangeListener</code> that is
|
|
1327 |
* responsible for listening for changes from the specified
|
|
1328 |
* <code>Action</code> and updating the appropriate properties.
|
|
1329 |
* <p>
|
|
1330 |
* <b>Warning:</b> If you subclass this do not create an anonymous
|
|
1331 |
* inner class. If you do the lifetime of the button will be tied to
|
|
1332 |
* that of the <code>Action</code>.
|
|
1333 |
*
|
|
1334 |
* @param a the button's action
|
|
1335 |
* @since 1.3
|
|
1336 |
* @see <a href="#actions">Actions</a>
|
|
1337 |
* @see Action
|
|
1338 |
* @see #setAction
|
|
1339 |
*/
|
|
1340 |
protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
|
|
1341 |
return createActionPropertyChangeListener0(a);
|
|
1342 |
}
|
|
1343 |
|
|
1344 |
|
|
1345 |
PropertyChangeListener createActionPropertyChangeListener0(Action a) {
|
|
1346 |
return new ButtonActionPropertyChangeListener(this, a);
|
|
1347 |
}
|
|
1348 |
|
|
1349 |
private static class ButtonActionPropertyChangeListener
|
|
1350 |
extends ActionPropertyChangeListener<AbstractButton> {
|
|
1351 |
ButtonActionPropertyChangeListener(AbstractButton b, Action a) {
|
|
1352 |
super(b, a);
|
|
1353 |
}
|
|
1354 |
protected void actionPropertyChanged(AbstractButton button,
|
|
1355 |
Action action,
|
|
1356 |
PropertyChangeEvent e) {
|
|
1357 |
if (AbstractAction.shouldReconfigure(e)) {
|
|
1358 |
button.configurePropertiesFromAction(action);
|
|
1359 |
} else {
|
|
1360 |
button.actionPropertyChanged(action, e.getPropertyName());
|
|
1361 |
}
|
|
1362 |
}
|
|
1363 |
}
|
|
1364 |
|
|
1365 |
/**
|
|
1366 |
* Gets the <code>borderPainted</code> property.
|
|
1367 |
*
|
|
1368 |
* @return the value of the <code>borderPainted</code> property
|
|
1369 |
* @see #setBorderPainted
|
|
1370 |
*/
|
|
1371 |
public boolean isBorderPainted() {
|
|
1372 |
return paintBorder;
|
|
1373 |
}
|
|
1374 |
|
|
1375 |
/**
|
|
1376 |
* Sets the <code>borderPainted</code> property.
|
|
1377 |
* If <code>true</code> and the button has a border,
|
|
1378 |
* the border is painted. The default value for the
|
|
1379 |
* <code>borderPainted</code> property is <code>true</code>.
|
|
1380 |
*
|
|
1381 |
* @param b if true and border property is not <code>null</code>,
|
|
1382 |
* the border is painted
|
|
1383 |
* @see #isBorderPainted
|
|
1384 |
* @beaninfo
|
|
1385 |
* bound: true
|
|
1386 |
* attribute: visualUpdate true
|
|
1387 |
* description: Whether the border should be painted.
|
|
1388 |
*/
|
|
1389 |
public void setBorderPainted(boolean b) {
|
|
1390 |
boolean oldValue = paintBorder;
|
|
1391 |
paintBorder = b;
|
|
1392 |
borderPaintedSet = true;
|
|
1393 |
firePropertyChange(BORDER_PAINTED_CHANGED_PROPERTY, oldValue, paintBorder);
|
|
1394 |
if (b != oldValue) {
|
|
1395 |
revalidate();
|
|
1396 |
repaint();
|
|
1397 |
}
|
|
1398 |
}
|
|
1399 |
|
|
1400 |
/**
|
|
1401 |
* Paint the button's border if <code>BorderPainted</code>
|
|
1402 |
* property is true and the button has a border.
|
|
1403 |
* @param g the <code>Graphics</code> context in which to paint
|
|
1404 |
*
|
|
1405 |
* @see #paint
|
|
1406 |
* @see #setBorder
|
|
1407 |
*/
|
|
1408 |
protected void paintBorder(Graphics g) {
|
|
1409 |
if (isBorderPainted()) {
|
|
1410 |
super.paintBorder(g);
|
|
1411 |
}
|
|
1412 |
}
|
|
1413 |
|
|
1414 |
/**
|
|
1415 |
* Gets the <code>paintFocus</code> property.
|
|
1416 |
*
|
|
1417 |
* @return the <code>paintFocus</code> property
|
|
1418 |
* @see #setFocusPainted
|
|
1419 |
*/
|
|
1420 |
public boolean isFocusPainted() {
|
|
1421 |
return paintFocus;
|
|
1422 |
}
|
|
1423 |
|
|
1424 |
/**
|
|
1425 |
* Sets the <code>paintFocus</code> property, which must
|
|
1426 |
* be <code>true</code> for the focus state to be painted.
|
|
1427 |
* The default value for the <code>paintFocus</code> property
|
|
1428 |
* is <code>true</code>.
|
|
1429 |
* Some look and feels might not paint focus state;
|
|
1430 |
* they will ignore this property.
|
|
1431 |
*
|
|
1432 |
* @param b if <code>true</code>, the focus state should be painted
|
|
1433 |
* @see #isFocusPainted
|
|
1434 |
* @beaninfo
|
|
1435 |
* bound: true
|
|
1436 |
* attribute: visualUpdate true
|
|
1437 |
* description: Whether focus should be painted
|
|
1438 |
*/
|
|
1439 |
public void setFocusPainted(boolean b) {
|
|
1440 |
boolean oldValue = paintFocus;
|
|
1441 |
paintFocus = b;
|
|
1442 |
firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY, oldValue, paintFocus);
|
|
1443 |
if (b != oldValue && isFocusOwner()) {
|
|
1444 |
revalidate();
|
|
1445 |
repaint();
|
|
1446 |
}
|
|
1447 |
}
|
|
1448 |
|
|
1449 |
/**
|
|
1450 |
* Gets the <code>contentAreaFilled</code> property.
|
|
1451 |
*
|
|
1452 |
* @return the <code>contentAreaFilled</code> property
|
|
1453 |
* @see #setContentAreaFilled
|
|
1454 |
*/
|
|
1455 |
public boolean isContentAreaFilled() {
|
|
1456 |
return contentAreaFilled;
|
|
1457 |
}
|
|
1458 |
|
|
1459 |
/**
|
|
1460 |
* Sets the <code>contentAreaFilled</code> property.
|
|
1461 |
* If <code>true</code> the button will paint the content
|
|
1462 |
* area. If you wish to have a transparent button, such as
|
|
1463 |
* an icon only button, for example, then you should set
|
|
1464 |
* this to <code>false</code>. Do not call <code>setOpaque(false)</code>.
|
|
1465 |
* The default value for the the <code>contentAreaFilled</code>
|
|
1466 |
* property is <code>true</code>.
|
|
1467 |
* <p>
|
|
1468 |
* This function may cause the component's opaque property to change.
|
|
1469 |
* <p>
|
|
1470 |
* The exact behavior of calling this function varies on a
|
|
1471 |
* component-by-component and L&F-by-L&F basis.
|
|
1472 |
*
|
|
1473 |
* @param b if true, the content should be filled; if false
|
|
1474 |
* the content area is not filled
|
|
1475 |
* @see #isContentAreaFilled
|
|
1476 |
* @see #setOpaque
|
|
1477 |
* @beaninfo
|
|
1478 |
* bound: true
|
|
1479 |
* attribute: visualUpdate true
|
|
1480 |
* description: Whether the button should paint the content area
|
|
1481 |
* or leave it transparent.
|
|
1482 |
*/
|
|
1483 |
public void setContentAreaFilled(boolean b) {
|
|
1484 |
boolean oldValue = contentAreaFilled;
|
|
1485 |
contentAreaFilled = b;
|
|
1486 |
contentAreaFilledSet = true;
|
|
1487 |
firePropertyChange(CONTENT_AREA_FILLED_CHANGED_PROPERTY, oldValue, contentAreaFilled);
|
|
1488 |
if (b != oldValue) {
|
|
1489 |
repaint();
|
|
1490 |
}
|
|
1491 |
}
|
|
1492 |
|
|
1493 |
/**
|
|
1494 |
* Gets the <code>rolloverEnabled</code> property.
|
|
1495 |
*
|
|
1496 |
* @return the value of the <code>rolloverEnabled</code> property
|
|
1497 |
* @see #setRolloverEnabled
|
|
1498 |
*/
|
|
1499 |
public boolean isRolloverEnabled() {
|
|
1500 |
return rolloverEnabled;
|
|
1501 |
}
|
|
1502 |
|
|
1503 |
/**
|
|
1504 |
* Sets the <code>rolloverEnabled</code> property, which
|
|
1505 |
* must be <code>true</code> for rollover effects to occur.
|
|
1506 |
* The default value for the <code>rolloverEnabled</code>
|
|
1507 |
* property is <code>false</code>.
|
|
1508 |
* Some look and feels might not implement rollover effects;
|
|
1509 |
* they will ignore this property.
|
|
1510 |
*
|
|
1511 |
* @param b if <code>true</code>, rollover effects should be painted
|
|
1512 |
* @see #isRolloverEnabled
|
|
1513 |
* @beaninfo
|
|
1514 |
* bound: true
|
|
1515 |
* attribute: visualUpdate true
|
|
1516 |
* description: Whether rollover effects should be enabled.
|
|
1517 |
*/
|
|
1518 |
public void setRolloverEnabled(boolean b) {
|
|
1519 |
boolean oldValue = rolloverEnabled;
|
|
1520 |
rolloverEnabled = b;
|
|
1521 |
rolloverEnabledSet = true;
|
|
1522 |
firePropertyChange(ROLLOVER_ENABLED_CHANGED_PROPERTY, oldValue, rolloverEnabled);
|
|
1523 |
if (b != oldValue) {
|
|
1524 |
repaint();
|
|
1525 |
}
|
|
1526 |
}
|
|
1527 |
|
|
1528 |
/**
|
|
1529 |
* Returns the keyboard mnemonic from the the current model.
|
|
1530 |
* @return the keyboard mnemonic from the model
|
|
1531 |
*/
|
|
1532 |
public int getMnemonic() {
|
|
1533 |
return mnemonic;
|
|
1534 |
}
|
|
1535 |
|
|
1536 |
/**
|
|
1537 |
* Sets the keyboard mnemonic on the current model.
|
|
1538 |
* The mnemonic is the key which when combined with the look and feel's
|
|
1539 |
* mouseless modifier (usually Alt) will activate this button
|
|
1540 |
* if focus is contained somewhere within this button's ancestor
|
|
1541 |
* window.
|
|
1542 |
* <p>
|
|
1543 |
* A mnemonic must correspond to a single key on the keyboard
|
|
1544 |
* and should be specified using one of the <code>VK_XXX</code>
|
|
1545 |
* keycodes defined in <code>java.awt.event.KeyEvent</code>.
|
|
1546 |
* Mnemonics are case-insensitive, therefore a key event
|
|
1547 |
* with the corresponding keycode would cause the button to be
|
|
1548 |
* activated whether or not the Shift modifier was pressed.
|
|
1549 |
* <p>
|
|
1550 |
* If the character defined by the mnemonic is found within
|
|
1551 |
* the button's label string, the first occurrence of it
|
|
1552 |
* will be underlined to indicate the mnemonic to the user.
|
|
1553 |
*
|
|
1554 |
* @param mnemonic the key code which represents the mnemonic
|
|
1555 |
* @see java.awt.event.KeyEvent
|
|
1556 |
* @see #setDisplayedMnemonicIndex
|
|
1557 |
*
|
|
1558 |
* @beaninfo
|
|
1559 |
* bound: true
|
|
1560 |
* attribute: visualUpdate true
|
|
1561 |
* description: the keyboard character mnemonic
|
|
1562 |
*/
|
|
1563 |
public void setMnemonic(int mnemonic) {
|
|
1564 |
int oldValue = getMnemonic();
|
|
1565 |
model.setMnemonic(mnemonic);
|
|
1566 |
updateMnemonicProperties();
|
|
1567 |
}
|
|
1568 |
|
|
1569 |
/**
|
|
1570 |
* This method is now obsolete, please use <code>setMnemonic(int)</code>
|
|
1571 |
* to set the mnemonic for a button. This method is only designed
|
|
1572 |
* to handle character values which fall between 'a' and 'z' or
|
|
1573 |
* 'A' and 'Z'.
|
|
1574 |
*
|
|
1575 |
* @param mnemonic a char specifying the mnemonic value
|
|
1576 |
* @see #setMnemonic(int)
|
|
1577 |
* @beaninfo
|
|
1578 |
* bound: true
|
|
1579 |
* attribute: visualUpdate true
|
|
1580 |
* description: the keyboard character mnemonic
|
|
1581 |
*/
|
|
1582 |
public void setMnemonic(char mnemonic) {
|
|
1583 |
int vk = (int) mnemonic;
|
|
1584 |
if(vk >= 'a' && vk <='z')
|
|
1585 |
vk -= ('a' - 'A');
|
|
1586 |
setMnemonic(vk);
|
|
1587 |
}
|
|
1588 |
|
|
1589 |
/**
|
|
1590 |
* Provides a hint to the look and feel as to which character in the
|
|
1591 |
* text should be decorated to represent the mnemonic. Not all look and
|
|
1592 |
* feels may support this. A value of -1 indicates either there is no
|
|
1593 |
* mnemonic, the mnemonic character is not contained in the string, or
|
|
1594 |
* the developer does not wish the mnemonic to be displayed.
|
|
1595 |
* <p>
|
|
1596 |
* The value of this is updated as the properties relating to the
|
|
1597 |
* mnemonic change (such as the mnemonic itself, the text...).
|
|
1598 |
* You should only ever have to call this if
|
|
1599 |
* you do not wish the default character to be underlined. For example, if
|
|
1600 |
* the text was 'Save As', with a mnemonic of 'a', and you wanted the 'A'
|
|
1601 |
* to be decorated, as 'Save <u>A</u>s', you would have to invoke
|
|
1602 |
* <code>setDisplayedMnemonicIndex(5)</code> after invoking
|
|
1603 |
* <code>setMnemonic(KeyEvent.VK_A)</code>.
|
|
1604 |
*
|
|
1605 |
* @since 1.4
|
|
1606 |
* @param index Index into the String to underline
|
|
1607 |
* @exception IllegalArgumentException will be thrown if <code>index</code>
|
|
1608 |
* is >= length of the text, or < -1
|
|
1609 |
* @see #getDisplayedMnemonicIndex
|
|
1610 |
*
|
|
1611 |
* @beaninfo
|
|
1612 |
* bound: true
|
|
1613 |
* attribute: visualUpdate true
|
|
1614 |
* description: the index into the String to draw the keyboard character
|
|
1615 |
* mnemonic at
|
|
1616 |
*/
|
|
1617 |
public void setDisplayedMnemonicIndex(int index)
|
|
1618 |
throws IllegalArgumentException {
|
|
1619 |
int oldValue = mnemonicIndex;
|
|
1620 |
if (index == -1) {
|
|
1621 |
mnemonicIndex = -1;
|
|
1622 |
} else {
|
|
1623 |
String text = getText();
|
|
1624 |
int textLength = (text == null) ? 0 : text.length();
|
|
1625 |
if (index < -1 || index >= textLength) { // index out of range
|
|
1626 |
throw new IllegalArgumentException("index == " + index);
|
|
1627 |
}
|
|
1628 |
}
|
|
1629 |
mnemonicIndex = index;
|
|
1630 |
firePropertyChange("displayedMnemonicIndex", oldValue, index);
|
|
1631 |
if (index != oldValue) {
|
|
1632 |
revalidate();
|
|
1633 |
repaint();
|
|
1634 |
}
|
|
1635 |
}
|
|
1636 |
|
|
1637 |
/**
|
|
1638 |
* Returns the character, as an index, that the look and feel should
|
|
1639 |
* provide decoration for as representing the mnemonic character.
|
|
1640 |
*
|
|
1641 |
* @since 1.4
|
|
1642 |
* @return index representing mnemonic character
|
|
1643 |
* @see #setDisplayedMnemonicIndex
|
|
1644 |
*/
|
|
1645 |
public int getDisplayedMnemonicIndex() {
|
|
1646 |
return mnemonicIndex;
|
|
1647 |
}
|
|
1648 |
|
|
1649 |
/**
|
|
1650 |
* Update the displayedMnemonicIndex property. This method
|
|
1651 |
* is called when either text or mnemonic changes. The new
|
|
1652 |
* value of the displayedMnemonicIndex property is the index
|
|
1653 |
* of the first occurrence of mnemonic in text.
|
|
1654 |
*/
|
|
1655 |
private void updateDisplayedMnemonicIndex(String text, int mnemonic) {
|
|
1656 |
setDisplayedMnemonicIndex(
|
|
1657 |
SwingUtilities.findDisplayedMnemonicIndex(text, mnemonic));
|
|
1658 |
}
|
|
1659 |
|
|
1660 |
/**
|
|
1661 |
* Brings the mnemonic property in accordance with model's mnemonic.
|
|
1662 |
* This is called when model's mnemonic changes. Also updates the
|
|
1663 |
* displayedMnemonicIndex property.
|
|
1664 |
*/
|
|
1665 |
private void updateMnemonicProperties() {
|
|
1666 |
int newMnemonic = model.getMnemonic();
|
|
1667 |
if (mnemonic != newMnemonic) {
|
|
1668 |
int oldValue = mnemonic;
|
|
1669 |
mnemonic = newMnemonic;
|
|
1670 |
firePropertyChange(MNEMONIC_CHANGED_PROPERTY,
|
|
1671 |
oldValue, mnemonic);
|
|
1672 |
updateDisplayedMnemonicIndex(getText(), mnemonic);
|
|
1673 |
revalidate();
|
|
1674 |
repaint();
|
|
1675 |
}
|
|
1676 |
}
|
|
1677 |
|
|
1678 |
/**
|
|
1679 |
* Sets the amount of time (in milliseconds) required between
|
|
1680 |
* mouse press events for the button to generate the corresponding
|
|
1681 |
* action events. After the initial mouse press occurs (and action
|
|
1682 |
* event generated) any subsequent mouse press events which occur
|
|
1683 |
* on intervals less than the threshhold will be ignored and no
|
|
1684 |
* corresponding action event generated. By default the threshhold is 0,
|
|
1685 |
* which means that for each mouse press, an action event will be
|
|
1686 |
* fired, no matter how quickly the mouse clicks occur. In buttons
|
|
1687 |
* where this behavior is not desirable (for example, the "OK" button
|
|
1688 |
* in a dialog), this threshhold should be set to an appropriate
|
|
1689 |
* positive value.
|
|
1690 |
*
|
|
1691 |
* @see #getMultiClickThreshhold
|
|
1692 |
* @param threshhold the amount of time required between mouse
|
|
1693 |
* press events to generate corresponding action events
|
|
1694 |
* @exception IllegalArgumentException if threshhold < 0
|
|
1695 |
* @since 1.4
|
|
1696 |
*/
|
|
1697 |
public void setMultiClickThreshhold(long threshhold) {
|
|
1698 |
if (threshhold < 0) {
|
|
1699 |
throw new IllegalArgumentException("threshhold must be >= 0");
|
|
1700 |
}
|
|
1701 |
this.multiClickThreshhold = threshhold;
|
|
1702 |
}
|
|
1703 |
|
|
1704 |
/**
|
|
1705 |
* Gets the amount of time (in milliseconds) required between
|
|
1706 |
* mouse press events for the button to generate the corresponding
|
|
1707 |
* action events.
|
|
1708 |
*
|
|
1709 |
* @see #setMultiClickThreshhold
|
|
1710 |
* @return the amount of time required between mouse press events
|
|
1711 |
* to generate corresponding action events
|
|
1712 |
* @since 1.4
|
|
1713 |
*/
|
|
1714 |
public long getMultiClickThreshhold() {
|
|
1715 |
return multiClickThreshhold;
|
|
1716 |
}
|
|
1717 |
|
|
1718 |
/**
|
|
1719 |
* Returns the model that this button represents.
|
|
1720 |
* @return the <code>model</code> property
|
|
1721 |
* @see #setModel
|
|
1722 |
*/
|
|
1723 |
public ButtonModel getModel() {
|
|
1724 |
return model;
|
|
1725 |
}
|
|
1726 |
|
|
1727 |
/**
|
|
1728 |
* Sets the model that this button represents.
|
|
1729 |
* @param newModel the new <code>ButtonModel</code>
|
|
1730 |
* @see #getModel
|
|
1731 |
* @beaninfo
|
|
1732 |
* bound: true
|
|
1733 |
* description: Model that the Button uses.
|
|
1734 |
*/
|
|
1735 |
public void setModel(ButtonModel newModel) {
|
|
1736 |
|
|
1737 |
ButtonModel oldModel = getModel();
|
|
1738 |
|
|
1739 |
if (oldModel != null) {
|
|
1740 |
oldModel.removeChangeListener(changeListener);
|
|
1741 |
oldModel.removeActionListener(actionListener);
|
|
1742 |
oldModel.removeItemListener(itemListener);
|
|
1743 |
changeListener = null;
|
|
1744 |
actionListener = null;
|
|
1745 |
itemListener = null;
|
|
1746 |
}
|
|
1747 |
|
|
1748 |
model = newModel;
|
|
1749 |
|
|
1750 |
if (newModel != null) {
|
|
1751 |
changeListener = createChangeListener();
|
|
1752 |
actionListener = createActionListener();
|
|
1753 |
itemListener = createItemListener();
|
|
1754 |
newModel.addChangeListener(changeListener);
|
|
1755 |
newModel.addActionListener(actionListener);
|
|
1756 |
newModel.addItemListener(itemListener);
|
|
1757 |
|
|
1758 |
updateMnemonicProperties();
|
|
1759 |
//We invoke setEnabled() from JComponent
|
|
1760 |
//because setModel() can be called from a constructor
|
|
1761 |
//when the button is not fully initialized
|
|
1762 |
super.setEnabled(newModel.isEnabled());
|
|
1763 |
|
|
1764 |
} else {
|
|
1765 |
mnemonic = '\0';
|
|
1766 |
}
|
|
1767 |
|
|
1768 |
updateDisplayedMnemonicIndex(getText(), mnemonic);
|
|
1769 |
|
|
1770 |
firePropertyChange(MODEL_CHANGED_PROPERTY, oldModel, newModel);
|
|
1771 |
if (newModel != oldModel) {
|
|
1772 |
revalidate();
|
|
1773 |
repaint();
|
|
1774 |
}
|
|
1775 |
}
|
|
1776 |
|
|
1777 |
|
|
1778 |
/**
|
|
1779 |
* Returns the L&F object that renders this component.
|
|
1780 |
* @return the ButtonUI object
|
|
1781 |
* @see #setUI
|
|
1782 |
*/
|
|
1783 |
public ButtonUI getUI() {
|
|
1784 |
return (ButtonUI) ui;
|
|
1785 |
}
|
|
1786 |
|
|
1787 |
|
|
1788 |
/**
|
|
1789 |
* Sets the L&F object that renders this component.
|
|
1790 |
* @param ui the <code>ButtonUI</code> L&F object
|
|
1791 |
* @see #getUI
|
|
1792 |
* @beaninfo
|
|
1793 |
* bound: true
|
|
1794 |
* hidden: true
|
|
1795 |
* attribute: visualUpdate true
|
|
1796 |
* description: The UI object that implements the LookAndFeel.
|
|
1797 |
*/
|
|
1798 |
public void setUI(ButtonUI ui) {
|
|
1799 |
super.setUI(ui);
|
|
1800 |
// disabled icons are generated by the LF so they should be unset here
|
|
1801 |
if (disabledIcon instanceof UIResource) {
|
|
1802 |
setDisabledIcon(null);
|
|
1803 |
}
|
|
1804 |
if (disabledSelectedIcon instanceof UIResource) {
|
|
1805 |
setDisabledSelectedIcon(null);
|
|
1806 |
}
|
|
1807 |
}
|
|
1808 |
|
|
1809 |
|
|
1810 |
/**
|
|
1811 |
* Resets the UI property to a value from the current look
|
|
1812 |
* and feel. Subtypes of <code>AbstractButton</code>
|
|
1813 |
* should override this to update the UI. For
|
|
1814 |
* example, <code>JButton</code> might do the following:
|
|
1815 |
* <pre>
|
|
1816 |
* setUI((ButtonUI)UIManager.getUI(
|
|
1817 |
* "ButtonUI", "javax.swing.plaf.basic.BasicButtonUI", this));
|
|
1818 |
* </pre>
|
|
1819 |
*/
|
|
1820 |
public void updateUI() {
|
|
1821 |
}
|
|
1822 |
|
|
1823 |
/**
|
|
1824 |
* Adds the specified component to this container at the specified
|
|
1825 |
* index, refer to
|
|
1826 |
* {@link java.awt.Container#addImpl(Component, Object, int)}
|
|
1827 |
* for a complete description of this method.
|
|
1828 |
*
|
|
1829 |
* @param comp the component to be added
|
|
1830 |
* @param constraints an object expressing layout constraints
|
|
1831 |
* for this component
|
|
1832 |
* @param index the position in the container's list at which to
|
|
1833 |
* insert the component, where <code>-1</code>
|
|
1834 |
* means append to the end
|
|
1835 |
* @exception IllegalArgumentException if <code>index</code> is invalid
|
|
1836 |
* @exception IllegalArgumentException if adding the container's parent
|
|
1837 |
* to itself
|
|
1838 |
* @exception IllegalArgumentException if adding a window to a container
|
|
1839 |
* @since 1.5
|
|
1840 |
*/
|
|
1841 |
protected void addImpl(Component comp, Object constraints, int index) {
|
|
1842 |
if (!setLayout) {
|
|
1843 |
setLayout(new OverlayLayout(this));
|
|
1844 |
}
|
|
1845 |
super.addImpl(comp, constraints, index);
|
|
1846 |
}
|
|
1847 |
|
|
1848 |
/**
|
|
1849 |
* Sets the layout manager for this container, refer to
|
|
1850 |
* {@link java.awt.Container#setLayout(LayoutManager)}
|
|
1851 |
* for a complete description of this method.
|
|
1852 |
*
|
|
1853 |
* @param mgr the specified layout manager
|
|
1854 |
* @since 1.5
|
|
1855 |
*/
|
|
1856 |
public void setLayout(LayoutManager mgr) {
|
|
1857 |
setLayout = true;
|
|
1858 |
super.setLayout(mgr);
|
|
1859 |
}
|
|
1860 |
|
|
1861 |
/**
|
|
1862 |
* Adds a <code>ChangeListener</code> to the button.
|
|
1863 |
* @param l the listener to be added
|
|
1864 |
*/
|
|
1865 |
public void addChangeListener(ChangeListener l) {
|
|
1866 |
listenerList.add(ChangeListener.class, l);
|
|
1867 |
}
|
|
1868 |
|
|
1869 |
/**
|
|
1870 |
* Removes a ChangeListener from the button.
|
|
1871 |
* @param l the listener to be removed
|
|
1872 |
*/
|
|
1873 |
public void removeChangeListener(ChangeListener l) {
|
|
1874 |
listenerList.remove(ChangeListener.class, l);
|
|
1875 |
}
|
|
1876 |
|
|
1877 |
/**
|
|
1878 |
* Returns an array of all the <code>ChangeListener</code>s added
|
|
1879 |
* to this AbstractButton with addChangeListener().
|
|
1880 |
*
|
|
1881 |
* @return all of the <code>ChangeListener</code>s added or an empty
|
|
1882 |
* array if no listeners have been added
|
|
1883 |
* @since 1.4
|
|
1884 |
*/
|
|
1885 |
public ChangeListener[] getChangeListeners() {
|
|
1886 |
return (ChangeListener[])(listenerList.getListeners(
|
|
1887 |
ChangeListener.class));
|
|
1888 |
}
|
|
1889 |
|
|
1890 |
/**
|
|
1891 |
* Notifies all listeners that have registered interest for
|
|
1892 |
* notification on this event type. The event instance
|
|
1893 |
* is lazily created.
|
|
1894 |
* @see EventListenerList
|
|
1895 |
*/
|
|
1896 |
protected void fireStateChanged() {
|
|
1897 |
// Guaranteed to return a non-null array
|
|
1898 |
Object[] listeners = listenerList.getListenerList();
|
|
1899 |
// Process the listeners last to first, notifying
|
|
1900 |
// those that are interested in this event
|
|
1901 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
1902 |
if (listeners[i]==ChangeListener.class) {
|
|
1903 |
// Lazily create the event:
|
|
1904 |
if (changeEvent == null)
|
|
1905 |
changeEvent = new ChangeEvent(this);
|
|
1906 |
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
|
|
1907 |
}
|
|
1908 |
}
|
|
1909 |
}
|
|
1910 |
|
|
1911 |
/**
|
|
1912 |
* Adds an <code>ActionListener</code> to the button.
|
|
1913 |
* @param l the <code>ActionListener</code> to be added
|
|
1914 |
*/
|
|
1915 |
public void addActionListener(ActionListener l) {
|
|
1916 |
listenerList.add(ActionListener.class, l);
|
|
1917 |
}
|
|
1918 |
|
|
1919 |
/**
|
|
1920 |
* Removes an <code>ActionListener</code> from the button.
|
|
1921 |
* If the listener is the currently set <code>Action</code>
|
|
1922 |
* for the button, then the <code>Action</code>
|
|
1923 |
* is set to <code>null</code>.
|
|
1924 |
*
|
|
1925 |
* @param l the listener to be removed
|
|
1926 |
*/
|
|
1927 |
public void removeActionListener(ActionListener l) {
|
|
1928 |
if ((l != null) && (getAction() == l)) {
|
|
1929 |
setAction(null);
|
|
1930 |
} else {
|
|
1931 |
listenerList.remove(ActionListener.class, l);
|
|
1932 |
}
|
|
1933 |
}
|
|
1934 |
|
|
1935 |
/**
|
|
1936 |
* Returns an array of all the <code>ActionListener</code>s added
|
|
1937 |
* to this AbstractButton with addActionListener().
|
|
1938 |
*
|
|
1939 |
* @return all of the <code>ActionListener</code>s added or an empty
|
|
1940 |
* array if no listeners have been added
|
|
1941 |
* @since 1.4
|
|
1942 |
*/
|
|
1943 |
public ActionListener[] getActionListeners() {
|
|
1944 |
return (ActionListener[])(listenerList.getListeners(
|
|
1945 |
ActionListener.class));
|
|
1946 |
}
|
|
1947 |
|
|
1948 |
/**
|
|
1949 |
* Subclasses that want to handle <code>ChangeEvents</code> differently
|
|
1950 |
* can override this to return another <code>ChangeListener</code>
|
|
1951 |
* implementation.
|
|
1952 |
*
|
|
1953 |
* @return the new <code>ChangeListener</code>
|
|
1954 |
*/
|
|
1955 |
protected ChangeListener createChangeListener() {
|
|
1956 |
return getHandler();
|
|
1957 |
}
|
|
1958 |
|
|
1959 |
/**
|
|
1960 |
* Extends <code>ChangeListener</code> to be serializable.
|
|
1961 |
* <p>
|
|
1962 |
* <strong>Warning:</strong>
|
|
1963 |
* Serialized objects of this class will not be compatible with
|
|
1964 |
* future Swing releases. The current serialization support is
|
|
1965 |
* appropriate for short term storage or RMI between applications running
|
|
1966 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
1967 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
1968 |
* has been added to the <code>java.beans</code> package.
|
|
1969 |
* Please see {@link java.beans.XMLEncoder}.
|
|
1970 |
*/
|
|
1971 |
protected class ButtonChangeListener implements ChangeListener, Serializable {
|
|
1972 |
// NOTE: This class is NOT used, instead the functionality has
|
|
1973 |
// been moved to Handler.
|
|
1974 |
ButtonChangeListener() {
|
|
1975 |
}
|
|
1976 |
|
|
1977 |
public void stateChanged(ChangeEvent e) {
|
|
1978 |
getHandler().stateChanged(e);
|
|
1979 |
}
|
|
1980 |
}
|
|
1981 |
|
|
1982 |
|
|
1983 |
/**
|
|
1984 |
* Notifies all listeners that have registered interest for
|
|
1985 |
* notification on this event type. The event instance
|
|
1986 |
* is lazily created using the <code>event</code>
|
|
1987 |
* parameter.
|
|
1988 |
*
|
|
1989 |
* @param event the <code>ActionEvent</code> object
|
|
1990 |
* @see EventListenerList
|
|
1991 |
*/
|
|
1992 |
protected void fireActionPerformed(ActionEvent event) {
|
|
1993 |
// Guaranteed to return a non-null array
|
|
1994 |
Object[] listeners = listenerList.getListenerList();
|
|
1995 |
ActionEvent e = null;
|
|
1996 |
// Process the listeners last to first, notifying
|
|
1997 |
// those that are interested in this event
|
|
1998 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
1999 |
if (listeners[i]==ActionListener.class) {
|
|
2000 |
// Lazily create the event:
|
|
2001 |
if (e == null) {
|
|
2002 |
String actionCommand = event.getActionCommand();
|
|
2003 |
if(actionCommand == null) {
|
|
2004 |
actionCommand = getActionCommand();
|
|
2005 |
}
|
|
2006 |
e = new ActionEvent(AbstractButton.this,
|
|
2007 |
ActionEvent.ACTION_PERFORMED,
|
|
2008 |
actionCommand,
|
|
2009 |
event.getWhen(),
|
|
2010 |
event.getModifiers());
|
|
2011 |
}
|
|
2012 |
((ActionListener)listeners[i+1]).actionPerformed(e);
|
|
2013 |
}
|
|
2014 |
}
|
|
2015 |
}
|
|
2016 |
|
|
2017 |
/**
|
|
2018 |
* Notifies all listeners that have registered interest for
|
|
2019 |
* notification on this event type. The event instance
|
|
2020 |
* is lazily created using the <code>event</code> parameter.
|
|
2021 |
*
|
|
2022 |
* @param event the <code>ItemEvent</code> object
|
|
2023 |
* @see EventListenerList
|
|
2024 |
*/
|
|
2025 |
protected void fireItemStateChanged(ItemEvent event) {
|
|
2026 |
// Guaranteed to return a non-null array
|
|
2027 |
Object[] listeners = listenerList.getListenerList();
|
|
2028 |
ItemEvent e = null;
|
|
2029 |
// Process the listeners last to first, notifying
|
|
2030 |
// those that are interested in this event
|
|
2031 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
2032 |
if (listeners[i]==ItemListener.class) {
|
|
2033 |
// Lazily create the event:
|
|
2034 |
if (e == null) {
|
|
2035 |
e = new ItemEvent(AbstractButton.this,
|
|
2036 |
ItemEvent.ITEM_STATE_CHANGED,
|
|
2037 |
AbstractButton.this,
|
|
2038 |
event.getStateChange());
|
|
2039 |
}
|
|
2040 |
((ItemListener)listeners[i+1]).itemStateChanged(e);
|
|
2041 |
}
|
|
2042 |
}
|
|
2043 |
if (accessibleContext != null) {
|
|
2044 |
if (event.getStateChange() == ItemEvent.SELECTED) {
|
|
2045 |
accessibleContext.firePropertyChange(
|
|
2046 |
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
|
|
2047 |
null, AccessibleState.SELECTED);
|
|
2048 |
accessibleContext.firePropertyChange(
|
|
2049 |
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
|
|
2050 |
new Integer(0), new Integer(1));
|
|
2051 |
} else {
|
|
2052 |
accessibleContext.firePropertyChange(
|
|
2053 |
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
|
|
2054 |
AccessibleState.SELECTED, null);
|
|
2055 |
accessibleContext.firePropertyChange(
|
|
2056 |
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
|
|
2057 |
new Integer(1), new Integer(0));
|
|
2058 |
}
|
|
2059 |
}
|
|
2060 |
}
|
|
2061 |
|
|
2062 |
|
|
2063 |
protected ActionListener createActionListener() {
|
|
2064 |
return getHandler();
|
|
2065 |
}
|
|
2066 |
|
|
2067 |
|
|
2068 |
protected ItemListener createItemListener() {
|
|
2069 |
return getHandler();
|
|
2070 |
}
|
|
2071 |
|
|
2072 |
|
|
2073 |
/**
|
|
2074 |
* Enables (or disables) the button.
|
|
2075 |
* @param b true to enable the button, otherwise false
|
|
2076 |
*/
|
|
2077 |
public void setEnabled(boolean b) {
|
|
2078 |
if (!b && model.isRollover()) {
|
|
2079 |
model.setRollover(false);
|
|
2080 |
}
|
|
2081 |
super.setEnabled(b);
|
|
2082 |
model.setEnabled(b);
|
|
2083 |
}
|
|
2084 |
|
|
2085 |
// *** Deprecated java.awt.Button APIs below *** //
|
|
2086 |
|
|
2087 |
/**
|
|
2088 |
* Returns the label text.
|
|
2089 |
*
|
|
2090 |
* @return a <code>String</code> containing the label
|
|
2091 |
* @deprecated - Replaced by <code>getText</code>
|
|
2092 |
*/
|
|
2093 |
@Deprecated
|
|
2094 |
public String getLabel() {
|
|
2095 |
return getText();
|
|
2096 |
}
|
|
2097 |
|
|
2098 |
/**
|
|
2099 |
* Sets the label text.
|
|
2100 |
*
|
|
2101 |
* @param label a <code>String</code> containing the text
|
|
2102 |
* @deprecated - Replaced by <code>setText(text)</code>
|
|
2103 |
* @beaninfo
|
|
2104 |
* bound: true
|
|
2105 |
* description: Replace by setText(text)
|
|
2106 |
*/
|
|
2107 |
@Deprecated
|
|
2108 |
public void setLabel(String label) {
|
|
2109 |
setText(label);
|
|
2110 |
}
|
|
2111 |
|
|
2112 |
/**
|
|
2113 |
* Adds an <code>ItemListener</code> to the <code>checkbox</code>.
|
|
2114 |
* @param l the <code>ItemListener</code> to be added
|
|
2115 |
*/
|
|
2116 |
public void addItemListener(ItemListener l) {
|
|
2117 |
listenerList.add(ItemListener.class, l);
|
|
2118 |
}
|
|
2119 |
|
|
2120 |
/**
|
|
2121 |
* Removes an <code>ItemListener</code> from the button.
|
|
2122 |
* @param l the <code>ItemListener</code> to be removed
|
|
2123 |
*/
|
|
2124 |
public void removeItemListener(ItemListener l) {
|
|
2125 |
listenerList.remove(ItemListener.class, l);
|
|
2126 |
}
|
|
2127 |
|
|
2128 |
/**
|
|
2129 |
* Returns an array of all the <code>ItemListener</code>s added
|
|
2130 |
* to this AbstractButton with addItemListener().
|
|
2131 |
*
|
|
2132 |
* @return all of the <code>ItemListener</code>s added or an empty
|
|
2133 |
* array if no listeners have been added
|
|
2134 |
* @since 1.4
|
|
2135 |
*/
|
|
2136 |
public ItemListener[] getItemListeners() {
|
|
2137 |
return (ItemListener[])listenerList.getListeners(ItemListener.class);
|
|
2138 |
}
|
|
2139 |
|
|
2140 |
/**
|
|
2141 |
* Returns an array (length 1) containing the label or
|
|
2142 |
* <code>null</code> if the button is not selected.
|
|
2143 |
*
|
|
2144 |
* @return an array containing 1 Object: the text of the button,
|
|
2145 |
* if the item is selected; otherwise <code>null</code>
|
|
2146 |
*/
|
|
2147 |
public Object[] getSelectedObjects() {
|
|
2148 |
if (isSelected() == false) {
|
|
2149 |
return null;
|
|
2150 |
}
|
|
2151 |
Object[] selectedObjects = new Object[1];
|
|
2152 |
selectedObjects[0] = getText();
|
|
2153 |
return selectedObjects;
|
|
2154 |
}
|
|
2155 |
|
|
2156 |
protected void init(String text, Icon icon) {
|
|
2157 |
if(text != null) {
|
|
2158 |
setText(text);
|
|
2159 |
}
|
|
2160 |
|
|
2161 |
if(icon != null) {
|
|
2162 |
setIcon(icon);
|
|
2163 |
}
|
|
2164 |
|
|
2165 |
// Set the UI
|
|
2166 |
updateUI();
|
|
2167 |
|
|
2168 |
setAlignmentX(LEFT_ALIGNMENT);
|
|
2169 |
setAlignmentY(CENTER_ALIGNMENT);
|
|
2170 |
}
|
|
2171 |
|
|
2172 |
|
|
2173 |
/**
|
|
2174 |
* This is overridden to return false if the current <code>Icon</code>'s
|
|
2175 |
* <code>Image</code> is not equal to the
|
|
2176 |
* passed in <code>Image</code> <code>img</code>.
|
|
2177 |
*
|
|
2178 |
* @param img the <code>Image</code> to be compared
|
|
2179 |
* @param infoflags flags used to repaint the button when the image
|
|
2180 |
* is updated and which determine how much is to be painted
|
|
2181 |
* @param x the x coordinate
|
|
2182 |
* @param y the y coordinate
|
|
2183 |
* @param w the width
|
|
2184 |
* @param h the height
|
|
2185 |
* @see java.awt.image.ImageObserver
|
|
2186 |
* @see java.awt.Component#imageUpdate(java.awt.Image, int, int, int, int, int)
|
|
2187 |
*/
|
|
2188 |
public boolean imageUpdate(Image img, int infoflags,
|
|
2189 |
int x, int y, int w, int h) {
|
|
2190 |
Icon iconDisplayed = getIcon();
|
|
2191 |
if (iconDisplayed == null) {
|
|
2192 |
return false;
|
|
2193 |
}
|
|
2194 |
|
|
2195 |
if (!model.isEnabled()) {
|
|
2196 |
if (model.isSelected()) {
|
|
2197 |
iconDisplayed = getDisabledSelectedIcon();
|
|
2198 |
} else {
|
|
2199 |
iconDisplayed = getDisabledIcon();
|
|
2200 |
}
|
|
2201 |
} else if (model.isPressed() && model.isArmed()) {
|
|
2202 |
iconDisplayed = getPressedIcon();
|
|
2203 |
} else if (isRolloverEnabled() && model.isRollover()) {
|
|
2204 |
if (model.isSelected()) {
|
|
2205 |
iconDisplayed = getRolloverSelectedIcon();
|
|
2206 |
} else {
|
|
2207 |
iconDisplayed = getRolloverIcon();
|
|
2208 |
}
|
|
2209 |
} else if (model.isSelected()) {
|
|
2210 |
iconDisplayed = getSelectedIcon();
|
|
2211 |
}
|
|
2212 |
|
|
2213 |
if (!SwingUtilities.doesIconReferenceImage(iconDisplayed, img)) {
|
|
2214 |
// We don't know about this image, disable the notification so
|
|
2215 |
// we don't keep repainting.
|
|
2216 |
return false;
|
|
2217 |
}
|
|
2218 |
return super.imageUpdate(img, infoflags, x, y, w, h);
|
|
2219 |
}
|
|
2220 |
|
|
2221 |
void setUIProperty(String propertyName, Object value) {
|
|
2222 |
if (propertyName == "borderPainted") {
|
|
2223 |
if (!borderPaintedSet) {
|
|
2224 |
setBorderPainted(((Boolean)value).booleanValue());
|
|
2225 |
borderPaintedSet = false;
|
|
2226 |
}
|
|
2227 |
} else if (propertyName == "rolloverEnabled") {
|
|
2228 |
if (!rolloverEnabledSet) {
|
|
2229 |
setRolloverEnabled(((Boolean)value).booleanValue());
|
|
2230 |
rolloverEnabledSet = false;
|
|
2231 |
}
|
|
2232 |
} else if (propertyName == "iconTextGap") {
|
|
2233 |
if (!iconTextGapSet) {
|
|
2234 |
setIconTextGap(((Number)value).intValue());
|
|
2235 |
iconTextGapSet = false;
|
|
2236 |
}
|
|
2237 |
} else if (propertyName == "contentAreaFilled") {
|
|
2238 |
if (!contentAreaFilledSet) {
|
|
2239 |
setContentAreaFilled(((Boolean)value).booleanValue());
|
|
2240 |
contentAreaFilledSet = false;
|
|
2241 |
}
|
|
2242 |
} else {
|
|
2243 |
super.setUIProperty(propertyName, value);
|
|
2244 |
}
|
|
2245 |
}
|
|
2246 |
|
|
2247 |
/**
|
|
2248 |
* Returns a string representation of this <code>AbstractButton</code>.
|
|
2249 |
* This method
|
|
2250 |
* is intended to be used only for debugging purposes, and the
|
|
2251 |
* content and format of the returned string may vary between
|
|
2252 |
* implementations. The returned string may be empty but may not
|
|
2253 |
* be <code>null</code>.
|
|
2254 |
* <P>
|
|
2255 |
* Overriding <code>paramString</code> to provide information about the
|
|
2256 |
* specific new aspects of the JFC components.
|
|
2257 |
*
|
|
2258 |
* @return a string representation of this <code>AbstractButton</code>
|
|
2259 |
*/
|
|
2260 |
protected String paramString() {
|
|
2261 |
String defaultIconString = ((defaultIcon != null)
|
|
2262 |
&& (defaultIcon != this) ?
|
|
2263 |
defaultIcon.toString() : "");
|
|
2264 |
String pressedIconString = ((pressedIcon != null)
|
|
2265 |
&& (pressedIcon != this) ?
|
|
2266 |
pressedIcon.toString() : "");
|
|
2267 |
String disabledIconString = ((disabledIcon != null)
|
|
2268 |
&& (disabledIcon != this) ?
|
|
2269 |
disabledIcon.toString() : "");
|
|
2270 |
String selectedIconString = ((selectedIcon != null)
|
|
2271 |
&& (selectedIcon != this) ?
|
|
2272 |
selectedIcon.toString() : "");
|
|
2273 |
String disabledSelectedIconString = ((disabledSelectedIcon != null) &&
|
|
2274 |
(disabledSelectedIcon != this) ?
|
|
2275 |
disabledSelectedIcon.toString()
|
|
2276 |
: "");
|
|
2277 |
String rolloverIconString = ((rolloverIcon != null)
|
|
2278 |
&& (rolloverIcon != this) ?
|
|
2279 |
rolloverIcon.toString() : "");
|
|
2280 |
String rolloverSelectedIconString = ((rolloverSelectedIcon != null) &&
|
|
2281 |
(rolloverSelectedIcon != this) ?
|
|
2282 |
rolloverSelectedIcon.toString()
|
|
2283 |
: "");
|
|
2284 |
String paintBorderString = (paintBorder ? "true" : "false");
|
|
2285 |
String paintFocusString = (paintFocus ? "true" : "false");
|
|
2286 |
String rolloverEnabledString = (rolloverEnabled ? "true" : "false");
|
|
2287 |
|
|
2288 |
return super.paramString() +
|
|
2289 |
",defaultIcon=" + defaultIconString +
|
|
2290 |
",disabledIcon=" + disabledIconString +
|
|
2291 |
",disabledSelectedIcon=" + disabledSelectedIconString +
|
|
2292 |
",margin=" + margin +
|
|
2293 |
",paintBorder=" + paintBorderString +
|
|
2294 |
",paintFocus=" + paintFocusString +
|
|
2295 |
",pressedIcon=" + pressedIconString +
|
|
2296 |
",rolloverEnabled=" + rolloverEnabledString +
|
|
2297 |
",rolloverIcon=" + rolloverIconString +
|
|
2298 |
",rolloverSelectedIcon=" + rolloverSelectedIconString +
|
|
2299 |
",selectedIcon=" + selectedIconString +
|
|
2300 |
",text=" + text;
|
|
2301 |
}
|
|
2302 |
|
|
2303 |
|
|
2304 |
private Handler getHandler() {
|
|
2305 |
if (handler == null) {
|
|
2306 |
handler = new Handler();
|
|
2307 |
}
|
|
2308 |
return handler;
|
|
2309 |
}
|
|
2310 |
|
|
2311 |
|
|
2312 |
//
|
|
2313 |
// Listeners that are added to model
|
|
2314 |
//
|
|
2315 |
class Handler implements ActionListener, ChangeListener, ItemListener,
|
|
2316 |
Serializable {
|
|
2317 |
//
|
|
2318 |
// ChangeListener
|
|
2319 |
//
|
|
2320 |
public void stateChanged(ChangeEvent e) {
|
|
2321 |
Object source = e.getSource();
|
|
2322 |
|
|
2323 |
updateMnemonicProperties();
|
|
2324 |
if (isEnabled() != model.isEnabled()) {
|
|
2325 |
setEnabled(model.isEnabled());
|
|
2326 |
}
|
|
2327 |
fireStateChanged();
|
|
2328 |
repaint();
|
|
2329 |
}
|
|
2330 |
|
|
2331 |
//
|
|
2332 |
// ActionListener
|
|
2333 |
//
|
|
2334 |
public void actionPerformed(ActionEvent event) {
|
|
2335 |
fireActionPerformed(event);
|
|
2336 |
}
|
|
2337 |
|
|
2338 |
//
|
|
2339 |
// ItemListener
|
|
2340 |
//
|
|
2341 |
public void itemStateChanged(ItemEvent event) {
|
|
2342 |
fireItemStateChanged(event);
|
|
2343 |
if (shouldUpdateSelectedStateFromAction()) {
|
|
2344 |
Action action = getAction();
|
|
2345 |
if (action != null && AbstractAction.hasSelectedKey(action)) {
|
|
2346 |
boolean selected = isSelected();
|
|
2347 |
boolean isActionSelected = AbstractAction.isSelected(
|
|
2348 |
action);
|
|
2349 |
if (isActionSelected != selected) {
|
|
2350 |
action.putValue(Action.SELECTED_KEY, selected);
|
|
2351 |
}
|
|
2352 |
}
|
|
2353 |
}
|
|
2354 |
}
|
|
2355 |
}
|
|
2356 |
|
|
2357 |
///////////////////
|
|
2358 |
// Accessibility support
|
|
2359 |
///////////////////
|
|
2360 |
/**
|
|
2361 |
* This class implements accessibility support for the
|
|
2362 |
* <code>AbstractButton</code> class. It provides an implementation of the
|
|
2363 |
* Java Accessibility API appropriate to button and menu item
|
|
2364 |
* user-interface elements.
|
|
2365 |
* <p>
|
|
2366 |
* <strong>Warning:</strong>
|
|
2367 |
* Serialized objects of this class will not be compatible with
|
|
2368 |
* future Swing releases. The current serialization support is
|
|
2369 |
* appropriate for short term storage or RMI between applications running
|
|
2370 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
2371 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
2372 |
* has been added to the <code>java.beans</code> package.
|
|
2373 |
* Please see {@link java.beans.XMLEncoder}.
|
|
2374 |
* @since 1.4
|
|
2375 |
*/
|
|
2376 |
protected abstract class AccessibleAbstractButton
|
|
2377 |
extends AccessibleJComponent implements AccessibleAction,
|
|
2378 |
AccessibleValue, AccessibleText, AccessibleExtendedComponent {
|
|
2379 |
|
|
2380 |
/**
|
|
2381 |
* Returns the accessible name of this object.
|
|
2382 |
*
|
|
2383 |
* @return the localized name of the object -- can be
|
|
2384 |
* <code>null</code> if this
|
|
2385 |
* object does not have a name
|
|
2386 |
*/
|
|
2387 |
public String getAccessibleName() {
|
|
2388 |
String name = accessibleName;
|
|
2389 |
|
|
2390 |
if (name == null) {
|
|
2391 |
name = (String)getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY);
|
|
2392 |
}
|
|
2393 |
if (name == null) {
|
|
2394 |
name = AbstractButton.this.getText();
|
|
2395 |
}
|
|
2396 |
if (name == null) {
|
|
2397 |
name = super.getAccessibleName();
|
|
2398 |
}
|
|
2399 |
return name;
|
|
2400 |
}
|
|
2401 |
|
|
2402 |
/**
|
|
2403 |
* Get the AccessibleIcons associated with this object if one
|
|
2404 |
* or more exist. Otherwise return null.
|
|
2405 |
* @since 1.3
|
|
2406 |
*/
|
|
2407 |
public AccessibleIcon [] getAccessibleIcon() {
|
|
2408 |
Icon defaultIcon = getIcon();
|
|
2409 |
|
|
2410 |
if (defaultIcon instanceof Accessible) {
|
|
2411 |
AccessibleContext ac =
|
|
2412 |
((Accessible)defaultIcon).getAccessibleContext();
|
|
2413 |
if (ac != null && ac instanceof AccessibleIcon) {
|
|
2414 |
return new AccessibleIcon[] { (AccessibleIcon)ac };
|
|
2415 |
}
|
|
2416 |
}
|
|
2417 |
return null;
|
|
2418 |
}
|
|
2419 |
|
|
2420 |
/**
|
|
2421 |
* Get the state set of this object.
|
|
2422 |
*
|
|
2423 |
* @return an instance of AccessibleState containing the current state
|
|
2424 |
* of the object
|
|
2425 |
* @see AccessibleState
|
|
2426 |
*/
|
|
2427 |
public AccessibleStateSet getAccessibleStateSet() {
|
|
2428 |
AccessibleStateSet states = super.getAccessibleStateSet();
|
|
2429 |
if (getModel().isArmed()) {
|
|
2430 |
states.add(AccessibleState.ARMED);
|
|
2431 |
}
|
|
2432 |
if (isFocusOwner()) {
|
|
2433 |
states.add(AccessibleState.FOCUSED);
|
|
2434 |
}
|
|
2435 |
if (getModel().isPressed()) {
|
|
2436 |
states.add(AccessibleState.PRESSED);
|
|
2437 |
}
|
|
2438 |
if (isSelected()) {
|
|
2439 |
states.add(AccessibleState.CHECKED);
|
|
2440 |
}
|
|
2441 |
return states;
|
|
2442 |
}
|
|
2443 |
|
|
2444 |
/**
|
|
2445 |
* Get the AccessibleRelationSet associated with this object if one
|
|
2446 |
* exists. Otherwise return null.
|
|
2447 |
* @see AccessibleRelation
|
|
2448 |
* @since 1.3
|
|
2449 |
*/
|
|
2450 |
public AccessibleRelationSet getAccessibleRelationSet() {
|
|
2451 |
|
|
2452 |
// Check where the AccessibleContext's relation
|
|
2453 |
// set already contains a MEMBER_OF relation.
|
|
2454 |
AccessibleRelationSet relationSet
|
|
2455 |
= super.getAccessibleRelationSet();
|
|
2456 |
|
|
2457 |
if (!relationSet.contains(AccessibleRelation.MEMBER_OF)) {
|
|
2458 |
// get the members of the button group if one exists
|
|
2459 |
ButtonModel model = getModel();
|
|
2460 |
if (model != null && model instanceof DefaultButtonModel) {
|
|
2461 |
ButtonGroup group = ((DefaultButtonModel)model).getGroup();
|
|
2462 |
if (group != null) {
|
|
2463 |
// set the target of the MEMBER_OF relation to be
|
|
2464 |
// the members of the button group.
|
|
2465 |
int len = group.getButtonCount();
|
|
2466 |
Object [] target = new Object[len];
|
|
2467 |
Enumeration elem = group.getElements();
|
|
2468 |
for (int i = 0; i < len; i++) {
|
|
2469 |
if (elem.hasMoreElements()) {
|
|
2470 |
target[i] = elem.nextElement();
|
|
2471 |
}
|
|
2472 |
}
|
|
2473 |
AccessibleRelation relation =
|
|
2474 |
new AccessibleRelation(AccessibleRelation.MEMBER_OF);
|
|
2475 |
relation.setTarget(target);
|
|
2476 |
relationSet.add(relation);
|
|
2477 |
}
|
|
2478 |
}
|
|
2479 |
}
|
|
2480 |
return relationSet;
|
|
2481 |
}
|
|
2482 |
|
|
2483 |
/**
|
|
2484 |
* Get the AccessibleAction associated with this object. In the
|
|
2485 |
* implementation of the Java Accessibility API for this class,
|
|
2486 |
* return this object, which is responsible for implementing the
|
|
2487 |
* AccessibleAction interface on behalf of itself.
|
|
2488 |
*
|
|
2489 |
* @return this object
|
|
2490 |
*/
|
|
2491 |
public AccessibleAction getAccessibleAction() {
|
|
2492 |
return this;
|
|
2493 |
}
|
|
2494 |
|
|
2495 |
/**
|
|
2496 |
* Get the AccessibleValue associated with this object. In the
|
|
2497 |
* implementation of the Java Accessibility API for this class,
|
|
2498 |
* return this object, which is responsible for implementing the
|
|
2499 |
* AccessibleValue interface on behalf of itself.
|
|
2500 |
*
|
|
2501 |
* @return this object
|
|
2502 |
*/
|
|
2503 |
public AccessibleValue getAccessibleValue() {
|
|
2504 |
return this;
|
|
2505 |
}
|
|
2506 |
|
|
2507 |
/**
|
|
2508 |
* Returns the number of Actions available in this object. The
|
|
2509 |
* default behavior of a button is to have one action - toggle
|
|
2510 |
* the button.
|
|
2511 |
*
|
|
2512 |
* @return 1, the number of Actions in this object
|
|
2513 |
*/
|
|
2514 |
public int getAccessibleActionCount() {
|
|
2515 |
return 1;
|
|
2516 |
}
|
|
2517 |
|
|
2518 |
/**
|
|
2519 |
* Return a description of the specified action of the object.
|
|
2520 |
*
|
|
2521 |
* @param i zero-based index of the actions
|
|
2522 |
*/
|
|
2523 |
public String getAccessibleActionDescription(int i) {
|
|
2524 |
if (i == 0) {
|
|
2525 |
return UIManager.getString("AbstractButton.clickText");
|
|
2526 |
} else {
|
|
2527 |
return null;
|
|
2528 |
}
|
|
2529 |
}
|
|
2530 |
|
|
2531 |
/**
|
|
2532 |
* Perform the specified Action on the object
|
|
2533 |
*
|
|
2534 |
* @param i zero-based index of actions
|
|
2535 |
* @return true if the the action was performed; else false.
|
|
2536 |
*/
|
|
2537 |
public boolean doAccessibleAction(int i) {
|
|
2538 |
if (i == 0) {
|
|
2539 |
doClick();
|
|
2540 |
return true;
|
|
2541 |
} else {
|
|
2542 |
return false;
|
|
2543 |
}
|
|
2544 |
}
|
|
2545 |
|
|
2546 |
/**
|
|
2547 |
* Get the value of this object as a Number.
|
|
2548 |
*
|
|
2549 |
* @return An Integer of 0 if this isn't selected or an Integer of 1 if
|
|
2550 |
* this is selected.
|
|
2551 |
* @see AbstractButton#isSelected
|
|
2552 |
*/
|
|
2553 |
public Number getCurrentAccessibleValue() {
|
|
2554 |
if (isSelected()) {
|
|
2555 |
return new Integer(1);
|
|
2556 |
} else {
|
|
2557 |
return new Integer(0);
|
|
2558 |
}
|
|
2559 |
}
|
|
2560 |
|
|
2561 |
/**
|
|
2562 |
* Set the value of this object as a Number.
|
|
2563 |
*
|
|
2564 |
* @return True if the value was set.
|
|
2565 |
*/
|
|
2566 |
public boolean setCurrentAccessibleValue(Number n) {
|
|
2567 |
// TIGER - 4422535
|
|
2568 |
if (n == null) {
|
|
2569 |
return false;
|
|
2570 |
}
|
|
2571 |
int i = n.intValue();
|
|
2572 |
if (i == 0) {
|
|
2573 |
setSelected(false);
|
|
2574 |
} else {
|
|
2575 |
setSelected(true);
|
|
2576 |
}
|
|
2577 |
return true;
|
|
2578 |
}
|
|
2579 |
|
|
2580 |
/**
|
|
2581 |
* Get the minimum value of this object as a Number.
|
|
2582 |
*
|
|
2583 |
* @return an Integer of 0.
|
|
2584 |
*/
|
|
2585 |
public Number getMinimumAccessibleValue() {
|
|
2586 |
return new Integer(0);
|
|
2587 |
}
|
|
2588 |
|
|
2589 |
/**
|
|
2590 |
* Get the maximum value of this object as a Number.
|
|
2591 |
*
|
|
2592 |
* @return An Integer of 1.
|
|
2593 |
*/
|
|
2594 |
public Number getMaximumAccessibleValue() {
|
|
2595 |
return new Integer(1);
|
|
2596 |
}
|
|
2597 |
|
|
2598 |
|
|
2599 |
/* AccessibleText ---------- */
|
|
2600 |
|
|
2601 |
public AccessibleText getAccessibleText() {
|
|
2602 |
View view = (View)AbstractButton.this.getClientProperty("html");
|
|
2603 |
if (view != null) {
|
|
2604 |
return this;
|
|
2605 |
} else {
|
|
2606 |
return null;
|
|
2607 |
}
|
|
2608 |
}
|
|
2609 |
|
|
2610 |
/**
|
|
2611 |
* Given a point in local coordinates, return the zero-based index
|
|
2612 |
* of the character under that Point. If the point is invalid,
|
|
2613 |
* this method returns -1.
|
|
2614 |
*
|
|
2615 |
* Note: the AbstractButton must have a valid size (e.g. have
|
|
2616 |
* been added to a parent container whose ancestor container
|
|
2617 |
* is a valid top-level window) for this method to be able
|
|
2618 |
* to return a meaningful value.
|
|
2619 |
*
|
|
2620 |
* @param p the Point in local coordinates
|
|
2621 |
* @return the zero-based index of the character under Point p; if
|
|
2622 |
* Point is invalid returns -1.
|
|
2623 |
* @since 1.3
|
|
2624 |
*/
|
|
2625 |
public int getIndexAtPoint(Point p) {
|
|
2626 |
View view = (View) AbstractButton.this.getClientProperty("html");
|
|
2627 |
if (view != null) {
|
|
2628 |
Rectangle r = getTextRectangle();
|
|
2629 |
if (r == null) {
|
|
2630 |
return -1;
|
|
2631 |
}
|
|
2632 |
Rectangle2D.Float shape =
|
|
2633 |
new Rectangle2D.Float(r.x, r.y, r.width, r.height);
|
|
2634 |
Position.Bias bias[] = new Position.Bias[1];
|
|
2635 |
return view.viewToModel(p.x, p.y, shape, bias);
|
|
2636 |
} else {
|
|
2637 |
return -1;
|
|
2638 |
}
|
|
2639 |
}
|
|
2640 |
|
|
2641 |
/**
|
|
2642 |
* Determine the bounding box of the character at the given
|
|
2643 |
* index into the string. The bounds are returned in local
|
|
2644 |
* coordinates. If the index is invalid an empty rectangle is
|
|
2645 |
* returned.
|
|
2646 |
*
|
|
2647 |
* Note: the AbstractButton must have a valid size (e.g. have
|
|
2648 |
* been added to a parent container whose ancestor container
|
|
2649 |
* is a valid top-level window) for this method to be able
|
|
2650 |
* to return a meaningful value.
|
|
2651 |
*
|
|
2652 |
* @param i the index into the String
|
|
2653 |
* @return the screen coordinates of the character's the bounding box,
|
|
2654 |
* if index is invalid returns an empty rectangle.
|
|
2655 |
* @since 1.3
|
|
2656 |
*/
|
|
2657 |
public Rectangle getCharacterBounds(int i) {
|
|
2658 |
View view = (View) AbstractButton.this.getClientProperty("html");
|
|
2659 |
if (view != null) {
|
|
2660 |
Rectangle r = getTextRectangle();
|
|
2661 |
if (r == null) {
|
|
2662 |
return null;
|
|
2663 |
}
|
|
2664 |
Rectangle2D.Float shape =
|
|
2665 |
new Rectangle2D.Float(r.x, r.y, r.width, r.height);
|
|
2666 |
try {
|
|
2667 |
Shape charShape =
|
|
2668 |
view.modelToView(i, shape, Position.Bias.Forward);
|
|
2669 |
return charShape.getBounds();
|
|
2670 |
} catch (BadLocationException e) {
|
|
2671 |
return null;
|
|
2672 |
}
|
|
2673 |
} else {
|
|
2674 |
return null;
|
|
2675 |
}
|
|
2676 |
}
|
|
2677 |
|
|
2678 |
/**
|
|
2679 |
* Return the number of characters (valid indicies)
|
|
2680 |
*
|
|
2681 |
* @return the number of characters
|
|
2682 |
* @since 1.3
|
|
2683 |
*/
|
|
2684 |
public int getCharCount() {
|
|
2685 |
View view = (View) AbstractButton.this.getClientProperty("html");
|
|
2686 |
if (view != null) {
|
|
2687 |
Document d = view.getDocument();
|
|
2688 |
if (d instanceof StyledDocument) {
|
|
2689 |
StyledDocument doc = (StyledDocument)d;
|
|
2690 |
return doc.getLength();
|
|
2691 |
}
|
|
2692 |
}
|
|
2693 |
return accessibleContext.getAccessibleName().length();
|
|
2694 |
}
|
|
2695 |
|
|
2696 |
/**
|
|
2697 |
* Return the zero-based offset of the caret.
|
|
2698 |
*
|
|
2699 |
* Note: That to the right of the caret will have the same index
|
|
2700 |
* value as the offset (the caret is between two characters).
|
|
2701 |
* @return the zero-based offset of the caret.
|
|
2702 |
* @since 1.3
|
|
2703 |
*/
|
|
2704 |
public int getCaretPosition() {
|
|
2705 |
// There is no caret.
|
|
2706 |
return -1;
|
|
2707 |
}
|
|
2708 |
|
|
2709 |
/**
|
|
2710 |
* Returns the String at a given index.
|
|
2711 |
*
|
|
2712 |
* @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
|
|
2713 |
* or AccessibleText.SENTENCE to retrieve
|
|
2714 |
* @param index an index within the text >= 0
|
|
2715 |
* @return the letter, word, or sentence,
|
|
2716 |
* null for an invalid index or part
|
|
2717 |
* @since 1.3
|
|
2718 |
*/
|
|
2719 |
public String getAtIndex(int part, int index) {
|
|
2720 |
if (index < 0 || index >= getCharCount()) {
|
|
2721 |
return null;
|
|
2722 |
}
|
|
2723 |
switch (part) {
|
|
2724 |
case AccessibleText.CHARACTER:
|
|
2725 |
try {
|
|
2726 |
return getText(index, 1);
|
|
2727 |
} catch (BadLocationException e) {
|
|
2728 |
return null;
|
|
2729 |
}
|
|
2730 |
case AccessibleText.WORD:
|
|
2731 |
try {
|
|
2732 |
String s = getText(0, getCharCount());
|
|
2733 |
BreakIterator words = BreakIterator.getWordInstance(getLocale());
|
|
2734 |
words.setText(s);
|
|
2735 |
int end = words.following(index);
|
|
2736 |
return s.substring(words.previous(), end);
|
|
2737 |
} catch (BadLocationException e) {
|
|
2738 |
return null;
|
|
2739 |
}
|
|
2740 |
case AccessibleText.SENTENCE:
|
|
2741 |
try {
|
|
2742 |
String s = getText(0, getCharCount());
|
|
2743 |
BreakIterator sentence =
|
|
2744 |
BreakIterator.getSentenceInstance(getLocale());
|
|
2745 |
sentence.setText(s);
|
|
2746 |
int end = sentence.following(index);
|
|
2747 |
return s.substring(sentence.previous(), end);
|
|
2748 |
} catch (BadLocationException e) {
|
|
2749 |
return null;
|
|
2750 |
}
|
|
2751 |
default:
|
|
2752 |
return null;
|
|
2753 |
}
|
|
2754 |
}
|
|
2755 |
|
|
2756 |
/**
|
|
2757 |
* Returns the String after a given index.
|
|
2758 |
*
|
|
2759 |
* @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
|
|
2760 |
* or AccessibleText.SENTENCE to retrieve
|
|
2761 |
* @param index an index within the text >= 0
|
|
2762 |
* @return the letter, word, or sentence, null for an invalid
|
|
2763 |
* index or part
|
|
2764 |
* @since 1.3
|
|
2765 |
*/
|
|
2766 |
public String getAfterIndex(int part, int index) {
|
|
2767 |
if (index < 0 || index >= getCharCount()) {
|
|
2768 |
return null;
|
|
2769 |
}
|
|
2770 |
switch (part) {
|
|
2771 |
case AccessibleText.CHARACTER:
|
|
2772 |
if (index+1 >= getCharCount()) {
|
|
2773 |
return null;
|
|
2774 |
}
|
|
2775 |
try {
|
|
2776 |
return getText(index+1, 1);
|
|
2777 |
} catch (BadLocationException e) {
|
|
2778 |
return null;
|
|
2779 |
}
|
|
2780 |
case AccessibleText.WORD:
|
|
2781 |
try {
|
|
2782 |
String s = getText(0, getCharCount());
|
|
2783 |
BreakIterator words = BreakIterator.getWordInstance(getLocale());
|
|
2784 |
words.setText(s);
|
|
2785 |
int start = words.following(index);
|
|
2786 |
if (start == BreakIterator.DONE || start >= s.length()) {
|
|
2787 |
return null;
|
|
2788 |
}
|
|
2789 |
int end = words.following(start);
|
|
2790 |
if (end == BreakIterator.DONE || end >= s.length()) {
|
|
2791 |
return null;
|
|
2792 |
}
|
|
2793 |
return s.substring(start, end);
|
|
2794 |
} catch (BadLocationException e) {
|
|
2795 |
return null;
|
|
2796 |
}
|
|
2797 |
case AccessibleText.SENTENCE:
|
|
2798 |
try {
|
|
2799 |
String s = getText(0, getCharCount());
|
|
2800 |
BreakIterator sentence =
|
|
2801 |
BreakIterator.getSentenceInstance(getLocale());
|
|
2802 |
sentence.setText(s);
|
|
2803 |
int start = sentence.following(index);
|
|
2804 |
if (start == BreakIterator.DONE || start > s.length()) {
|
|
2805 |
return null;
|
|
2806 |
}
|
|
2807 |
int end = sentence.following(start);
|
|
2808 |
if (end == BreakIterator.DONE || end > s.length()) {
|
|
2809 |
return null;
|
|
2810 |
}
|
|
2811 |
return s.substring(start, end);
|
|
2812 |
} catch (BadLocationException e) {
|
|
2813 |
return null;
|
|
2814 |
}
|
|
2815 |
default:
|
|
2816 |
return null;
|
|
2817 |
}
|
|
2818 |
}
|
|
2819 |
|
|
2820 |
/**
|
|
2821 |
* Returns the String before a given index.
|
|
2822 |
*
|
|
2823 |
* @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
|
|
2824 |
* or AccessibleText.SENTENCE to retrieve
|
|
2825 |
* @param index an index within the text >= 0
|
|
2826 |
* @return the letter, word, or sentence, null for an invalid index
|
|
2827 |
* or part
|
|
2828 |
* @since 1.3
|
|
2829 |
*/
|
|
2830 |
public String getBeforeIndex(int part, int index) {
|
|
2831 |
if (index < 0 || index > getCharCount()-1) {
|
|
2832 |
return null;
|
|
2833 |
}
|
|
2834 |
switch (part) {
|
|
2835 |
case AccessibleText.CHARACTER:
|
|
2836 |
if (index == 0) {
|
|
2837 |
return null;
|
|
2838 |
}
|
|
2839 |
try {
|
|
2840 |
return getText(index-1, 1);
|
|
2841 |
} catch (BadLocationException e) {
|
|
2842 |
return null;
|
|
2843 |
}
|
|
2844 |
case AccessibleText.WORD:
|
|
2845 |
try {
|
|
2846 |
String s = getText(0, getCharCount());
|
|
2847 |
BreakIterator words = BreakIterator.getWordInstance(getLocale());
|
|
2848 |
words.setText(s);
|
|
2849 |
int end = words.following(index);
|
|
2850 |
end = words.previous();
|
|
2851 |
int start = words.previous();
|
|
2852 |
if (start == BreakIterator.DONE) {
|
|
2853 |
return null;
|
|
2854 |
}
|
|
2855 |
return s.substring(start, end);
|
|
2856 |
} catch (BadLocationException e) {
|
|
2857 |
return null;
|
|
2858 |
}
|
|
2859 |
case AccessibleText.SENTENCE:
|
|
2860 |
try {
|
|
2861 |
String s = getText(0, getCharCount());
|
|
2862 |
BreakIterator sentence =
|
|
2863 |
BreakIterator.getSentenceInstance(getLocale());
|
|
2864 |
sentence.setText(s);
|
|
2865 |
int end = sentence.following(index);
|
|
2866 |
end = sentence.previous();
|
|
2867 |
int start = sentence.previous();
|
|
2868 |
if (start == BreakIterator.DONE) {
|
|
2869 |
return null;
|
|
2870 |
}
|
|
2871 |
return s.substring(start, end);
|
|
2872 |
} catch (BadLocationException e) {
|
|
2873 |
return null;
|
|
2874 |
}
|
|
2875 |
default:
|
|
2876 |
return null;
|
|
2877 |
}
|
|
2878 |
}
|
|
2879 |
|
|
2880 |
/**
|
|
2881 |
* Return the AttributeSet for a given character at a given index
|
|
2882 |
*
|
|
2883 |
* @param i the zero-based index into the text
|
|
2884 |
* @return the AttributeSet of the character
|
|
2885 |
* @since 1.3
|
|
2886 |
*/
|
|
2887 |
public AttributeSet getCharacterAttribute(int i) {
|
|
2888 |
View view = (View) AbstractButton.this.getClientProperty("html");
|
|
2889 |
if (view != null) {
|
|
2890 |
Document d = view.getDocument();
|
|
2891 |
if (d instanceof StyledDocument) {
|
|
2892 |
StyledDocument doc = (StyledDocument)d;
|
|
2893 |
Element elem = doc.getCharacterElement(i);
|
|
2894 |
if (elem != null) {
|
|
2895 |
return elem.getAttributes();
|
|
2896 |
}
|
|
2897 |
}
|
|
2898 |
}
|
|
2899 |
return null;
|
|
2900 |
}
|
|
2901 |
|
|
2902 |
/**
|
|
2903 |
* Returns the start offset within the selected text.
|
|
2904 |
* If there is no selection, but there is
|
|
2905 |
* a caret, the start and end offsets will be the same.
|
|
2906 |
*
|
|
2907 |
* @return the index into the text of the start of the selection
|
|
2908 |
* @since 1.3
|
|
2909 |
*/
|
|
2910 |
public int getSelectionStart() {
|
|
2911 |
// Text cannot be selected.
|
|
2912 |
return -1;
|
|
2913 |
}
|
|
2914 |
|
|
2915 |
/**
|
|
2916 |
* Returns the end offset within the selected text.
|
|
2917 |
* If there is no selection, but there is
|
|
2918 |
* a caret, the start and end offsets will be the same.
|
|
2919 |
*
|
|
2920 |
* @return the index into teh text of the end of the selection
|
|
2921 |
* @since 1.3
|
|
2922 |
*/
|
|
2923 |
public int getSelectionEnd() {
|
|
2924 |
// Text cannot be selected.
|
|
2925 |
return -1;
|
|
2926 |
}
|
|
2927 |
|
|
2928 |
/**
|
|
2929 |
* Returns the portion of the text that is selected.
|
|
2930 |
*
|
|
2931 |
* @return the String portion of the text that is selected
|
|
2932 |
* @since 1.3
|
|
2933 |
*/
|
|
2934 |
public String getSelectedText() {
|
|
2935 |
// Text cannot be selected.
|
|
2936 |
return null;
|
|
2937 |
}
|
|
2938 |
|
|
2939 |
/*
|
|
2940 |
* Returns the text substring starting at the specified
|
|
2941 |
* offset with the specified length.
|
|
2942 |
*/
|
|
2943 |
private String getText(int offset, int length)
|
|
2944 |
throws BadLocationException {
|
|
2945 |
|
|
2946 |
View view = (View) AbstractButton.this.getClientProperty("html");
|
|
2947 |
if (view != null) {
|
|
2948 |
Document d = view.getDocument();
|
|
2949 |
if (d instanceof StyledDocument) {
|
|
2950 |
StyledDocument doc = (StyledDocument)d;
|
|
2951 |
return doc.getText(offset, length);
|
|
2952 |
}
|
|
2953 |
}
|
|
2954 |
return null;
|
|
2955 |
}
|
|
2956 |
|
|
2957 |
/*
|
|
2958 |
* Returns the bounding rectangle for the component text.
|
|
2959 |
*/
|
|
2960 |
private Rectangle getTextRectangle() {
|
|
2961 |
|
|
2962 |
String text = AbstractButton.this.getText();
|
|
2963 |
Icon icon = (AbstractButton.this.isEnabled()) ? AbstractButton.this.getIcon() : AbstractButton.this.getDisabledIcon();
|
|
2964 |
|
|
2965 |
if ((icon == null) && (text == null)) {
|
|
2966 |
return null;
|
|
2967 |
}
|
|
2968 |
|
|
2969 |
Rectangle paintIconR = new Rectangle();
|
|
2970 |
Rectangle paintTextR = new Rectangle();
|
|
2971 |
Rectangle paintViewR = new Rectangle();
|
|
2972 |
Insets paintViewInsets = new Insets(0, 0, 0, 0);
|
|
2973 |
|
|
2974 |
paintViewInsets = AbstractButton.this.getInsets(paintViewInsets);
|
|
2975 |
paintViewR.x = paintViewInsets.left;
|
|
2976 |
paintViewR.y = paintViewInsets.top;
|
|
2977 |
paintViewR.width = AbstractButton.this.getWidth() - (paintViewInsets.left + paintViewInsets.right);
|
|
2978 |
paintViewR.height = AbstractButton.this.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);
|
|
2979 |
|
|
2980 |
String clippedText = SwingUtilities.layoutCompoundLabel(
|
|
2981 |
(JComponent)AbstractButton.this,
|
|
2982 |
getFontMetrics(getFont()),
|
|
2983 |
text,
|
|
2984 |
icon,
|
|
2985 |
AbstractButton.this.getVerticalAlignment(),
|
|
2986 |
AbstractButton.this.getHorizontalAlignment(),
|
|
2987 |
AbstractButton.this.getVerticalTextPosition(),
|
|
2988 |
AbstractButton.this.getHorizontalTextPosition(),
|
|
2989 |
paintViewR,
|
|
2990 |
paintIconR,
|
|
2991 |
paintTextR,
|
|
2992 |
0);
|
|
2993 |
|
|
2994 |
return paintTextR;
|
|
2995 |
}
|
|
2996 |
|
|
2997 |
// ----- AccessibleExtendedComponent
|
|
2998 |
|
|
2999 |
/**
|
|
3000 |
* Returns the AccessibleExtendedComponent
|
|
3001 |
*
|
|
3002 |
* @return the AccessibleExtendedComponent
|
|
3003 |
*/
|
|
3004 |
AccessibleExtendedComponent getAccessibleExtendedComponent() {
|
|
3005 |
return this;
|
|
3006 |
}
|
|
3007 |
|
|
3008 |
/**
|
|
3009 |
* Returns the tool tip text
|
|
3010 |
*
|
|
3011 |
* @return the tool tip text, if supported, of the object;
|
|
3012 |
* otherwise, null
|
|
3013 |
* @since 1.4
|
|
3014 |
*/
|
|
3015 |
public String getToolTipText() {
|
|
3016 |
return AbstractButton.this.getToolTipText();
|
|
3017 |
}
|
|
3018 |
|
|
3019 |
/**
|
|
3020 |
* Returns the titled border text
|
|
3021 |
*
|
|
3022 |
* @return the titled border text, if supported, of the object;
|
|
3023 |
* otherwise, null
|
|
3024 |
* @since 1.4
|
|
3025 |
*/
|
|
3026 |
public String getTitledBorderText() {
|
|
3027 |
return super.getTitledBorderText();
|
|
3028 |
}
|
|
3029 |
|
|
3030 |
/**
|
|
3031 |
* Returns key bindings associated with this object
|
|
3032 |
*
|
|
3033 |
* @return the key bindings, if supported, of the object;
|
|
3034 |
* otherwise, null
|
|
3035 |
* @see AccessibleKeyBinding
|
|
3036 |
* @since 1.4
|
|
3037 |
*/
|
|
3038 |
public AccessibleKeyBinding getAccessibleKeyBinding() {
|
|
3039 |
int mnemonic = AbstractButton.this.getMnemonic();
|
|
3040 |
if (mnemonic == 0) {
|
|
3041 |
return null;
|
|
3042 |
}
|
|
3043 |
return new ButtonKeyBinding(mnemonic);
|
|
3044 |
}
|
|
3045 |
|
|
3046 |
class ButtonKeyBinding implements AccessibleKeyBinding {
|
|
3047 |
int mnemonic;
|
|
3048 |
|
|
3049 |
ButtonKeyBinding(int mnemonic) {
|
|
3050 |
this.mnemonic = mnemonic;
|
|
3051 |
}
|
|
3052 |
|
|
3053 |
/**
|
|
3054 |
* Returns the number of key bindings for this object
|
|
3055 |
*
|
|
3056 |
* @return the zero-based number of key bindings for this object
|
|
3057 |
*/
|
|
3058 |
public int getAccessibleKeyBindingCount() {
|
|
3059 |
return 1;
|
|
3060 |
}
|
|
3061 |
|
|
3062 |
/**
|
|
3063 |
* Returns a key binding for this object. The value returned is an
|
|
3064 |
* java.lang.Object which must be cast to appropriate type depending
|
|
3065 |
* on the underlying implementation of the key. For example, if the
|
|
3066 |
* Object returned is a javax.swing.KeyStroke, the user of this
|
|
3067 |
* method should do the following:
|
|
3068 |
* <nf><code>
|
|
3069 |
* Component c = <get the component that has the key bindings>
|
|
3070 |
* AccessibleContext ac = c.getAccessibleContext();
|
|
3071 |
* AccessibleKeyBinding akb = ac.getAccessibleKeyBinding();
|
|
3072 |
* for (int i = 0; i < akb.getAccessibleKeyBindingCount(); i++) {
|
|
3073 |
* Object o = akb.getAccessibleKeyBinding(i);
|
|
3074 |
* if (o instanceof javax.swing.KeyStroke) {
|
|
3075 |
* javax.swing.KeyStroke keyStroke = (javax.swing.KeyStroke)o;
|
|
3076 |
* <do something with the key binding>
|
|
3077 |
* }
|
|
3078 |
* }
|
|
3079 |
* </code></nf>
|
|
3080 |
*
|
|
3081 |
* @param i zero-based index of the key bindings
|
|
3082 |
* @return a javax.lang.Object which specifies the key binding
|
|
3083 |
* @exception IllegalArgumentException if the index is
|
|
3084 |
* out of bounds
|
|
3085 |
* @see #getAccessibleKeyBindingCount
|
|
3086 |
*/
|
|
3087 |
public java.lang.Object getAccessibleKeyBinding(int i) {
|
|
3088 |
if (i != 0) {
|
|
3089 |
throw new IllegalArgumentException();
|
|
3090 |
}
|
|
3091 |
return KeyStroke.getKeyStroke(mnemonic, 0);
|
|
3092 |
}
|
|
3093 |
}
|
|
3094 |
}
|
|
3095 |
}
|