1 7 8 package com.sun.java.swing.plaf.windows; 9 10 import java.awt.*; 11 import java.awt.event.MouseEvent ; 12 13 import javax.swing.plaf.ComponentUI ; 14 import javax.swing.plaf.basic.BasicMenuUI ; 15 import javax.swing.event.MouseInputListener ; 16 import javax.swing.*; 17 18 import com.sun.java.swing.plaf.windows.TMSchema.Part; 19 import com.sun.java.swing.plaf.windows.TMSchema.State; 20 21 31 public class WindowsMenuUI extends BasicMenuUI { 32 33 final WindowsMenuItemUIAccessor accessor = 34 new WindowsMenuItemUIAccessor() { 35 36 public JMenuItem getMenuItem() { 37 return menuItem; 38 } 39 40 public State getState(JMenuItem menu) { 41 State state = menu.isEnabled() ? State.NORMAL 42 : State.DISABLED; 43 ButtonModel model = menu.getModel(); 44 if (model.isArmed() || model.isSelected()) { 45 state = (menu.isEnabled()) ? State.PUSHED 46 : State.DISABLEDPUSHED; 47 } else if (model.isRollover() 48 && ((JMenu) menu).isTopLevelMenu()) { 49 53 State stateTmp = state; 54 state = (menu.isEnabled()) ? State.HOT 55 : State.DISABLEDHOT; 56 for (MenuElement menuElement : 57 ((JMenuBar) menu.getParent()).getSubElements()) { 58 if (((JMenuItem) menuElement).isSelected()) { 59 state = stateTmp; 60 break; 61 } 62 } 63 } 64 65 if (!((JMenu) menu).isTopLevelMenu()) { 67 if (state == State.PUSHED) { 68 state = State.HOT; 69 } else if (state == State.DISABLEDPUSHED) { 70 state = State.DISABLEDHOT; 71 } 72 } 73 74 77 if (((JMenu) menu).isTopLevelMenu() && WindowsMenuItemUI.isVistaPainting()) { 78 if (! WindowsMenuBarUI.isActive(menu)) { 79 state = State.DISABLED; 80 } 81 } 82 return state; 83 } 84 85 public Part getPart(JMenuItem menuItem) { 86 return ((JMenu) menuItem).isTopLevelMenu() ? Part.MP_BARITEM 87 : Part.MP_POPUPITEM; 88 } 89 }; 90 public static ComponentUI createUI(JComponent x) { 91 return new WindowsMenuUI(); 92 } 93 94 protected void installDefaults() { 95 super.installDefaults(); 96 if (!WindowsLookAndFeel.isClassicWindows()) { 97 menuItem.setRolloverEnabled(true); 98 } 99 } 100 101 105 protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) { 106 if (WindowsMenuItemUI.isVistaPainting()) { 107 WindowsMenuItemUI.paintBackground(accessor, g, menuItem, bgColor); 108 return; 109 } 110 111 JMenu menu = (JMenu)menuItem; 112 ButtonModel model = menu.getModel(); 113 114 if (WindowsLookAndFeel.isClassicWindows() || 117 !menu.isTopLevelMenu() || 118 (XPStyle.getXP() != null && (model.isArmed() || model.isSelected()))) { 119 120 super.paintBackground(g, menu, bgColor); 121 return; 122 } 123 124 Color oldColor = g.getColor(); 125 int menuWidth = menu.getWidth(); 126 int menuHeight = menu.getHeight(); 127 128 UIDefaults table = UIManager.getLookAndFeelDefaults(); 129 Color highlight = table.getColor("controlLtHighlight"); 130 Color shadow = table.getColor("controlShadow"); 131 132 g.setColor(menu.getBackground()); 133 g.fillRect(0,0, menuWidth, menuHeight); 134 135 if (menu.isOpaque()) { 136 if (model.isArmed() || model.isSelected()) { 137 g.setColor(shadow); 139 g.drawLine(0,0, menuWidth - 1,0); 140 g.drawLine(0,0, 0,menuHeight - 2); 141 142 g.setColor(highlight); 143 g.drawLine(menuWidth - 1,0, menuWidth - 1,menuHeight - 2); 144 g.drawLine(0,menuHeight - 2, menuWidth - 1,menuHeight - 2); 145 } else if (model.isRollover() && model.isEnabled()) { 146 boolean otherMenuSelected = false; 148 MenuElement[] menus = ((JMenuBar)menu.getParent()).getSubElements(); 149 for (int i = 0; i < menus.length; i++) { 150 if (((JMenuItem)menus[i]).isSelected()) { 151 otherMenuSelected = true; 152 break; 153 } 154 } 155 if (!otherMenuSelected) { 156 if (XPStyle.getXP() != null) { 157 g.setColor(selectionBackground); g.fillRect(0, 0, menuWidth, menuHeight); 159 } else { 160 g.setColor(highlight); 162 g.drawLine(0,0, menuWidth - 1,0); 163 g.drawLine(0,0, 0,menuHeight - 2); 164 165 g.setColor(shadow); 166 g.drawLine(menuWidth - 1,0, menuWidth - 1,menuHeight - 2); 167 g.drawLine(0,menuHeight - 2, menuWidth - 1,menuHeight - 2); 168 } 169 } 170 } 171 } 172 g.setColor(oldColor); 173 } 174 175 184 protected void paintText(Graphics g, JMenuItem menuItem, 185 Rectangle textRect, String text) { 186 if (WindowsMenuItemUI.isVistaPainting()) { 187 WindowsMenuItemUI.paintText(accessor, g, menuItem, textRect, text); 188 return; 189 } 190 JMenu menu = (JMenu)menuItem; 191 ButtonModel model = menuItem.getModel(); 192 Color oldColor = g.getColor(); 193 194 boolean paintRollover = model.isRollover(); 196 if (paintRollover && menu.isTopLevelMenu()) { 197 MenuElement[] menus = ((JMenuBar)menu.getParent()).getSubElements(); 198 for (int i = 0; i < menus.length; i++) { 199 if (((JMenuItem)menus[i]).isSelected()) { 200 paintRollover = false; 201 break; 202 } 203 } 204 } 205 206 if ((model.isSelected() && (WindowsLookAndFeel.isClassicWindows() || 207 !menu.isTopLevelMenu())) || 208 (XPStyle.getXP() != null && (paintRollover || 209 model.isArmed() || 210 model.isSelected()))) { 211 g.setColor(selectionForeground); } 213 214 WindowsGraphicsUtils.paintText(g, menuItem, textRect, text, 0); 215 216 g.setColor(oldColor); 217 } 218 219 protected MouseInputListener createMouseInputListener(JComponent c) { 220 return new WindowsMouseInputHandler(); 221 } 222 223 228 protected class WindowsMouseInputHandler extends BasicMenuUI.MouseInputHandler { 229 public void mouseEntered(MouseEvent evt) { 230 super.mouseEntered(evt); 231 232 JMenu menu = (JMenu)evt.getSource(); 233 ButtonModel model = menu.getModel(); 234 if (menu.isRolloverEnabled()) { 235 model.setRollover(true); 236 menuItem.repaint(); 237 } 238 } 239 240 public void mouseExited(MouseEvent evt) { 241 super.mouseExited(evt); 242 243 JMenu menu = (JMenu)evt.getSource(); 244 ButtonModel model = menu.getModel(); 245 if (menu.isRolloverEnabled()) { 246 model.setRollover(false); 247 menuItem.repaint(); 248 } 249 } 250 } 251 } 252 253 | Popular Tags |