1 7 8 package com.sun.java.swing.plaf.windows; 9 10 import java.awt.*; 11 import javax.swing.*; 12 import javax.swing.plaf.*; 13 import javax.swing.plaf.basic.*; 14 15 import com.sun.java.swing.SwingUtilities2; 16 17 import com.sun.java.swing.plaf.windows.TMSchema.*; 18 import com.sun.java.swing.plaf.windows.XPStyle.*; 19 20 33 34 public class WindowsMenuItemUI extends BasicMenuItemUI { 35 final WindowsMenuItemUIAccessor accessor = 36 new WindowsMenuItemUIAccessor() { 37 38 public JMenuItem getMenuItem() { 39 return menuItem; 40 } 41 42 public State getState(JMenuItem menuItem) { 43 return WindowsMenuItemUI.getState(this, menuItem); 44 } 45 46 public Part getPart(JMenuItem menuItem) { 47 return WindowsMenuItemUI.getPart(this, menuItem); 48 } 49 }; 50 public static ComponentUI createUI(JComponent c) { 51 return new WindowsMenuItemUI(); 52 } 53 54 62 protected void paintText(Graphics g, JMenuItem menuItem, 63 Rectangle textRect, String text) { 64 if (WindowsMenuItemUI.isVistaPainting()) { 65 WindowsMenuItemUI.paintText(accessor, g, menuItem, textRect, text); 66 return; 67 } 68 ButtonModel model = menuItem.getModel(); 69 Color oldColor = g.getColor(); 70 71 if(model.isEnabled() && 72 (model.isArmed() || (menuItem instanceof JMenu && 73 model.isSelected()))) { 74 g.setColor(selectionForeground); } 76 77 WindowsGraphicsUtils.paintText(g, menuItem, textRect, text, 0); 78 79 g.setColor(oldColor); 80 } 81 82 @Override 83 protected void paintBackground(Graphics g, JMenuItem menuItem, 84 Color bgColor) { 85 if (WindowsMenuItemUI.isVistaPainting()) { 86 WindowsMenuItemUI.paintBackground(accessor, g, menuItem, bgColor); 87 return; 88 } 89 super.paintBackground(g, menuItem, bgColor); 90 } 91 92 static void paintBackground(WindowsMenuItemUIAccessor menuItemUI, 93 Graphics g, JMenuItem menuItem, Color bgColor) { 94 assert isVistaPainting(); 95 if (isVistaPainting()) { 96 int menuWidth = menuItem.getWidth(); 97 int menuHeight = menuItem.getHeight(); 98 if (menuItem.isOpaque()) { 99 Color oldColor = g.getColor(); 100 g.setColor(menuItem.getBackground()); 101 g.fillRect(0,0, menuWidth, menuHeight); 102 g.setColor(oldColor); 103 } 104 XPStyle xp = XPStyle.getXP(); 105 Part part = menuItemUI.getPart(menuItem); 106 Skin skin = xp.getSkin(menuItem, part); 107 skin.paintSkin(g, 0 , 0, 108 menuWidth, 109 menuHeight, 110 menuItemUI.getState(menuItem)); 111 } 112 } 113 114 static void paintText(WindowsMenuItemUIAccessor menuItemUI, Graphics g, 115 JMenuItem menuItem, Rectangle textRect, 116 String text) { 117 assert isVistaPainting(); 118 if (isVistaPainting()) { 119 State state = menuItemUI.getState(menuItem); 120 121 122 FontMetrics fm = SwingUtilities2.getFontMetrics(menuItem, g); 123 int mnemIndex = menuItem.getDisplayedMnemonicIndex(); 124 if (WindowsLookAndFeel.isMnemonicHidden() == true) { 126 mnemIndex = -1; 127 } 128 XPStyle xp = XPStyle.getXP(); 129 Color textColor = menuItem.getForeground(); 130 if (textColor instanceof UIResource) { 131 Part part = menuItemUI.getPart(menuItem); 132 textColor = xp.getColor(menuItem, part, state, Prop.TEXTCOLOR, textColor); 133 } 134 g.setColor(textColor); 135 SwingUtilities2.drawStringUnderlineCharAt(menuItem, 136 g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent()); 137 } 138 } 139 140 static State getState(WindowsMenuItemUIAccessor menuItemUI, JMenuItem menuItem) { 141 State state; 142 ButtonModel model = menuItem.getModel(); 143 if (model.isArmed()) { 144 state = (model.isEnabled()) ? State.HOT : State.DISABLEDHOT; 145 } else { 146 state = (model.isEnabled()) ? State.NORMAL : State.DISABLED; 147 } 148 return state; 149 } 150 151 static Part getPart(WindowsMenuItemUIAccessor menuItemUI, JMenuItem menuItem) { 152 return Part.MP_POPUPITEM; 153 } 154 155 160 static boolean isVistaPainting() { 161 XPStyle xp = XPStyle.getXP(); 162 return xp != null && xp.isSkinDefined(null, Part.MP_POPUPITEM); 163 } 164 } 165 166 | Popular Tags |