1 7 8 package com.sun.java.swing.plaf.windows; 9 10 import javax.swing.plaf.basic.*; 11 import javax.swing.*; 12 import javax.swing.plaf.ActionMapUIResource ; 13 import javax.swing.plaf.ComponentUI ; 14 import java.awt.event.ActionEvent ; 15 import java.awt.event.HierarchyEvent ; 16 import java.awt.event.HierarchyListener ; 17 import java.awt.event.WindowAdapter ; 18 import java.awt.event.WindowEvent ; 19 import java.awt.event.WindowListener ; 20 import java.awt.event.WindowStateListener ; 21 22 import java.awt.*; 23 24 import com.sun.java.swing.plaf.windows.TMSchema.*; 25 import com.sun.java.swing.plaf.windows.XPStyle.*; 26 27 37 public class WindowsMenuBarUI extends BasicMenuBarUI 38 { 39 40 private WindowListener windowListener = null; 41 private HierarchyListener hierarchyListener = null; 42 private Window window = null; 43 44 public static ComponentUI createUI(JComponent x) { 45 return new WindowsMenuBarUI(); 46 } 47 48 @Override 49 protected void uninstallListeners() { 50 uninstallWindowListener(); 51 if (hierarchyListener != null) { 52 menuBar.removeHierarchyListener(hierarchyListener); 53 hierarchyListener = null; 54 } 55 super.uninstallListeners(); 56 } 57 private void installWindowListener() { 58 if (windowListener == null) { 59 Component component = menuBar.getTopLevelAncestor(); 60 if (component instanceof Window) { 61 window = (Window) component; 62 windowListener = new WindowAdapter () { 63 @Override 64 public void windowActivated(WindowEvent e) { 65 menuBar.repaint(); 66 } 67 @Override 68 public void windowDeactivated(WindowEvent e) { 69 menuBar.repaint(); 70 } 71 }; 72 ((Window) component).addWindowListener(windowListener); 73 } 74 } 75 } 76 private void uninstallWindowListener() { 77 if (windowListener != null && window != null) { 78 window.removeWindowListener(windowListener); 79 } 80 window = null; 81 windowListener = null; 82 } 83 @Override 84 protected void installListeners() { 85 if (WindowsLookAndFeel.isOnVista()) { 86 installWindowListener(); 87 hierarchyListener = 88 new HierarchyListener () { 89 public void hierarchyChanged(HierarchyEvent e) { 90 if ((e.getChangeFlags() 91 & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) { 92 if (menuBar.isDisplayable()) { 93 installWindowListener(); 94 } else { 95 uninstallWindowListener(); 96 } 97 } 98 } 99 }; 100 menuBar.addHierarchyListener(hierarchyListener); 101 } 102 super.installListeners(); 103 } 104 105 protected void installKeyboardActions() { 106 super.installKeyboardActions(); 107 ActionMap map = SwingUtilities.getUIActionMap(menuBar); 108 if (map == null) { 109 map = new ActionMapUIResource (); 110 SwingUtilities.replaceUIActionMap(menuBar, map); 111 } 112 map.put("takeFocus", new TakeFocus()); 113 } 114 115 119 private static class TakeFocus extends AbstractAction { 120 public void actionPerformed(ActionEvent e) { 121 JMenuBar menuBar = (JMenuBar)e.getSource(); 122 JMenu menu = menuBar.getMenu(0); 123 if (menu != null) { 124 MenuSelectionManager msm = 125 MenuSelectionManager.defaultManager(); 126 MenuElement path[] = new MenuElement[2]; 127 path[0] = (MenuElement)menuBar; 128 path[1] = (MenuElement)menu; 129 msm.setSelectedPath(path); 130 131 WindowsLookAndFeel.setMnemonicHidden(false); 133 WindowsLookAndFeel.repaintRootPane(menuBar); 134 } 135 } 136 } 137 138 @Override 139 public void paint(Graphics g, JComponent c) { 140 if (WindowsMenuItemUI.isVistaPainting()) { 141 XPStyle xp = XPStyle.getXP(); 142 Skin skin; 143 skin = xp.getSkin(c, Part.MP_BARBACKGROUND); 144 int width = c.getWidth(); 145 int height = c.getHeight(); 146 State state = isActive(c) ? State.ACTIVE : State.INACTIVE; 147 skin.paintSkin(g, 0, 0, width, height, state); 148 } else { 149 super.paint(g, c); 150 } 151 } 152 153 158 static boolean isActive(JComponent c) { 159 JRootPane rootPane = c.getRootPane(); 160 if (rootPane != null) { 161 Component component = rootPane.getParent(); 162 if (component instanceof Window) { 163 return ((Window) component).isActive(); 164 } 165 } 166 return true; 167 } 168 } 169 170 | Popular Tags |