1 7 8 package javax.swing.plaf.basic; 9 10 import sun.swing.DefaultLookup; 11 import sun.swing.UIAction; 12 import javax.swing.*; 13 import javax.swing.event.*; 14 import java.awt.Color ; 15 import java.awt.Component ; 16 import java.awt.Container ; 17 import java.awt.Dimension ; 18 import java.awt.Graphics ; 19 import java.awt.Insets ; 20 import java.awt.Point ; 21 import java.awt.Rectangle ; 22 import java.awt.event.*; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 26 import javax.swing.border.*; 27 import javax.swing.plaf.*; 28 29 30 39 public class BasicMenuBarUI extends MenuBarUI { 40 protected JMenuBar menuBar = null; 41 protected ContainerListener containerListener; 42 protected ChangeListener changeListener; 43 private Handler handler; 44 45 public static ComponentUI createUI(JComponent x) { 46 return new BasicMenuBarUI (); 47 } 48 49 static void loadActionMap(LazyActionMap map) { 50 map.put(new Actions(Actions.TAKE_FOCUS)); 51 } 52 53 public void installUI(JComponent c) { 54 menuBar = (JMenuBar) c; 55 56 installDefaults(); 57 installListeners(); 58 installKeyboardActions(); 59 60 } 61 62 protected void installDefaults() { 63 if (menuBar.getLayout() == null || 64 menuBar.getLayout() instanceof UIResource) { 65 menuBar.setLayout(new DefaultMenuLayout (menuBar,BoxLayout.LINE_AXIS)); 66 } 67 68 LookAndFeel.installProperty(menuBar, "opaque", Boolean.TRUE); 69 LookAndFeel.installBorder(menuBar,"MenuBar.border"); 70 LookAndFeel.installColorsAndFont(menuBar, 71 "MenuBar.background", 72 "MenuBar.foreground", 73 "MenuBar.font"); 74 } 75 76 protected void installListeners() { 77 containerListener = createContainerListener(); 78 changeListener = createChangeListener(); 79 80 for (int i = 0; i < menuBar.getMenuCount(); i++) { 81 JMenu menu = menuBar.getMenu(i); 82 if (menu!=null) 83 menu.getModel().addChangeListener(changeListener); 84 } 85 menuBar.addContainerListener(containerListener); 86 } 87 88 protected void installKeyboardActions() { 89 InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 90 91 SwingUtilities.replaceUIInputMap(menuBar, 92 JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap); 93 94 LazyActionMap.installLazyActionMap(menuBar, BasicMenuBarUI .class, 95 "MenuBar.actionMap"); 96 } 97 98 InputMap getInputMap(int condition) { 99 if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) { 100 Object [] bindings = (Object [])DefaultLookup.get 101 (menuBar, this, "MenuBar.windowBindings"); 102 if (bindings != null) { 103 return LookAndFeel.makeComponentInputMap(menuBar, bindings); 104 } 105 } 106 return null; 107 } 108 109 public void uninstallUI(JComponent c) { 110 uninstallDefaults(); 111 uninstallListeners(); 112 uninstallKeyboardActions(); 113 114 menuBar = null; 115 } 116 117 protected void uninstallDefaults() { 118 if (menuBar!=null) { 119 LookAndFeel.uninstallBorder(menuBar); 120 } 121 } 122 123 protected void uninstallListeners() { 124 menuBar.removeContainerListener(containerListener); 125 126 for (int i = 0; i < menuBar.getMenuCount(); i++) { 127 JMenu menu = menuBar.getMenu(i); 128 if (menu !=null) 129 menu.getModel().removeChangeListener(changeListener); 130 } 131 132 containerListener = null; 133 changeListener = null; 134 handler = null; 135 } 136 137 protected void uninstallKeyboardActions() { 138 SwingUtilities.replaceUIInputMap(menuBar, JComponent. 139 WHEN_IN_FOCUSED_WINDOW, null); 140 SwingUtilities.replaceUIActionMap(menuBar, null); 141 } 142 143 protected ContainerListener createContainerListener() { 144 return getHandler(); 145 } 146 147 protected ChangeListener createChangeListener() { 148 return getHandler(); 149 } 150 151 private Handler getHandler() { 152 if (handler == null) { 153 handler = new Handler(); 154 } 155 return handler; 156 } 157 158 159 public Dimension getMinimumSize(JComponent c) { 160 return null; 161 } 162 163 public Dimension getMaximumSize(JComponent c) { 164 return null; 165 } 166 167 private class Handler implements ChangeListener , ContainerListener { 168 public void stateChanged(ChangeEvent e) { 172 int i,c; 173 for(i=0,c = menuBar.getMenuCount() ; i < c ; i++) { 174 JMenu menu = menuBar.getMenu(i); 175 if(menu !=null && menu.isSelected()) { 176 menuBar.getSelectionModel().setSelectedIndex(i); 177 break; 178 } 179 } 180 } 181 182 public void componentAdded(ContainerEvent e) { 186 Component c = e.getChild(); 187 if (c instanceof JMenu) 188 ((JMenu)c).getModel().addChangeListener(changeListener); 189 } 190 public void componentRemoved(ContainerEvent e) { 191 Component c = e.getChild(); 192 if (c instanceof JMenu) 193 ((JMenu)c).getModel().removeChangeListener(changeListener); 194 } 195 } 196 197 198 private static class Actions extends UIAction { 199 private static final String TAKE_FOCUS = "takeFocus"; 200 201 Actions(String key) { 202 super(key); 203 } 204 205 public void actionPerformed(ActionEvent e) { 206 JMenuBar menuBar = (JMenuBar)e.getSource(); 208 MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager(); 209 MenuElement me[]; 210 MenuElement subElements[]; 211 JMenu menu = menuBar.getMenu(0); 212 if (menu!=null) { 213 me = new MenuElement[3]; 214 me[0] = (MenuElement) menuBar; 215 me[1] = (MenuElement) menu; 216 me[2] = (MenuElement) menu.getPopupMenu(); 217 defaultManager.setSelectedPath(me); 218 } 219 } 220 } 221 } 222 223 224 | Popular Tags |