author | alexsch |
Wed, 18 Nov 2015 19:13:42 +0400 | |
changeset 34407 | 2b40f400a30b |
parent 25859 | 3317bb8137f4 |
child 34408 | ab286f70dd5e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2002, 2014, 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 com.sun.java.swing.plaf.gtk; |
|
26 |
||
34407
2b40f400a30b
8081411: Add an API for painting an icon with a SynthContext
alexsch
parents:
25859
diff
changeset
|
27 |
import javax.swing.plaf.synth.SynthIcon; |
2 | 28 |
import java.util.*; |
29 |
import javax.swing.plaf.synth.*; |
|
30 |
import java.awt.*; |
|
31 |
import java.awt.image.BufferedImage; |
|
32 |
import java.lang.reflect.*; |
|
33 |
import javax.swing.*; |
|
34 |
import javax.swing.plaf.*; |
|
35 |
import sun.swing.plaf.synth.*; |
|
36 |
import com.sun.java.swing.plaf.gtk.GTKConstants.ArrowType; |
|
37 |
import com.sun.java.swing.plaf.gtk.GTKConstants.ExpanderStyle; |
|
38 |
import com.sun.java.swing.plaf.gtk.GTKConstants.Orientation; |
|
39 |
import com.sun.java.swing.plaf.gtk.GTKConstants.ShadowType; |
|
40 |
||
41 |
/** |
|
42 |
*/ |
|
43 |
class GTKIconFactory { |
|
44 |
static final int CHECK_ICON_EXTRA_INSET = 1; |
|
45 |
static final int DEFAULT_ICON_SPACING = 2; |
|
46 |
static final int DEFAULT_ICON_SIZE = 13; |
|
47 |
static final int DEFAULT_TOGGLE_MENU_ITEM_SIZE = 12; // For pre-gtk2.4 |
|
48 |
||
49 |
private static final String RADIO_BUTTON_ICON = "paintRadioButtonIcon"; |
|
50 |
private static final String CHECK_BOX_ICON = "paintCheckBoxIcon"; |
|
51 |
private static final String MENU_ARROW_ICON = "paintMenuArrowIcon"; |
|
52 |
private static final String CHECK_BOX_MENU_ITEM_CHECK_ICON = |
|
53 |
"paintCheckBoxMenuItemCheckIcon"; |
|
54 |
private static final String RADIO_BUTTON_MENU_ITEM_CHECK_ICON = |
|
55 |
"paintRadioButtonMenuItemCheckIcon"; |
|
56 |
private static final String TREE_EXPANDED_ICON = "paintTreeExpandedIcon"; |
|
57 |
private static final String TREE_COLLAPSED_ICON = "paintTreeCollapsedIcon"; |
|
58 |
private static final String ASCENDING_SORT_ICON = "paintAscendingSortIcon"; |
|
59 |
private static final String DESCENDING_SORT_ICON = "paintDescendingSortIcon"; |
|
60 |
private static final String TOOL_BAR_HANDLE_ICON = "paintToolBarHandleIcon"; |
|
61 |
||
62 |
private static Map<String, DelegatingIcon> iconsPool = |
|
63 |
Collections.synchronizedMap(new HashMap<String, DelegatingIcon>()); |
|
64 |
||
65 |
private static DelegatingIcon getIcon(String methodName) { |
|
66 |
DelegatingIcon result = iconsPool.get(methodName); |
|
67 |
if (result == null) { |
|
68 |
if (methodName == TREE_COLLAPSED_ICON || |
|
69 |
methodName == TREE_EXPANDED_ICON) |
|
70 |
{ |
|
71 |
result = new SynthExpanderIcon(methodName); |
|
72 |
||
73 |
} else if (methodName == TOOL_BAR_HANDLE_ICON) { |
|
74 |
result = new ToolBarHandleIcon(); |
|
75 |
||
76 |
} else if (methodName == MENU_ARROW_ICON) { |
|
77 |
result = new MenuArrowIcon(); |
|
78 |
||
79 |
} else { |
|
80 |
result = new DelegatingIcon(methodName); |
|
81 |
} |
|
82 |
iconsPool.put(methodName, result); |
|
83 |
} |
|
84 |
return result; |
|
85 |
} |
|
86 |
||
87 |
// |
|
88 |
// Sort arrow |
|
89 |
// |
|
90 |
public static Icon getAscendingSortIcon() { |
|
91 |
return getIcon(ASCENDING_SORT_ICON); |
|
92 |
} |
|
93 |
||
94 |
public static Icon getDescendingSortIcon() { |
|
95 |
return getIcon(DESCENDING_SORT_ICON); |
|
96 |
} |
|
97 |
||
98 |
// |
|
99 |
// Tree methods |
|
100 |
// |
|
101 |
public static SynthIcon getTreeExpandedIcon() { |
|
102 |
return getIcon(TREE_EXPANDED_ICON); |
|
103 |
} |
|
104 |
||
105 |
public static SynthIcon getTreeCollapsedIcon() { |
|
106 |
return getIcon(TREE_COLLAPSED_ICON); |
|
107 |
} |
|
108 |
||
109 |
// |
|
110 |
// Radio button |
|
111 |
// |
|
112 |
public static SynthIcon getRadioButtonIcon() { |
|
113 |
return getIcon(RADIO_BUTTON_ICON); |
|
114 |
} |
|
115 |
||
116 |
// |
|
117 |
// CheckBox |
|
118 |
// |
|
119 |
public static SynthIcon getCheckBoxIcon() { |
|
120 |
return getIcon(CHECK_BOX_ICON); |
|
121 |
} |
|
122 |
||
123 |
// |
|
124 |
// Menus |
|
125 |
// |
|
126 |
public static SynthIcon getMenuArrowIcon() { |
|
127 |
return getIcon(MENU_ARROW_ICON); |
|
128 |
} |
|
129 |
||
130 |
public static SynthIcon getCheckBoxMenuItemCheckIcon() { |
|
131 |
return getIcon(CHECK_BOX_MENU_ITEM_CHECK_ICON); |
|
132 |
} |
|
133 |
||
134 |
public static SynthIcon getRadioButtonMenuItemCheckIcon() { |
|
135 |
return getIcon(RADIO_BUTTON_MENU_ITEM_CHECK_ICON); |
|
136 |
} |
|
137 |
||
138 |
// |
|
139 |
// ToolBar Handle |
|
140 |
// |
|
141 |
public static SynthIcon getToolBarHandleIcon() { |
|
142 |
return getIcon(TOOL_BAR_HANDLE_ICON); |
|
143 |
} |
|
144 |
||
145 |
static void resetIcons() { |
|
146 |
synchronized (iconsPool) { |
|
147 |
for (DelegatingIcon di: iconsPool.values()) { |
|
148 |
di.resetIconDimensions(); |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
private static class DelegatingIcon extends SynthIcon implements |
|
154 |
UIResource { |
|
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
5506
diff
changeset
|
155 |
private static final Class<?>[] PARAM_TYPES = new Class<?>[] { |
2 | 156 |
SynthContext.class, Graphics.class, int.class, |
157 |
int.class, int.class, int.class, int.class |
|
158 |
}; |
|
159 |
||
160 |
private Object method; |
|
161 |
int iconDimension = -1; |
|
162 |
||
163 |
DelegatingIcon(String methodName ){ |
|
164 |
this.method = methodName; |
|
165 |
} |
|
166 |
||
167 |
public void paintIcon(SynthContext context, Graphics g, |
|
168 |
int x, int y, int w, int h) { |
|
169 |
if (context != null) { |
|
170 |
GTKPainter.INSTANCE.paintIcon(context, g, |
|
171 |
getMethod(), x, y, w, h); |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
public int getIconWidth(SynthContext context) { |
|
176 |
return getIconDimension(context); |
|
177 |
} |
|
178 |
||
179 |
public int getIconHeight(SynthContext context) { |
|
180 |
return getIconDimension(context); |
|
181 |
} |
|
182 |
||
183 |
void resetIconDimensions() { |
|
184 |
iconDimension = -1; |
|
185 |
} |
|
186 |
||
187 |
protected Method getMethod() { |
|
188 |
if (method instanceof String) { |
|
189 |
method = resolveMethod((String)method); |
|
190 |
} |
|
191 |
return (Method)method; |
|
192 |
} |
|
193 |
||
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
5506
diff
changeset
|
194 |
protected Class<?>[] getMethodParamTypes() { |
2 | 195 |
return PARAM_TYPES; |
196 |
} |
|
197 |
||
198 |
private Method resolveMethod(String name) { |
|
199 |
try { |
|
200 |
return GTKPainter.class.getMethod(name, getMethodParamTypes()); |
|
201 |
} catch (NoSuchMethodException e) { |
|
202 |
assert false; |
|
203 |
} |
|
204 |
return null; |
|
205 |
} |
|
206 |
||
207 |
int getIconDimension(SynthContext context) { |
|
208 |
if (iconDimension >= 0) { |
|
209 |
return iconDimension; |
|
210 |
} |
|
211 |
||
212 |
if (context == null) { |
|
213 |
return DEFAULT_ICON_SIZE; |
|
214 |
} |
|
215 |
||
216 |
Region region = context.getRegion(); |
|
217 |
GTKStyle style = (GTKStyle) context.getStyle(); |
|
218 |
iconDimension = style.getClassSpecificIntValue(context, |
|
219 |
"indicator-size", |
|
220 |
(region == Region.CHECK_BOX_MENU_ITEM || |
|
221 |
region == Region.RADIO_BUTTON_MENU_ITEM) ? |
|
222 |
DEFAULT_TOGGLE_MENU_ITEM_SIZE : DEFAULT_ICON_SIZE); |
|
223 |
||
224 |
if (region == Region.CHECK_BOX || region == Region.RADIO_BUTTON) { |
|
225 |
iconDimension += 2 * style.getClassSpecificIntValue(context, |
|
226 |
"indicator-spacing", DEFAULT_ICON_SPACING); |
|
227 |
} else if (region == Region.CHECK_BOX_MENU_ITEM || |
|
228 |
region == Region.RADIO_BUTTON_MENU_ITEM) { |
|
229 |
iconDimension += 2 * CHECK_ICON_EXTRA_INSET; |
|
230 |
} |
|
231 |
return iconDimension; |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
private static class SynthExpanderIcon extends DelegatingIcon { |
|
236 |
SynthExpanderIcon(String method) { |
|
237 |
super(method); |
|
238 |
} |
|
239 |
||
240 |
public void paintIcon(SynthContext context, Graphics g, int x, int y, |
|
241 |
int w, int h) { |
|
242 |
if (context != null) { |
|
243 |
super.paintIcon(context, g, x, y, w, h); |
|
244 |
updateSizeIfNecessary(context); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
int getIconDimension(SynthContext context) { |
|
249 |
updateSizeIfNecessary(context); |
|
250 |
return (iconDimension == -1) ? DEFAULT_ICON_SIZE : |
|
251 |
iconDimension; |
|
252 |
} |
|
253 |
||
254 |
private void updateSizeIfNecessary(SynthContext context) { |
|
255 |
if (iconDimension == -1 && context != null) { |
|
256 |
iconDimension = context.getStyle().getInt(context, |
|
257 |
"Tree.expanderSize", 10); |
|
258 |
} |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
// GTK has a separate widget for the handle box, to mirror this |
|
263 |
// we create a unique icon per ToolBar and lookup the style for the |
|
264 |
// HandleBox. |
|
265 |
private static class ToolBarHandleIcon extends DelegatingIcon { |
|
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
5506
diff
changeset
|
266 |
private static final Class<?>[] PARAM_TYPES = new Class<?>[] { |
2 | 267 |
SynthContext.class, Graphics.class, int.class, |
268 |
int.class, int.class, int.class, int.class, Orientation.class, |
|
269 |
}; |
|
270 |
||
271 |
private SynthStyle style; |
|
272 |
||
273 |
public ToolBarHandleIcon() { |
|
274 |
super(TOOL_BAR_HANDLE_ICON); |
|
275 |
} |
|
276 |
||
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
5506
diff
changeset
|
277 |
protected Class<?>[] getMethodParamTypes() { |
2 | 278 |
return PARAM_TYPES; |
279 |
} |
|
280 |
||
281 |
public void paintIcon(SynthContext context, Graphics g, int x, int y, |
|
282 |
int w, int h) { |
|
2495
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
283 |
if (context != null) { |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
284 |
JToolBar toolbar = (JToolBar)context.getComponent(); |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
285 |
Orientation orientation = |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
286 |
(toolbar.getOrientation() == JToolBar.HORIZONTAL ? |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
287 |
Orientation.HORIZONTAL : Orientation.VERTICAL); |
2 | 288 |
|
2495
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
289 |
if (style == null) { |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
290 |
style = SynthLookAndFeel.getStyleFactory().getStyle( |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
291 |
context.getComponent(), GTKRegion.HANDLE_BOX); |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
292 |
} |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
293 |
context = new SynthContext(toolbar, GTKRegion.HANDLE_BOX, |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
294 |
style, SynthConstants.ENABLED); |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
295 |
|
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
296 |
GTKPainter.INSTANCE.paintIcon(context, g, |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
297 |
getMethod(), x, y, w, h, orientation); |
2 | 298 |
} |
299 |
} |
|
300 |
||
301 |
public int getIconWidth(SynthContext context) { |
|
302 |
if (context == null) { |
|
303 |
return 10; |
|
304 |
} |
|
305 |
if (((JToolBar)context.getComponent()).getOrientation() == |
|
306 |
JToolBar.HORIZONTAL) { |
|
307 |
return 10; |
|
308 |
} else { |
|
309 |
return context.getComponent().getWidth(); |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
public int getIconHeight(SynthContext context) { |
|
314 |
if (context == null) { |
|
315 |
return 10; |
|
316 |
} |
|
317 |
if (((JToolBar)context.getComponent()).getOrientation() == |
|
318 |
JToolBar.HORIZONTAL) { |
|
319 |
return context.getComponent().getHeight(); |
|
320 |
} else { |
|
321 |
return 10; |
|
322 |
} |
|
323 |
} |
|
324 |
} |
|
325 |
||
326 |
private static class MenuArrowIcon extends DelegatingIcon { |
|
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
5506
diff
changeset
|
327 |
private static final Class<?>[] PARAM_TYPES = new Class<?>[] { |
2 | 328 |
SynthContext.class, Graphics.class, int.class, |
329 |
int.class, int.class, int.class, int.class, ArrowType.class, |
|
330 |
}; |
|
331 |
||
332 |
public MenuArrowIcon() { |
|
333 |
super(MENU_ARROW_ICON); |
|
334 |
} |
|
335 |
||
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
5506
diff
changeset
|
336 |
protected Class<?>[] getMethodParamTypes() { |
2 | 337 |
return PARAM_TYPES; |
338 |
} |
|
339 |
||
340 |
public void paintIcon(SynthContext context, Graphics g, int x, int y, |
|
341 |
int w, int h) { |
|
2495
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
342 |
if (context != null) { |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
343 |
ArrowType arrowDir = ArrowType.RIGHT; |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
344 |
if (!context.getComponent().getComponentOrientation().isLeftToRight()) { |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
345 |
arrowDir = ArrowType.LEFT; |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
346 |
} |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
347 |
GTKPainter.INSTANCE.paintIcon(context, g, |
3e32bdf43a35
6635110: GTK problem when testing Sun Studio IDE on snv_77 with jdk1.6 using Gnome window manager
peterz
parents:
2
diff
changeset
|
348 |
getMethod(), x, y, w, h, arrowDir); |
2 | 349 |
} |
350 |
} |
|
351 |
} |
|
352 |
} |