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 java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.awt.event.ItemEvent;
023: import java.awt.event.ItemListener;
024: import java.io.Serializable;
025: import java.util.EventListener;
026: import javax.swing.event.ChangeEvent;
027: import javax.swing.event.ChangeListener;
028: import javax.swing.event.EventListenerList;
029:
030: /**
031: * <p>
032: * <i>DefaultButtonModel</i>
033: * </p>
034: * <h3>Implementation Notes:</h3>
035: * <ul>
036: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
037: * optimization, not as a guarantee of serialization compatibility.</li>
038: * </ul>
039: */
040: public class DefaultButtonModel implements ButtonModel, Serializable {
041: private static final long serialVersionUID = -8004185980087291435L;
042:
043: public static final int ARMED = 1;
044:
045: public static final int SELECTED = 2;
046:
047: public static final int PRESSED = 4;
048:
049: public static final int ENABLED = 8;
050:
051: public static final int ROLLOVER = 16;
052:
053: protected int stateMask = ENABLED;
054:
055: protected String actionCommand;
056:
057: protected ButtonGroup group;
058:
059: protected int mnemonic;
060:
061: protected transient ChangeEvent changeEvent;
062:
063: protected EventListenerList listenerList = new EventListenerList();
064:
065: public <T extends EventListener> T[] getListeners(
066: Class<T> listenersClass) {
067: return listenerList.getListeners(listenersClass);
068: }
069:
070: public void addChangeListener(ChangeListener listener) {
071: listenerList.add(ChangeListener.class, listener);
072: }
073:
074: public void removeChangeListener(ChangeListener listener) {
075: listenerList.remove(ChangeListener.class, listener);
076: }
077:
078: public ChangeListener[] getChangeListeners() {
079: return listenerList.getListeners(ChangeListener.class);
080: }
081:
082: public void addItemListener(ItemListener listener) {
083: listenerList.add(ItemListener.class, listener);
084: }
085:
086: public void removeItemListener(ItemListener listener) {
087: listenerList.remove(ItemListener.class, listener);
088: }
089:
090: public ItemListener[] getItemListeners() {
091: return listenerList.getListeners(ItemListener.class);
092: }
093:
094: public void addActionListener(ActionListener listener) {
095: listenerList.add(ActionListener.class, listener);
096: }
097:
098: public void removeActionListener(ActionListener listener) {
099: listenerList.remove(ActionListener.class, listener);
100: }
101:
102: public ActionListener[] getActionListeners() {
103: return listenerList.getListeners(ActionListener.class);
104: }
105:
106: public void setGroup(ButtonGroup group) {
107: this .group = group;
108: }
109:
110: public ButtonGroup getGroup() {
111: return group;
112: }
113:
114: public void setActionCommand(String command) {
115: actionCommand = command;
116: }
117:
118: public String getActionCommand() {
119: return actionCommand;
120: }
121:
122: public Object[] getSelectedObjects() {
123: return null;
124: }
125:
126: public void setSelected(boolean selected) {
127: if (isSelected() != selected) {
128: toggleState(SELECTED);
129: int state = selected ? ItemEvent.SELECTED
130: : ItemEvent.DESELECTED;
131: ItemEvent event = new ItemEvent(this ,
132: ItemEvent.ITEM_STATE_CHANGED, this , state);
133: fireItemStateChanged(event);
134: }
135: }
136:
137: public boolean isSelected() {
138: return isStateSet(SELECTED);
139: }
140:
141: public void setRollover(boolean rollover) {
142: if (isEnabled() && isRollover() != rollover) {
143: toggleState(ROLLOVER);
144: }
145: }
146:
147: public boolean isRollover() {
148: return isStateSet(ROLLOVER);
149: }
150:
151: public void setPressed(boolean pressed) {
152: if (isEnabled() && isPressed() != pressed) {
153: toggleState(PRESSED);
154: if (!pressed && isArmed()) {
155: fireActionPerformed(new ActionEvent(this ,
156: ActionEvent.ACTION_PERFORMED, actionCommand,
157: System.currentTimeMillis(), 0));
158: }
159: }
160: }
161:
162: public boolean isPressed() {
163: return isStateSet(PRESSED);
164: }
165:
166: public void setEnabled(boolean enabled) {
167: if (isEnabled() != enabled) {
168: stateMask = isSelected() ? SELECTED : 0;
169: if (enabled) {
170: stateMask |= ENABLED;
171: }
172: fireStateChanged();
173: }
174: }
175:
176: public boolean isEnabled() {
177: return isStateSet(ENABLED);
178: }
179:
180: public void setArmed(boolean armed) {
181: if (isEnabled() && isArmed() != armed) {
182: toggleState(ARMED);
183: }
184: }
185:
186: public boolean isArmed() {
187: return isStateSet(ARMED);
188: }
189:
190: public void setMnemonic(int mnemonic) {
191: if (this .mnemonic != mnemonic) {
192: this .mnemonic = mnemonic;
193: fireStateChanged();
194: }
195: }
196:
197: public int getMnemonic() {
198: return mnemonic;
199: }
200:
201: protected void fireStateChanged() {
202: ChangeListener[] listeners = getChangeListeners();
203: if (listeners.length == 0) {
204: return;
205: }
206: if (changeEvent == null) {
207: changeEvent = new ChangeEvent(this );
208: }
209: for (int i = 0; i < listeners.length; i++) {
210: listeners[i].stateChanged(changeEvent);
211: }
212: }
213:
214: protected void fireItemStateChanged(ItemEvent event) {
215: ItemListener[] listeners = getItemListeners();
216: for (int i = 0; i < listeners.length; i++) {
217: listeners[i].itemStateChanged(event);
218: }
219: }
220:
221: protected void fireActionPerformed(ActionEvent event) {
222: ActionListener[] listeners = getActionListeners();
223: for (int i = 0; i < listeners.length; i++) {
224: listeners[i].actionPerformed(event);
225: }
226: }
227:
228: void toggleState(int stateFlag) {
229: // visibility is changed from private to default because according to
230: // HARMONY-4658 patch the method is needed by ToggleButtonModel
231: stateMask ^= stateFlag;
232: fireStateChanged();
233: }
234:
235: private boolean isStateSet(int stateFlag) {
236: return (stateMask & stateFlag) != 0;
237: }
238: }
|