0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package javax.swing;
0019:
0020: import java.awt.Graphics;
0021: import java.awt.Image;
0022: import java.awt.Insets;
0023: import java.awt.ItemSelectable;
0024: import java.awt.Point;
0025: import java.awt.Rectangle;
0026: import java.awt.event.ActionEvent;
0027: import java.awt.event.ActionListener;
0028: import java.awt.event.InputEvent;
0029: import java.awt.event.ItemEvent;
0030: import java.awt.event.ItemListener;
0031: import java.awt.event.KeyEvent;
0032: import java.beans.PropertyChangeEvent;
0033: import java.beans.PropertyChangeListener;
0034: import java.io.Serializable;
0035:
0036: import javax.accessibility.AccessibleAction;
0037: import javax.accessibility.AccessibleExtendedComponent;
0038: import javax.accessibility.AccessibleIcon;
0039: import javax.accessibility.AccessibleKeyBinding;
0040: import javax.accessibility.AccessibleRelationSet;
0041: import javax.accessibility.AccessibleState;
0042: import javax.accessibility.AccessibleStateSet;
0043: import javax.accessibility.AccessibleText;
0044: import javax.accessibility.AccessibleValue;
0045: import javax.swing.event.ChangeEvent;
0046: import javax.swing.event.ChangeListener;
0047: import javax.swing.plaf.ButtonUI;
0048: import javax.swing.plaf.InsetsUIResource;
0049: import javax.swing.plaf.UIResource;
0050: import javax.swing.text.AttributeSet;
0051:
0052: import org.apache.harmony.x.swing.ButtonCommons;
0053: import org.apache.harmony.x.swing.StringConstants;
0054: import org.apache.harmony.x.swing.Utilities;
0055: import org.apache.harmony.x.swing.internal.nls.Messages;
0056:
0057: /**
0058: * <p>
0059: * <i>AbstractButton</i>
0060: * </p>
0061: * <h3>Implementation Notes:</h3>
0062: * <ul>
0063: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
0064: * optimization, not as a guarantee of serialization compatibility.</li>
0065: * </ul>
0066: */
0067: public abstract class AbstractButton extends JComponent implements
0068: ItemSelectable, SwingConstants {
0069: protected abstract class AccessibleAbstractButton extends
0070: AccessibleJComponent implements AccessibleAction,
0071: AccessibleValue, AccessibleText,
0072: AccessibleExtendedComponent, Serializable {
0073: @Override
0074: public AccessibleKeyBinding getAccessibleKeyBinding() {
0075: return null;
0076: }
0077:
0078: public int getAccessibleActionCount() {
0079: return 1;
0080: }
0081:
0082: @Override
0083: public String getToolTipText() {
0084: return AbstractButton.this .getToolTipText();
0085: }
0086:
0087: @Override
0088: public AccessibleValue getAccessibleValue() {
0089: return this ;
0090: }
0091:
0092: @Override
0093: public AccessibleText getAccessibleText() {
0094: return null;
0095: }
0096:
0097: @Override
0098: public String getAccessibleName() {
0099: return (super .getAccessibleName() != null) ? super
0100: .getAccessibleName() : getText();
0101: }
0102:
0103: @Override
0104: public AccessibleRelationSet getAccessibleRelationSet() {
0105: return super .getAccessibleRelationSet();
0106: }
0107:
0108: @Override
0109: public String getTitledBorderText() {
0110: return super .getTitledBorderText();
0111: }
0112:
0113: @Override
0114: public AccessibleStateSet getAccessibleStateSet() {
0115: AccessibleStateSet set = super .getAccessibleStateSet();
0116: if (isSelected()) {
0117: set.add(AccessibleState.CHECKED);
0118: }
0119: return set;
0120: }
0121:
0122: @Override
0123: public AccessibleIcon[] getAccessibleIcon() {
0124: if (icon != null && icon instanceof ImageIcon) {
0125: return new AccessibleIcon[] { (AccessibleIcon) ((ImageIcon) icon)
0126: .getAccessibleContext() };
0127: }
0128: return null;
0129: }
0130:
0131: @Override
0132: public AccessibleAction getAccessibleAction() {
0133: return this ;
0134: }
0135:
0136: public boolean doAccessibleAction(int index) {
0137: if (0 <= index && index < getAccessibleActionCount()) {
0138: return true;
0139: }
0140: return false;
0141: }
0142:
0143: public String getAccessibleActionDescription(int index) {
0144: if (0 <= index && index < getAccessibleActionCount()) {
0145: return "click";
0146: }
0147: return null;
0148: }
0149:
0150: public Number getCurrentAccessibleValue() {
0151: return (AbstractButton.this .isSelected()) ? new Integer(1)
0152: : new Integer(0);
0153: }
0154:
0155: public Number getMaximumAccessibleValue() {
0156: return new Integer(1);
0157: }
0158:
0159: public Number getMinimumAccessibleValue() {
0160: return new Integer(0);
0161: }
0162:
0163: public boolean setCurrentAccessibleValue(Number value) {
0164: boolean valueSet = (value.intValue() == 0) ? false : true;
0165: if (valueSet != isSelected()) {
0166: setSelected(valueSet);
0167: if (valueSet) {
0168: firePropertyChange("AccessibleState", null,
0169: AccessibleState.SELECTED);
0170: firePropertyChange("AccessibleValue",
0171: new Integer(0), new Integer(1));
0172: } else {
0173: firePropertyChange("AccessibleState",
0174: AccessibleState.SELECTED, null);
0175: firePropertyChange("AccessibleValue",
0176: new Integer(1), new Integer(0));
0177: }
0178: }
0179: return true;
0180: }
0181:
0182: public int getCaretPosition() {
0183: return -1;
0184: }
0185:
0186: public int getCharCount() {
0187: String text = AbstractButton.this .getText();
0188: return (text != null) ? text.length() : 0;
0189: }
0190:
0191: public int getSelectionEnd() {
0192: return -1;
0193: }
0194:
0195: public int getSelectionStart() {
0196: return -1;
0197: }
0198:
0199: public int getIndexAtPoint(Point point) {
0200: return -1;
0201: }
0202:
0203: public Rectangle getCharacterBounds(int arg0) {
0204: return null;
0205: }
0206:
0207: public String getSelectedText() {
0208: return null;
0209: }
0210:
0211: public String getAfterIndex(int part, int index) {
0212: return null;
0213: }
0214:
0215: public String getAtIndex(int part, int index) {
0216: return null;
0217: }
0218:
0219: public String getBeforeIndex(int part, int index) {
0220: return null;
0221: }
0222:
0223: public AttributeSet getCharacterAttribute(int index) {
0224: return null;
0225: }
0226: };
0227:
0228: protected class ButtonChangeListener implements ChangeListener,
0229: Serializable {
0230: private static final long serialVersionUID = 1L;
0231:
0232: private ButtonChangeListener() {
0233: }
0234:
0235: public void stateChanged(ChangeEvent event) {
0236: int mn = model.getMnemonic();
0237: updateMnemonic(mn, Utilities.keyCodeToKeyChar(mn));
0238: fireStateChanged();
0239: }
0240: };
0241:
0242: private final class ActionAndModelListener implements ItemListener,
0243: ActionListener, PropertyChangeListener, Serializable {
0244: private static final long serialVersionUID = 1L;
0245:
0246: public void itemStateChanged(ItemEvent event) {
0247: fireItemStateChanged(event);
0248: }
0249:
0250: public void actionPerformed(ActionEvent event) {
0251: fireActionPerformed(event);
0252: }
0253:
0254: public void propertyChange(PropertyChangeEvent event) {
0255: configurePropertyFromAction((Action) event.getSource(),
0256: event.getPropertyName());
0257: }
0258: };
0259:
0260: public static final String MODEL_CHANGED_PROPERTY = "model";
0261:
0262: public static final String TEXT_CHANGED_PROPERTY = "text";
0263:
0264: public static final String MNEMONIC_CHANGED_PROPERTY = "mnemonic";
0265:
0266: public static final String MARGIN_CHANGED_PROPERTY = "margin";
0267:
0268: public static final String VERTICAL_ALIGNMENT_CHANGED_PROPERTY = "verticalAlignment";
0269:
0270: public static final String HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = "horizontalAlignment";
0271:
0272: public static final String VERTICAL_TEXT_POSITION_CHANGED_PROPERTY = "verticalTextPosition";
0273:
0274: public static final String HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = "horizontalTextPosition";
0275:
0276: public static final String BORDER_PAINTED_CHANGED_PROPERTY = "borderPainted";
0277:
0278: public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";
0279:
0280: public static final String ROLLOVER_ENABLED_CHANGED_PROPERTY = "rolloverEnabled";
0281:
0282: public static final String CONTENT_AREA_FILLED_CHANGED_PROPERTY = "contentAreaFilled";
0283:
0284: public static final String ICON_CHANGED_PROPERTY = "icon";
0285:
0286: public static final String PRESSED_ICON_CHANGED_PROPERTY = "pressedIcon";
0287:
0288: public static final String SELECTED_ICON_CHANGED_PROPERTY = "selectedIcon";
0289:
0290: public static final String ROLLOVER_ICON_CHANGED_PROPERTY = "rolloverIcon";
0291:
0292: public static final String ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY = "rolloverSelectedIcon";
0293:
0294: public static final String DISABLED_ICON_CHANGED_PROPERTY = "disabledIcon";
0295:
0296: public static final String DISABLED_SELECTED_ICON_CHANGED_PROPERTY = "disabledSelectedIcon";
0297:
0298: private static final Object ALL_ACTION_PROPERTIES = new Object() { // $NON-LOCK-1$
0299: @Override
0300: public boolean equals(Object o) {
0301: return true;
0302: }
0303: };
0304:
0305: private static final Action CLEAR_ACTION_PROPERTIES = new AbstractAction() {
0306: private static final long serialVersionUID = 1L;
0307:
0308: public void actionPerformed(ActionEvent e) {
0309: }
0310:
0311: @Override
0312: public void putValue(String name, Object value) {
0313: }
0314:
0315: @Override
0316: public void setEnabled(boolean enabled) {
0317: }
0318: };
0319:
0320: protected transient ChangeEvent changeEvent = new ChangeEvent(this );
0321:
0322: protected ButtonModel model;
0323:
0324: protected ChangeListener changeListener = createChangeListener();
0325:
0326: protected ActionListener actionListener = createActionListener();
0327:
0328: protected ItemListener itemListener = createItemListener();
0329:
0330: private PropertyChangeListener actionPropertyChangeListener;
0331:
0332: private ActionAndModelListener handler;
0333:
0334: private String text = "";
0335:
0336: private Insets margin;
0337:
0338: private Action action;
0339:
0340: private Icon icon;
0341:
0342: private Icon pressedIcon;
0343:
0344: private Icon disabledIcon;
0345:
0346: private Icon defaultDisabledIcon;
0347:
0348: private Icon selectedIcon;
0349:
0350: private Icon disabledSelectedIcon;
0351:
0352: private Icon defaultDisabledSelectedIcon;
0353:
0354: private Icon rolloverIcon;
0355:
0356: private Icon rolloverSelectedIcon;
0357:
0358: private boolean borderPainted = true;
0359:
0360: private boolean focusPainted = true;
0361:
0362: private boolean rolloverEnabled;
0363:
0364: private boolean contentAreaFilled = true;
0365:
0366: private int verticalAlignment = SwingConstants.CENTER;
0367:
0368: private int horizontalAlignment = SwingConstants.CENTER;
0369:
0370: private int verticalTextPosition = SwingConstants.CENTER;
0371:
0372: private int horizontalTextPosition = SwingConstants.TRAILING;
0373:
0374: private int iconTextGap = 4;
0375:
0376: private int mnemonic;
0377:
0378: private int mnemonicIndex = -1;
0379:
0380: private long multiClickThreshhold;
0381:
0382: private InsetsUIResource defaultMargin;
0383:
0384: protected void init(String text, Icon icon) {
0385: if (text != null) {
0386: setText(text);
0387: }
0388: if (icon != null) {
0389: setIcon(icon);
0390: }
0391: updateUI();
0392: }
0393:
0394: protected PropertyChangeListener createActionPropertyChangeListener(
0395: Action action) {
0396: return (handler != null) ? handler
0397: : (handler = new ActionAndModelListener());
0398: }
0399:
0400: public void setUI(ButtonUI ui) {
0401: super .setUI(ui);
0402: }
0403:
0404: public ButtonUI getUI() {
0405: return (ButtonUI) ui;
0406: }
0407:
0408: public void removeChangeListener(ChangeListener listener) {
0409: listenerList.remove(ChangeListener.class, listener);
0410: }
0411:
0412: public void addChangeListener(ChangeListener listener) {
0413: listenerList.add(ChangeListener.class, listener);
0414: }
0415:
0416: public ChangeListener[] getChangeListeners() {
0417: return listenerList.getListeners(ChangeListener.class);
0418: }
0419:
0420: protected ChangeListener createChangeListener() {
0421: return new ButtonChangeListener();
0422: }
0423:
0424: public void setSelectedIcon(Icon selectedIcon) {
0425: Icon oldValue = this .selectedIcon;
0426: this .selectedIcon = selectedIcon;
0427: resetDefaultDisabledIcons();
0428: firePropertyChange(SELECTED_ICON_CHANGED_PROPERTY, oldValue,
0429: selectedIcon);
0430: }
0431:
0432: public void setRolloverSelectedIcon(Icon rolloverSelectedIcon) {
0433: if (this .rolloverSelectedIcon != rolloverSelectedIcon) {
0434: Icon oldValue = this .rolloverSelectedIcon;
0435: this .rolloverSelectedIcon = rolloverSelectedIcon;
0436: firePropertyChange(ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY,
0437: oldValue, rolloverSelectedIcon);
0438: setRolloverEnabled(true);
0439: }
0440: }
0441:
0442: public void setRolloverIcon(Icon rolloverIcon) {
0443: if (this .rolloverIcon != rolloverIcon) {
0444: Icon oldValue = this .rolloverIcon;
0445: this .rolloverIcon = rolloverIcon;
0446: firePropertyChange(ROLLOVER_ICON_CHANGED_PROPERTY,
0447: oldValue, rolloverIcon);
0448: setRolloverEnabled(true);
0449: }
0450: }
0451:
0452: public void setPressedIcon(Icon pressedIcon) {
0453: Icon oldValue = this .pressedIcon;
0454: this .pressedIcon = pressedIcon;
0455: firePropertyChange(PRESSED_ICON_CHANGED_PROPERTY, oldValue,
0456: pressedIcon);
0457: }
0458:
0459: private void resetDefaultDisabledIcons() {
0460: defaultDisabledIcon = null;
0461: defaultDisabledSelectedIcon = null;
0462: }
0463:
0464: public void setIcon(Icon icon) {
0465: Icon oldValue = this .icon;
0466: this .icon = icon;
0467: resetDefaultDisabledIcons();
0468: firePropertyChange(ICON_CHANGED_PROPERTY, oldValue, icon);
0469: }
0470:
0471: public void setDisabledSelectedIcon(Icon disabledSelectedIcon) {
0472: Icon oldValue = this .disabledSelectedIcon;
0473: this .disabledSelectedIcon = disabledSelectedIcon;
0474: resetDefaultDisabledIcons();
0475: firePropertyChange(DISABLED_SELECTED_ICON_CHANGED_PROPERTY,
0476: oldValue, disabledSelectedIcon);
0477: }
0478:
0479: public void setDisabledIcon(Icon disabledIcon) {
0480: Icon oldValue = this .disabledIcon;
0481: this .disabledIcon = disabledIcon;
0482: resetDefaultDisabledIcons();
0483: firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, oldValue,
0484: disabledIcon);
0485: }
0486:
0487: public Icon getSelectedIcon() {
0488: return selectedIcon;
0489: }
0490:
0491: public Icon getRolloverSelectedIcon() {
0492: return rolloverSelectedIcon;
0493: }
0494:
0495: public Icon getRolloverIcon() {
0496: return rolloverIcon;
0497: }
0498:
0499: public Icon getPressedIcon() {
0500: return pressedIcon;
0501: }
0502:
0503: public Icon getIcon() {
0504: return icon;
0505: }
0506:
0507: private Icon createDefaultDisabledSelectedIcon() {
0508: if (defaultDisabledSelectedIcon != null) {
0509: return defaultDisabledSelectedIcon;
0510: }
0511: if (selectedIcon instanceof ImageIcon) {
0512: defaultDisabledIcon = new ImageIcon(GrayFilter
0513: .createDisabledImage(((ImageIcon) selectedIcon)
0514: .getImage()));
0515: } else if (disabledIcon instanceof ImageIcon) {
0516: defaultDisabledIcon = new ImageIcon(GrayFilter
0517: .createDisabledImage(((ImageIcon) disabledIcon)
0518: .getImage()));
0519: } else if (icon instanceof ImageIcon) {
0520: defaultDisabledIcon = new ImageIcon(GrayFilter
0521: .createDisabledImage(((ImageIcon) icon).getImage()));
0522: }
0523: return defaultDisabledIcon;
0524: }
0525:
0526: public Icon getDisabledSelectedIcon() {
0527: return (disabledSelectedIcon != null) ? disabledSelectedIcon
0528: : createDefaultDisabledSelectedIcon();
0529: }
0530:
0531: private Icon createDefaultDisabledIcon() {
0532: if (defaultDisabledIcon != null) {
0533: return defaultDisabledIcon;
0534: }
0535: if (icon instanceof ImageIcon) {
0536: defaultDisabledIcon = new ImageIcon(GrayFilter
0537: .createDisabledImage(((ImageIcon) icon).getImage()));
0538: }
0539: return defaultDisabledIcon;
0540: }
0541:
0542: public Icon getDisabledIcon() {
0543: return (disabledIcon != null) ? disabledIcon
0544: : createDefaultDisabledIcon();
0545: }
0546:
0547: public void setModel(ButtonModel m) {
0548: if (model != m) {
0549: ButtonModel oldValue = model;
0550: if (model != null) {
0551: model.removeActionListener(actionListener);
0552: model.removeItemListener(itemListener);
0553: model.removeChangeListener(changeListener);
0554: }
0555: model = m;
0556: if (model != null) {
0557: model.addChangeListener(changeListener);
0558: model.addItemListener(itemListener);
0559: model.addActionListener(actionListener);
0560: int mn = model.getMnemonic();
0561: updateMnemonic(mn, Utilities.keyCodeToKeyChar(mn));
0562: }
0563: firePropertyChange(MODEL_CHANGED_PROPERTY, oldValue, model);
0564: }
0565: }
0566:
0567: public ButtonModel getModel() {
0568: return model;
0569: }
0570:
0571: public void setAction(Action action) {
0572: if (this .action == action && action != null) {
0573: return;
0574: }
0575: Action oldValue = this .action;
0576: if (oldValue != null) {
0577: if (hasListener(Action.class, oldValue)) {
0578: removeActionListener(oldValue);
0579: }
0580: if (actionPropertyChangeListener != null) {
0581: oldValue
0582: .removePropertyChangeListener(actionPropertyChangeListener);
0583: }
0584: }
0585: this .action = action;
0586: if (action != null) {
0587: if (!hasListener(ActionListener.class, action)) {
0588: listenerList.add(Action.class, action);
0589: addActionListener(action);
0590: }
0591: actionPropertyChangeListener = createActionPropertyChangeListener(action);
0592: action
0593: .addPropertyChangeListener(actionPropertyChangeListener);
0594: }
0595: firePropertyChange(StringConstants.ACTION_PROPERTY_CHANGED,
0596: oldValue, action);
0597: configurePropertiesFromAction(action);
0598: }
0599:
0600: void configurePropertyFromAction(Action action, Object propertyName) {
0601: if (propertyName == null) {
0602: return;
0603: }
0604: if (propertyName.equals(Action.MNEMONIC_KEY)) {
0605: Object actionMnemonic = action
0606: .getValue(Action.MNEMONIC_KEY);
0607: setMnemonic((actionMnemonic != null) ? ((Integer) actionMnemonic)
0608: .intValue()
0609: : 0);
0610: }
0611: if (propertyName.equals(Action.SHORT_DESCRIPTION)) {
0612: setToolTipText((String) action
0613: .getValue(Action.SHORT_DESCRIPTION));
0614: }
0615: if (propertyName.equals(Action.SMALL_ICON)) {
0616: setIcon((Icon) action.getValue(Action.SMALL_ICON));
0617: }
0618: if (propertyName
0619: .equals(StringConstants.ENABLED_PROPERTY_CHANGED)) {
0620: setEnabled(action.isEnabled());
0621: }
0622: if (propertyName.equals(Action.NAME)) {
0623: setText((String) action.getValue(Action.NAME));
0624: }
0625: if (propertyName.equals(Action.ACTION_COMMAND_KEY)) {
0626: setActionCommand((String) action
0627: .getValue(Action.ACTION_COMMAND_KEY));
0628: }
0629: }
0630:
0631: protected void configurePropertiesFromAction(Action action) {
0632: final Action a = (action != null) ? action
0633: : CLEAR_ACTION_PROPERTIES;
0634: configurePropertyFromAction(a, getActionPropertiesFilter());
0635: }
0636:
0637: public Action getAction() {
0638: return action;
0639: }
0640:
0641: public void setText(String text) {
0642: if (text != this .text) {
0643: String oldValue = this .text;
0644: this .text = text;
0645: firePropertyChange(TEXT_CHANGED_PROPERTY, oldValue, text);
0646: updateDisplayedMnemonicsIndex(Utilities
0647: .keyCodeToKeyChar(mnemonic));
0648: }
0649: }
0650:
0651: @Deprecated
0652: public void setLabel(String label) {
0653: setText(label);
0654: }
0655:
0656: public void setActionCommand(String command) {
0657: model.setActionCommand(command);
0658: }
0659:
0660: protected int checkVerticalKey(int key, String exceptionText) {
0661: return Utilities.checkVerticalKey(key, exceptionText);
0662: }
0663:
0664: protected int checkHorizontalKey(int key, String exceptionText) {
0665: return Utilities.checkHorizontalKey(key, exceptionText);
0666: }
0667:
0668: public String getText() {
0669: return text;
0670: }
0671:
0672: @Deprecated
0673: public String getLabel() {
0674: return getText();
0675: }
0676:
0677: public String getActionCommand() {
0678: String command = model.getActionCommand();
0679: return (command != null) ? command : getText();
0680: }
0681:
0682: public Object[] getSelectedObjects() {
0683: return model.isSelected() ? new Object[] { getText() } : null;
0684: }
0685:
0686: public void removeItemListener(ItemListener listener) {
0687: listenerList.remove(ItemListener.class, listener);
0688: }
0689:
0690: public void addItemListener(ItemListener listener) {
0691: listenerList.add(ItemListener.class, listener);
0692: }
0693:
0694: public ItemListener[] getItemListeners() {
0695: return listenerList.getListeners(ItemListener.class);
0696: }
0697:
0698: protected ItemListener createItemListener() {
0699: return (handler != null) ? handler
0700: : (handler = new ActionAndModelListener());
0701: }
0702:
0703: protected void fireItemStateChanged(ItemEvent event) {
0704: ItemListener[] listeners = getItemListeners();
0705: if (listeners.length > 0) {
0706: ItemEvent itemEvent = new ItemEvent(this ,
0707: ItemEvent.ITEM_STATE_CHANGED, this , event
0708: .getStateChange());
0709: for (int i = 0; i < listeners.length; i++) {
0710: listeners[i].itemStateChanged(itemEvent);
0711: }
0712: }
0713: }
0714:
0715: public void removeActionListener(ActionListener listener) {
0716: listenerList.remove(ActionListener.class, listener);
0717: }
0718:
0719: public void addActionListener(ActionListener listener) {
0720: listenerList.add(ActionListener.class, listener);
0721: }
0722:
0723: public ActionListener[] getActionListeners() {
0724: return listenerList.getListeners(ActionListener.class);
0725: }
0726:
0727: protected ActionListener createActionListener() {
0728: return (handler != null) ? handler
0729: : (handler = new ActionAndModelListener());
0730: }
0731:
0732: protected void fireActionPerformed(ActionEvent event) {
0733: ActionListener[] listeners = getActionListeners();
0734: if (listeners.length > 0) {
0735: String command = (event.getActionCommand() != null) ? event
0736: .getActionCommand() : getText();
0737: ActionEvent actionEvent = new ActionEvent(this ,
0738: ActionEvent.ACTION_PERFORMED, command, event
0739: .getModifiers());
0740: for (int i = 0; i < listeners.length; i++) {
0741: listeners[i].actionPerformed(actionEvent);
0742: }
0743: }
0744: }
0745:
0746: public void setMargin(Insets margin) {
0747:
0748: /* default values are obtained from UI (Harmony-4655) */
0749: if (margin instanceof InsetsUIResource) {
0750: defaultMargin = (InsetsUIResource) margin;
0751: } else if (margin == null) {
0752: /*
0753: * According to spec if margin == null default value sets
0754: * (Harmony-4655)
0755: */
0756: margin = defaultMargin;
0757: }
0758: Insets oldValue = this .margin;
0759: this .margin = margin;
0760: firePropertyChange(MARGIN_CHANGED_PROPERTY, oldValue, margin);
0761: }
0762:
0763: public Insets getMargin() {
0764: return margin;
0765: }
0766:
0767: @Override
0768: public boolean imageUpdate(Image img, int infoflags, int x, int y,
0769: int w, int h) {
0770: Icon curIcon = ButtonCommons.getCurrentIcon(this );
0771: if ((curIcon == null) || !(curIcon instanceof ImageIcon)
0772: || (((ImageIcon) curIcon).getImage() != img)) {
0773: return false;
0774: }
0775: return super .imageUpdate(img, infoflags, x, y, w, h);
0776: }
0777:
0778: @Override
0779: protected void paintBorder(Graphics g) {
0780: if (isBorderPainted()) {
0781: super .paintBorder(g);
0782: }
0783: }
0784:
0785: public void doClick(int pressTime) {
0786: final ButtonModel model = getModel();
0787: model.setArmed(true);
0788: model.setPressed(true);
0789: if (pressTime > 0) {
0790: paintImmediately(0, 0, getWidth(), getHeight());
0791: try {
0792: Thread.sleep(pressTime);
0793: } catch (InterruptedException e) {
0794: }
0795: }
0796: model.setPressed(false);
0797: model.setArmed(false);
0798: }
0799:
0800: /**
0801: * The click delay is based on 1.5 release behavior which can be revealed using the
0802: * following code:
0803: *
0804: * <pre>
0805: * AbstractButton ab = new AbstractButton() {
0806: * };
0807: * long startTime = System.currentTimeMillis();
0808: * ab.setModel(new DefaultButtonModel());
0809: * for (int i = 0; i < 100; i++) {
0810: * ab.doClick();
0811: * }
0812: * long stopTime = System.currentTimeMillis();
0813: * System.err.println("doClick takes " + (stopTime - startTime) / 100);
0814: * </pre>
0815: */
0816: public void doClick() {
0817: doClick(70);
0818: }
0819:
0820: public void setSelected(boolean selected) {
0821: model.setSelected(selected);
0822: }
0823:
0824: public void setRolloverEnabled(boolean rollover) {
0825: boolean oldValue = rolloverEnabled;
0826: rolloverEnabled = rollover;
0827: firePropertyChange(ROLLOVER_ENABLED_CHANGED_PROPERTY, oldValue,
0828: rolloverEnabled);
0829: }
0830:
0831: public void setFocusPainted(boolean painted) {
0832: boolean oldValue = focusPainted;
0833: focusPainted = painted;
0834: firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY, oldValue,
0835: painted);
0836: }
0837:
0838: @Override
0839: public void setEnabled(boolean enabled) {
0840: model.setEnabled(enabled);
0841: super .setEnabled(enabled);
0842: }
0843:
0844: public void setContentAreaFilled(boolean filled) {
0845: boolean oldValue = contentAreaFilled;
0846: contentAreaFilled = filled;
0847: firePropertyChange(CONTENT_AREA_FILLED_CHANGED_PROPERTY,
0848: oldValue, contentAreaFilled);
0849: }
0850:
0851: public void setBorderPainted(boolean painted) {
0852: boolean oldValue = borderPainted;
0853: borderPainted = painted;
0854: firePropertyChange(BORDER_PAINTED_CHANGED_PROPERTY, oldValue,
0855: borderPainted);
0856: }
0857:
0858: public void setMultiClickThreshhold(long threshold) {
0859: if (threshold < 0) {
0860: throw new IllegalArgumentException(Messages
0861: .getString("swing.05")); //$NON-NLS-1$
0862: }
0863: multiClickThreshhold = threshold;
0864: }
0865:
0866: public void setVerticalTextPosition(int pos) {
0867: int oldValue = verticalTextPosition;
0868: verticalTextPosition = checkVerticalKey(pos,
0869: VERTICAL_TEXT_POSITION_CHANGED_PROPERTY);
0870: firePropertyChange(VERTICAL_TEXT_POSITION_CHANGED_PROPERTY,
0871: oldValue, verticalTextPosition);
0872: }
0873:
0874: public void setVerticalAlignment(int alignment) {
0875: int oldValue = verticalAlignment;
0876: verticalAlignment = checkVerticalKey(alignment,
0877: VERTICAL_ALIGNMENT_CHANGED_PROPERTY);
0878: firePropertyChange(VERTICAL_ALIGNMENT_CHANGED_PROPERTY,
0879: oldValue, verticalAlignment);
0880: }
0881:
0882: public void setMnemonic(char keyChar) {
0883: setMnemonic(Utilities.keyCharToKeyCode(keyChar), keyChar);
0884: }
0885:
0886: public void setMnemonic(int mnemonicCode) {
0887: setMnemonic(mnemonicCode, Utilities
0888: .keyCodeToKeyChar(mnemonicCode));
0889: }
0890:
0891: private void setMnemonic(int keyCode, char keyChar) {
0892: model.setMnemonic(keyCode);
0893: }
0894:
0895: private void updateMnemonic(int keyCode, char keyChar) {
0896: int oldKeyCode = mnemonic;
0897: if (oldKeyCode == keyCode) {
0898: return;
0899: }
0900: mnemonic = keyCode;
0901: firePropertyChange(MNEMONIC_CHANGED_PROPERTY, oldKeyCode,
0902: keyCode);
0903: updateDisplayedMnemonicsIndex(keyChar);
0904: }
0905:
0906: private void updateDisplayedMnemonicsIndex(char keyChar) {
0907: setDisplayedMnemonicIndex(Utilities.getDisplayedMnemonicIndex(
0908: text, keyChar));
0909: }
0910:
0911: public int getMnemonic() {
0912: return mnemonic;
0913: }
0914:
0915: public void setDisplayedMnemonicIndex(int index)
0916: throws IllegalArgumentException {
0917: if (index < -1 || index >= 0
0918: && (text == null || index >= text.length())) {
0919: throw new IllegalArgumentException(Messages.getString(
0920: "swing.10", index)); //$NON-NLS-1$
0921: }
0922: int oldValue = mnemonicIndex;
0923: mnemonicIndex = index;
0924: firePropertyChange(
0925: StringConstants.MNEMONIC_INDEX_PROPERTY_CHANGED,
0926: oldValue, index);
0927: }
0928:
0929: public int getDisplayedMnemonicIndex() {
0930: return mnemonicIndex;
0931: }
0932:
0933: public void setIconTextGap(int gap) {
0934: LookAndFeel.markPropertyNotInstallable(this ,
0935: StringConstants.ICON_TEXT_GAP_PROPERTY_CHANGED);
0936: int oldValue = iconTextGap;
0937: iconTextGap = gap;
0938: firePropertyChange(
0939: StringConstants.ICON_TEXT_GAP_PROPERTY_CHANGED,
0940: oldValue, iconTextGap);
0941: }
0942:
0943: public void setHorizontalTextPosition(int pos) {
0944: int oldValue = horizontalTextPosition;
0945: horizontalTextPosition = checkHorizontalKey(pos,
0946: HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY);
0947: firePropertyChange(HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY,
0948: oldValue, horizontalTextPosition);
0949: }
0950:
0951: public void setHorizontalAlignment(int alignment) {
0952: int oldValue = horizontalAlignment;
0953: horizontalAlignment = checkHorizontalKey(alignment,
0954: HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY);
0955: firePropertyChange(HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY,
0956: oldValue, horizontalAlignment);
0957: }
0958:
0959: public boolean isSelected() {
0960: return model.isSelected();
0961: }
0962:
0963: public boolean isRolloverEnabled() {
0964: return rolloverEnabled;
0965: }
0966:
0967: public boolean isFocusPainted() {
0968: return focusPainted;
0969: }
0970:
0971: public boolean isContentAreaFilled() {
0972: return contentAreaFilled;
0973: }
0974:
0975: public boolean isBorderPainted() {
0976: return borderPainted;
0977: }
0978:
0979: protected void fireStateChanged() {
0980: ChangeListener[] listeners = getChangeListeners();
0981: for (int i = 0; i < listeners.length; i++) {
0982: listeners[i].stateChanged(changeEvent);
0983: }
0984: }
0985:
0986: public long getMultiClickThreshhold() {
0987: return multiClickThreshhold;
0988: }
0989:
0990: public int getVerticalTextPosition() {
0991: return verticalTextPosition;
0992: }
0993:
0994: public int getVerticalAlignment() {
0995: return verticalAlignment;
0996: }
0997:
0998: public int getIconTextGap() {
0999: return iconTextGap;
1000: }
1001:
1002: public int getHorizontalTextPosition() {
1003: return horizontalTextPosition;
1004: }
1005:
1006: public int getHorizontalAlignment() {
1007: return horizontalAlignment;
1008: }
1009:
1010: Object getActionPropertiesFilter() {
1011: return ALL_ACTION_PROPERTIES;
1012: }
1013:
1014: boolean processMnemonics(KeyEvent event) {
1015: final KeyStroke keyStroke = KeyStroke
1016: .getKeyStrokeForEvent(event);
1017: if (keyStroke.isOnKeyRelease() || getMnemonic() == 0) {
1018: return false;
1019: }
1020: if (isMnemonicKeyStroke(keyStroke)) {
1021: Action action = getActionMap().get(
1022: StringConstants.MNEMONIC_ACTION);
1023: if (action != null) {
1024: SwingUtilities.notifyAction(action, keyStroke, event,
1025: this , event.getModifiersEx());
1026: return true;
1027: }
1028: }
1029: return false;
1030: }
1031:
1032: boolean isMnemonicKeyStroke(KeyStroke keyStroke) {
1033: return keyStroke.getKeyCode() == getMnemonic()
1034: && (keyStroke.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0;
1035: }
1036: }
|