author | alexsch |
Wed, 18 Nov 2015 19:13:42 +0400 | |
changeset 34407 | 2b40f400a30b |
parent 25859 | 3317bb8137f4 |
child 37698 | 4d798c873df0 |
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 |
context.dispose(); |
|
128 |
||
129 |
SynthContext accContext = getContext(mi, Region.MENU_ITEM_ACCELERATOR, |
|
130 |
ENABLED); |
|
131 |
||
132 |
accStyle = SynthLookAndFeel.updateStyle(accContext, this); |
|
133 |
accContext.dispose(); |
|
134 |
} |
|
135 |
||
4394 | 136 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
137 |
* {@inheritDoc} |
4394 | 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 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
156 |
* {@inheritDoc} |
4394 | 157 |
*/ |
158 |
@Override |
|
2 | 159 |
protected void uninstallListeners() { |
160 |
super.uninstallListeners(); |
|
161 |
menuItem.removePropertyChangeListener(this); |
|
162 |
} |
|
163 |
||
4394 | 164 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
165 |
* {@inheritDoc} |
4394 | 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) { |
|
25100 | 173 |
return SynthContext.getContext(c, style, state); |
2 | 174 |
} |
175 |
||
4394 | 176 |
SynthContext getContext(JComponent c, Region region) { |
2 | 177 |
return getContext(c, region, getComponentState(c, region)); |
178 |
} |
|
179 |
||
180 |
private SynthContext getContext(JComponent c, Region region, int state) { |
|
25100 | 181 |
return SynthContext.getContext(c, region, accStyle, state); |
2 | 182 |
} |
183 |
||
184 |
private int getComponentState(JComponent c) { |
|
185 |
int state; |
|
186 |
||
187 |
if (!c.isEnabled()) { |
|
188 |
state = DISABLED; |
|
189 |
} |
|
190 |
else if (menuItem.isArmed()) { |
|
191 |
state = MOUSE_OVER; |
|
192 |
} |
|
193 |
else { |
|
194 |
state = SynthLookAndFeel.getComponentState(c); |
|
195 |
} |
|
196 |
if (menuItem.isSelected()) { |
|
197 |
state |= SELECTED; |
|
198 |
} |
|
199 |
return state; |
|
200 |
} |
|
201 |
||
202 |
private int getComponentState(JComponent c, Region region) { |
|
203 |
return getComponentState(c); |
|
204 |
} |
|
205 |
||
4394 | 206 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
207 |
* {@inheritDoc} |
4394 | 208 |
*/ |
209 |
@Override |
|
2 | 210 |
protected Dimension getPreferredMenuItemSize(JComponent c, |
211 |
Icon checkIcon, |
|
212 |
Icon arrowIcon, |
|
213 |
int defaultTextIconGap) { |
|
214 |
SynthContext context = getContext(c); |
|
215 |
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
|
216 |
Dimension value = SynthGraphicsUtils.getPreferredMenuItemSize( |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
217 |
context, accContext, c, checkIcon, arrowIcon, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
218 |
defaultTextIconGap, acceleratorDelimiter, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
219 |
MenuItemLayoutHelper.useCheckAndArrow(menuItem), |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
220 |
getPropertyPrefix()); |
2 | 221 |
context.dispose(); |
222 |
accContext.dispose(); |
|
223 |
return value; |
|
224 |
} |
|
225 |
||
226 |
||
4394 | 227 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
228 |
* 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
|
229 |
* 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
|
230 |
* 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
|
231 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
232 |
* <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
|
233 |
* 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
|
234 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
235 |
* @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
|
236 |
* @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
|
237 |
* @see #paint(SynthContext,Graphics) |
4394 | 238 |
*/ |
239 |
@Override |
|
2 | 240 |
public void update(Graphics g, JComponent c) { |
241 |
SynthContext context = getContext(c); |
|
242 |
||
243 |
SynthLookAndFeel.update(context, g); |
|
244 |
paintBackground(context, g, c); |
|
245 |
paint(context, g); |
|
246 |
context.dispose(); |
|
247 |
} |
|
248 |
||
4394 | 249 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
250 |
* 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
|
251 |
* <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
|
252 |
* 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
|
253 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
254 |
* @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
|
255 |
* @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
|
256 |
* @see #paint(SynthContext,Graphics) |
4394 | 257 |
*/ |
258 |
@Override |
|
2 | 259 |
public void paint(Graphics g, JComponent c) { |
260 |
SynthContext context = getContext(c); |
|
261 |
||
262 |
paint(context, g); |
|
263 |
context.dispose(); |
|
264 |
} |
|
265 |
||
4394 | 266 |
/** |
267 |
* Paints the specified component. |
|
268 |
* |
|
269 |
* @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
|
270 |
* @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
|
271 |
* @see #update(Graphics,JComponent) |
4394 | 272 |
*/ |
2 | 273 |
protected void paint(SynthContext context, Graphics g) { |
274 |
SynthContext accContext = getContext(menuItem, |
|
275 |
Region.MENU_ITEM_ACCELERATOR); |
|
276 |
||
277 |
// Refetch the appropriate check indicator for the current state |
|
278 |
String prefix = getPropertyPrefix(); |
|
279 |
Icon checkIcon = style.getIcon(context, prefix + ".checkIcon"); |
|
280 |
Icon arrowIcon = style.getIcon(context, prefix + ".arrowIcon"); |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
281 |
SynthGraphicsUtils.paint(context, accContext, g, checkIcon, arrowIcon, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
1290
diff
changeset
|
282 |
acceleratorDelimiter, defaultTextIconGap, getPropertyPrefix()); |
2 | 283 |
accContext.dispose(); |
284 |
} |
|
285 |
||
286 |
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
|
287 |
SynthGraphicsUtils.paintBackground(context, g, c); |
2 | 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 paintBorder(SynthContext context, Graphics g, int x, |
295 |
int y, int w, int h) { |
|
296 |
context.getPainter().paintMenuItemBorder(context, g, x, y, w, h); |
|
297 |
} |
|
298 |
||
4394 | 299 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
300 |
* {@inheritDoc} |
4394 | 301 |
*/ |
302 |
@Override |
|
2 | 303 |
public void propertyChange(PropertyChangeEvent e) { |
304 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
|
305 |
updateStyle((JMenuItem)e.getSource()); |
|
306 |
} |
|
307 |
} |
|
308 |
} |