1 30 31 package com.jgoodies.looks.windows; 32 33 import java.awt.Color ; 34 import java.awt.Dimension ; 35 import java.awt.Graphics ; 36 37 import javax.swing.Icon ; 38 import javax.swing.JComponent ; 39 import javax.swing.JMenu ; 40 import javax.swing.JMenuItem ; 41 import javax.swing.UIManager ; 42 import javax.swing.plaf.ComponentUI ; 43 import javax.swing.plaf.UIResource ; 44 45 import com.jgoodies.looks.common.MenuItemRenderer; 46 47 59 60 public final class WindowsMenuUI extends com.sun.java.swing.plaf.windows.WindowsMenuUI { 61 62 private static final String MENU_PROPERTY_PREFIX = "Menu"; 63 private static final String SUBMENU_PROPERTY_PREFIX = "MenuItem"; 64 65 private String propertyPrefix = MENU_PROPERTY_PREFIX; 67 68 private MenuItemRenderer renderer; 69 70 public static ComponentUI createUI(JComponent b) { 71 return new WindowsMenuUI(); 72 } 73 74 76 protected void installDefaults() { 77 super.installDefaults(); 78 if (arrowIcon == null || arrowIcon instanceof UIResource ) { 79 arrowIcon = UIManager.getIcon("Menu.arrowIcon"); 80 } 81 renderer = 82 new MenuItemRenderer( 83 menuItem, 84 false, 85 acceleratorFont, 86 selectionForeground, 87 disabledForeground, 88 acceleratorForeground, 89 acceleratorSelectionForeground); 90 Integer gap = 91 (Integer ) UIManager.get(getPropertyPrefix() + ".textIconGap"); 92 defaultTextIconGap = gap != null ? gap.intValue() : 2; 93 } 94 95 protected void uninstallDefaults() { 96 super.uninstallDefaults(); 97 renderer = null; 98 } 99 100 protected String getPropertyPrefix() { 101 return propertyPrefix; 102 } 103 104 protected Dimension getPreferredMenuItemSize( 105 JComponent c, 106 Icon aCheckIcon, 107 Icon anArrowIcon, 108 int textIconGap) { 109 110 if (isSubMenu(menuItem)) { 111 ensureSubMenuInstalled(); 112 return renderer.getPreferredMenuItemSize( 113 c, 114 aCheckIcon, 115 anArrowIcon, 116 textIconGap); 117 } else { 118 Dimension size = 119 super.getPreferredMenuItemSize( 120 c, 121 aCheckIcon, 122 anArrowIcon, 123 textIconGap); 124 int width = size.width; 125 int height = size.height; 126 if (height % 2 == 1) 127 height--; 128 return new Dimension (width, height); 129 } 130 } 131 132 protected void paintMenuItem( 133 Graphics g, 134 JComponent c, 135 Icon aCheckIcon, 136 Icon anArrowIcon, 137 Color background, 138 Color foreground, 139 int textIconGap) { 140 if (isSubMenu(menuItem)) { 141 renderer.paintMenuItem( 142 g, 143 c, 144 aCheckIcon, 145 anArrowIcon, 146 background, 147 foreground, 148 textIconGap); 149 } else { 150 super.paintMenuItem( 151 g, 152 c, 153 aCheckIcon, 154 anArrowIcon, 155 background, 156 foreground, 157 textIconGap); 158 } 159 } 160 161 165 private void ensureSubMenuInstalled() { 166 if (propertyPrefix.equals(SUBMENU_PROPERTY_PREFIX)) 167 return; 168 uninstallDefaults(); 169 propertyPrefix = SUBMENU_PROPERTY_PREFIX; 170 installDefaults(); 171 } 172 173 174 176 private boolean isSubMenu(JMenuItem aMenuItem) { 177 return !((JMenu ) aMenuItem).isTopLevelMenu(); 178 } 179 180 181 } | Popular Tags |