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: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Component;
023: import java.awt.event.KeyEvent;
024: import java.awt.event.MouseEvent;
025:
026: import javax.accessibility.Accessible;
027: import javax.accessibility.AccessibleContext;
028: import javax.accessibility.AccessibleRole;
029: import javax.swing.event.ChangeEvent;
030: import javax.swing.event.ChangeListener;
031: import javax.swing.event.MenuDragMouseEvent;
032: import javax.swing.event.MenuDragMouseListener;
033: import javax.swing.event.MenuKeyEvent;
034: import javax.swing.event.MenuKeyListener;
035: import javax.swing.plaf.MenuItemUI;
036:
037: import org.apache.harmony.x.swing.StringConstants;
038:
039: import org.apache.harmony.x.swing.internal.nls.Messages;
040:
041: public class JMenuItem extends AbstractButton implements Accessible,
042: MenuElement {
043:
044: // TODO implement
045: protected class AccessibleJMenuItem extends
046: AccessibleAbstractButton implements ChangeListener {
047:
048: AccessibleJMenuItem() {
049: }
050:
051: public AccessibleRole getAccessibleRole() {
052: return AccessibleRole.MENU_ITEM;
053: }
054:
055: public void stateChanged(final ChangeEvent e) {
056: throw new UnsupportedOperationException(Messages
057: .getString("swing.27")); //$NON-NLS-1$
058: }
059: }
060:
061: private final static String UI_CLASS_ID = "MenuItemUI";
062:
063: private KeyStroke accelerator;
064: private int prevMouseEventID;
065:
066: public JMenuItem() {
067: this (null, null);
068: }
069:
070: public JMenuItem(final Icon icon) {
071: this (null, icon);
072: }
073:
074: public JMenuItem(final String text) {
075: this (text, null);
076: }
077:
078: public JMenuItem(final String text, final int mnemonic) {
079: setDefaultModelAndFocus();
080: setMnemonic(mnemonic);
081: init(text, null);
082: }
083:
084: public JMenuItem(final String text, final Icon icon) {
085: setDefaultModelAndFocus();
086: init(text, icon);
087: }
088:
089: public JMenuItem(final Action action) {
090: setDefaultModelAndFocus();
091: setAction(action);
092: init(getText(), getIcon());
093: }
094:
095: void configurePropertyFromAction(final Action action,
096: final Object propertyName) {
097: if (propertyName != null
098: && propertyName.equals(Action.ACCELERATOR_KEY)) {
099: setAccelerator((KeyStroke) action
100: .getValue(Action.ACCELERATOR_KEY));
101: }
102: super .configurePropertyFromAction(action, propertyName);
103: }
104:
105: public void setUI(final MenuItemUI ui) {
106: super .setUI(ui);
107: }
108:
109: public void updateUI() {
110: setUI((MenuItemUI) UIManager.getUI(this ));
111: }
112:
113: public String getUIClassID() {
114: return UI_CLASS_ID;
115: }
116:
117: public void setArmed(final boolean armed) {
118: model.setArmed(armed);
119: }
120:
121: public boolean isArmed() {
122: return model.isArmed();
123: }
124:
125: public void setAccelerator(final KeyStroke keyStroke) {
126: KeyStroke oldValue = accelerator;
127: accelerator = keyStroke;
128: firePropertyChange(StringConstants.ACCELERATOR_PROPERTY,
129: oldValue, accelerator);
130: }
131:
132: public KeyStroke getAccelerator() {
133: return accelerator;
134: }
135:
136: protected void fireMenuDragMouseEntered(
137: final MenuDragMouseEvent event) {
138: final MenuDragMouseListener[] listeners = getMenuDragMouseListeners();
139: for (int i = 0; i < listeners.length; i++) {
140: listeners[i].menuDragMouseEntered(event);
141: }
142: }
143:
144: protected void fireMenuDragMouseExited(
145: final MenuDragMouseEvent event) {
146: final MenuDragMouseListener[] listeners = getMenuDragMouseListeners();
147: for (int i = 0; i < listeners.length; i++) {
148: listeners[i].menuDragMouseExited(event);
149: }
150: }
151:
152: protected void fireMenuDragMouseDragged(
153: final MenuDragMouseEvent event) {
154: final MenuDragMouseListener[] listeners = getMenuDragMouseListeners();
155: for (int i = 0; i < listeners.length; i++) {
156: listeners[i].menuDragMouseDragged(event);
157: }
158: }
159:
160: protected void fireMenuDragMouseReleased(
161: final MenuDragMouseEvent event) {
162: final MenuDragMouseListener[] listeners = getMenuDragMouseListeners();
163: for (int i = 0; i < listeners.length; i++) {
164: listeners[i].menuDragMouseReleased(event);
165: }
166: }
167:
168: protected void fireMenuKeyPressed(final MenuKeyEvent event) {
169: final MenuKeyListener[] listeners = getMenuKeyListeners();
170: for (int i = 0; i < listeners.length; i++) {
171: listeners[i].menuKeyPressed(event);
172: }
173: }
174:
175: protected void fireMenuKeyReleased(final MenuKeyEvent event) {
176: final MenuKeyListener[] listeners = getMenuKeyListeners();
177: for (int i = 0; i < listeners.length; i++) {
178: listeners[i].menuKeyReleased(event);
179: }
180: }
181:
182: protected void fireMenuKeyTyped(final MenuKeyEvent event) {
183: final MenuKeyListener[] listeners = getMenuKeyListeners();
184: for (int i = 0; i < listeners.length; i++) {
185: listeners[i].menuKeyTyped(event);
186: }
187: }
188:
189: public void addMenuDragMouseListener(
190: final MenuDragMouseListener listener) {
191: listenerList.add(MenuDragMouseListener.class, listener);
192: }
193:
194: public void removeMenuDragMouseListener(
195: final MenuDragMouseListener listener) {
196: listenerList.remove(MenuDragMouseListener.class, listener);
197: }
198:
199: public MenuDragMouseListener[] getMenuDragMouseListeners() {
200: return (MenuDragMouseListener[]) getListeners(MenuDragMouseListener.class);
201: }
202:
203: public void addMenuKeyListener(final MenuKeyListener listener) {
204: listenerList.add(MenuKeyListener.class, listener);
205: }
206:
207: public void removeMenuKeyListener(final MenuKeyListener listener) {
208: listenerList.remove(MenuKeyListener.class, listener);
209: }
210:
211: public MenuKeyListener[] getMenuKeyListeners() {
212: return (MenuKeyListener[]) getListeners(MenuKeyListener.class);
213: }
214:
215: public AccessibleContext getAccessibleContext() {
216: return (accessibleContext == null) ? (accessibleContext = new AccessibleJMenuItem())
217: : accessibleContext;
218: }
219:
220: public Component getComponent() {
221: return this ;
222: }
223:
224: public MenuElement[] getSubElements() {
225: return MenuSelectionManager.EMPTY_PATH;
226: }
227:
228: public void menuSelectionChanged(final boolean isSelected) {
229: setArmed(isSelected);
230: }
231:
232: public void processMenuKeyEvent(final MenuKeyEvent e) {
233: if (e.getID() == KeyEvent.KEY_PRESSED) {
234: fireMenuKeyPressed(e);
235: } else if (e.getID() == KeyEvent.KEY_RELEASED) {
236: fireMenuKeyReleased(e);
237: } else if (e.getID() == KeyEvent.KEY_TYPED) {
238: fireMenuKeyTyped(e);
239: }
240: }
241:
242: public void processKeyEvent(final KeyEvent event,
243: final MenuElement[] path, final MenuSelectionManager manager) {
244: final MenuKeyEvent menuKeyEvent = new MenuKeyEvent(event
245: .getComponent(), event.getID(), event.getWhen(), event
246: .getModifiersEx(), event.getKeyCode(), event
247: .getKeyChar(), path, manager);
248: processMenuKeyEvent(menuKeyEvent);
249: if (menuKeyEvent.isConsumed()) {
250: event.consume();
251: return;
252: }
253: if (processMnemonics(event)) {
254: event.consume();
255: return;
256: }
257: }
258:
259: public void processMenuDragMouseEvent(final MenuDragMouseEvent e) {
260: if (e.getID() == MouseEvent.MOUSE_ENTERED) {
261: fireMenuDragMouseEntered(e);
262: } else if (e.getID() == MouseEvent.MOUSE_EXITED) {
263: fireMenuDragMouseExited(e);
264: } else if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
265: fireMenuDragMouseDragged(e);
266: } else if (e.getID() == MouseEvent.MOUSE_RELEASED
267: && prevMouseEventID == MouseEvent.MOUSE_DRAGGED) {
268:
269: fireMenuDragMouseReleased(e);
270: }
271: prevMouseEventID = e.getID();
272: }
273:
274: public void processMouseEvent(final MouseEvent event,
275: final MenuElement[] path, final MenuSelectionManager manager) {
276:
277: processMenuDragMouseEvent(new MenuDragMouseEvent(
278: (Component) event.getSource(), event.getID(), event
279: .getWhen(), event.getModifiers(), event.getX(),
280: event.getY(), event.getClickCount(), event
281: .isPopupTrigger(), path, manager));
282: }
283:
284: ButtonModel createDefaultModel() {
285: return new DefaultButtonModel();
286: }
287:
288: void setDefaultModelAndFocus() {
289: setModel(createDefaultModel());
290: setFocusPainted(false);
291: setFocusable(false);
292: setHorizontalAlignment(SwingConstants.LEADING);
293: }
294:
295: static JMenuItem createJMenuItem(final Action action) {
296: final JMenuItem result = new JMenuItem();
297: result.configurePropertiesFromAction(action);
298: result.setActionCommand(result.getText());
299: return result;
300: }
301:
302: boolean isMnemonicKeyStroke(final KeyStroke keyStroke) {
303: return (keyStroke.getKeyCode() == getMnemonic())
304: && !MenuSelectionManager.defaultManager().isPathEmpty();
305: }
306: }
|