2
|
1 |
/*
|
|
2 |
* Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package javax.swing.plaf.synth;
|
|
26 |
|
|
27 |
import java.awt.*;
|
|
28 |
import java.awt.event.*;
|
|
29 |
import java.beans.*;
|
|
30 |
import javax.swing.*;
|
|
31 |
import javax.swing.event.*;
|
|
32 |
import javax.swing.plaf.*;
|
|
33 |
import javax.swing.plaf.basic.*;
|
|
34 |
import javax.swing.border.*;
|
|
35 |
import java.util.Arrays;
|
|
36 |
import java.util.ArrayList;
|
|
37 |
import sun.swing.plaf.synth.SynthUI;
|
|
38 |
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Synth's MenuUI.
|
|
42 |
*
|
|
43 |
* @author Georges Saab
|
|
44 |
* @author David Karlton
|
|
45 |
* @author Arnaud Weber
|
|
46 |
*/
|
|
47 |
class SynthMenuUI extends BasicMenuUI implements PropertyChangeListener,
|
|
48 |
SynthUI {
|
|
49 |
private SynthStyle style;
|
|
50 |
private SynthStyle accStyle;
|
|
51 |
|
|
52 |
private String acceleratorDelimiter;
|
|
53 |
|
|
54 |
public static ComponentUI createUI(JComponent x) {
|
|
55 |
return new SynthMenuUI();
|
|
56 |
}
|
|
57 |
|
|
58 |
protected void installDefaults() {
|
|
59 |
updateStyle(menuItem);
|
|
60 |
}
|
|
61 |
|
|
62 |
protected void installListeners() {
|
|
63 |
super.installListeners();
|
|
64 |
menuItem.addPropertyChangeListener(this);
|
|
65 |
}
|
|
66 |
|
|
67 |
private void updateStyle(JMenuItem mi) {
|
|
68 |
SynthStyle oldStyle = style;
|
|
69 |
SynthContext context = getContext(mi, ENABLED);
|
|
70 |
|
|
71 |
style = SynthLookAndFeel.updateStyle(context, this);
|
|
72 |
if (oldStyle != style) {
|
|
73 |
String prefix = getPropertyPrefix();
|
|
74 |
defaultTextIconGap = style.getInt(
|
|
75 |
context, prefix + ".textIconGap", 4);
|
|
76 |
if (menuItem.getMargin() == null ||
|
|
77 |
(menuItem.getMargin() instanceof UIResource)) {
|
|
78 |
Insets insets = (Insets)style.get(context, prefix + ".margin");
|
|
79 |
|
|
80 |
if (insets == null) {
|
|
81 |
// Some places assume margins are non-null.
|
|
82 |
insets = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
|
|
83 |
}
|
|
84 |
menuItem.setMargin(insets);
|
|
85 |
}
|
|
86 |
acceleratorDelimiter = style.getString(context, prefix +
|
|
87 |
".acceleratorDelimiter", "+");
|
|
88 |
|
|
89 |
if (useCheckAndArrow()) {
|
|
90 |
checkIcon = style.getIcon(context, prefix + ".checkIcon");
|
|
91 |
arrowIcon = style.getIcon(context, prefix + ".arrowIcon");
|
|
92 |
} else {
|
|
93 |
// Not needed in this case
|
|
94 |
checkIcon = null;
|
|
95 |
arrowIcon = null;
|
|
96 |
}
|
|
97 |
|
|
98 |
((JMenu)menuItem).setDelay(style.getInt(context, prefix +
|
|
99 |
".delay", 200));
|
|
100 |
if (oldStyle != null) {
|
|
101 |
uninstallKeyboardActions();
|
|
102 |
installKeyboardActions();
|
|
103 |
}
|
|
104 |
}
|
|
105 |
context.dispose();
|
|
106 |
|
|
107 |
SynthContext accContext = getContext(mi, Region.MENU_ITEM_ACCELERATOR,
|
|
108 |
ENABLED);
|
|
109 |
|
|
110 |
accStyle = SynthLookAndFeel.updateStyle(accContext, this);
|
|
111 |
accContext.dispose();
|
|
112 |
}
|
|
113 |
|
|
114 |
protected void uninstallDefaults() {
|
|
115 |
SynthContext context = getContext(menuItem, ENABLED);
|
|
116 |
style.uninstallDefaults(context);
|
|
117 |
context.dispose();
|
|
118 |
style = null;
|
|
119 |
|
|
120 |
SynthContext accContext = getContext(menuItem,
|
|
121 |
Region.MENU_ITEM_ACCELERATOR, ENABLED);
|
|
122 |
accStyle.uninstallDefaults(accContext);
|
|
123 |
accContext.dispose();
|
|
124 |
accStyle = null;
|
|
125 |
|
|
126 |
super.uninstallDefaults();
|
|
127 |
}
|
|
128 |
|
|
129 |
protected void uninstallListeners() {
|
|
130 |
super.uninstallListeners();
|
|
131 |
menuItem.removePropertyChangeListener(this);
|
|
132 |
}
|
|
133 |
|
|
134 |
public SynthContext getContext(JComponent c) {
|
|
135 |
return getContext(c, getComponentState(c));
|
|
136 |
}
|
|
137 |
|
|
138 |
SynthContext getContext(JComponent c, int state) {
|
|
139 |
Region region = getRegion(c);
|
|
140 |
return SynthContext.getContext(SynthContext.class, c, region,
|
|
141 |
style, state);
|
|
142 |
}
|
|
143 |
|
|
144 |
public SynthContext getContext(JComponent c, Region region) {
|
|
145 |
return getContext(c, region, getComponentState(c, region));
|
|
146 |
}
|
|
147 |
|
|
148 |
private SynthContext getContext(JComponent c, Region region, int state) {
|
|
149 |
return SynthContext.getContext(SynthContext.class, c,
|
|
150 |
region, accStyle, state);
|
|
151 |
}
|
|
152 |
|
|
153 |
private Region getRegion(JComponent c) {
|
|
154 |
return SynthLookAndFeel.getRegion(c);
|
|
155 |
}
|
|
156 |
|
|
157 |
private int getComponentState(JComponent c) {
|
|
158 |
int state;
|
|
159 |
|
|
160 |
if (!c.isEnabled()) {
|
|
161 |
return DISABLED;
|
|
162 |
}
|
|
163 |
if (menuItem.isArmed()) {
|
|
164 |
state = MOUSE_OVER;
|
|
165 |
}
|
|
166 |
else {
|
|
167 |
state = SynthLookAndFeel.getComponentState(c);
|
|
168 |
}
|
|
169 |
if (menuItem.isSelected()) {
|
|
170 |
state |= SELECTED;
|
|
171 |
}
|
|
172 |
return state;
|
|
173 |
}
|
|
174 |
|
|
175 |
private int getComponentState(JComponent c, Region region) {
|
|
176 |
return getComponentState(c);
|
|
177 |
}
|
|
178 |
|
|
179 |
protected Dimension getPreferredMenuItemSize(JComponent c,
|
|
180 |
Icon checkIcon,
|
|
181 |
Icon arrowIcon,
|
|
182 |
int defaultTextIconGap) {
|
|
183 |
SynthContext context = getContext(c);
|
|
184 |
SynthContext accContext = getContext(c, Region.MENU_ITEM_ACCELERATOR);
|
|
185 |
Dimension value = SynthMenuItemUI.getPreferredMenuItemSize(
|
|
186 |
context, accContext, c, checkIcon, arrowIcon,
|
|
187 |
defaultTextIconGap, acceleratorDelimiter);
|
|
188 |
context.dispose();
|
|
189 |
accContext.dispose();
|
|
190 |
return value;
|
|
191 |
}
|
|
192 |
|
|
193 |
|
|
194 |
public void update(Graphics g, JComponent c) {
|
|
195 |
SynthContext context = getContext(c);
|
|
196 |
|
|
197 |
SynthLookAndFeel.update(context, g);
|
|
198 |
context.getPainter().paintMenuBackground(context,
|
|
199 |
g, 0, 0, c.getWidth(), c.getHeight());
|
|
200 |
paint(context, g);
|
|
201 |
context.dispose();
|
|
202 |
}
|
|
203 |
|
|
204 |
public void paint(Graphics g, JComponent c) {
|
|
205 |
SynthContext context = getContext(c);
|
|
206 |
|
|
207 |
paint(context, g);
|
|
208 |
context.dispose();
|
|
209 |
}
|
|
210 |
|
|
211 |
protected void paint(SynthContext context, Graphics g) {
|
|
212 |
SynthContext accContext = getContext(menuItem,
|
|
213 |
Region.MENU_ITEM_ACCELERATOR);
|
|
214 |
SynthStyle style = context.getStyle();
|
|
215 |
Icon checkIcon;
|
|
216 |
Icon arrowIcon;
|
|
217 |
if (useCheckAndArrow()) {
|
|
218 |
// Refetch the appropriate icons for the current state
|
|
219 |
String prefix = getPropertyPrefix();
|
|
220 |
checkIcon = style.getIcon(context, prefix + ".checkIcon");
|
|
221 |
arrowIcon = style.getIcon(context, prefix + ".arrowIcon");
|
|
222 |
} else {
|
|
223 |
// Not needed in this case
|
|
224 |
checkIcon = null;
|
|
225 |
arrowIcon = null;
|
|
226 |
}
|
|
227 |
SynthMenuItemUI.paint(context, accContext, g, checkIcon, arrowIcon,
|
|
228 |
acceleratorDelimiter, defaultTextIconGap);
|
|
229 |
accContext.dispose();
|
|
230 |
}
|
|
231 |
|
|
232 |
public void paintBorder(SynthContext context, Graphics g, int x,
|
|
233 |
int y, int w, int h) {
|
|
234 |
context.getPainter().paintMenuBorder(context, g, x, y, w, h);
|
|
235 |
}
|
|
236 |
|
|
237 |
public void propertyChange(PropertyChangeEvent e) {
|
|
238 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
|
|
239 |
updateStyle((JMenu)e.getSource());
|
|
240 |
}
|
|
241 |
}
|
|
242 |
|
|
243 |
private boolean useCheckAndArrow() {
|
|
244 |
return !((JMenu)menuItem).isTopLevelMenu();
|
|
245 |
}
|
|
246 |
}
|