01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Alexander T. Simbirtsev
19: * @version $Revision$
20: */package javax.swing.plaf.basic;
21:
22: import java.awt.Component;
23: import java.awt.KeyEventPostProcessor;
24: import java.awt.event.KeyEvent;
25:
26: import javax.swing.Action;
27: import javax.swing.JMenu;
28: import javax.swing.JMenuBar;
29: import javax.swing.JMenuItem;
30: import javax.swing.JRootPane;
31: import javax.swing.KeyStroke;
32: import javax.swing.SwingUtilities;
33:
34: import org.apache.harmony.x.swing.StringConstants;
35:
36: class AcceleratorsProcessor implements KeyEventPostProcessor {
37:
38: public boolean postProcessKeyEvent(final KeyEvent event) {
39: if (event.isConsumed()) {
40: return false;
41: }
42:
43: final Component source = (Component) event.getSource();
44: final KeyStroke ks = KeyStroke.getKeyStrokeForEvent(event);
45:
46: JRootPane parent = SwingUtilities.getRootPane(source);
47: while (parent != null) {
48: final JMenuBar menu = parent.getJMenuBar();
49: if (menu != null && processMenuAccelerator(menu, ks, event)) {
50: return true;
51: }
52: parent = (JRootPane) SwingUtilities.getAncestorOfClass(
53: JRootPane.class, parent);
54: }
55:
56: return false;
57: }
58:
59: private boolean processMenuAccelerator(final JMenuBar menu,
60: final KeyStroke ks, final KeyEvent event) {
61: final int menuCount = menu.getMenuCount();
62: for (int i = 0; i < menuCount; i++) {
63: final JMenu child = menu.getMenu(i);
64: if (child != null
65: && processMenuAccelerator(child, ks, event)) {
66: return true;
67: }
68: }
69: return false;
70: }
71:
72: private boolean processMenuAccelerator(final JMenu menu,
73: final KeyStroke ks, final KeyEvent event) {
74: final int menuCount = menu.getMenuComponentCount();
75: for (int i = 0; i < menuCount; i++) {
76: final Component child = menu.getMenuComponent(i);
77: if (child instanceof JMenu) {
78: if (processMenuAccelerator((JMenu) child, ks, event)) {
79: return true;
80: }
81: } else if (child instanceof JMenuItem) {
82: final JMenuItem menuItem = (JMenuItem) child;
83: if (ks.equals(menuItem.getAccelerator())) {
84: final Action action = menuItem.getActionMap().get(
85: StringConstants.MNEMONIC_ACTION);
86: if (action != null) {
87: SwingUtilities.notifyAction(action, ks, event,
88: menuItem, event.getModifiersEx());
89: return true;
90: }
91: }
92: }
93: }
94: return false;
95: }
96:
97: }
|