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.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.lang.reflect.Array;
023: import java.util.EventListener;
024: import javax.accessibility.Accessible;
025: import javax.accessibility.AccessibleAction;
026: import javax.accessibility.AccessibleContext;
027: import javax.accessibility.AccessibleRole;
028: import javax.accessibility.AccessibleValue;
029: import org.apache.harmony.awt.state.MenuItemState;
030:
031: public class MenuItem extends MenuComponent implements Accessible {
032: private static final long serialVersionUID = -21757335363267194L;
033:
034: private String label;
035:
036: private MenuShortcut shortcut;
037:
038: private long enabledEvents;
039:
040: private boolean enabled;
041:
042: private String actionCommand;
043:
044: private final AWTListenerList<ActionListener> actionListeners = new AWTListenerList<ActionListener>();
045:
046: protected class AccessibleAWTMenuItem extends
047: AccessibleAWTMenuComponent implements AccessibleAction,
048: AccessibleValue {
049: private static final long serialVersionUID = -217847831945965825L;
050:
051: @Override
052: public String getAccessibleName() {
053: String aName = super .getAccessibleName();
054: if (aName == null) {
055: aName = getLabel();
056: }
057: return aName;
058: }
059:
060: @Override
061: public AccessibleRole getAccessibleRole() {
062: return AccessibleRole.MENU_ITEM;
063: }
064:
065: @Override
066: public AccessibleAction getAccessibleAction() {
067: return this ;
068: }
069:
070: @Override
071: public AccessibleValue getAccessibleValue() {
072: return this ;
073: }
074:
075: public int getAccessibleActionCount() {
076: return 1;
077: }
078:
079: public boolean doAccessibleAction(int i) {
080: if (i != 0) {
081: return false;
082: }
083: itemSelected(System.currentTimeMillis(), 0);
084: return true;
085: }
086:
087: public String getAccessibleActionDescription(int i) {
088: if (i != 0) {
089: return null;
090: }
091: return "click"; //$NON-NLS-1$
092: }
093:
094: public Number getCurrentAccessibleValue() {
095: return new Integer(0);
096: }
097:
098: public Number getMaximumAccessibleValue() {
099: return new Integer(0);
100: }
101:
102: public Number getMinimumAccessibleValue() {
103: return new Integer(0);
104: }
105:
106: public boolean setCurrentAccessibleValue(Number n) {
107: return false;
108: }
109: }
110:
111: /**
112: * The internal menu item's state utilized by the visual theme
113: */
114: class State implements MenuItemState {
115: private Rectangle textBounds;
116:
117: private Rectangle shortcutBounds;
118:
119: private Rectangle itemBounds;
120:
121: public String getText() {
122: return MenuItem.this .getLabel();
123: }
124:
125: public Rectangle getTextBounds() {
126: return textBounds;
127: }
128:
129: public void setTextBounds(int x, int y, int w, int h) {
130: textBounds = new Rectangle(x, y, w, h);
131: }
132:
133: public boolean isMenu() {
134: return MenuItem.this instanceof Menu;
135: }
136:
137: public boolean isChecked() {
138: return (MenuItem.this instanceof CheckboxMenuItem)
139: && ((CheckboxMenuItem) MenuItem.this ).getState();
140: }
141:
142: public boolean isEnabled() {
143: return enabled;
144: }
145:
146: public String getShortcut() {
147: return (shortcut != null ? shortcut.toString() : ""); //$NON-NLS-1$
148: }
149:
150: public Rectangle getShortcutBounds() {
151: return shortcutBounds;
152: }
153:
154: public void setShortcutBounds(int x, int y, int w, int h) {
155: shortcutBounds = new Rectangle(x, y, w, h);
156: }
157:
158: public boolean isCheckBox() {
159: return (MenuItem.this instanceof CheckboxMenuItem);
160: }
161:
162: public boolean isSeparator() {
163: String label = MenuItem.this .getLabel();
164: return label != null && label.equals("-"); //$NON-NLS-1$
165: }
166:
167: public Dimension getMenuSize() {
168: if (MenuItem.this instanceof Menu) {
169: return ((Menu) MenuItem.this ).getSize();
170: }
171: return null;
172: }
173:
174: public Rectangle getItemBounds() {
175: return itemBounds;
176: }
177:
178: public void setItemBounds(int x, int y, int w, int h) {
179: itemBounds = new Rectangle(x, y, w, h);
180: }
181:
182: void reset() {
183: textBounds = null;
184: shortcutBounds = null;
185: itemBounds = null;
186: }
187: }
188:
189: final State itemState = new State();
190:
191: public MenuItem() throws HeadlessException {
192: this ("", null); //$NON-NLS-1$
193: toolkit.lockAWT();
194: try {
195: } finally {
196: toolkit.unlockAWT();
197: }
198: }
199:
200: public MenuItem(String label) throws HeadlessException {
201: this (label, null);
202: toolkit.lockAWT();
203: try {
204: } finally {
205: toolkit.unlockAWT();
206: }
207: }
208:
209: public MenuItem(String label, MenuShortcut shortcut)
210: throws HeadlessException {
211: toolkit.lockAWT();
212: try {
213: this .label = label;
214: this .shortcut = shortcut;
215: enabled = true;
216: enabledEvents = 0;
217: } finally {
218: toolkit.unlockAWT();
219: }
220: }
221:
222: /**
223: * @deprecated
224: */
225: @Deprecated
226: public void disable() {
227: toolkit.lockAWT();
228: try {
229: setEnabled(false);
230: } finally {
231: toolkit.unlockAWT();
232: }
233: }
234:
235: /**
236: * @deprecated
237: */
238: @Deprecated
239: public void enable(boolean b) {
240: toolkit.lockAWT();
241: try {
242: setEnabled(b);
243: } finally {
244: toolkit.unlockAWT();
245: }
246: }
247:
248: /**
249: * @deprecated
250: */
251: @Deprecated
252: public void enable() {
253: toolkit.lockAWT();
254: try {
255: setEnabled(true);
256: } finally {
257: toolkit.unlockAWT();
258: }
259: }
260:
261: @SuppressWarnings("unchecked")
262: public <T extends EventListener> T[] getListeners(
263: Class<T> listenerType) {
264: toolkit.lockAWT();
265: try {
266: if (ActionListener.class.isAssignableFrom(listenerType)) {
267: return (T[]) getActionListeners();
268: }
269: return (T[]) Array.newInstance(listenerType, 0);
270: } finally {
271: toolkit.unlockAWT();
272: }
273: }
274:
275: @Override
276: public String paramString() {
277: /*
278: * The format of paramString is based on 1.5 release behavior which can
279: * be revealed using the following code:
280: *
281: * MenuItem obj = new MenuItem("Label", new
282: * MenuShortcut(KeyEvent.VK_A)); obj.setActionCommand("Action");
283: * obj.disable(); System.out.println(obj.toString());
284: */
285: toolkit.lockAWT();
286: try {
287: String result = super .paramString() + ",label=" + label; //$NON-NLS-1$
288: if (!enabled) {
289: result += ",disabled"; //$NON-NLS-1$
290: }
291: if (actionCommand != null) {
292: result += ",command=" + actionCommand; //$NON-NLS-1$
293: }
294: if (shortcut != null) {
295: result += ",shortcut=" + shortcut.toString(); //$NON-NLS-1$
296: }
297: return result;
298: } finally {
299: toolkit.unlockAWT();
300: }
301: }
302:
303: public String getActionCommand() {
304: toolkit.lockAWT();
305: try {
306: return actionCommand != null ? actionCommand : label;
307: } finally {
308: toolkit.unlockAWT();
309: }
310: }
311:
312: public String getLabel() {
313: toolkit.lockAWT();
314: try {
315: return label;
316: } finally {
317: toolkit.unlockAWT();
318: }
319: }
320:
321: public void addNotify() {
322: toolkit.lockAWT();
323: try {
324: } finally {
325: toolkit.unlockAWT();
326: }
327: }
328:
329: protected final void disableEvents(long eventsToDisable) {
330: toolkit.lockAWT();
331: try {
332: enabledEvents &= ~eventsToDisable;
333: deprecatedEventHandler = false;
334: } finally {
335: toolkit.unlockAWT();
336: }
337: }
338:
339: protected final void enableEvents(long eventsToEnable) {
340: toolkit.lockAWT();
341: try {
342: enabledEvents |= eventsToEnable;
343: deprecatedEventHandler = false;
344: } finally {
345: toolkit.unlockAWT();
346: }
347: }
348:
349: @Override
350: public AccessibleContext getAccessibleContext() {
351: toolkit.lockAWT();
352: try {
353: return super .getAccessibleContext();
354: } finally {
355: toolkit.unlockAWT();
356: }
357: }
358:
359: public boolean isEnabled() {
360: toolkit.lockAWT();
361: try {
362: return enabled;
363: } finally {
364: toolkit.unlockAWT();
365: }
366: }
367:
368: @Override
369: protected void processEvent(AWTEvent event) {
370: if (toolkit.eventTypeLookup.getEventMask(event) == AWTEvent.ACTION_EVENT_MASK) {
371: processActionEvent((ActionEvent) event);
372: } else {
373: super .processEvent(event);
374: }
375: }
376:
377: public void setEnabled(boolean enabled) {
378: toolkit.lockAWT();
379: try {
380: this .enabled = enabled;
381: } finally {
382: toolkit.unlockAWT();
383: }
384: }
385:
386: public void addActionListener(ActionListener listener) {
387: actionListeners.addUserListener(listener);
388: }
389:
390: public ActionListener[] getActionListeners() {
391: return actionListeners.getUserListeners(new ActionListener[0]);
392: }
393:
394: protected void processActionEvent(ActionEvent event) {
395: for (ActionListener listener : actionListeners
396: .getUserListeners()) {
397: switch (event.getID()) {
398: case ActionEvent.ACTION_PERFORMED:
399: listener.actionPerformed(event);
400: break;
401: }
402: }
403: }
404:
405: public void removeActionListener(ActionListener listener) {
406: actionListeners.removeUserListener(listener);
407: }
408:
409: public void deleteShortcut() {
410: toolkit.lockAWT();
411: try {
412: shortcut = null;
413: } finally {
414: toolkit.unlockAWT();
415: }
416: }
417:
418: public MenuShortcut getShortcut() {
419: toolkit.lockAWT();
420: try {
421: return shortcut;
422: } finally {
423: toolkit.unlockAWT();
424: }
425: }
426:
427: public void setActionCommand(String command) {
428: toolkit.lockAWT();
429: try {
430: actionCommand = command;
431: } finally {
432: toolkit.unlockAWT();
433: }
434: }
435:
436: public void setLabel(String label) {
437: toolkit.lockAWT();
438: try {
439: this .label = label;
440: } finally {
441: toolkit.unlockAWT();
442: }
443: }
444:
445: public void setShortcut(MenuShortcut shortcut) {
446: toolkit.lockAWT();
447: try {
448: this .shortcut = shortcut;
449: } finally {
450: toolkit.unlockAWT();
451: }
452: }
453:
454: @Override
455: void itemSelected(long when, int modifiers) {
456: AWTEvent event = createEvent(when, modifiers);
457: toolkit.getSystemEventQueueImpl().postEvent(event);
458: super .itemSelected(when, modifiers);
459: }
460:
461: AWTEvent createEvent(long when, int modifiers) {
462: return new ActionEvent(this , ActionEvent.ACTION_PERFORMED,
463: getActionCommand(), when, modifiers);
464: }
465:
466: @Override
467: AccessibleContext createAccessibleContext() {
468: return new AccessibleAWTMenuItem();
469: }
470: }
|