001 /*
002 * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.plaf.synth;
026
027 import java.awt.*;
028 import java.awt.event.*;
029 import java.beans.*;
030 import javax.swing.*;
031 import javax.swing.event.*;
032 import javax.swing.plaf.*;
033 import javax.swing.plaf.basic.*;
034 import javax.swing.border.*;
035 import java.util.Arrays;
036 import java.util.ArrayList;
037 import sun.swing.plaf.synth.SynthUI;
038
039 /**
040 * Synth's MenuUI.
041 *
042 * @version 1.20, 05/05/07
043 * @author Georges Saab
044 * @author David Karlton
045 * @author Arnaud Weber
046 */
047 class SynthMenuUI extends BasicMenuUI implements
048 PropertyChangeListener, SynthUI {
049 private SynthStyle style;
050 private SynthStyle accStyle;
051
052 private String acceleratorDelimiter;
053
054 public static ComponentUI createUI(JComponent x) {
055 return new SynthMenuUI();
056 }
057
058 protected void installDefaults() {
059 updateStyle(menuItem);
060 }
061
062 protected void installListeners() {
063 super .installListeners();
064 menuItem.addPropertyChangeListener(this );
065 }
066
067 private void updateStyle(JMenuItem mi) {
068 SynthStyle oldStyle = style;
069 SynthContext context = getContext(mi, ENABLED);
070
071 style = SynthLookAndFeel.updateStyle(context, this );
072 if (oldStyle != style) {
073 String prefix = getPropertyPrefix();
074 defaultTextIconGap = style.getInt(context, prefix
075 + ".textIconGap", 4);
076 if (menuItem.getMargin() == null
077 || (menuItem.getMargin() instanceof UIResource)) {
078 Insets insets = (Insets) style.get(context, prefix
079 + ".margin");
080
081 if (insets == null) {
082 // Some places assume margins are non-null.
083 insets = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
084 }
085 menuItem.setMargin(insets);
086 }
087 acceleratorDelimiter = style.getString(context, prefix
088 + ".acceleratorDelimiter", "+");
089
090 if (useCheckAndArrow()) {
091 checkIcon = style.getIcon(context, prefix
092 + ".checkIcon");
093 arrowIcon = style.getIcon(context, prefix
094 + ".arrowIcon");
095 } else {
096 // Not needed in this case
097 checkIcon = null;
098 arrowIcon = null;
099 }
100
101 ((JMenu) menuItem).setDelay(style.getInt(context, prefix
102 + ".delay", 200));
103 if (oldStyle != null) {
104 uninstallKeyboardActions();
105 installKeyboardActions();
106 }
107 }
108 context.dispose();
109
110 SynthContext accContext = getContext(mi,
111 Region.MENU_ITEM_ACCELERATOR, ENABLED);
112
113 accStyle = SynthLookAndFeel.updateStyle(accContext, this );
114 accContext.dispose();
115 }
116
117 protected void uninstallDefaults() {
118 SynthContext context = getContext(menuItem, ENABLED);
119 style.uninstallDefaults(context);
120 context.dispose();
121 style = null;
122
123 SynthContext accContext = getContext(menuItem,
124 Region.MENU_ITEM_ACCELERATOR, ENABLED);
125 accStyle.uninstallDefaults(accContext);
126 accContext.dispose();
127 accStyle = null;
128
129 super .uninstallDefaults();
130 }
131
132 protected void uninstallListeners() {
133 super .uninstallListeners();
134 menuItem.removePropertyChangeListener(this );
135 }
136
137 public SynthContext getContext(JComponent c) {
138 return getContext(c, getComponentState(c));
139 }
140
141 SynthContext getContext(JComponent c, int state) {
142 Region region = getRegion(c);
143 return SynthContext.getContext(SynthContext.class, c, region,
144 style, state);
145 }
146
147 public SynthContext getContext(JComponent c, Region region) {
148 return getContext(c, region, getComponentState(c, region));
149 }
150
151 private SynthContext getContext(JComponent c, Region region,
152 int state) {
153 return SynthContext.getContext(SynthContext.class, c, region,
154 accStyle, state);
155 }
156
157 private Region getRegion(JComponent c) {
158 return SynthLookAndFeel.getRegion(c);
159 }
160
161 private int getComponentState(JComponent c) {
162 int state;
163
164 if (!c.isEnabled()) {
165 return DISABLED;
166 }
167 if (menuItem.isArmed()) {
168 state = MOUSE_OVER;
169 } else {
170 state = SynthLookAndFeel.getComponentState(c);
171 }
172 if (menuItem.isSelected()) {
173 state |= SELECTED;
174 }
175 return state;
176 }
177
178 private int getComponentState(JComponent c, Region region) {
179 return getComponentState(c);
180 }
181
182 protected Dimension getPreferredMenuItemSize(JComponent c,
183 Icon checkIcon, Icon arrowIcon, int defaultTextIconGap) {
184 SynthContext context = getContext(c);
185 SynthContext accContext = getContext(c,
186 Region.MENU_ITEM_ACCELERATOR);
187 Dimension value = SynthMenuItemUI.getPreferredMenuItemSize(
188 context, accContext, c, checkIcon, arrowIcon,
189 defaultTextIconGap, acceleratorDelimiter);
190 context.dispose();
191 accContext.dispose();
192 return value;
193 }
194
195 public void update(Graphics g, JComponent c) {
196 SynthContext context = getContext(c);
197
198 SynthLookAndFeel.update(context, g);
199 context.getPainter().paintMenuBackground(context, g, 0, 0,
200 c.getWidth(), c.getHeight());
201 paint(context, g);
202 context.dispose();
203 }
204
205 public void paint(Graphics g, JComponent c) {
206 SynthContext context = getContext(c);
207
208 paint(context, g);
209 context.dispose();
210 }
211
212 protected void paint(SynthContext context, Graphics g) {
213 SynthContext accContext = getContext(menuItem,
214 Region.MENU_ITEM_ACCELERATOR);
215 SynthStyle style = context.getStyle();
216 Icon checkIcon;
217 Icon arrowIcon;
218 if (useCheckAndArrow()) {
219 // Refetch the appropriate icons for the current state
220 String prefix = getPropertyPrefix();
221 checkIcon = style.getIcon(context, prefix + ".checkIcon");
222 arrowIcon = style.getIcon(context, prefix + ".arrowIcon");
223 } else {
224 // Not needed in this case
225 checkIcon = null;
226 arrowIcon = null;
227 }
228 SynthMenuItemUI.paint(context, accContext, g, checkIcon,
229 arrowIcon, acceleratorDelimiter, defaultTextIconGap);
230 accContext.dispose();
231 }
232
233 public void paintBorder(SynthContext context, Graphics g, int x,
234 int y, int w, int h) {
235 context.getPainter().paintMenuBorder(context, g, x, y, w, h);
236 }
237
238 public void propertyChange(PropertyChangeEvent e) {
239 if (SynthLookAndFeel.shouldUpdateStyle(e)) {
240 updateStyle((JMenu) e.getSource());
241 }
242 }
243
244 private boolean useCheckAndArrow() {
245 return !((JMenu) menuItem).isTopLevelMenu();
246 }
247 }
|