KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > gtk > GTKIconFactory


1 /*
2  * @(#)GTKIconFactory.java 1.23 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.gtk;
8
9 import javax.swing.plaf.synth.*;
10 import java.awt.*;
11 import java.lang.reflect.*;
12 import javax.swing.*;
13 import javax.swing.plaf.*;
14 import sun.swing.plaf.synth.*;
15
16 /**
17  * @version 1.23, 12/19/03
18  */

19 class GTKIconFactory {
20     // Tree icons
21
private static SynthIcon expandedIcon;
22     private static SynthIcon collapsedIcon;
23
24     private static SynthIcon radioButtonIcon;
25
26     private static SynthIcon checkBoxIcon;
27
28     private static SynthIcon menuArrowIcon;
29     private static SynthIcon menuCheckIcon;
30
31     private static SynthIcon menuItemArrowIcon;
32     private static SynthIcon menuItemCheckIcon;
33
34     private static SynthIcon checkBoxMenuItemArrowIcon;
35     private static SynthIcon checkBoxMenuItemCheckIcon;
36
37     private static SynthIcon radioButtonMenuItemArrowIcon;
38     private static SynthIcon radioButtonMenuItemCheckIcon;
39
40     //
41
// Tree methods
42
//
43
public static SynthIcon getTreeExpandedIcon() {
44         if (expandedIcon == null) {
45             expandedIcon =
46                 new SynthExpanderIcon("paintTreeExpandedIcon");
47         }
48         return expandedIcon;
49     }
50
51     public static void paintTreeExpandedIcon(SynthContext context, Graphics g,
52                                       int x, int y, int w, int h) {
53         ((GTKStyle)context.getStyle()).getEngine(context).paintExpander(
54                context, g, GTKLookAndFeel.synthStateToGTKState(
55                context.getRegion(), context.getComponentState()),
56                GTKConstants.EXPANDER_EXPANDED, "treeview", x, y, w, h);
57     }
58
59     public static SynthIcon getTreeCollapsedIcon() {
60         if (collapsedIcon == null) {
61             collapsedIcon =
62                 new SynthExpanderIcon("paintTreeCollapsedIcon");
63         }
64         return collapsedIcon;
65     }
66
67
68     public static void paintTreeCollapsedIcon(SynthContext context, Graphics g,
69                                       int x, int y, int w, int h) {
70         ((GTKStyle)context.getStyle()).getEngine(context).paintExpander(
71                context, g, GTKLookAndFeel.synthStateToGTKState(
72                context.getRegion(), context.getComponentState()),
73                GTKConstants.EXPANDER_COLLAPSED, "treeview", x, y, w, h);
74     }
75
76     //
77
// Radio button
78
//
79
public static SynthIcon getRadioButtonIcon() {
80         if (radioButtonIcon == null) {
81             radioButtonIcon = new DelegatingIcon("paintRadioButtonIcon",
82                                                  13, 13);
83         }
84         return radioButtonIcon;
85     }
86
87     public static void paintRadioButtonIcon(SynthContext context, Graphics g,
88                                             int x, int y, int w, int h) {
89         GTKStyle style = (GTKStyle)context.getStyle();
90         int state = context.getComponentState();
91         int gtkState = GTKLookAndFeel.synthStateToGTKState(context.getRegion(),
92                                                            state);
93         int shadowType = GTKConstants.SHADOW_OUT;
94         // RadioButton painting appears to be special cased to pass
95
// SELECTED into the engine even though text colors are PRESSED.
96
if ((state & SynthConstants.SELECTED) != 0) {
97             gtkState = SynthConstants.SELECTED;
98         }
99         if (gtkState == SynthConstants.SELECTED) {
100             shadowType = GTKConstants.SHADOW_IN;
101         }
102         ((GTKStyle)context.getStyle()).getEngine(
103                 context).paintOption(context, g, gtkState, shadowType,
104                     "radiobutton", x, y, w, h);
105     }
106
107     //
108
// CheckBox
109
//
110
public static SynthIcon getCheckBoxIcon() {
111         if (checkBoxIcon == null) {
112             checkBoxIcon = new DelegatingIcon("paintCheckBoxIcon", 13, 13);
113         }
114         return checkBoxIcon;
115     }
116
117     public static void paintCheckBoxIcon(SynthContext context, Graphics g,
118                                          int x, int y, int w, int h) {
119         GTKStyle style = (GTKStyle)context.getStyle();
120         int state = context.getComponentState();
121         int shadowType = GTKConstants.SHADOW_OUT;
122         if (((JCheckBox)context.getComponent()).isSelected()) {
123             shadowType = GTKConstants.SHADOW_IN;
124         }
125         ((GTKStyle)context.getStyle()).getEngine(
126                 context).paintCheck(context, g,
127                 GTKLookAndFeel.synthStateToGTKState(context.getRegion(),
128                 context.getComponentState()), shadowType, "checkbutton",
129                 x, y, w, h);
130     }
131
132     //
133
// Menus
134
//
135
public static SynthIcon getMenuArrowIcon() {
136         if (menuArrowIcon == null) {
137             menuArrowIcon = new DelegatingIcon("paintMenuArrowIcon", 13, 13);
138         }
139         return menuArrowIcon;
140     }
141
142     public static void paintMenuArrowIcon(SynthContext context, Graphics g,
143                                           int x, int y, int w, int h) {
144         GTKStyle style = (GTKStyle)context.getStyle();
145         int gtkState = GTKLookAndFeel.synthStateToGTKState(
146                 context.getRegion(), context.getComponentState());
147         int shadow = GTKConstants.SHADOW_OUT;
148         if (gtkState == SynthConstants.MOUSE_OVER) {
149             shadow = GTKConstants.SHADOW_IN;
150         }
151         int arrowDir;
152         if (context.getComponent().getComponentOrientation().isLeftToRight()) {
153             arrowDir = GTKConstants.ARROW_RIGHT;
154         }
155         else {
156             arrowDir = GTKConstants.ARROW_LEFT;
157         }
158         style.getEngine(context).paintArrow(context, g, gtkState,
159              shadow, arrowDir, "menuitem", x + 3, y + 3, 7, 7);
160     }
161
162     public static SynthIcon getMenuItemArrowIcon() {
163         if (menuItemArrowIcon == null) {
164             menuItemArrowIcon = new DelegatingIcon("paintMenuItemArrowIcon",
165                                                    13, 13);
166         }
167         return menuItemArrowIcon;
168     }
169
170     public static void paintMenuItemArrowIcon(SynthContext context, Graphics g,
171                                           int x, int y, int w, int h) {
172         // Don't paint anything. We are just reserving space so we align the
173
// menu items correctly.
174
}
175
176     public static SynthIcon getCheckBoxMenuItemArrowIcon() {
177         if (checkBoxMenuItemArrowIcon == null) {
178             checkBoxMenuItemArrowIcon = new DelegatingIcon(
179                 "paintCheckBoxMenuItemArrowIcon", 13, 13);
180         }
181         return checkBoxMenuItemArrowIcon;
182     }
183
184     public static void paintCheckBoxMenuItemArrowIcon(SynthContext context,
185                             Graphics g, int x, int y, int w, int h) {
186         // Don't paint anything. We are just reserving space so we align the
187
// menu items correctly.
188
}
189
190     public static SynthIcon getCheckBoxMenuItemCheckIcon() {
191         if (checkBoxMenuItemCheckIcon == null) {
192             checkBoxMenuItemCheckIcon = new DelegatingIcon(
193                 "paintCheckBoxMenuItemCheckIcon", 13, 13);
194         }
195         return checkBoxMenuItemCheckIcon;
196     }
197
198     public static void paintCheckBoxMenuItemCheckIcon(
199              SynthContext context, Graphics g, int x, int y, int w, int h) {
200         GTKStyle style = (GTKStyle)context.getStyle();
201         int state = context.getComponentState();
202         int shadowType = GTKConstants.SHADOW_OUT;
203         int gtkState = GTKLookAndFeel.synthStateToGTKState(
204                                       context.getRegion(), state);
205         if ((state & SynthConstants.MOUSE_OVER) != 0) {
206             gtkState = SynthConstants.MOUSE_OVER;
207         }
208         if ((state & SynthConstants.SELECTED) != 0) {
209             shadowType = GTKConstants.SHADOW_IN;
210         }
211         style.getEngine(context).paintCheck(context, g, gtkState,
212                     shadowType, "check", x, y, w, h);
213     }
214
215     public static SynthIcon getRadioButtonMenuItemArrowIcon() {
216         if (radioButtonMenuItemArrowIcon == null) {
217             radioButtonMenuItemArrowIcon = new DelegatingIcon(
218                 "paintRadioButtonMenuItemArrowIcon", 13, 13);
219         }
220         return radioButtonMenuItemArrowIcon;
221     }
222
223     public static void paintRadioButtonMenuItemArrowIcon(SynthContext context,
224                             Graphics g, int x, int y, int w, int h) {
225         // Don't paint anything. We are just reserving space so we align the
226
// menu items correctly.
227
}
228
229     public static SynthIcon getRadioButtonMenuItemCheckIcon() {
230         if (radioButtonMenuItemCheckIcon == null) {
231             radioButtonMenuItemCheckIcon = new DelegatingIcon(
232                 "paintRadioButtonMenuItemCheckIcon", 13, 13);
233         }
234         return radioButtonMenuItemCheckIcon;
235     }
236
237     public static void paintRadioButtonMenuItemCheckIcon(
238              SynthContext context, Graphics g, int x, int y, int w, int h) {
239         GTKStyle style = (GTKStyle)context.getStyle();
240         int state = context.getComponentState();
241         int gtkState = GTKLookAndFeel.synthStateToGTKState(
242                                       context.getRegion(), state);
243         if ((state & SynthConstants.MOUSE_OVER) != 0) {
244             gtkState = SynthConstants.MOUSE_OVER;
245         }
246         int shadowType = GTKConstants.SHADOW_OUT;
247         if ((state & SynthConstants.SELECTED) != 0) {
248             shadowType = GTKConstants.SHADOW_IN;
249         }
250         ((GTKStyle)context.getStyle()).getEngine(
251                 context).paintOption(context, g, gtkState, shadowType,
252                     "option", x, y, w, h);
253     }
254
255     //
256
// ToolBar Handle
257
//
258
public static SynthIcon getToolBarHandleIcon() {
259         return new ToolBarHandleIcon();
260     }
261
262     public static void paintToolBarHandleIcon(SynthContext context,
263             Graphics g, int x, int y, int w, int h) {
264         int orientation =
265             ((JToolBar)context.getComponent()).getOrientation() ==
266                 JToolBar.HORIZONTAL ?
267                     GTKConstants.HORIZONTAL : GTKConstants.VERTICAL;
268         GTKStyle style = (GTKStyle)context.getStyle();
269         int gtkState = GTKLookAndFeel.synthStateToGTKState(
270                 context.getRegion(), context.getComponentState());
271         style.getEngine(context).paintHandle(context, g, gtkState,
272                     GTKConstants.SHADOW_OUT, "handlebox", x, y, w, h,
273                     orientation);
274     }
275
276
277     private static class DelegatingIcon extends SynthIcon implements
278                                    UIResource {
279         private static final Class JavaDoc[] PARAM_TYPES = new Class JavaDoc[] {
280             SynthContext.class, Graphics.class, int.class, int.class,
281             int.class, int.class };
282         private int width;
283         private int height;
284         private Object JavaDoc method;
285
286         DelegatingIcon(String JavaDoc methodName, int width, int height) {
287             this.method = methodName;
288             this.width = width;
289             this.height = height;
290         }
291
292         public void paintIcon(SynthContext context, Graphics g, int x, int y,
293                               int w, int h) {
294             if (context != null) {
295                 try {
296                     getMethod().invoke(GTKIconFactory.class, new Object JavaDoc[] {
297                                 context, g, new Integer JavaDoc(x), new Integer JavaDoc(y),
298                                 new Integer JavaDoc(w), new Integer JavaDoc(h) });
299                 } catch (IllegalAccessException JavaDoc iae) {
300                 } catch (InvocationTargetException ite) {
301                 }
302             }
303         }
304
305         public int getIconWidth(SynthContext context) {
306             return width;
307         }
308
309         public int getIconHeight(SynthContext context) {
310             return height;
311         }
312
313         private Method getMethod() {
314             if (method instanceof String JavaDoc) {
315                 Method[] methods = GTKIconFactory.class.getMethods();
316                 try {
317                     method = GTKIconFactory.class.getMethod((String JavaDoc)method,
318                                                             PARAM_TYPES);
319                 } catch (NoSuchMethodException JavaDoc nsme) {
320                     System.out.println("NSME: " + nsme);
321                 }
322             }
323             return (Method)method;
324         }
325     }
326
327     private static class SynthExpanderIcon extends SynthIcon {
328         private static final Class JavaDoc[] PARAM_TYPES = new Class JavaDoc[] {
329             SynthContext.class, Graphics.class, int.class, int.class,
330             int.class, int.class };
331
332             private int width = -1;
333             private int height = -1;
334             private Object JavaDoc method;
335
336         SynthExpanderIcon(String JavaDoc method) {
337             this.method = method;
338         }
339
340         public void paintIcon(SynthContext context, Graphics g, int x, int y,
341                               int w, int h) {
342             if (context != null) {
343                 try {
344                     getMethod().invoke(GTKIconFactory.class, new Object JavaDoc[] {
345                                 context, g, new Integer JavaDoc(x), new Integer JavaDoc(y),
346                                 new Integer JavaDoc(w), new Integer JavaDoc(h) });
347                     updateSizeIfNecessary(context);
348                 } catch (IllegalAccessException JavaDoc iae) {
349                 } catch (InvocationTargetException ite) {
350                 }
351             }
352         }
353
354         public int getIconWidth(SynthContext context) {
355             updateSizeIfNecessary(context);
356             return width;
357         }
358
359         public int getIconHeight(SynthContext context) {
360             updateSizeIfNecessary(context);
361             return height;
362         }
363
364         private void updateSizeIfNecessary(SynthContext context) {
365             if (width == -1 && context != null) {
366                 width = height = context.getStyle().getInt(context,
367                         "Tree.expanderSize", 10);
368             }
369         }
370
371         private Method getMethod() {
372             if (method instanceof String JavaDoc) {
373                 Method[] methods = GTKIconFactory.class.getMethods();
374                 try {
375                     method = GTKIconFactory.class.getMethod((String JavaDoc)method,
376                                                             PARAM_TYPES);
377                 } catch (NoSuchMethodException JavaDoc nsme) {
378                     System.out.println("NSME: " + nsme);
379                 }
380             }
381             return (Method)method;
382         }
383     }
384
385
386     // GTK has a separate widget for the handle box, to mirror this
387
// we create a unique icon per ToolBar and lookup the style for the
388
// HandleBox.
389
private static class ToolBarHandleIcon extends SynthIcon {
390         private SynthStyle style;
391
392         public void paintIcon(SynthContext context, Graphics g, int x, int y,
393                               int w, int h) {
394             if (context != null) {
395                 context = getContext(context);
396                 paintToolBarHandleIcon(context, g, x, y, w, h);
397             }
398         }
399
400         public int getIconWidth(SynthContext context) {
401             if (((JToolBar)context.getComponent()).getOrientation() ==
402                     JToolBar.HORIZONTAL) {
403                 return 10;
404             } else {
405                 return context.getComponent().getWidth();
406             }
407         }
408
409         public int getIconHeight(SynthContext context) {
410             if (((JToolBar)context.getComponent()).getOrientation() ==
411                     JToolBar.HORIZONTAL) {
412                 return context.getComponent().getHeight();
413             } else {
414                 return 10;
415             }
416         }
417
418         private SynthContext getContext(SynthContext context) {
419             if (style == null) {
420                 style = SynthLookAndFeel.getStyleFactory().getStyle(
421                              context.getComponent(), GTKRegion.HANDLE_BOX);
422             }
423             return new SynthContext(context.getComponent(),
424                                     GTKRegion.HANDLE_BOX,
425                                     style, SynthConstants.ENABLED);
426         }
427     }
428 }
429
Popular Tags