home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
jfc.bin
/
AbstractButton.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
33KB
|
1,164 lines
/*
* @(#)AbstractButton.java 1.73 98/02/03
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.Serializable;
import com.sun.java.swing.event.*;
import com.sun.java.swing.border.*;
import com.sun.java.swing.plaf.*;
import com.sun.java.accessibility.*;
/**
* Defines the common behaviors for the JButton, JToggleButton, JCheckbox,
* and the JRadioButton classes.
* <p>
* Warning: serialized objects of this class will not be compatible with
* future swing releases. The current serialization support is appropriate
* for short term storage or RMI between Swing1.0 applications. It will
* not be possible to load serialized Swing1.0 objects with future releases
* of Swing. The JDK1.2 release of Swing will be the compatibility
* baseline for the serialized form of Swing objects.
*
* @version 1.73 02/03/98
* @author Jeff Dinkins
*/
public abstract class AbstractButton extends JComponent implements ItemSelectable, SwingConstants {
protected ButtonModel model = null;
private String text = ""; // for BeanBox
private Insets margin = null;
// Button icons
// PENDING(jeff) - hold icons in an array
private Icon defaultIcon = null;
private Icon pressedIcon = null;
private Icon disabledIcon = null;
private Icon selectedIcon = null;
private Icon disabledSelectedIcon = null;
private Icon rolloverIcon = null;
private Icon rolloverSelectedIcon = null;
// Display properties
private boolean paintBorder = true;
private boolean paintFocus = true;
private boolean rolloverEnabled = false;
// Icon/Label Alignment
private int verticalAlignment = CENTER;
private int horizontalAlignment = CENTER;
private int verticalTextPosition = CENTER;
private int horizontalTextPosition = RIGHT;
/**
* The button's model listeners.
*/
protected ChangeListener changeListener = null;
protected ActionListener actionListener = null;
protected ItemListener itemListener = null;
/**
* Only one ChangeEvent is needed per button instance since the
* event's only state is the source property. The source of events
* generated is always "this".
*/
protected transient ChangeEvent changeEvent;
/**
* Returns the button's text.
* @see setText
*/
public String getText() {
return text;
}
/**
* Sets the button's text.
* @param t the string used to set the text
* @see getText
* @beaninfo
* bound: true
* description: The button's text.
*/
public void setText(String text) {
String oldValue = text;
this.text = text;
firePropertyChange("text", oldValue, text);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, text);
}
invalidate();
repaint();
}
/**
* Returns the state of the button. True if the
* toggle button is selected, false if it's not.
*/
public boolean isSelected() {
return model.isSelected();
}
/**
* Sets the state of the button.
*/
public void setSelected(boolean b) {
boolean oldValue = isSelected();
if (accessibleContext != null && oldValue != b) {
if (b) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
null, AccessibleState.SELECTED);
} else {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
AccessibleState.SELECTED, null);
}
}
model.setSelected(b);
}
/**
* Programatically perform a "click". This does the same
* thing as if the user had pressed and released the button.
*/
public void doClick() {
doClick(68);
}
/**
* Programatically perform a "click". This does the same
* thing as if the user had pressed and released the button.
* The button stays visually "pressed" for pressTime milliseconds.
*/
public void doClick(int pressTime) {
Dimension size = getSize();
model.setArmed(true);
model.setPressed(true);
paintImmediately(new Rectangle(0,0, size.width, size.height));
try {
Thread.currentThread().sleep(pressTime);
} catch(InterruptedException ie) {
}
model.setPressed(false);
model.setArmed(false);
}
/**
* Sets space for margin between the button's border and
* the label. Setting to null will cause the button to
* use the default margin. The button's default Border
* object will use this value to create the proper margin.
* However, if a non-default border is set on the button,
* it is that Border object's responsibility to create the
* appropriate margin space (else this property will
* effectively be ignored).
*
* @param m the space between the border and the label
*
* @beaninfo
* bound: true
* description: The space between the button's border and the label.
*/
public void setMargin(Insets m) {
Insets old = margin;
margin = m;
firePropertyChange("margin", old, m);
invalidate();
}
/**
* Returns the margin between the button's border and
* the label.
*/
public Insets getMargin() {
if(margin == null) {
return ((ButtonUI)ui).getDefaultMargin(this);
} else {
return margin;
}
}
/**
* Returns the default icon.
* @see setIcon
*/
public Icon getIcon() {
return defaultIcon;
}
/**
* Sets the button's default icon. This icon is
* also used as the "pressed" and "disabled" icon if
* there is no explicitly set pressed icon.
* @param g the icon used as the default image
* @see getIcon
* @see setPressedIcon
* @beaninfo
* bound: true
* description: The button's default icon
*/
public void setIcon(Icon defaultIcon) {
Icon oldValue = this.defaultIcon;
this.defaultIcon = defaultIcon;
firePropertyChange("icon", oldValue, defaultIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, defaultIcon);
}
invalidate();
repaint();
}
/**
* Returns the pressed icon for the button.
* @see setPressedIcon
*/
public Icon getPressedIcon() {
return pressedIcon;
}
/**
* Sets the pressed icon for the button.
* @param g the icon used as the "pressed" image
* @see getPressedIcon
* @beaninfo
* bound: true
* description: The pressed icon for the button.
*/
public void setPressedIcon(Icon pressedIcon) {
Icon oldValue = this.pressedIcon;
this.pressedIcon = pressedIcon;
firePropertyChange("pressedIcon", oldValue, pressedIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, defaultIcon);
}
invalidate();
repaint();
}
/**
* Returns the selected icon for the button.
* @see setSelectedIcon
*/
public Icon getSelectedIcon() {
return selectedIcon;
}
/**
* Sets the selected icon for the button.
* @param g the icon used as the "selected" image
* @see getSelectedIcon
* @beaninfo
* bound: true
* description: The selected icon for the button.
*/
public void setSelectedIcon(Icon selectedIcon) {
Icon oldValue = this.selectedIcon;
this.selectedIcon = selectedIcon;
firePropertyChange("selectedIcon", oldValue, selectedIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, selectedIcon);
}
invalidate();
repaint();
}
/**
* Returns the rollover icon for the button.
* @see setRolloverIcon
*/
public Icon getRolloverIcon() {
return rolloverIcon;
}
/**
* Sets the rollover icon for the button.
* @param g the icon used as the "rollover" image
* @see getRolloverIcon
* @beaninfo
* bound: true
* description: The rollover icon for the button.
*/
public void setRolloverIcon(Icon rolloverIcon) {
Icon oldValue = this.rolloverIcon;
this.rolloverIcon = rolloverIcon;
firePropertyChange("rolloverIcon", oldValue, rolloverIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, rolloverIcon);
}
setRolloverEnabled(true);
invalidate();
repaint();
}
/**
* Returns the rollover seletion icon for the button.
* @see setRolloverSelectedIcon
*/
public Icon getRolloverSelectedIcon() {
return rolloverSelectedIcon;
}
/**
* Sets the rollover selected icon for the button.
* @param g the icon used as the "selected rollover" image
* @see getRolloverSelectedIcon
* @beaninfo
* bound: true
* description: The rollover selected icon for the button.
*/
public void setRolloverSelectedIcon(Icon rolloverSelectedIcon) {
Icon oldValue = this.rolloverSelectedIcon;
this.rolloverSelectedIcon = rolloverSelectedIcon;
firePropertyChange("rolloverSelectedIcon", oldValue, rolloverSelectedIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, rolloverSelectedIcon);
}
invalidate();
repaint();
}
/**
* Returns the icon used by the button when it's disabled.
* If not no disabled icon has been set, the button constructs
* one from the default icon.
* PENDING(jeff): the disabled icon really should be created
* (if necesary) by the L&F.
* @see getPressedIcon
* @see setDisabledIcon
*/
public Icon getDisabledIcon() {
if(disabledIcon == null) {
if(defaultIcon != null
&& defaultIcon instanceof ImageIcon) {
disabledIcon = new ImageIcon(
GrayFilter.createDisabledImage(
((ImageIcon)defaultIcon).getImage()));
}
}
return disabledIcon;
}
/**
* Sets the disabled icon for the button.
* @param g the icon used as the disabled image
* @see getDisabledIcon
* @beaninfo
* bound: true
* description: The disabled icon for the button.
*/
public void setDisabledIcon(Icon disabledIcon) {
Icon oldValue = this.disabledIcon;
this.disabledIcon = disabledIcon;
firePropertyChange("disabledIcon", oldValue, disabledIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, disabledIcon);
}
invalidate();
repaint();
}
/**
* Returns the icon used by the button when it's disabled and selected.
* If not no disabled selection icon has been set, the button constructs
* one from the selection icon.
* PENDING(jeff): the disabled selection icon really should be created
* (if necesary) by the L&F.
* @see getPressedIcon
* @see setDisabledIcon
*/
public Icon getDisabledSelectedIcon() {
if(disabledSelectedIcon == null) {
if(selectedIcon != null && selectedIcon instanceof ImageIcon) {
disabledSelectedIcon = new ImageIcon(
GrayFilter.createDisabledImage(((ImageIcon)selectedIcon).getImage()));
} else {
return disabledIcon;
}
}
return disabledSelectedIcon;
}
/**
* Sets the disabled selection icon for the button.
* @param g the icon used as the disabled selection image
* @see getDisabledSelectedIcon
*/
public void setDisabledSelectedIcon(Icon disabledSelectedIcon) {
Icon oldValue = this.disabledSelectedIcon;
this.disabledSelectedIcon = disabledSelectedIcon;
firePropertyChange("disabledSelectedIcon", oldValue, disabledSelectedIcon);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, disabledSelectedIcon);
}
invalidate();
repaint();
}
/**
* Returns the vertical alignment of the text and icon.
* Valid keys: CENTER (the default), TOP, BOTTOM
*/
public int getVerticalAlignment() {
return verticalAlignment;
}
/**
* Sets the vertical alignment of the icon and text.
* Valid keys: CENTER (the default), TOP, BOTTOM
* @beaninfo
* bound: true
* description: The vertical alignment of the icon and text.
*/
public void setVerticalAlignment(int alignment) {
if (alignment == verticalAlignment) return;
int oldValue = verticalAlignment;
verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
firePropertyChange("verticalAlignment", oldValue, verticalAlignment);
invalidate();
repaint();
}
/**
* Returns the horizontal alignment of the icon and text.
* Valid keys: CENTER (the default), LEFT, RIGHT
*/
public int getHorizontalAlignment() {
return horizontalAlignment;
}
/**
* Sets the horizontal alignment of the icon and text.
* Valid keys: CENTER (the default), LEFT, RIGHT
* @beaninfo
* bound: true
* description: The horizontal alignment of the icon and text.
*/
public void setHorizontalAlignment(int alignment) {
if (alignment == horizontalAlignment) return;
int oldValue = horizontalAlignment;
horizontalAlignment = checkHorizontalKey(alignment, "horizontalAlignment");
firePropertyChange("horizontalAlignment", oldValue, horizontalAlignment);
invalidate();
repaint();
}
/**
* Returns the vertical position of the text relative to the icon
* Valid keys: CENTER (the default), TOP, BOTTOM
*/
public int getVerticalTextPosition() {
return verticalTextPosition;
}
/**
* Sets the vertical position of the text relative to the icon.
* Valid keys: CENTER (the default), TOP, BOTTOM
* @beaninfo
* bound: true
* description: The vertical position of the text relative to the icon.
*/
public void setVerticalTextPosition(int textPosition) {
if (textPosition == verticalTextPosition) return;
int oldValue = verticalTextPosition;
verticalTextPosition = checkVerticalKey(textPosition, "verticalTextPosition");
firePropertyChange("verticalTextPosition", oldValue, verticalTextPosition);
invalidate();
repaint();
}
/**
* Sets the horizontal position of the text relative to the icon.
* Valid keys: RIGHT (the default), LEFT, CENTER
*/
public int getHorizontalTextPosition() {
return horizontalTextPosition;
}
/**
* Sets the horizontal position of the text relative to the icon.
* Valid keys: RIGHT (the default), LEFT, CENTER
* @beaninfo
* bound: true
* description: The horizontal position of the text relative to the icon.
*/
public void setHorizontalTextPosition(int textPosition) {
if (textPosition == horizontalTextPosition) return;
int oldValue = horizontalTextPosition;
horizontalTextPosition = checkHorizontalKey(textPosition, "horizontalTextPosition");
firePropertyChange("horizontalTextPosition", oldValue, horizontalTextPosition);
invalidate();
repaint();
}
/**
* Ensures that the key is a valid. Throws an IllegalArgument exception
* exception otherwise.
*/
protected int checkHorizontalKey(int key, String exception) {
if ((key == LEFT) || (key == CENTER) || (key == RIGHT)) {
return key;
} else {
throw new IllegalArgumentException(exception);
}
}
/**
* Ensures that the key is a valid. Throws an IllegalArgument exception
* exception otherwise.
*/
protected int checkVerticalKey(int key, String exception) {
if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
return key;
} else {
throw new IllegalArgumentException(exception);
}
}
/**
* Sets the action command for this button.
*/
public void setActionCommand(String actionCommand) {
getModel().setActionCommand(actionCommand);
}
/**
* Returns the action command for this button.
*/
public String getActionCommand() {
String ac = getModel().getActionCommand();
if(ac == null) {
ac = getText();
}
return ac;
}
/**
* Returns whether the border should be painted.
* @see setBorderPainted
*/
public boolean isBorderPainted() {
return paintBorder;
}
/**
* Sets whether the border should be painted.
* @param b if true and border property is not null, the border is painted.
* @see isBorderPainted
* @beaninfo
* bound: true
* description: Whether the border should be painted.
*/
public void setBorderPainted(boolean b) {
boolean oldValue = paintBorder;
paintBorder = b;
firePropertyChange("borderPainted", oldValue, paintBorder);
invalidate();
repaint();
}
/**
* Paint the button's border if BorderPainted property is true.
*
* @see #paint
* @see #setBorder
*/
protected void paintBorder(Graphics g) {
if (isBorderPainted()) {
super.paintBorder(g);
}
}
/**
* Returns whether focus should be painted.
* @see setFocusPainted
*/
public boolean isFocusPainted() {
return paintFocus;
}
/**
* Sets whether focus should be painted.
* @param b if true, the focus state is painted.
* @see isFocusPainted
* @beaninfo
* bound: true
* description: Whether focus should be painted
*/
public void setFocusPainted(boolean b) {
boolean oldValue = paintFocus;
paintFocus = b;
firePropertyChange("focusPainted", oldValue, paintFocus);
invalidate();
repaint();
}
/**
* Checks whether rollover effects are enabled.
* @see setFocusPainted
*/
public boolean isRolloverEnabled() {
return rolloverEnabled;
}
/**
* Sets whether rollover effects should be enabled.
* @param b if true, rollover effects should be painted.
* @see isRolloverEnabled
* @beaninfo
* bound: true
* description: Whether rollover effects should be enabled.
*/
public void setRolloverEnabled(boolean b) {
boolean oldValue = rolloverEnabled;
rolloverEnabled = b;
firePropertyChange("rolloverEnabled", oldValue, rolloverEnabled);
invalidate();
repaint();
}
/**
* Convenience to get the keyboard mnemonic from the the current model
*/
public int getMnemonic() {
return model.getMnemonic();
}
/**
* Convenience to set the keyboard mnemonic on the current model
* @param mnemonic the key code which represents the mnemonic
*/
public void setMnemonic(int mnemonic) {
model.setMnemonic(mnemonic);
invalidate();
repaint();
}
public void setMnemonic(char mnemonic) {
int vk = (int) mnemonic;
if(vk >= 'a' && vk <='z')
vk -= ('a' - 'A');
setMnemonic(vk);
}
/**
* Get the model that this button represents.
* @see setModel
*/
public ButtonModel getModel() {
return model;
}
/**
* Set the model that this button represents.
* @param m the Model
* @see getModel
* @beaninfo
* bound: true
* description: Model that the Button uses.
*/
public void setModel(ButtonModel newModel) {
ButtonModel oldModel = getModel();
if (oldModel != null) {
oldModel.removeChangeListener(changeListener);
oldModel.removeActionListener(actionListener);
changeListener = null;
actionListener = null;
}
model = newModel;
if (newModel != null) {
changeListener = createChangeListener();
actionListener = createActionListener();
itemListener = createItemListener();
newModel.addChangeListener(changeListener);
newModel.addActionListener(actionListener);
newModel.addItemListener(itemListener);
}
firePropertyChange("model", oldModel, newModel);
invalidate();
repaint();
}
/**
* Returns the button's current UI.
* @see setUI
*/
public ButtonUI getUI() {
return (ButtonUI) ui;
}
/**
* Sets the button's UI.
* @param ui the new ButtonUI
* @see getUI
*/
public void setUI(ButtonUI ui) {
super.setUI(ui);
}
/**
* Gets a new UI object from the default UIFactory. Subtypes of
* AbstractButton should override this to update the UI. For
* example, JButton might do the following:
* setUI((ButtonUI)UIManager.getUI(
* "ButtonUI", "com.sun.java.swing.plaf.basic.BasicButtonUI", this));
*/
public void updateUI() {
}
/**
* Adds a ChangeListener to the button.
*/
public void addChangeListener(ChangeListener l) {
listenerList.add(ChangeListener.class, l);
}
/**
* Removes a ChangeListener from the button.
*/
public void removeChangeListener(ChangeListener l) {
listenerList.remove(ChangeListener.class, l);
}
/*
* Notify all listeners that have registered interest for
* notification on this event type. The event instance
* is lazily created using the parameters passed into
* the fire method.
* @see EventListenerList
*/
protected void fireStateChanged() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==ChangeListener.class) {
// Lazily create the event:
if (changeEvent == null)
changeEvent = new ChangeEvent(this);
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
}
}
}
/**
* adds an ActionListener to the button
*/
public void addActionListener(ActionListener l) {
listenerList.add(ActionListener.class, l);
}
/**
* removes an ActionListener from the button
*/
public void removeActionListener(ActionListener l) {
listenerList.remove(ActionListener.class, l);
}
/**
* Subclasses that want to handle ChangeEvents differently
* can override this to return another ChangeListener implementation.
*/
protected ChangeListener createChangeListener() {
return (ChangeListener) new ButtonChangeListener();
}
/**
* Extend ChangeListener to be serializable
* <p>
* Warning: serialized objects of this class will not be compatible with
* future swing releases. The current serialization support is appropriate
* for short term storage or RMI between Swing1.0 applications. It will
* not be possible to load serialized Swing1.0 objects with future releases
* of Swing. The JDK1.2 release of Swing will be the compatibility
* baseline for the serialized form of Swing objects.
*/
protected class ButtonChangeListener implements ChangeListener, Serializable {
ButtonChangeListener() {
}
public void stateChanged(ChangeEvent e) {
fireStateChanged();
repaint();
}
}
/*
* Notify all listeners that have registered interest for
* notification on this event type. The event instance
* is lazily created using the parameters passed into
* the fire method.
* @see EventListenerList
*/
protected void fireActionPerformed(ActionEvent event) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
ActionEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==ActionListener.class) {
// Lazily create the event:
if (e == null) {
e = new ActionEvent(AbstractButton.this,
ActionEvent.ACTION_PERFORMED,
getActionCommand());
}
((ActionListener)listeners[i+1]).actionPerformed(e);
}
}
}
/*
* Notify all listeners that have registered interest for
* notification on this event type. The event instance
* is lazily created using the parameters passed into
* the fire method.
* @see EventListenerList
*/
protected void fireItemStateChanged(ItemEvent event) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
ItemEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==ItemListener.class) {
// Lazily create the event:
if (e == null) {
e = new ItemEvent(AbstractButton.this,
ItemEvent.ITEM_STATE_CHANGED,
AbstractButton.this,
event.getStateChange());
}
((ItemListener)listeners[i+1]).itemStateChanged(e);
}
}
}
private class ForwardActionEvents implements ActionListener, Serializable {
public void actionPerformed(ActionEvent event) {
fireActionPerformed(event);
}
}
protected ActionListener createActionListener() {
return new ForwardActionEvents();
}
private class ForwardItemEvents implements ItemListener, Serializable {
public void itemStateChanged(ItemEvent event) {
fireItemStateChanged(event);
}
}
protected ItemListener createItemListener() {
return new ForwardItemEvents();
}
/**
* Enables (or disables) the button.
*/
public void setEnabled(boolean b) {
super.setEnabled(b);
model.setEnabled(b);
}
// *** Deprecated java.awt.Button APIs below *** //
/**
* @deprecated - Replaced by getText()
*/
public String getLabel() {
return getText();
}
/**
* @deprecated - Replaced by setText(text)
* @beaninfo
* bound: true
* description: Replace by setText(text)
*/
public void setLabel(String label) {
setText(label);
}
/**
* adds an ItemListener to the checkbox
*/
public void addItemListener(ItemListener l) {
listenerList.add(ItemListener.class, l);
}
/**
* removes an ItemListener from the button
*/
public void removeItemListener(ItemListener l) {
listenerList.remove(ItemListener.class, l);
}
public Object[] getSelectedObjects() {
// REMINDER: finish
return null;
}
protected void init(String text, Icon icon) {
setLayout(new OverlayLayout(this));
if(text != null) {
setText(text);
}
if(icon != null) {
setIcon(icon);
}
// Set the UI
updateUI();
// Listen for Focus events
addFocusListener(
new FocusListener() {
public void focusGained(FocusEvent event) {
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
null, AccessibleState.FOCUSED);
}
}
public void focusLost(FocusEvent event) {
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
null, AccessibleState.FOCUSED);
}
// repaint focus is lost
if(isFocusPainted()) {
repaint();
}
}
}
);
setAlignmentX(LEFT_ALIGNMENT);
}
///////////////////
// Accessibility support
///////////////////
/**
* Accessiblity support.
* <p>
* Warning: serialized objects of this class will not be compatible with
* future swing releases. The current serialization support is appropriate
* for short term storage or RMI between Swing1.0 applications. It will
* not be possible to load serialized Swing1.0 objects with future releases
* of Swing. The JDK1.2 release of Swing will be the compatibility
* baseline for the serialized form of Swing objects.
*/
protected abstract class AccessibleAbstractButton
extends AccessibleJComponent implements AccessibleValue {
/**
* Get the accessible name of this object.
*
* @return the localized name of the object -- can be null if this
* object does not have a name
*/
public String getAccessibleName() {
if (accessibleName != null) {
return accessibleName;
} else {
if (getText() == null) {
return super.getAccessibleName();
} else {
return getText();
}
}
}
/**
* Get the state set of this object.
*
* @return an instance of AccessibleState containing the current state
* of the object
* @see AccessibleState
*/
public AccessibleStateSet getAccessibleStateSet() {
AccessibleStateSet states = super.getAccessibleStateSet();
if (getModel().isArmed()) {
states.add(AccessibleState.ARMED);
}
if (hasFocus()) {
states.add(AccessibleState.FOCUSED);
}
if (getModel().isPressed()) {
states.add(AccessibleState.PRESSED);
}
if (isSelected()) {
states.add(AccessibleState.CHECKED);
}
return states;
}
/**
* Get the AccessibleValue associated with this object if one
* exists. Otherwise return null.
*/
public AccessibleValue getAccessibleValue() {
return this;
}
/**
* Returns the number of Actions available in this object.
* If there is more than one, the first one is the "default"
* action.
*
* @return the number of Actions in this object
*/
public int getAccessibleActionCount() {
return 1;
}
/**
* Return a description of the specified action of the object.
*
* @param i zero-based index of the actions
*/
public String getAccessibleActionDescription(int i) {
if (i == 0) {
// [[[PENDING: WDW -- need to provide a localized string]]]
return new String("click");
} else {
return null;
}
}
/**
* Perform the specified Action on the object
*
* @param i zero-based index of actions
* @return true if the the action was performed; else false.
*/
public boolean doAccessibleAction(int i) {
if (i == 0) {
doClick();
return true;
} else {
return false;
}
}
/**
* Get the value of this object as a Number.
*
* @return An Integer of 0 if this isn't selected or an Integer of 1 if
* this is selected.
* @see #isSelected
*/
public Number getCurrentAccessibleValue() {
if (isSelected()) {
return new Integer(1);
} else {
return new Integer(0);
}
}
/**
* Set the value of this object as a Number.
*
* @return True if the value was set.
*/
public boolean setCurrentAccessibleValue(Number n) {
if (n instanceof Integer) {
int i = n.intValue();
if (i == 0) {
setSelected(false);
} else {
setSelected(true);
}
return true;
} else {
return false;
}
}
/**
* Get the minimum value of this object as a Number.
*
* @return An Integer of 0.
*/
public Number getMinimumAccessibleValue() {
return new Integer(0);
}
/**
* Get the maximum value of this object as a Number.
*
* @return An Integer of 1.
*/
public Number getMaximumAccessibleValue() {
return new Integer(1);
}
}
}