1 30 31 package com.jgoodies.looks.common; 32 33 import java.awt.Color ; 34 import java.awt.Dimension ; 35 import java.awt.Graphics ; 36 import java.awt.event.MouseAdapter ; 37 import java.awt.event.MouseEvent ; 38 import java.awt.event.MouseListener ; 39 40 import javax.swing.AbstractButton ; 41 import javax.swing.Icon ; 42 import javax.swing.JComponent ; 43 import javax.swing.JMenu ; 44 import javax.swing.JMenuItem ; 45 import javax.swing.UIManager ; 46 import javax.swing.plaf.ComponentUI ; 47 import javax.swing.plaf.UIResource ; 48 import javax.swing.plaf.basic.BasicMenuUI ; 49 50 57 58 public class ExtBasicMenuUI extends BasicMenuUI { 59 60 private static final String MENU_PROPERTY_PREFIX = "Menu"; 61 private static final String SUBMENU_PROPERTY_PREFIX = "MenuItem"; 62 63 private String propertyPrefix = MENU_PROPERTY_PREFIX; 65 66 private MenuItemRenderer renderer; 67 private MouseListener mouseListener; 68 69 70 public static ComponentUI createUI(JComponent b) { 71 return new ExtBasicMenuUI(); 72 } 73 74 75 77 protected void installDefaults() { 78 super.installDefaults(); 79 if (arrowIcon == null || arrowIcon instanceof UIResource ) { 80 arrowIcon = UIManager.getIcon("Menu.arrowIcon"); 81 } 82 renderer = 83 new MenuItemRenderer( 84 menuItem, 85 false, 86 acceleratorFont, 87 selectionForeground, 88 disabledForeground, 89 acceleratorForeground, 90 acceleratorSelectionForeground); 91 Integer gap = 92 (Integer ) UIManager.get(getPropertyPrefix() + ".textIconGap"); 93 defaultTextIconGap = gap != null ? gap.intValue() : 2; 94 } 95 96 protected void uninstallDefaults() { 97 super.uninstallDefaults(); 98 renderer = null; 99 } 100 101 protected String getPropertyPrefix() { 102 return propertyPrefix; 103 } 104 105 protected Dimension getPreferredMenuItemSize( 106 JComponent c, 107 Icon aCheckIcon, 108 Icon anArrowIcon, 109 int textIconGap) { 110 111 if (isSubMenu(menuItem)) { 112 ensureSubMenuInstalled(); 113 return renderer.getPreferredMenuItemSize( 114 c, 115 aCheckIcon, 116 anArrowIcon, 117 textIconGap); 118 } else 119 return super.getPreferredMenuItemSize( 120 c, 121 aCheckIcon, 122 anArrowIcon, 123 textIconGap); 124 } 125 126 protected void paintMenuItem( 127 Graphics g, 128 JComponent c, 129 Icon aCheckIcon, 130 Icon anArrowIcon, 131 Color background, 132 Color foreground, 133 int textIconGap) { 134 if (isSubMenu(menuItem)) { 135 renderer.paintMenuItem( 136 g, 137 c, 138 aCheckIcon, 139 anArrowIcon, 140 background, 141 foreground, 142 textIconGap); 143 } else { 144 super.paintMenuItem( 145 g, 146 c, 147 aCheckIcon, 148 anArrowIcon, 149 background, 150 foreground, 151 textIconGap); 152 } 153 } 154 155 159 private void ensureSubMenuInstalled() { 160 if (propertyPrefix.equals(SUBMENU_PROPERTY_PREFIX)) 161 return; 162 163 uninstallRolloverListener(); 165 uninstallDefaults(); 166 propertyPrefix = SUBMENU_PROPERTY_PREFIX; 167 installDefaults(); 168 } 169 170 172 protected void installListeners() { 173 super.installListeners(); 174 mouseListener = createRolloverListener(); 175 menuItem.addMouseListener(mouseListener); 176 } 177 178 protected void uninstallListeners() { 179 super.uninstallListeners(); 180 uninstallRolloverListener(); 181 } 182 183 private void uninstallRolloverListener() { 184 if (mouseListener != null) { 185 menuItem.removeMouseListener(mouseListener); 186 mouseListener = null; 187 } 188 } 189 190 protected MouseListener createRolloverListener() { 191 return new MouseAdapter () { 192 public void mouseEntered(MouseEvent e) { 193 AbstractButton b = (AbstractButton ) e.getSource(); 194 b.getModel().setRollover(true); 195 } 196 public void mouseExited(MouseEvent e) { 197 AbstractButton b = (AbstractButton ) e.getSource(); 198 b.getModel().setRollover(false); 199 } 200 }; 201 } 202 203 204 206 private boolean isSubMenu(JMenuItem aMenuItem) { 207 return !((JMenu ) aMenuItem).isTopLevelMenu(); 208 } 209 210 } | Popular Tags |