001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing;
019:
020: import javax.accessibility.Accessible;
021: import javax.accessibility.AccessibleContext;
022: import javax.accessibility.AccessibleRole;
023: import org.apache.harmony.x.swing.StringConstants;
024:
025: /**
026: * <p>
027: * <i>JButton</i>
028: * </p>
029: * <h3>Implementation Notes:</h3>
030: * <ul>
031: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
032: * optimization, not as a guarantee of serialization compatibility.</li>
033: * </ul>
034: */
035: public class JButton extends AbstractButton implements Accessible {
036: private static final long serialVersionUID = 8822265937932828454L;
037:
038: protected class AccessibleJButton extends AccessibleAbstractButton {
039: private static final long serialVersionUID = -1171440163825721899L;
040:
041: @Override
042: public AccessibleRole getAccessibleRole() {
043: return AccessibleRole.PUSH_BUTTON;
044: }
045: }
046:
047: private boolean defaultCapable = true;
048:
049: private static final String UI_CLASS_ID = "ButtonUI";
050:
051: public JButton(String text, Icon icon) {
052: setModel(new DefaultButtonModel());
053: init(text, icon);
054: }
055:
056: public JButton(Icon icon) {
057: this (null, icon);
058: }
059:
060: public JButton(Action action) {
061: setModel(new DefaultButtonModel());
062: setAction(action);
063: init(getText(), getIcon());
064: }
065:
066: public JButton(String text) {
067: this (text, null);
068: }
069:
070: public JButton() {
071: this (null, null);
072: }
073:
074: @Override
075: public AccessibleContext getAccessibleContext() {
076: return (accessibleContext == null) ? (accessibleContext = new AccessibleJButton())
077: : accessibleContext;
078: }
079:
080: @Override
081: public String getUIClassID() {
082: return UI_CLASS_ID;
083: }
084:
085: public void setDefaultCapable(boolean defaultCapable) {
086: boolean oldValue = this .defaultCapable;
087: this .defaultCapable = defaultCapable;
088: firePropertyChange(
089: StringConstants.DEFAULT_CAPABLE_PROPERTY_CHANGED,
090: oldValue, defaultCapable);
091: }
092:
093: public boolean isDefaultCapable() {
094: return defaultCapable;
095: }
096:
097: public boolean isDefaultButton() {
098: final JRootPane rootPane = getRootPane();
099: return isDefaultButton(rootPane);
100: }
101:
102: @Override
103: public void removeNotify() {
104: final JRootPane rootPane = getRootPane();
105: if (isDefaultButton(rootPane)) {
106: rootPane.setDefaultButton(null);
107: }
108: super .removeNotify();
109: }
110:
111: private boolean isDefaultButton(JRootPane rootPane) {
112: return (rootPane != null)
113: && (rootPane.getDefaultButton() == this);
114: }
115: }
|