author | ant |
Fri, 31 Aug 2012 16:31:29 +0400 | |
changeset 13652 | 42544e68dc39 |
parent 5506 | 202f599c92aa |
child 14310 | 708173457cc0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.java.swing.plaf.windows; |
|
27 |
||
28 |
import java.awt.Component; |
|
29 |
import java.awt.Container; |
|
30 |
import java.awt.Event; |
|
31 |
import java.awt.KeyEventPostProcessor; |
|
32 |
import java.awt.Window; |
|
13652
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
33 |
import java.awt.Toolkit; |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
34 |
import sun.awt.SunToolkit; |
2 | 35 |
|
36 |
import java.awt.event.ActionEvent; |
|
37 |
import java.awt.event.KeyEvent; |
|
38 |
||
39 |
import javax.swing.AbstractAction; |
|
40 |
import javax.swing.ActionMap; |
|
41 |
import javax.swing.InputMap; |
|
42 |
import javax.swing.KeyStroke; |
|
43 |
import javax.swing.JComponent; |
|
44 |
import javax.swing.JLabel; |
|
45 |
import javax.swing.JRootPane; |
|
46 |
import javax.swing.SwingUtilities; |
|
47 |
import javax.swing.UIManager; |
|
48 |
import javax.swing.AbstractButton; |
|
49 |
import javax.swing.JFrame; |
|
50 |
import javax.swing.JMenu; |
|
51 |
import javax.swing.JMenuBar; |
|
52 |
import javax.swing.MenuElement; |
|
53 |
import javax.swing.MenuSelectionManager; |
|
54 |
||
55 |
import javax.swing.plaf.ActionMapUIResource; |
|
56 |
import javax.swing.plaf.ComponentUI; |
|
57 |
import javax.swing.plaf.InputMapUIResource; |
|
58 |
||
59 |
import javax.swing.plaf.basic.BasicRootPaneUI; |
|
60 |
import javax.swing.plaf.basic.ComboPopup; |
|
61 |
||
62 |
/** |
|
63 |
* Windows implementation of RootPaneUI, there is one shared between all |
|
64 |
* JRootPane instances. |
|
65 |
* |
|
66 |
* @author Mark Davidson |
|
67 |
* @since 1.4 |
|
68 |
*/ |
|
69 |
public class WindowsRootPaneUI extends BasicRootPaneUI { |
|
70 |
||
71 |
private final static WindowsRootPaneUI windowsRootPaneUI = new WindowsRootPaneUI(); |
|
72 |
static final AltProcessor altProcessor = new AltProcessor(); |
|
73 |
||
74 |
public static ComponentUI createUI(JComponent c) { |
|
75 |
return windowsRootPaneUI; |
|
76 |
} |
|
77 |
||
78 |
static class AltProcessor implements KeyEventPostProcessor { |
|
79 |
static boolean altKeyPressed = false; |
|
80 |
static boolean menuCanceledOnPress = false; |
|
81 |
static JRootPane root = null; |
|
82 |
static Window winAncestor = null; |
|
83 |
||
84 |
void altPressed(KeyEvent ev) { |
|
85 |
MenuSelectionManager msm = |
|
86 |
MenuSelectionManager.defaultManager(); |
|
87 |
MenuElement[] path = msm.getSelectedPath(); |
|
88 |
if (path.length > 0 && ! (path[0] instanceof ComboPopup)) { |
|
89 |
msm.clearSelectedPath(); |
|
90 |
menuCanceledOnPress = true; |
|
91 |
ev.consume(); |
|
92 |
} else if(path.length > 0) { // We are in ComboBox |
|
93 |
menuCanceledOnPress = false; |
|
94 |
WindowsLookAndFeel.setMnemonicHidden(false); |
|
95 |
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor); |
|
96 |
ev.consume(); |
|
97 |
} else { |
|
98 |
menuCanceledOnPress = false; |
|
99 |
WindowsLookAndFeel.setMnemonicHidden(false); |
|
100 |
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor); |
|
101 |
JMenuBar mbar = root != null ? root.getJMenuBar() : null; |
|
102 |
if(mbar == null && winAncestor instanceof JFrame) { |
|
103 |
mbar = ((JFrame)winAncestor).getJMenuBar(); |
|
104 |
} |
|
105 |
JMenu menu = mbar != null ? mbar.getMenu(0) : null; |
|
106 |
if(menu != null) { |
|
107 |
ev.consume(); |
|
108 |
} |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
void altReleased(KeyEvent ev) { |
|
113 |
if (menuCanceledOnPress) { |
|
114 |
WindowsLookAndFeel.setMnemonicHidden(true); |
|
115 |
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor); |
|
116 |
return; |
|
117 |
} |
|
118 |
||
119 |
MenuSelectionManager msm = |
|
120 |
MenuSelectionManager.defaultManager(); |
|
121 |
if (msm.getSelectedPath().length == 0) { |
|
122 |
// if no menu is active, we try activating the menubar |
|
123 |
||
124 |
JMenuBar mbar = root != null ? root.getJMenuBar() : null; |
|
125 |
if(mbar == null && winAncestor instanceof JFrame) { |
|
126 |
mbar = ((JFrame)winAncestor).getJMenuBar(); |
|
127 |
} |
|
128 |
JMenu menu = mbar != null ? mbar.getMenu(0) : null; |
|
129 |
||
13652
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
130 |
// It might happen that the altRelease event is processed |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
131 |
// with a reasonable delay since it has been generated. |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
132 |
// Here we check the last deactivation time of the containing |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
133 |
// window. If this time appears to be greater than the altRelease |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
134 |
// event time the event is skipped to avoid unexpected menu |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
135 |
// activation. See 7121442. |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
136 |
boolean skip = false; |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
137 |
Toolkit tk = Toolkit.getDefaultToolkit(); |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
138 |
if (tk instanceof SunToolkit) { |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
139 |
skip = ev.getWhen() <= ((SunToolkit)tk).getWindowDeactivationTime(winAncestor); |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
140 |
} |
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
141 |
|
42544e68dc39
6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents:
5506
diff
changeset
|
142 |
if (menu != null && !skip) { |
2 | 143 |
MenuElement[] path = new MenuElement[2]; |
144 |
path[0] = mbar; |
|
145 |
path[1] = menu; |
|
146 |
msm.setSelectedPath(path); |
|
147 |
} else if(!WindowsLookAndFeel.isMnemonicHidden()) { |
|
148 |
WindowsLookAndFeel.setMnemonicHidden(true); |
|
149 |
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor); |
|
150 |
} |
|
151 |
} else { |
|
152 |
if((msm.getSelectedPath())[0] instanceof ComboPopup) { |
|
153 |
WindowsLookAndFeel.setMnemonicHidden(true); |
|
154 |
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor); |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
} |
|
159 |
||
160 |
public boolean postProcessKeyEvent(KeyEvent ev) { |
|
161 |
if(ev.isConsumed()) { |
|
162 |
// do not manage consumed event |
|
163 |
return false; |
|
164 |
} |
|
165 |
if (ev.getKeyCode() == KeyEvent.VK_ALT) { |
|
166 |
root = SwingUtilities.getRootPane(ev.getComponent()); |
|
167 |
winAncestor = (root == null ? null : |
|
168 |
SwingUtilities.getWindowAncestor(root)); |
|
169 |
||
170 |
if (ev.getID() == KeyEvent.KEY_PRESSED) { |
|
171 |
if (!altKeyPressed) { |
|
172 |
altPressed(ev); |
|
173 |
} |
|
174 |
altKeyPressed = true; |
|
175 |
return true; |
|
176 |
} else if (ev.getID() == KeyEvent.KEY_RELEASED) { |
|
177 |
if (altKeyPressed) { |
|
178 |
altReleased(ev); |
|
179 |
} else { |
|
180 |
MenuSelectionManager msm = |
|
181 |
MenuSelectionManager.defaultManager(); |
|
182 |
MenuElement[] path = msm.getSelectedPath(); |
|
183 |
if (path.length <= 0) { |
|
184 |
WindowsLookAndFeel.setMnemonicHidden(true); |
|
185 |
WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor); |
|
186 |
} |
|
187 |
} |
|
188 |
altKeyPressed = false; |
|
189 |
} |
|
190 |
root = null; |
|
191 |
winAncestor = null; |
|
192 |
} else { |
|
193 |
altKeyPressed = false; |
|
194 |
} |
|
195 |
return false; |
|
196 |
} |
|
197 |
} |
|
198 |
} |