author | mlapshin |
Fri, 08 Aug 2008 20:49:26 +0400 | |
changeset 1295 | 3cf2264a5743 |
parent 2 | 90ce3da70b43 |
child 4394 | 92a8ec883f5d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
2 |
* Copyright 2002-2008 Sun Microsystems, Inc. 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 |
|
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; |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
38 |
import sun.swing.MenuItemLayoutHelper; |
2 | 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 |
||
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
89 |
if (MenuItemLayoutHelper.useCheckAndArrow(menuItem)) { |
2 | 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 |
||
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
114 |
public void uninstallUI(JComponent c) { |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
115 |
super.uninstallUI(c); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
116 |
// Remove values from the parent's Client Properties. |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
117 |
JComponent p = MenuItemLayoutHelper.getMenuItemParent((JMenuItem) c); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
118 |
if (p != null) { |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
119 |
p.putClientProperty( |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
120 |
SynthMenuItemLayoutHelper.MAX_ACC_OR_ARROW_WIDTH, null); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
121 |
} |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
122 |
} |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
123 |
|
2 | 124 |
protected void uninstallDefaults() { |
125 |
SynthContext context = getContext(menuItem, ENABLED); |
|
126 |
style.uninstallDefaults(context); |
|
127 |
context.dispose(); |
|
128 |
style = null; |
|
129 |
||
130 |
SynthContext accContext = getContext(menuItem, |
|
131 |
Region.MENU_ITEM_ACCELERATOR, ENABLED); |
|
132 |
accStyle.uninstallDefaults(accContext); |
|
133 |
accContext.dispose(); |
|
134 |
accStyle = null; |
|
135 |
||
136 |
super.uninstallDefaults(); |
|
137 |
} |
|
138 |
||
139 |
protected void uninstallListeners() { |
|
140 |
super.uninstallListeners(); |
|
141 |
menuItem.removePropertyChangeListener(this); |
|
142 |
} |
|
143 |
||
144 |
public SynthContext getContext(JComponent c) { |
|
145 |
return getContext(c, getComponentState(c)); |
|
146 |
} |
|
147 |
||
148 |
SynthContext getContext(JComponent c, int state) { |
|
149 |
Region region = getRegion(c); |
|
150 |
return SynthContext.getContext(SynthContext.class, c, region, |
|
151 |
style, state); |
|
152 |
} |
|
153 |
||
154 |
public SynthContext getContext(JComponent c, Region region) { |
|
155 |
return getContext(c, region, getComponentState(c, region)); |
|
156 |
} |
|
157 |
||
158 |
private SynthContext getContext(JComponent c, Region region, int state) { |
|
159 |
return SynthContext.getContext(SynthContext.class, c, |
|
160 |
region, accStyle, state); |
|
161 |
} |
|
162 |
||
163 |
private Region getRegion(JComponent c) { |
|
164 |
return SynthLookAndFeel.getRegion(c); |
|
165 |
} |
|
166 |
||
167 |
private int getComponentState(JComponent c) { |
|
168 |
int state; |
|
169 |
||
170 |
if (!c.isEnabled()) { |
|
171 |
return DISABLED; |
|
172 |
} |
|
173 |
if (menuItem.isArmed()) { |
|
174 |
state = MOUSE_OVER; |
|
175 |
} |
|
176 |
else { |
|
177 |
state = SynthLookAndFeel.getComponentState(c); |
|
178 |
} |
|
179 |
if (menuItem.isSelected()) { |
|
180 |
state |= SELECTED; |
|
181 |
} |
|
182 |
return state; |
|
183 |
} |
|
184 |
||
185 |
private int getComponentState(JComponent c, Region region) { |
|
186 |
return getComponentState(c); |
|
187 |
} |
|
188 |
||
189 |
protected Dimension getPreferredMenuItemSize(JComponent c, |
|
190 |
Icon checkIcon, |
|
191 |
Icon arrowIcon, |
|
192 |
int defaultTextIconGap) { |
|
193 |
SynthContext context = getContext(c); |
|
194 |
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
|
195 |
Dimension value = SynthGraphicsUtils.getPreferredMenuItemSize( |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
196 |
context, accContext, c, checkIcon, arrowIcon, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
197 |
defaultTextIconGap, acceleratorDelimiter, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
198 |
MenuItemLayoutHelper.useCheckAndArrow(menuItem), |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
199 |
getPropertyPrefix()); |
2 | 200 |
context.dispose(); |
201 |
accContext.dispose(); |
|
202 |
return value; |
|
203 |
} |
|
204 |
||
205 |
||
206 |
public void update(Graphics g, JComponent c) { |
|
207 |
SynthContext context = getContext(c); |
|
208 |
||
209 |
SynthLookAndFeel.update(context, g); |
|
210 |
context.getPainter().paintMenuBackground(context, |
|
211 |
g, 0, 0, c.getWidth(), c.getHeight()); |
|
212 |
paint(context, g); |
|
213 |
context.dispose(); |
|
214 |
} |
|
215 |
||
216 |
public void paint(Graphics g, JComponent c) { |
|
217 |
SynthContext context = getContext(c); |
|
218 |
||
219 |
paint(context, g); |
|
220 |
context.dispose(); |
|
221 |
} |
|
222 |
||
223 |
protected void paint(SynthContext context, Graphics g) { |
|
224 |
SynthContext accContext = getContext(menuItem, |
|
225 |
Region.MENU_ITEM_ACCELERATOR); |
|
1295
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
226 |
// 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
|
227 |
String prefix = getPropertyPrefix(); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
228 |
Icon checkIcon = style.getIcon(context, prefix + ".checkIcon"); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
229 |
Icon arrowIcon = style.getIcon(context, prefix + ".arrowIcon"); |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
230 |
SynthGraphicsUtils.paint(context, accContext, g, checkIcon, arrowIcon, |
3cf2264a5743
6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents:
2
diff
changeset
|
231 |
acceleratorDelimiter, defaultTextIconGap, getPropertyPrefix()); |
2 | 232 |
accContext.dispose(); |
233 |
} |
|
234 |
||
235 |
public void paintBorder(SynthContext context, Graphics g, int x, |
|
236 |
int y, int w, int h) { |
|
237 |
context.getPainter().paintMenuBorder(context, g, x, y, w, h); |
|
238 |
} |
|
239 |
||
240 |
public void propertyChange(PropertyChangeEvent e) { |
|
241 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) { |
|
242 |
updateStyle((JMenu)e.getSource()); |
|
243 |
} |
|
244 |
} |
|
245 |
} |