1 7 8 package com.sun.java.swing.plaf.windows; 9 10 import java.awt.Component ; 11 import java.awt.Container ; 12 import java.awt.Event ; 13 import java.awt.KeyEventPostProcessor ; 14 import java.awt.Window ; 15 16 import java.awt.event.ActionEvent ; 17 import java.awt.event.KeyEvent ; 18 19 import javax.swing.AbstractAction ; 20 import javax.swing.ActionMap ; 21 import javax.swing.InputMap ; 22 import javax.swing.KeyStroke ; 23 import javax.swing.JComponent ; 24 import javax.swing.JLabel ; 25 import javax.swing.JRootPane ; 26 import javax.swing.SwingUtilities ; 27 import javax.swing.UIManager ; 28 import javax.swing.AbstractButton ; 29 import javax.swing.JFrame ; 30 import javax.swing.JMenu ; 31 import javax.swing.JMenuBar ; 32 import javax.swing.MenuElement ; 33 import javax.swing.MenuSelectionManager ; 34 35 import javax.swing.plaf.ActionMapUIResource ; 36 import javax.swing.plaf.ComponentUI ; 37 import javax.swing.plaf.InputMapUIResource ; 38 39 import javax.swing.plaf.basic.BasicRootPaneUI ; 40 import javax.swing.plaf.basic.ComboPopup ; 41 42 50 public class WindowsRootPaneUI extends BasicRootPaneUI { 51 52 private final static WindowsRootPaneUI windowsRootPaneUI = new WindowsRootPaneUI(); 53 static final AltProcessor altProcessor = new AltProcessor(); 54 55 public static ComponentUI createUI(JComponent c) { 56 return windowsRootPaneUI; 57 } 58 59 static class AltProcessor implements KeyEventPostProcessor { 60 static boolean altKeyPressed = false; 61 static boolean menuCanceledOnPress = false; 62 static JRootPane root = null; 63 static Window winAncestor = null; 64 65 void altPressed(KeyEvent ev) { 66 MenuSelectionManager msm = 67 MenuSelectionManager.defaultManager(); 68 MenuElement [] path = msm.getSelectedPath(); 69 if (path.length > 0 && ! (path[0] instanceof ComboPopup )) { 70 msm.clearSelectedPath(); 71 menuCanceledOnPress = true; 72 ev.consume(); 73 } else if(path.length > 0) { 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 mbar = root != null ? root.getJMenuBar() : null; 83 if(mbar == null && winAncestor instanceof JFrame ) { 84 mbar = ((JFrame )winAncestor).getJMenuBar(); 85 } 86 JMenu menu = mbar != null ? mbar.getMenu(0) : null; 87 if(menu != null) { 88 ev.consume(); 89 } 90 } 91 } 92 93 void altReleased(KeyEvent ev) { 94 if (menuCanceledOnPress) { 95 WindowsLookAndFeel.setMnemonicHidden(true); 96 WindowsUtils.repaintMnemonicsInWindow(winAncestor); 97 return; 98 } 99 100 MenuSelectionManager msm = 101 MenuSelectionManager.defaultManager(); 102 if (msm.getSelectedPath().length == 0) { 103 105 JMenuBar mbar = root != null ? root.getJMenuBar() : null; 106 if(mbar == null && winAncestor instanceof JFrame ) { 107 mbar = ((JFrame )winAncestor).getJMenuBar(); 108 } 109 JMenu menu = mbar != null ? mbar.getMenu(0) : null; 110 111 if (menu != null) { 112 MenuElement [] path = new MenuElement [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 ) { 122 WindowsLookAndFeel.setMnemonicHidden(true); 123 WindowsUtils.repaintMnemonicsInWindow(winAncestor); 124 } 125 } 126 127 } 128 129 public boolean postProcessKeyEvent(KeyEvent 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 msm = 146 MenuSelectionManager.defaultManager(); 147 MenuElement [] 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 |