author | weijun |
Thu, 11 Dec 2014 15:19:12 +0800 | |
changeset 28228 | be83f404724d |
parent 25859 | 3317bb8137f4 |
child 35650 | 15f1f0ff7790 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
23010
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, 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 javax.swing.plaf.basic; |
|
27 |
||
28 |
import sun.swing.DefaultLookup; |
|
29 |
import sun.swing.UIAction; |
|
30 |
import java.awt.*; |
|
31 |
import java.awt.event.*; |
|
32 |
import java.beans.*; |
|
33 |
import javax.swing.*; |
|
34 |
import javax.swing.event.*; |
|
35 |
import javax.swing.plaf.*; |
|
36 |
import javax.swing.border.*; |
|
37 |
import java.util.Arrays; |
|
38 |
import java.util.ArrayList; |
|
39 |
||
40 |
||
41 |
/** |
|
20169 | 42 |
* A default L&F implementation of MenuUI. This implementation |
2 | 43 |
* is a "combined" view/controller. |
44 |
* |
|
45 |
* @author Georges Saab |
|
46 |
* @author David Karlton |
|
47 |
* @author Arnaud Weber |
|
48 |
*/ |
|
49 |
public class BasicMenuUI extends BasicMenuItemUI |
|
50 |
{ |
|
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
51 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
52 |
* The instance of {@code ChangeListener}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
53 |
*/ |
2 | 54 |
protected ChangeListener changeListener; |
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
55 |
|
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
56 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
57 |
* The instance of {@code MenuListener}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
58 |
*/ |
2 | 59 |
protected MenuListener menuListener; |
60 |
||
61 |
private int lastMnemonic = 0; |
|
62 |
||
63 |
/** Uses as the parent of the windowInputMap when selected. */ |
|
64 |
private InputMap selectedWindowInputMap; |
|
65 |
||
66 |
/* diagnostic aids -- should be false for production builds. */ |
|
67 |
private static final boolean TRACE = false; // trace creates and disposes |
|
68 |
private static final boolean VERBOSE = false; // show reuse hits/misses |
|
69 |
private static final boolean DEBUG = false; // show bad params, misc. |
|
70 |
||
71 |
private static boolean crossMenuMnemonic = true; |
|
72 |
||
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
73 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
74 |
* Constructs a new instance of {@code BasicMenuUI}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
75 |
* |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
76 |
* @param x a component |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
77 |
* @return a new instance of {@code BasicMenuUI} |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
78 |
*/ |
2 | 79 |
public static ComponentUI createUI(JComponent x) { |
80 |
return new BasicMenuUI(); |
|
81 |
} |
|
82 |
||
83 |
static void loadActionMap(LazyActionMap map) { |
|
84 |
BasicMenuItemUI.loadActionMap(map); |
|
85 |
map.put(new Actions(Actions.SELECT, null, true)); |
|
86 |
} |
|
87 |
||
88 |
||
89 |
protected void installDefaults() { |
|
90 |
super.installDefaults(); |
|
91 |
updateDefaultBackgroundColor(); |
|
92 |
((JMenu)menuItem).setDelay(200); |
|
93 |
crossMenuMnemonic = UIManager.getBoolean("Menu.crossMenuMnemonic"); |
|
94 |
} |
|
95 |
||
96 |
protected String getPropertyPrefix() { |
|
97 |
return "Menu"; |
|
98 |
} |
|
99 |
||
100 |
protected void installListeners() { |
|
101 |
super.installListeners(); |
|
102 |
||
103 |
if (changeListener == null) |
|
104 |
changeListener = createChangeListener(menuItem); |
|
105 |
||
106 |
if (changeListener != null) |
|
107 |
menuItem.addChangeListener(changeListener); |
|
108 |
||
109 |
if (menuListener == null) |
|
110 |
menuListener = createMenuListener(menuItem); |
|
111 |
||
112 |
if (menuListener != null) |
|
113 |
((JMenu)menuItem).addMenuListener(menuListener); |
|
114 |
} |
|
115 |
||
116 |
protected void installKeyboardActions() { |
|
117 |
super.installKeyboardActions(); |
|
118 |
updateMnemonicBinding(); |
|
119 |
} |
|
120 |
||
121 |
void installLazyActionMap() { |
|
122 |
LazyActionMap.installLazyActionMap(menuItem, BasicMenuUI.class, |
|
123 |
getPropertyPrefix() + ".actionMap"); |
|
124 |
} |
|
125 |
||
126 |
void updateMnemonicBinding() { |
|
127 |
int mnemonic = menuItem.getModel().getMnemonic(); |
|
128 |
int[] shortcutKeys = (int[])DefaultLookup.get(menuItem, this, |
|
129 |
"Menu.shortcutKeys"); |
|
130 |
if (shortcutKeys == null) { |
|
131 |
shortcutKeys = new int[] {KeyEvent.ALT_MASK}; |
|
132 |
} |
|
133 |
if (mnemonic == lastMnemonic) { |
|
134 |
return; |
|
135 |
} |
|
136 |
InputMap windowInputMap = SwingUtilities.getUIInputMap( |
|
137 |
menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW); |
|
138 |
if (lastMnemonic != 0 && windowInputMap != null) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
139 |
for (int shortcutKey : shortcutKeys) { |
2 | 140 |
windowInputMap.remove(KeyStroke.getKeyStroke |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
141 |
(lastMnemonic, shortcutKey, false)); |
2 | 142 |
} |
143 |
} |
|
144 |
if (mnemonic != 0) { |
|
145 |
if (windowInputMap == null) { |
|
146 |
windowInputMap = createInputMap(JComponent. |
|
147 |
WHEN_IN_FOCUSED_WINDOW); |
|
148 |
SwingUtilities.replaceUIInputMap(menuItem, JComponent. |
|
149 |
WHEN_IN_FOCUSED_WINDOW, windowInputMap); |
|
150 |
} |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
151 |
for (int shortcutKey : shortcutKeys) { |
2 | 152 |
windowInputMap.put(KeyStroke.getKeyStroke(mnemonic, |
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
153 |
shortcutKey, false), "selectMenu"); |
2 | 154 |
} |
155 |
} |
|
156 |
lastMnemonic = mnemonic; |
|
157 |
} |
|
158 |
||
159 |
protected void uninstallKeyboardActions() { |
|
160 |
super.uninstallKeyboardActions(); |
|
161 |
lastMnemonic = 0; |
|
162 |
} |
|
163 |
||
164 |
protected MouseInputListener createMouseInputListener(JComponent c) { |
|
165 |
return getHandler(); |
|
166 |
} |
|
167 |
||
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
168 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
169 |
* Returns an instance of {@code MenuListener}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
170 |
* |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
171 |
* @param c a component |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
172 |
* @return an instance of {@code MenuListener} |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
173 |
*/ |
2 | 174 |
protected MenuListener createMenuListener(JComponent c) { |
175 |
return null; |
|
176 |
} |
|
177 |
||
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
178 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
179 |
* Returns an instance of {@code ChangeListener}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
180 |
* |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
181 |
* @param c a component |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
182 |
* @return an instance of {@code ChangeListener} |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
183 |
*/ |
2 | 184 |
protected ChangeListener createChangeListener(JComponent c) { |
185 |
return null; |
|
186 |
} |
|
187 |
||
188 |
protected PropertyChangeListener createPropertyChangeListener(JComponent c) { |
|
189 |
return getHandler(); |
|
190 |
} |
|
191 |
||
192 |
BasicMenuItemUI.Handler getHandler() { |
|
193 |
if (handler == null) { |
|
194 |
handler = new Handler(); |
|
195 |
} |
|
196 |
return handler; |
|
197 |
} |
|
198 |
||
199 |
protected void uninstallDefaults() { |
|
200 |
menuItem.setArmed(false); |
|
201 |
menuItem.setSelected(false); |
|
202 |
menuItem.resetKeyboardActions(); |
|
203 |
super.uninstallDefaults(); |
|
204 |
} |
|
205 |
||
206 |
protected void uninstallListeners() { |
|
207 |
super.uninstallListeners(); |
|
208 |
||
209 |
if (changeListener != null) |
|
210 |
menuItem.removeChangeListener(changeListener); |
|
211 |
||
212 |
if (menuListener != null) |
|
213 |
((JMenu)menuItem).removeMenuListener(menuListener); |
|
214 |
||
215 |
changeListener = null; |
|
216 |
menuListener = null; |
|
217 |
handler = null; |
|
218 |
} |
|
219 |
||
220 |
protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) { |
|
221 |
return getHandler(); |
|
222 |
} |
|
223 |
||
7246
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
224 |
protected MenuKeyListener createMenuKeyListener(JComponent c) { |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
225 |
return (MenuKeyListener)getHandler(); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
226 |
} |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
227 |
|
2 | 228 |
public Dimension getMaximumSize(JComponent c) { |
229 |
if (((JMenu)menuItem).isTopLevelMenu() == true) { |
|
230 |
Dimension d = c.getPreferredSize(); |
|
231 |
return new Dimension(d.width, Short.MAX_VALUE); |
|
232 |
} |
|
233 |
return null; |
|
234 |
} |
|
235 |
||
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
236 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
237 |
* Sets timer to the {@code menu}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
238 |
* |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
239 |
* @param menu an instance of {@code JMenu}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
240 |
*/ |
2 | 241 |
protected void setupPostTimer(JMenu menu) { |
242 |
Timer timer = new Timer(menu.getDelay(), new Actions( |
|
243 |
Actions.SELECT, menu,false)); |
|
244 |
timer.setRepeats(false); |
|
245 |
timer.start(); |
|
246 |
} |
|
247 |
||
248 |
private static void appendPath(MenuElement[] path, MenuElement elem) { |
|
249 |
MenuElement newPath[] = new MenuElement[path.length+1]; |
|
250 |
System.arraycopy(path, 0, newPath, 0, path.length); |
|
251 |
newPath[path.length] = elem; |
|
252 |
MenuSelectionManager.defaultManager().setSelectedPath(newPath); |
|
253 |
} |
|
254 |
||
255 |
private static class Actions extends UIAction { |
|
256 |
private static final String SELECT = "selectMenu"; |
|
257 |
||
258 |
// NOTE: This will be null if the action is registered in the |
|
259 |
// ActionMap. For the timer use it will be non-null. |
|
260 |
private JMenu menu; |
|
261 |
private boolean force=false; |
|
262 |
||
263 |
Actions(String key, JMenu menu, boolean shouldForce) { |
|
264 |
super(key); |
|
265 |
this.menu = menu; |
|
266 |
this.force = shouldForce; |
|
267 |
} |
|
268 |
||
269 |
private JMenu getMenu(ActionEvent e) { |
|
270 |
if (e.getSource() instanceof JMenu) { |
|
271 |
return (JMenu)e.getSource(); |
|
272 |
} |
|
273 |
return menu; |
|
274 |
} |
|
275 |
||
276 |
public void actionPerformed(ActionEvent e) { |
|
277 |
JMenu menu = getMenu(e); |
|
278 |
if (!crossMenuMnemonic) { |
|
279 |
JPopupMenu pm = BasicPopupMenuUI.getLastPopup(); |
|
280 |
if (pm != null && pm != menu.getParent()) { |
|
281 |
return; |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
final MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager(); |
|
286 |
if(force) { |
|
287 |
Container cnt = menu.getParent(); |
|
288 |
if(cnt != null && cnt instanceof JMenuBar) { |
|
289 |
MenuElement me[]; |
|
290 |
MenuElement subElements[]; |
|
291 |
||
292 |
subElements = menu.getPopupMenu().getSubElements(); |
|
293 |
if(subElements.length > 0) { |
|
294 |
me = new MenuElement[4]; |
|
295 |
me[0] = (MenuElement) cnt; |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
296 |
me[1] = menu; |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
297 |
me[2] = menu.getPopupMenu(); |
2 | 298 |
me[3] = subElements[0]; |
299 |
} else { |
|
300 |
me = new MenuElement[3]; |
|
301 |
me[0] = (MenuElement)cnt; |
|
302 |
me[1] = menu; |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
303 |
me[2] = menu.getPopupMenu(); |
2 | 304 |
} |
305 |
defaultManager.setSelectedPath(me); |
|
306 |
} |
|
307 |
} else { |
|
308 |
MenuElement path[] = defaultManager.getSelectedPath(); |
|
309 |
if(path.length > 0 && path[path.length-1] == menu) { |
|
310 |
appendPath(path, menu.getPopupMenu()); |
|
311 |
} |
|
312 |
} |
|
313 |
} |
|
314 |
||
315 |
public boolean isEnabled(Object c) { |
|
316 |
if (c instanceof JMenu) { |
|
317 |
return ((JMenu)c).isEnabled(); |
|
318 |
} |
|
319 |
return true; |
|
320 |
} |
|
321 |
} |
|
322 |
||
323 |
/* |
|
324 |
* Set the background color depending on whether this is a toplevel menu |
|
325 |
* in a menubar or a submenu of another menu. |
|
326 |
*/ |
|
327 |
private void updateDefaultBackgroundColor() { |
|
328 |
if (!UIManager.getBoolean("Menu.useMenuBarBackgroundForTopLevel")) { |
|
329 |
return; |
|
330 |
} |
|
331 |
JMenu menu = (JMenu)menuItem; |
|
332 |
if (menu.getBackground() instanceof UIResource) { |
|
333 |
if (menu.isTopLevelMenu()) { |
|
334 |
menu.setBackground(UIManager.getColor("MenuBar.background")); |
|
335 |
} else { |
|
336 |
menu.setBackground(UIManager.getColor(getPropertyPrefix() + ".background")); |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
||
341 |
/** |
|
342 |
* Instantiated and used by a menu item to handle the current menu selection |
|
343 |
* from mouse events. A MouseInputHandler processes and forwards all mouse events |
|
344 |
* to a shared instance of the MenuSelectionManager. |
|
345 |
* <p> |
|
346 |
* This class is protected so that it can be subclassed by other look and |
|
347 |
* feels to implement their own mouse handling behavior. All overridden |
|
348 |
* methods should call the parent methods so that the menu selection |
|
349 |
* is correct. |
|
350 |
* |
|
351 |
* @see javax.swing.MenuSelectionManager |
|
352 |
* @since 1.4 |
|
353 |
*/ |
|
354 |
protected class MouseInputHandler implements MouseInputListener { |
|
21278 | 355 |
// NOTE: This class exists only for backward compatibility. All |
2 | 356 |
// its functionality has been moved into Handler. If you need to add |
357 |
// new functionality add it to the Handler, but make sure this |
|
358 |
// class calls into the Handler. |
|
359 |
||
360 |
public void mouseClicked(MouseEvent e) { |
|
361 |
getHandler().mouseClicked(e); |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Invoked when the mouse has been clicked on the menu. This |
|
366 |
* method clears or sets the selection path of the |
|
367 |
* MenuSelectionManager. |
|
368 |
* |
|
369 |
* @param e the mouse event |
|
370 |
*/ |
|
371 |
public void mousePressed(MouseEvent e) { |
|
372 |
getHandler().mousePressed(e); |
|
373 |
} |
|
374 |
||
375 |
/** |
|
376 |
* Invoked when the mouse has been released on the menu. Delegates the |
|
377 |
* mouse event to the MenuSelectionManager. |
|
378 |
* |
|
379 |
* @param e the mouse event |
|
380 |
*/ |
|
381 |
public void mouseReleased(MouseEvent e) { |
|
382 |
getHandler().mouseReleased(e); |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Invoked when the cursor enters the menu. This method sets the selected |
|
387 |
* path for the MenuSelectionManager and handles the case |
|
388 |
* in which a menu item is used to pop up an additional menu, as in a |
|
389 |
* hierarchical menu system. |
|
390 |
* |
|
391 |
* @param e the mouse event; not used |
|
392 |
*/ |
|
393 |
public void mouseEntered(MouseEvent e) { |
|
394 |
getHandler().mouseEntered(e); |
|
395 |
} |
|
396 |
public void mouseExited(MouseEvent e) { |
|
397 |
getHandler().mouseExited(e); |
|
398 |
} |
|
399 |
||
400 |
/** |
|
401 |
* Invoked when a mouse button is pressed on the menu and then dragged. |
|
402 |
* Delegates the mouse event to the MenuSelectionManager. |
|
403 |
* |
|
404 |
* @param e the mouse event |
|
405 |
* @see java.awt.event.MouseMotionListener#mouseDragged |
|
406 |
*/ |
|
407 |
public void mouseDragged(MouseEvent e) { |
|
408 |
getHandler().mouseDragged(e); |
|
409 |
} |
|
410 |
||
411 |
public void mouseMoved(MouseEvent e) { |
|
412 |
getHandler().mouseMoved(e); |
|
413 |
} |
|
414 |
} |
|
415 |
||
416 |
/** |
|
417 |
* As of Java 2 platform 1.4, this previously undocumented class |
|
418 |
* is now obsolete. KeyBindings are now managed by the popup menu. |
|
419 |
*/ |
|
420 |
public class ChangeHandler implements ChangeListener { |
|
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
421 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
422 |
* The instance of {@code JMenu}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
423 |
*/ |
2 | 424 |
public JMenu menu; |
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
425 |
|
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
426 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
427 |
* The instance of {@code BasicMenuUI}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
428 |
*/ |
2 | 429 |
public BasicMenuUI ui; |
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
430 |
|
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
431 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
432 |
* {@code true} if an item of popup menu is selected. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
433 |
*/ |
2 | 434 |
public boolean isSelected = false; |
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
435 |
|
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
436 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
437 |
* The component that was focused. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
438 |
*/ |
2 | 439 |
public Component wasFocused; |
440 |
||
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
441 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
442 |
* Constructs a new instance of {@code ChangeHandler}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
443 |
* |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
444 |
* @param m an instance of {@code JMenu} |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
445 |
* @param ui an instance of {@code BasicMenuUI} |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
446 |
*/ |
2 | 447 |
public ChangeHandler(JMenu m, BasicMenuUI ui) { |
448 |
menu = m; |
|
449 |
this.ui = ui; |
|
450 |
} |
|
451 |
||
452 |
public void stateChanged(ChangeEvent e) { } |
|
453 |
} |
|
454 |
||
7246
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
455 |
private class Handler extends BasicMenuItemUI.Handler implements MenuKeyListener { |
2 | 456 |
// |
457 |
// PropertyChangeListener |
|
458 |
// |
|
459 |
public void propertyChange(PropertyChangeEvent e) { |
|
460 |
if (e.getPropertyName() == AbstractButton. |
|
461 |
MNEMONIC_CHANGED_PROPERTY) { |
|
462 |
updateMnemonicBinding(); |
|
463 |
} |
|
464 |
else { |
|
465 |
if (e.getPropertyName().equals("ancestor")) { |
|
466 |
updateDefaultBackgroundColor(); |
|
467 |
} |
|
468 |
super.propertyChange(e); |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
// |
|
473 |
// MouseInputListener |
|
474 |
// |
|
475 |
public void mouseClicked(MouseEvent e) { |
|
476 |
} |
|
477 |
||
478 |
/** |
|
479 |
* Invoked when the mouse has been clicked on the menu. This |
|
480 |
* method clears or sets the selection path of the |
|
481 |
* MenuSelectionManager. |
|
482 |
* |
|
483 |
* @param e the mouse event |
|
484 |
*/ |
|
485 |
public void mousePressed(MouseEvent e) { |
|
486 |
JMenu menu = (JMenu)menuItem; |
|
487 |
if (!menu.isEnabled()) |
|
488 |
return; |
|
489 |
||
490 |
MenuSelectionManager manager = |
|
491 |
MenuSelectionManager.defaultManager(); |
|
492 |
if(menu.isTopLevelMenu()) { |
|
493 |
if(menu.isSelected() && menu.getPopupMenu().isShowing()) { |
|
494 |
manager.clearSelectedPath(); |
|
495 |
} else { |
|
496 |
Container cnt = menu.getParent(); |
|
497 |
if(cnt != null && cnt instanceof JMenuBar) { |
|
498 |
MenuElement me[] = new MenuElement[2]; |
|
499 |
me[0]=(MenuElement)cnt; |
|
500 |
me[1]=menu; |
|
501 |
manager.setSelectedPath(me); |
|
502 |
} |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
MenuElement selectedPath[] = manager.getSelectedPath(); |
|
507 |
if (selectedPath.length > 0 && |
|
508 |
selectedPath[selectedPath.length-1] != menu.getPopupMenu()) { |
|
509 |
||
510 |
if(menu.isTopLevelMenu() || |
|
511 |
menu.getDelay() == 0) { |
|
512 |
appendPath(selectedPath, menu.getPopupMenu()); |
|
513 |
} else { |
|
514 |
setupPostTimer(menu); |
|
515 |
} |
|
516 |
} |
|
517 |
} |
|
518 |
||
519 |
/** |
|
520 |
* Invoked when the mouse has been released on the menu. Delegates the |
|
521 |
* mouse event to the MenuSelectionManager. |
|
522 |
* |
|
523 |
* @param e the mouse event |
|
524 |
*/ |
|
525 |
public void mouseReleased(MouseEvent e) { |
|
526 |
JMenu menu = (JMenu)menuItem; |
|
527 |
if (!menu.isEnabled()) |
|
528 |
return; |
|
529 |
MenuSelectionManager manager = |
|
530 |
MenuSelectionManager.defaultManager(); |
|
531 |
manager.processMouseEvent(e); |
|
532 |
if (!e.isConsumed()) |
|
533 |
manager.clearSelectedPath(); |
|
534 |
} |
|
535 |
||
536 |
/** |
|
537 |
* Invoked when the cursor enters the menu. This method sets the selected |
|
538 |
* path for the MenuSelectionManager and handles the case |
|
539 |
* in which a menu item is used to pop up an additional menu, as in a |
|
540 |
* hierarchical menu system. |
|
541 |
* |
|
542 |
* @param e the mouse event; not used |
|
543 |
*/ |
|
544 |
public void mouseEntered(MouseEvent e) { |
|
545 |
JMenu menu = (JMenu)menuItem; |
|
546 |
// only disable the menu highlighting if it's disabled and the property isn't |
|
547 |
// true. This allows disabled rollovers to work in WinL&F |
|
548 |
if (!menu.isEnabled() && !UIManager.getBoolean("MenuItem.disabledAreNavigable")) { |
|
549 |
return; |
|
550 |
} |
|
551 |
||
552 |
MenuSelectionManager manager = |
|
553 |
MenuSelectionManager.defaultManager(); |
|
554 |
MenuElement selectedPath[] = manager.getSelectedPath(); |
|
555 |
if (!menu.isTopLevelMenu()) { |
|
556 |
if(!(selectedPath.length > 0 && |
|
557 |
selectedPath[selectedPath.length-1] == |
|
558 |
menu.getPopupMenu())) { |
|
559 |
if(menu.getDelay() == 0) { |
|
560 |
appendPath(getPath(), menu.getPopupMenu()); |
|
561 |
} else { |
|
562 |
manager.setSelectedPath(getPath()); |
|
563 |
setupPostTimer(menu); |
|
564 |
} |
|
565 |
} |
|
566 |
} else { |
|
567 |
if(selectedPath.length > 0 && |
|
568 |
selectedPath[0] == menu.getParent()) { |
|
569 |
MenuElement newPath[] = new MenuElement[3]; |
|
570 |
// A top level menu's parent is by definition |
|
571 |
// a JMenuBar |
|
572 |
newPath[0] = (MenuElement)menu.getParent(); |
|
573 |
newPath[1] = menu; |
|
574 |
if (BasicPopupMenuUI.getLastPopup() != null) { |
|
575 |
newPath[2] = menu.getPopupMenu(); |
|
576 |
} |
|
577 |
manager.setSelectedPath(newPath); |
|
578 |
} |
|
579 |
} |
|
580 |
} |
|
581 |
public void mouseExited(MouseEvent e) { |
|
582 |
} |
|
583 |
||
584 |
/** |
|
585 |
* Invoked when a mouse button is pressed on the menu and then dragged. |
|
586 |
* Delegates the mouse event to the MenuSelectionManager. |
|
587 |
* |
|
588 |
* @param e the mouse event |
|
589 |
* @see java.awt.event.MouseMotionListener#mouseDragged |
|
590 |
*/ |
|
591 |
public void mouseDragged(MouseEvent e) { |
|
592 |
JMenu menu = (JMenu)menuItem; |
|
593 |
if (!menu.isEnabled()) |
|
594 |
return; |
|
595 |
MenuSelectionManager.defaultManager().processMouseEvent(e); |
|
596 |
} |
|
597 |
public void mouseMoved(MouseEvent e) { |
|
598 |
} |
|
599 |
||
600 |
||
601 |
// |
|
602 |
// MenuDragHandler |
|
603 |
// |
|
604 |
public void menuDragMouseEntered(MenuDragMouseEvent e) {} |
|
605 |
public void menuDragMouseDragged(MenuDragMouseEvent e) { |
|
606 |
if (menuItem.isEnabled() == false) |
|
607 |
return; |
|
608 |
||
609 |
MenuSelectionManager manager = e.getMenuSelectionManager(); |
|
610 |
MenuElement path[] = e.getPath(); |
|
611 |
||
612 |
Point p = e.getPoint(); |
|
613 |
if(p.x >= 0 && p.x < menuItem.getWidth() && |
|
614 |
p.y >= 0 && p.y < menuItem.getHeight()) { |
|
615 |
JMenu menu = (JMenu)menuItem; |
|
616 |
MenuElement selectedPath[] = manager.getSelectedPath(); |
|
617 |
if(!(selectedPath.length > 0 && |
|
618 |
selectedPath[selectedPath.length-1] == |
|
619 |
menu.getPopupMenu())) { |
|
620 |
if(menu.isTopLevelMenu() || |
|
621 |
menu.getDelay() == 0 || |
|
622 |
e.getID() == MouseEvent.MOUSE_DRAGGED) { |
|
623 |
appendPath(path, menu.getPopupMenu()); |
|
624 |
} else { |
|
625 |
manager.setSelectedPath(path); |
|
626 |
setupPostTimer(menu); |
|
627 |
} |
|
628 |
} |
|
629 |
} else if(e.getID() == MouseEvent.MOUSE_RELEASED) { |
|
630 |
Component comp = manager.componentForPoint(e.getComponent(), e.getPoint()); |
|
631 |
if (comp == null) |
|
632 |
manager.clearSelectedPath(); |
|
633 |
} |
|
634 |
||
635 |
} |
|
636 |
public void menuDragMouseExited(MenuDragMouseEvent e) {} |
|
637 |
public void menuDragMouseReleased(MenuDragMouseEvent e) {} |
|
7246
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
638 |
|
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
639 |
// |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
640 |
// MenuKeyListener |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
641 |
// |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
642 |
/** |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
643 |
* Open the Menu |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
644 |
*/ |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
645 |
public void menuKeyTyped(MenuKeyEvent e) { |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
646 |
if (!crossMenuMnemonic && BasicPopupMenuUI.getLastPopup() != null) { |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
647 |
// when crossMenuMnemonic is not set, we don't open a toplevel |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
648 |
// menu if another toplevel menu is already open |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
649 |
return; |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
650 |
} |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
651 |
|
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
652 |
if (BasicPopupMenuUI.getPopups().size() != 0) { |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
653 |
//Fix 6939261: to return in case not on the main menu |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
654 |
//and has a pop-up. |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
655 |
//after return code will be handled in BasicPopupMenuUI.java |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
656 |
return; |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
657 |
} |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
658 |
|
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
659 |
char key = Character.toLowerCase((char)menuItem.getMnemonic()); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
660 |
MenuElement path[] = e.getPath(); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
661 |
if (key == Character.toLowerCase(e.getKeyChar())) { |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
662 |
JPopupMenu popupMenu = ((JMenu)menuItem).getPopupMenu(); |
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
23010
diff
changeset
|
663 |
ArrayList<MenuElement> newList = new ArrayList<>(Arrays.asList(path)); |
7246
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
664 |
newList.add(popupMenu); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
665 |
MenuElement subs[] = popupMenu.getSubElements(); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
666 |
MenuElement sub = |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
667 |
BasicPopupMenuUI.findEnabledChild(subs, -1, true); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
668 |
if(sub != null) { |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
669 |
newList.add(sub); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
670 |
} |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
671 |
MenuSelectionManager manager = e.getMenuSelectionManager(); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
672 |
MenuElement newPath[] = new MenuElement[0];; |
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
23010
diff
changeset
|
673 |
newPath = newList.toArray(newPath); |
7246
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
674 |
manager.setSelectedPath(newPath); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
675 |
e.consume(); |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
676 |
} |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
677 |
} |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
678 |
|
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
679 |
public void menuKeyPressed(MenuKeyEvent e) {} |
97ce36d18925
6939261: Since 1.6.0_18 JMenus at JMenuBar are not selectable by their Mnemonic key anymore
vikram
parents:
5506
diff
changeset
|
680 |
public void menuKeyReleased(MenuKeyEvent e) {} |
2 | 681 |
} |
682 |
} |