author | rupashka |
Thu, 28 Jun 2012 14:05:06 +0400 | |
changeset 13147 | eada40c7d74d |
parent 7668 | d4a77089c587 |
child 20168 | 137788883a22 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package javax.swing.plaf.synth; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.beans.*; |
|
29 |
import javax.swing.*; |
|
30 |
import javax.swing.plaf.*; |
|
31 |
import javax.swing.plaf.basic.*; |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
32 |
import sun.swing.MenuItemLayoutHelper; |
2 | 33 |
|
34 |
/** |
|
4394 | 35 |
* Provides the Synth L&F UI delegate for |
36 |
* {@link javax.swing.JMenu}. |
|
2 | 37 |
* |
38 |
* @author Georges Saab |
|
39 |
* @author David Karlton |
|
40 |
* @author Arnaud Weber |
|
4394 | 41 |
* @since 1.7 |
2 | 42 |
*/ |
4394 | 43 |
public class SynthMenuUI extends BasicMenuUI |
44 |
implements PropertyChangeListener, SynthUI { |
|
2 | 45 |
private SynthStyle style; |
46 |
private SynthStyle accStyle; |
|
47 |
||
4394 | 48 |
/** |
49 |
* Creates a new UI object for the given component. |
|
50 |
* |
|
51 |
* @param x component to create UI object for |
|
52 |
* @return the UI object |
|
53 |
*/ |
|
2 | 54 |
public static ComponentUI createUI(JComponent x) { |
55 |
return new SynthMenuUI(); |
|
56 |
} |
|
57 |
||
4394 | 58 |
/** |
59 |
* @inheritDoc |
|
60 |
*/ |
|
61 |
@Override |
|
2 | 62 |
protected void installDefaults() { |
63 |
updateStyle(menuItem); |
|
64 |
} |
|
65 |
||
4394 | 66 |
/** |
67 |
* @inheritDoc |
|
68 |
*/ |
|
69 |
@Override |
|
2 | 70 |
protected void installListeners() { |
71 |
super.installListeners(); |
|
72 |
menuItem.addPropertyChangeListener(this); |
|
73 |
} |
|
74 |
||
75 |
private void updateStyle(JMenuItem mi) { |
|
76 |
SynthStyle oldStyle = style; |
|
77 |
SynthContext context = getContext(mi, ENABLED); |
|
78 |
||
79 |
style = SynthLookAndFeel.updateStyle(context, this); |
|
80 |
if (oldStyle != style) { |
|
81 |
String prefix = getPropertyPrefix(); |
|
82 |
defaultTextIconGap = style.getInt( |
|
83 |
context, prefix + ".textIconGap", 4); |
|
84 |
if (menuItem.getMargin() == null || |
|
85 |
(menuItem.getMargin() instanceof UIResource)) { |
|
86 |
Insets insets = (Insets)style.get(context, prefix + ".margin"); |
|
87 |
||
88 |
if (insets == null) { |
|
89 |
// Some places assume margins are non-null. |
|
90 |
insets = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS; |
|
91 |
} |
|
92 |
menuItem.setMargin(insets); |
|
93 |
} |
|
94 |
acceleratorDelimiter = style.getString(context, prefix + |
|
95 |
".acceleratorDelimiter", "+"); |
|
96 |
||
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
97 |
if (MenuItemLayoutHelper.useCheckAndArrow(menuItem)) { |
2 | 98 |
checkIcon = style.getIcon(context, prefix + ".checkIcon"); |
99 |
arrowIcon = style.getIcon(context, prefix + ".arrowIcon"); |
|
100 |
} else { |
|
101 |
// Not needed in this case |
|
102 |
checkIcon = null; |
|
103 |
arrowIcon = null; |
|
104 |
} |
|
105 |
||
106 |
((JMenu)menuItem).setDelay(style.getInt(context, prefix + |
|
107 |
".delay", 200)); |
|
108 |
if (oldStyle != null) { |
|
109 |
uninstallKeyboardActions(); |
|
110 |
installKeyboardActions(); |
|
111 |
} |
|
112 |
} |
|
113 |
context.dispose(); |
|
114 |
||
115 |
SynthContext accContext = getContext(mi, Region.MENU_ITEM_ACCELERATOR, |
|
116 |
ENABLED); |
|
117 |
||
118 |
accStyle = SynthLookAndFeel.updateStyle(accContext, this); |
|
119 |
accContext.dispose(); |
|
120 |
} |
|
121 |
||
4394 | 122 |
/** |
123 |
* @inheritDoc |
|
124 |
*/ |
|
125 |
@Override |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
126 |
public void uninstallUI(JComponent c) { |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
127 |
super.uninstallUI(c); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
128 |
// Remove values from the parent's Client Properties. |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
129 |
JComponent p = MenuItemLayoutHelper.getMenuItemParent((JMenuItem) c); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
130 |
if (p != null) { |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
131 |
p.putClientProperty( |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
132 |
SynthMenuItemLayoutHelper.MAX_ACC_OR_ARROW_WIDTH, null); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
133 |
} |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
134 |
} |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
135 |
|
4394 | 136 |
/** |
137 |
* @inheritDoc |
|
138 |
*/ |
|
139 |
@Override |
|
2 | 140 |
protected void uninstallDefaults() { |
141 |
SynthContext context = getContext(menuItem, ENABLED); |
|
142 |
style.uninstallDefaults(context); |
|
143 |
context.dispose(); |
|
144 |
style = null; |
|
145 |
||
146 |
SynthContext accContext = getContext(menuItem, |
|
147 |
Region.MENU_ITEM_ACCELERATOR, ENABLED); |
|
148 |
accStyle.uninstallDefaults(accContext); |
|
149 |
accContext.dispose(); |
|
150 |
accStyle = null; |
|
151 |
||
152 |
super.uninstallDefaults(); |
|
153 |
} |
|
154 |
||
4394 | 155 |
/** |
156 |
* @inheritDoc |
|
157 |
*/ |
|
158 |
@Override |
|
2 | 159 |
protected void uninstallListeners() { |
160 |
super.uninstallListeners(); |
|
161 |
menuItem.removePropertyChangeListener(this); |
|
162 |
} |
|
163 |
||
4394 | 164 |
/** |
165 |
* @inheritDoc |
|
166 |
*/ |
|
167 |
@Override |
|
2 | 168 |
public SynthContext getContext(JComponent c) { |
169 |
return getContext(c, getComponentState(c)); |
|
170 |
} |
|
171 |
||
172 |
SynthContext getContext(JComponent c, int state) { |
|
4394 | 173 |
Region region = SynthLookAndFeel.getRegion(c); |
2 | 174 |
return SynthContext.getContext(SynthContext.class, c, region, |
175 |
style, state); |
|
176 |
} |
|
177 |
||
4394 | 178 |
SynthContext getContext(JComponent c, Region region) { |
2 | 179 |
return getContext(c, region, getComponentState(c, region)); |
180 |
} |
|
181 |
||
182 |
private SynthContext getContext(JComponent c, Region region, int state) { |
|
183 |
return SynthContext.getContext(SynthContext.class, c, |
|
184 |
region, accStyle, state); |
|
185 |
} |
|
186 |
||
187 |
private int getComponentState(JComponent c) { |
|
188 |
int state; |
|
189 |
||
190 |
if (!c.isEnabled()) { |
|
191 |
return DISABLED; |
|
192 |
} |
|
193 |
if (menuItem.isArmed()) { |
|
194 |
state = MOUSE_OVER; |
|
195 |
} |
|
196 |
else { |
|
197 |
state = SynthLookAndFeel.getComponentState(c); |
|
198 |
} |
|
199 |
if (menuItem.isSelected()) { |
|
200 |
state |= SELECTED; |
|
201 |
} |
|
202 |
return state; |
|
203 |
} |
|
204 |
||
205 |
private int getComponentState(JComponent c, Region region) { |
|
206 |
return getComponentState(c); |
|
207 |
} |
|
208 |
||
4394 | 209 |
/** |
210 |
* @inheritDoc |
|
211 |
*/ |
|
212 |
@Override |
|
2 | 213 |
protected Dimension getPreferredMenuItemSize(JComponent c, |
214 |
Icon checkIcon, |
|
215 |
Icon arrowIcon, |
|
216 |
int defaultTextIconGap) { |
|
217 |
SynthContext context = getContext(c); |
|
218 |
SynthContext accContext = getContext(c, Region.MENU_ITEM_ACCELERATOR); |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
219 |
Dimension value = SynthGraphicsUtils.getPreferredMenuItemSize( |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
220 |
context, accContext, c, checkIcon, arrowIcon, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
221 |
defaultTextIconGap, acceleratorDelimiter, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
222 |
MenuItemLayoutHelper.useCheckAndArrow(menuItem), |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
223 |
getPropertyPrefix()); |
2 | 224 |
context.dispose(); |
225 |
accContext.dispose(); |
|
226 |
return value; |
|
227 |
} |
|
228 |
||
4394 | 229 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
230 |
* Notifies this UI delegate to repaint the specified component. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
231 |
* This method paints the component background, then calls |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
232 |
* the {@link #paint(SynthContext,Graphics)} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
233 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
234 |
* <p>In general, this method does not need to be overridden by subclasses. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
235 |
* All Look and Feel rendering code should reside in the {@code paint} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
236 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
237 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
238 |
* @param c the component being painted |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
239 |
* @see #paint(SynthContext,Graphics) |
4394 | 240 |
*/ |
241 |
@Override |
|
2 | 242 |
public void update(Graphics g, JComponent c) { |
243 |
SynthContext context = getContext(c); |
|
244 |
||
245 |
SynthLookAndFeel.update(context, g); |
|
246 |
context.getPainter().paintMenuBackground(context, |
|
247 |
g, 0, 0, c.getWidth(), c.getHeight()); |
|
248 |
paint(context, g); |
|
249 |
context.dispose(); |
|
250 |
} |
|
251 |
||
4394 | 252 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
253 |
* Paints the specified component according to the Look and Feel. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
254 |
* <p>This method is not used by Synth Look and Feel. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
255 |
* Painting is handled by the {@link #paint(SynthContext,Graphics)} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
256 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
257 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
258 |
* @param c the component being painted |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
259 |
* @see #paint(SynthContext,Graphics) |
4394 | 260 |
*/ |
261 |
@Override |
|
2 | 262 |
public void paint(Graphics g, JComponent c) { |
263 |
SynthContext context = getContext(c); |
|
264 |
||
265 |
paint(context, g); |
|
266 |
context.dispose(); |
|
267 |
} |
|
268 |
||
4394 | 269 |
/** |
270 |
* Paints the specified component. This implementation does nothing. |
|
271 |
* |
|
272 |
* @param context context for the component being painted |
|
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
273 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
274 |
* @see #update(Graphics,JComponent) |
4394 | 275 |
*/ |
2 | 276 |
protected void paint(SynthContext context, Graphics g) { |
277 |
SynthContext accContext = getContext(menuItem, |
|
278 |
Region.MENU_ITEM_ACCELERATOR); |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
279 |
// Refetch the appropriate check indicator for the current state |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
280 |
String prefix = getPropertyPrefix(); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
281 |
Icon checkIcon = style.getIcon(context, prefix + ".checkIcon"); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
282 |
Icon arrowIcon = style.getIcon(context, prefix + ".arrowIcon"); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
283 |
SynthGraphicsUtils.paint(context, accContext, g, checkIcon, arrowIcon, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
284 |
acceleratorDelimiter, defaultTextIconGap, getPropertyPrefix()); |
2 | 285 |
accContext.dispose(); |
286 |
} |
|
287 |
||
4394 | 288 |
/** |
289 |
* @inheritDoc |
|
290 |
*/ |
|
291 |
@Override |
|
2 | 292 |
public void paintBorder(SynthContext context, Graphics g, int x, |
293 |
int y, int w, int h) { |
|
294 |
context.getPainter().paintMenuBorder(context, g, x, y, w, h); |
|
295 |
} |
|
296 |
||
4394 | 297 |
/** |
298 |
* @inheritDoc |
|
299 |
*/ |
|
300 |
@Override |
|
2 | 301 |
public void propertyChange(PropertyChangeEvent e) { |
13147
eada40c7d74d
7169111: Unreadable menu bar with Ambiance theme in GTK L&F
rupashka
parents:
7668
diff
changeset
|
302 |
if (SynthLookAndFeel.shouldUpdateStyle(e) || |
eada40c7d74d
7169111: Unreadable menu bar with Ambiance theme in GTK L&F
rupashka
parents:
7668
diff
changeset
|
303 |
(e.getPropertyName().equals("ancestor") && UIManager.getBoolean("Menu.useMenuBarForTopLevelMenus"))) { |
2 | 304 |
updateStyle((JMenu)e.getSource()); |
305 |
} |
|
306 |
} |
|
307 |
} |