KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsRootPaneUI


1 /*
2  * @(#)WindowsRootPaneUI.java 1.15 04/04/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import java.awt.Component JavaDoc;
11 import java.awt.Container JavaDoc;
12 import java.awt.Event JavaDoc;
13 import java.awt.KeyEventPostProcessor JavaDoc;
14 import java.awt.Window JavaDoc;
15
16 import java.awt.event.ActionEvent JavaDoc;
17 import java.awt.event.KeyEvent JavaDoc;
18
19 import javax.swing.AbstractAction JavaDoc;
20 import javax.swing.ActionMap JavaDoc;
21 import javax.swing.InputMap JavaDoc;
22 import javax.swing.KeyStroke JavaDoc;
23 import javax.swing.JComponent JavaDoc;
24 import javax.swing.JLabel JavaDoc;
25 import javax.swing.JRootPane JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import javax.swing.UIManager JavaDoc;
28 import javax.swing.AbstractButton JavaDoc;
29 import javax.swing.JFrame JavaDoc;
30 import javax.swing.JMenu JavaDoc;
31 import javax.swing.JMenuBar JavaDoc;
32 import javax.swing.MenuElement JavaDoc;
33 import javax.swing.MenuSelectionManager JavaDoc;
34
35 import javax.swing.plaf.ActionMapUIResource JavaDoc;
36 import javax.swing.plaf.ComponentUI JavaDoc;
37 import javax.swing.plaf.InputMapUIResource JavaDoc;
38
39 import javax.swing.plaf.basic.BasicRootPaneUI JavaDoc;
40 import javax.swing.plaf.basic.ComboPopup JavaDoc;
41
42 /**
43  * Windows implementation of RootPaneUI, there is one shared between all
44  * JRootPane instances.
45  *
46  * @version 1.15 04/16/04
47  * @author Mark Davidson
48  * @since 1.4
49  */

50 public class WindowsRootPaneUI extends BasicRootPaneUI JavaDoc {
51
52     private final static WindowsRootPaneUI windowsRootPaneUI = new WindowsRootPaneUI();
53     static final AltProcessor altProcessor = new AltProcessor();
54
55     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
56     return windowsRootPaneUI;
57     }
58
59     static class AltProcessor implements KeyEventPostProcessor JavaDoc {
60         static boolean altKeyPressed = false;
61         static boolean menuCanceledOnPress = false;
62         static JRootPane JavaDoc root = null;
63         static Window JavaDoc winAncestor = null;
64
65         void altPressed(KeyEvent JavaDoc ev) {
66             MenuSelectionManager JavaDoc msm =
67                 MenuSelectionManager.defaultManager();
68             MenuElement JavaDoc[] path = msm.getSelectedPath();
69             if (path.length > 0 && ! (path[0] instanceof ComboPopup JavaDoc)) {
70                 msm.clearSelectedPath();
71                 menuCanceledOnPress = true;
72                 ev.consume();
73             } else if(path.length > 0) { // We are in ComboBox
74
menuCanceledOnPress = false;
75                 WindowsLookAndFeel.setMnemonicHidden(false);
76                 WindowsUtils.repaintMnemonicsInWindow(winAncestor);
77                 ev.consume();
78             } else {
79                 menuCanceledOnPress = false;
80             WindowsLookAndFeel.setMnemonicHidden(false);
81                 WindowsUtils.repaintMnemonicsInWindow(winAncestor);
82                 JMenuBar JavaDoc mbar = root != null ? root.getJMenuBar() : null;
83                 if(mbar == null && winAncestor instanceof JFrame JavaDoc) {
84                     mbar = ((JFrame JavaDoc)winAncestor).getJMenuBar();
85                 }
86                 JMenu JavaDoc menu = mbar != null ? mbar.getMenu(0) : null;
87                 if(menu != null) {
88                     ev.consume();
89                 }
90             }
91         }
92
93         void altReleased(KeyEvent JavaDoc ev) {
94             if (menuCanceledOnPress) {
95             WindowsLookAndFeel.setMnemonicHidden(true);
96                 WindowsUtils.repaintMnemonicsInWindow(winAncestor);
97                 return;
98             }
99
100             MenuSelectionManager JavaDoc msm =
101                 MenuSelectionManager.defaultManager();
102             if (msm.getSelectedPath().length == 0) {
103                 // if no menu is active, we try activating the menubar
104

105                 JMenuBar JavaDoc mbar = root != null ? root.getJMenuBar() : null;
106                 if(mbar == null && winAncestor instanceof JFrame JavaDoc) {
107                     mbar = ((JFrame JavaDoc)winAncestor).getJMenuBar();
108                 }
109                 JMenu JavaDoc menu = mbar != null ? mbar.getMenu(0) : null;
110
111                 if (menu != null) {
112                     MenuElement JavaDoc[] path = new MenuElement JavaDoc[2];
113                     path[0] = mbar;
114                     path[1] = menu;
115                     msm.setSelectedPath(path);
116                 } else if(!WindowsLookAndFeel.isMnemonicHidden()) {
117                     WindowsLookAndFeel.setMnemonicHidden(true);
118                     WindowsUtils.repaintMnemonicsInWindow(winAncestor);
119                 }
120             } else {
121                 if((msm.getSelectedPath())[0] instanceof ComboPopup JavaDoc) {
122                     WindowsLookAndFeel.setMnemonicHidden(true);
123                     WindowsUtils.repaintMnemonicsInWindow(winAncestor);
124                 }
125             }
126
127         }
128
129         public boolean postProcessKeyEvent(KeyEvent JavaDoc ev) {
130             if (ev.getKeyCode() == KeyEvent.VK_ALT) {
131                 root = SwingUtilities.getRootPane(ev.getComponent());
132                 winAncestor = (root == null ? null :
133                         SwingUtilities.getWindowAncestor(root));
134
135                 if (ev.getID() == KeyEvent.KEY_PRESSED) {
136                     if (!altKeyPressed) {
137                         altPressed(ev);
138                     }
139                     altKeyPressed = true;
140                     return true;
141                 } else if (ev.getID() == KeyEvent.KEY_RELEASED) {
142                     if (altKeyPressed) {
143                         altReleased(ev);
144                     } else {
145                         MenuSelectionManager JavaDoc msm =
146                             MenuSelectionManager.defaultManager();
147                         MenuElement JavaDoc[] path = msm.getSelectedPath();
148                         if (path.length <= 0) {
149                             WindowsLookAndFeel.setMnemonicHidden(true);
150                             WindowsUtils.repaintMnemonicsInWindow(winAncestor);
151                         }
152                     }
153                     altKeyPressed = false;
154                 }
155                 root = null;
156                 winAncestor = null;
157             } else {
158                 altKeyPressed = false;
159             }
160             return false;
161         }
162     }
163 }
164
Popular Tags