author | alexsch |
Fri, 15 Apr 2016 19:15:12 +0400 | |
changeset 37698 | 4d798c873df0 |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
20168
diff
changeset
|
2 |
* Copyright (c) 2002, 2013, 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.PropertyChangeEvent; |
|
29 |
import java.beans.PropertyChangeListener; |
|
30 |
import javax.swing.*; |
|
31 |
import javax.swing.plaf.*; |
|
32 |
import javax.swing.plaf.basic.*; |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
33 |
import sun.swing.MenuItemLayoutHelper; |
2 | 34 |
|
35 |
||
36 |
/** |
|
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
37 |
* Provides the Synth L&F UI delegate for |
4394 | 38 |
* {@link javax.swing.JMenuItem}. |
2 | 39 |
* |
40 |
* @author Georges Saab |
|
41 |
* @author David Karlton |
|
42 |
* @author Arnaud Weber |
|
43 |
* @author Fredrik Lagerblad |
|
4394 | 44 |
* @since 1.7 |
2 | 45 |
*/ |
4394 | 46 |
public class SynthMenuItemUI extends BasicMenuItemUI implements |
2 | 47 |
PropertyChangeListener, SynthUI { |
48 |
private SynthStyle style; |
|
49 |
private SynthStyle accStyle; |
|
50 |
||
4394 | 51 |
/** |
52 |
* Creates a new UI object for the given component. |
|
53 |
* |
|
54 |
* @param c component to create UI object for |
|
55 |
* @return the UI object |
|
56 |
*/ |
|
2 | 57 |
public static ComponentUI createUI(JComponent c) { |
58 |
return new SynthMenuItemUI(); |
|
59 |
} |
|
60 |
||
4394 | 61 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
62 |
* {@inheritDoc} |
4394 | 63 |
*/ |
64 |
@Override |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
65 |
public void uninstallUI(JComponent c) { |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
66 |
super.uninstallUI(c); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
67 |
// Remove values from the parent's Client Properties. |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
68 |
JComponent p = MenuItemLayoutHelper.getMenuItemParent((JMenuItem) c); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
69 |
if (p != null) { |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
70 |
p.putClientProperty( |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
71 |
SynthMenuItemLayoutHelper.MAX_ACC_OR_ARROW_WIDTH, null); |
2 | 72 |
} |
73 |
} |
|
74 |
||
4394 | 75 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
76 |
* {@inheritDoc} |
4394 | 77 |
*/ |
78 |
@Override |
|
2 | 79 |
protected void installDefaults() { |
80 |
updateStyle(menuItem); |
|
81 |
} |
|
82 |
||
4394 | 83 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
84 |
* {@inheritDoc} |
4394 | 85 |
*/ |
86 |
@Override |
|
2 | 87 |
protected void installListeners() { |
88 |
super.installListeners(); |
|
89 |
menuItem.addPropertyChangeListener(this); |
|
90 |
} |
|
91 |
||
92 |
private void updateStyle(JMenuItem mi) { |
|
93 |
SynthContext context = getContext(mi, ENABLED); |
|
94 |
SynthStyle oldStyle = style; |
|
95 |
||
96 |
style = SynthLookAndFeel.updateStyle(context, this); |
|
97 |
if (oldStyle != style) { |
|
98 |
String prefix = getPropertyPrefix(); |
|
99 |
||
100 |
Object value = style.get(context, prefix + ".textIconGap"); |
|
101 |
if (value != null) { |
|
102 |
LookAndFeel.installProperty(mi, "iconTextGap", value); |
|
103 |
} |
|
104 |
defaultTextIconGap = mi.getIconTextGap(); |
|
105 |
||
106 |
if (menuItem.getMargin() == null || |
|
107 |
(menuItem.getMargin() instanceof UIResource)) { |
|
108 |
Insets insets = (Insets)style.get(context, prefix + ".margin"); |
|
109 |
||
110 |
if (insets == null) { |
|
111 |
// Some places assume margins are non-null. |
|
112 |
insets = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS; |
|
113 |
} |
|
114 |
menuItem.setMargin(insets); |
|
115 |
} |
|
116 |
acceleratorDelimiter = style.getString(context, prefix + |
|
117 |
".acceleratorDelimiter", "+"); |
|
118 |
||
119 |
arrowIcon = style.getIcon(context, prefix + ".arrowIcon"); |
|
120 |
||
121 |
checkIcon = style.getIcon(context, prefix + ".checkIcon"); |
|
122 |
if (oldStyle != null) { |
|
123 |
uninstallKeyboardActions(); |
|
124 |
installKeyboardActions(); |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
SynthContext accContext = getContext(mi, Region.MENU_ITEM_ACCELERATOR, |
|
129 |
ENABLED); |
|
130 |
||
131 |
accStyle = SynthLookAndFeel.updateStyle(accContext, this); |
|
132 |
} |
|
133 |
||
4394 | 134 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
135 |
* {@inheritDoc} |
4394 | 136 |
*/ |
137 |
@Override |
|
2 | 138 |
protected void uninstallDefaults() { |
139 |
SynthContext context = getContext(menuItem, ENABLED); |
|
140 |
style.uninstallDefaults(context); |
|
141 |
style = null; |
|
142 |
||
143 |
SynthContext accContext = getContext(menuItem, |
|
144 |
Region.MENU_ITEM_ACCELERATOR, ENABLED); |
|
145 |
accStyle.uninstallDefaults(accContext); |
|
146 |
accStyle = null; |
|
147 |
||
148 |
super.uninstallDefaults(); |
|
149 |
} |
|
150 |
||
4394 | 151 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
152 |
* {@inheritDoc} |
4394 | 153 |
*/ |
154 |
@Override |
|
2 | 155 |
protected void uninstallListeners() { |
156 |
super.uninstallListeners(); |
|
157 |
menuItem.removePropertyChangeListener(this); |
|
158 |
} |
|
159 |
||
4394 | 160 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
161 |
* {@inheritDoc} |
4394 | 162 |
*/ |
163 |
@Override |
|
2 | 164 |
public SynthContext getContext(JComponent c) { |
165 |
return getContext(c, getComponentState(c)); |
|
166 |
} |
|
167 |
||
168 |
SynthContext getContext(JComponent c, int state) { |
|
25100 | 169 |
return SynthContext.getContext(c, style, state); |
2 | 170 |
} |
171 |
||
4394 | 172 |
SynthContext getContext(JComponent c, Region region) { |
2 | 173 |
return getContext(c, region, getComponentState(c, region)); |
174 |
} |
|
175 |
||
176 |
private SynthContext getContext(JComponent c, Region region, int state) { |
|
25100 | 177 |
return SynthContext.getContext(c, region, accStyle, state); |
2 | 178 |
} |
179 |
||
180 |
private int getComponentState(JComponent c) { |
|
181 |
int state; |
|
182 |
||
183 |
if (!c.isEnabled()) { |
|
184 |
state = DISABLED; |
|
185 |
} |
|
186 |
else if (menuItem.isArmed()) { |
|
187 |
state = MOUSE_OVER; |
|
188 |
} |
|
189 |
else { |
|
190 |
state = SynthLookAndFeel.getComponentState(c); |
|
191 |
} |
|
192 |
if (menuItem.isSelected()) { |
|
193 |
state |= SELECTED; |
|
194 |
} |
|
195 |
return state; |
|
196 |
} |
|
197 |
||
198 |
private int getComponentState(JComponent c, Region region) { |
|
199 |
return getComponentState(c); |
|
200 |
} |
|
201 |
||
4394 | 202 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
203 |
* {@inheritDoc} |
4394 | 204 |
*/ |
205 |
@Override |
|
2 | 206 |
protected Dimension getPreferredMenuItemSize(JComponent c, |
207 |
Icon checkIcon, |
|
208 |
Icon arrowIcon, |
|
209 |
int defaultTextIconGap) { |
|
210 |
SynthContext context = getContext(c); |
|
211 |
SynthContext accContext = getContext(c, Region.MENU_ITEM_ACCELERATOR); |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
212 |
Dimension value = SynthGraphicsUtils.getPreferredMenuItemSize( |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
213 |
context, accContext, c, checkIcon, arrowIcon, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
214 |
defaultTextIconGap, acceleratorDelimiter, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
215 |
MenuItemLayoutHelper.useCheckAndArrow(menuItem), |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
216 |
getPropertyPrefix()); |
2 | 217 |
return value; |
218 |
} |
|
219 |
||
220 |
||
4394 | 221 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
222 |
* 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
|
223 |
* 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
|
224 |
* 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
|
225 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
226 |
* <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
|
227 |
* 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
|
228 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
229 |
* @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
|
230 |
* @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
|
231 |
* @see #paint(SynthContext,Graphics) |
4394 | 232 |
*/ |
233 |
@Override |
|
2 | 234 |
public void update(Graphics g, JComponent c) { |
235 |
SynthContext context = getContext(c); |
|
236 |
||
237 |
SynthLookAndFeel.update(context, g); |
|
238 |
paintBackground(context, g, c); |
|
239 |
paint(context, g); |
|
240 |
} |
|
241 |
||
4394 | 242 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
243 |
* 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
|
244 |
* <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
|
245 |
* 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
|
246 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
247 |
* @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
|
248 |
* @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
|
249 |
* @see #paint(SynthContext,Graphics) |
4394 | 250 |
*/ |
251 |
@Override |
|
2 | 252 |
public void paint(Graphics g, JComponent c) { |
253 |
SynthContext context = getContext(c); |
|
254 |
||
255 |
paint(context, g); |
|
256 |
} |
|
257 |
||
4394 | 258 |
/** |
259 |
* Paints the specified component. |
|
260 |
* |
|
261 |
* @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
|
262 |
* @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
|
263 |
* @see #update(Graphics,JComponent) |
4394 | 264 |
*/ |
2 | 265 |
protected void paint(SynthContext context, Graphics g) { |
266 |
SynthContext accContext = getContext(menuItem, |
|
267 |
Region.MENU_ITEM_ACCELERATOR); |
|
268 |
||
269 |
// Refetch the appropriate check indicator for the current state |
|
270 |
String prefix = getPropertyPrefix(); |
|
271 |
Icon checkIcon = style.getIcon(context, prefix + ".checkIcon"); |
|
272 |
Icon arrowIcon = style.getIcon(context, prefix + ".arrowIcon"); |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
273 |
SynthGraphicsUtils.paint(context, accContext, g, checkIcon, arrowIcon, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
274 |
acceleratorDelimiter, defaultTextIconGap, getPropertyPrefix()); |
2 | 275 |
} |
276 |
||
277 |
void paintBackground(SynthContext context, Graphics g, JComponent c) { |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
278 |
SynthGraphicsUtils.paintBackground(context, g, c); |
2 | 279 |
} |
280 |
||
4394 | 281 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
282 |
* {@inheritDoc} |
4394 | 283 |
*/ |
284 |
@Override |
|
2 | 285 |
public void paintBorder(SynthContext context, Graphics g, int x, |
286 |
int y, int w, int h) { |
|
287 |
context.getPainter().paintMenuItemBorder(context, g, x, y, w, h); |
|
288 |
} |
|
289 |
||
4394 | 290 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
291 |
* {@inheritDoc} |
4394 | 292 |
*/ |
293 |
@Override |
|
2 | 294 |
public void propertyChange(PropertyChangeEvent e) { |
295 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
|
296 |
updateStyle((JMenuItem)e.getSource()); |
|
297 |
} |
|
298 |
} |
|
299 |
} |