1 7 package javax.swing.plaf.synth; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.beans.*; 12 import javax.swing.*; 13 import javax.swing.event.*; 14 import javax.swing.plaf.*; 15 import javax.swing.plaf.basic.*; 16 import javax.swing.border.*; 17 import java.util.Arrays ; 18 import java.util.ArrayList ; 19 import sun.swing.plaf.synth.SynthUI; 20 21 22 30 class SynthMenuUI extends BasicMenuUI implements PropertyChangeListener, 31 SynthUI { 32 private SynthStyle style; 33 private SynthStyle accStyle; 34 35 private String acceleratorDelimiter; 36 37 public static ComponentUI createUI(JComponent x) { 38 return new SynthMenuUI (); 39 } 40 41 protected void installDefaults() { 42 updateStyle(menuItem); 43 } 44 45 protected void installListeners() { 46 super.installListeners(); 47 menuItem.addPropertyChangeListener(this); 48 } 49 50 private void updateStyle(JMenuItem mi) { 51 SynthStyle oldStyle = style; 52 SynthContext context = getContext(mi, ENABLED); 53 54 style = SynthLookAndFeel.updateStyle(context, this); 55 if (oldStyle != style) { 56 String prefix = getPropertyPrefix(); 57 defaultTextIconGap = style.getInt( 58 context, prefix + ".textIconGap", 4); 59 if (menuItem.getMargin() == null || 60 (menuItem.getMargin() instanceof UIResource)) { 61 Insets insets = (Insets)style.get(context, prefix + ".margin"); 62 63 if (insets == null) { 64 insets = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS; 66 } 67 menuItem.setMargin(insets); 68 } 69 acceleratorDelimiter = style.getString(context, prefix + 70 ".acceleratorDelimiter", "+"); 71 72 arrowIcon = style.getIcon(context, prefix + ".arrowIcon"); 73 74 checkIcon = style.getIcon(context, prefix + ".checkIcon"); 75 76 77 ((JMenu)menuItem).setDelay(style.getInt(context, prefix + 78 ".delay", 200)); 79 if (oldStyle != null) { 80 uninstallKeyboardActions(); 81 installKeyboardActions(); 82 } 83 } 84 context.dispose(); 85 86 SynthContext accContext = getContext(mi, Region.MENU_ITEM_ACCELERATOR, 87 ENABLED); 88 89 accStyle = SynthLookAndFeel.updateStyle(accContext, this); 90 accContext.dispose(); 91 } 92 93 protected void uninstallDefaults() { 94 SynthContext context = getContext(menuItem, ENABLED); 95 style.uninstallDefaults(context); 96 context.dispose(); 97 style = null; 98 99 SynthContext accContext = getContext(menuItem, 100 Region.MENU_ITEM_ACCELERATOR, ENABLED); 101 accStyle.uninstallDefaults(accContext); 102 accContext.dispose(); 103 accStyle = null; 104 105 super.uninstallDefaults(); 106 } 107 108 protected void uninstallListeners() { 109 super.uninstallListeners(); 110 menuItem.removePropertyChangeListener(this); 111 } 112 113 public SynthContext getContext(JComponent c) { 114 return getContext(c, getComponentState(c)); 115 } 116 117 SynthContext getContext(JComponent c, int state) { 118 Region region = getRegion(c); 119 return SynthContext.getContext(SynthContext .class, c, region, 120 style, state); 121 } 122 123 public SynthContext getContext(JComponent c, Region region) { 124 return getContext(c, region, getComponentState(c, region)); 125 } 126 127 private SynthContext getContext(JComponent c, Region region, int state) { 128 return SynthContext.getContext(SynthContext .class, c, 129 region, accStyle, state); 130 } 131 132 private Region getRegion(JComponent c) { 133 return SynthLookAndFeel.getRegion(c); 134 } 135 136 private int getComponentState(JComponent c) { 137 int state; 138 139 if (!c.isEnabled()) { 140 return DISABLED; 141 } 142 if (menuItem.isArmed()) { 143 state = MOUSE_OVER; 144 } 145 else { 146 state = SynthLookAndFeel.getComponentState(c); 147 } 148 if (menuItem.isSelected()) { 149 state |= SELECTED; 150 } 151 return state; 152 } 153 154 private int getComponentState(JComponent c, Region region) { 155 return getComponentState(c); 156 } 157 158 protected Dimension getPreferredMenuItemSize(JComponent c, 159 Icon checkIcon, 160 Icon arrowIcon, 161 int defaultTextIconGap) { 162 SynthContext context = getContext(c); 163 SynthContext accContext = getContext(c, Region.MENU_ITEM_ACCELERATOR); 164 Dimension value = SynthMenuItemUI.getPreferredMenuItemSize( 165 context, accContext, useCheckAndArrow(), c, checkIcon, 166 arrowIcon, defaultTextIconGap, acceleratorDelimiter); 167 context.dispose(); 168 accContext.dispose(); 169 return value; 170 } 171 172 173 public void update(Graphics g, JComponent c) { 174 SynthContext context = getContext(c); 175 176 SynthLookAndFeel.update(context, g); 177 context.getPainter().paintMenuBackground(context, 178 g, 0, 0, c.getWidth(), c.getHeight()); 179 paint(context, g); 180 context.dispose(); 181 } 182 183 public void paint(Graphics g, JComponent c) { 184 SynthContext context = getContext(c); 185 186 paint(context, g); 187 context.dispose(); 188 } 189 190 protected void paint(SynthContext context, Graphics g) { 191 SynthContext accContext = getContext(menuItem, 192 Region.MENU_ITEM_ACCELERATOR); 193 SynthMenuItemUI.paint(context, accContext, g, checkIcon, arrowIcon, 194 useCheckAndArrow(), acceleratorDelimiter, 195 defaultTextIconGap); 196 accContext.dispose(); 197 } 198 199 public void paintBorder(SynthContext context, Graphics g, int x, 200 int y, int w, int h) { 201 context.getPainter().paintMenuBorder(context, g, x, y, w, h); 202 } 203 204 public void propertyChange(PropertyChangeEvent e) { 205 if (SynthLookAndFeel.shouldUpdateStyle(e)) { 206 updateStyle((JMenu)e.getSource()); 207 } 208 } 209 210 private boolean useCheckAndArrow() { 211 return !((JMenu)menuItem).isTopLevelMenu(); 212 } 213 } 214 | Popular Tags |