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 java.awt;
019:
020: import java.awt.event.ItemEvent;
021: import java.awt.event.ItemListener;
022: import java.util.EventListener;
023: import javax.accessibility.Accessible;
024: import javax.accessibility.AccessibleAction;
025: import javax.accessibility.AccessibleContext;
026: import javax.accessibility.AccessibleRole;
027: import javax.accessibility.AccessibleValue;
028:
029: public class CheckboxMenuItem extends MenuItem implements
030: ItemSelectable, Accessible {
031: private static final long serialVersionUID = 6190621106981774043L;
032:
033: private final AWTListenerList<ItemListener> itemListeners = new AWTListenerList<ItemListener>();
034:
035: private boolean checked;
036:
037: protected class AccessibleAWTCheckboxMenuItem extends
038: AccessibleAWTMenuItem implements AccessibleAction,
039: AccessibleValue {
040: private static final long serialVersionUID = -1122642964303476L;
041:
042: @Override
043: public boolean doAccessibleAction(int i) {
044: return false; // do nothing
045: }
046:
047: @Override
048: public AccessibleAction getAccessibleAction() {
049: return this ;
050: }
051:
052: @Override
053: public int getAccessibleActionCount() {
054: return 0; // no accessible actions
055: }
056:
057: @Override
058: public String getAccessibleActionDescription(int i) {
059: return null;
060: }
061:
062: @Override
063: public AccessibleRole getAccessibleRole() {
064: return AccessibleRole.CHECK_BOX;
065: }
066:
067: @Override
068: public AccessibleValue getAccessibleValue() {
069: return this ;
070: }
071:
072: @Override
073: public Number getCurrentAccessibleValue() {
074: return null;
075: }
076:
077: @Override
078: public Number getMaximumAccessibleValue() {
079: return null;
080: }
081:
082: @Override
083: public Number getMinimumAccessibleValue() {
084: return null;
085: }
086:
087: @Override
088: public boolean setCurrentAccessibleValue(Number n) {
089: return false;
090: }
091: }
092:
093: public CheckboxMenuItem() throws HeadlessException {
094: super ();
095: }
096:
097: public CheckboxMenuItem(String label) throws HeadlessException {
098: this (label, false);
099: }
100:
101: public CheckboxMenuItem(String label, boolean state)
102: throws HeadlessException {
103: super (label);
104: toolkit.lockAWT();
105: try {
106: checked = state;
107: } finally {
108: toolkit.unlockAWT();
109: }
110: }
111:
112: public boolean getState() {
113: toolkit.lockAWT();
114: try {
115: return checked;
116: } finally {
117: toolkit.unlockAWT();
118: }
119: }
120:
121: @Override
122: public void addNotify() {
123: toolkit.lockAWT();
124: try {
125: super .addNotify();
126: } finally {
127: toolkit.unlockAWT();
128: }
129: }
130:
131: @Override
132: public AccessibleContext getAccessibleContext() {
133: toolkit.lockAWT();
134: try {
135: return super .getAccessibleContext();
136: } finally {
137: toolkit.unlockAWT();
138: }
139: }
140:
141: @Override
142: public String paramString() {
143: /*
144: * The format of paramString is based on 1.5 release behavior which can
145: * be revealed using the following code:
146: *
147: * CheckboxMenuItem obj = new CheckboxMenuItem("Label", true);
148: * System.out.println(obj.toString());
149: */
150: toolkit.lockAWT();
151: try {
152: return super .paramString() + (checked ? ",checked" : ""); //$NON-NLS-1$ //$NON-NLS-2$
153: } finally {
154: toolkit.unlockAWT();
155: }
156: }
157:
158: public Object[] getSelectedObjects() {
159: toolkit.lockAWT();
160: try {
161: return checked ? new Object[] { getLabel() } : null;
162: } finally {
163: toolkit.unlockAWT();
164: }
165: }
166:
167: public void setState(boolean b) {
168: toolkit.lockAWT();
169: try {
170: checked = b;
171: } finally {
172: toolkit.unlockAWT();
173: }
174: }
175:
176: @SuppressWarnings("unchecked")
177: @Override
178: public <T extends EventListener> T[] getListeners(
179: Class<T> listenerType) {
180: if (ItemListener.class.isAssignableFrom(listenerType)) {
181: return (T[]) getItemListeners();
182: }
183: return super .getListeners(listenerType);
184: }
185:
186: public void addItemListener(ItemListener l) {
187: itemListeners.addUserListener(l);
188: }
189:
190: public void removeItemListener(ItemListener l) {
191: itemListeners.removeUserListener(l);
192: }
193:
194: public ItemListener[] getItemListeners() {
195: return itemListeners.getUserListeners(new ItemListener[0]);
196: }
197:
198: @Override
199: protected void processEvent(AWTEvent e) {
200: if (toolkit.eventTypeLookup.getEventMask(e) == AWTEvent.ITEM_EVENT_MASK) {
201: processItemEvent((ItemEvent) e);
202: } else {
203: super .processEvent(e);
204: }
205: }
206:
207: protected void processItemEvent(ItemEvent e) {
208: for (ItemListener listener : itemListeners.getUserListeners()) {
209: switch (e.getID()) {
210: case ItemEvent.ITEM_STATE_CHANGED:
211: listener.itemStateChanged(e);
212: break;
213: }
214: }
215: }
216:
217: @Override
218: AccessibleContext createAccessibleContext() {
219: return new AccessibleAWTCheckboxMenuItem();
220: }
221:
222: @Override
223: void itemSelected(long when, int modifiers) {
224: checked = !checked;
225: super .itemSelected(when, modifiers);
226: }
227:
228: @Override
229: AWTEvent createEvent(long when, int modifiers) {
230: int state = checked ? ItemEvent.SELECTED : ItemEvent.DESELECTED;
231: return new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
232: getLabel(), state);
233: }
234: }
|