KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsIconFactory


1 /*
2  * @(#)WindowsIconFactory.java 1.24 07/01/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import javax.swing.*;
11 import javax.swing.plaf.ButtonUI JavaDoc;
12 import javax.swing.plaf.UIResource JavaDoc;
13
14 import java.awt.*;
15 import java.io.Serializable JavaDoc;
16
17 import com.sun.java.swing.plaf.windows.TMSchema.*;
18 import com.sun.java.swing.plaf.windows.XPStyle.Skin;
19
20 import sun.swing.MenuItemCheckIconFactory;
21
22 /**
23  * Factory object that can vend Icons appropriate for the Windows L & F.
24  * <p>
25  * <strong>Warning:</strong>
26  * Serialized objects of this class will not be compatible with
27  * future Swing releases. The current serialization support is appropriate
28  * for short term storage or RMI between applications running the same
29  * version of Swing. A future release of Swing will provide support for
30  * long term persistence.
31  *
32  * @version 1.24 01/18/07
33  * @author David Kloba
34  * @author Georges Saab
35  * @author Rich Schiavi
36  */

37 public class WindowsIconFactory implements Serializable JavaDoc
38 {
39     private static Icon frame_closeIcon;
40     private static Icon frame_iconifyIcon;
41     private static Icon frame_maxIcon;
42     private static Icon frame_minIcon;
43     private static Icon frame_resizeIcon;
44     private static Icon checkBoxIcon;
45     private static Icon radioButtonIcon;
46     private static Icon checkBoxMenuItemIcon;
47     private static Icon radioButtonMenuItemIcon;
48     private static Icon menuItemCheckIcon;
49     private static Icon menuItemArrowIcon;
50     private static Icon menuArrowIcon;
51     private static VistaMenuItemCheckIconFactory menuItemCheckIconFactory;
52
53     public static Icon getMenuItemCheckIcon() {
54     if (menuItemCheckIcon == null) {
55         menuItemCheckIcon = new MenuItemCheckIcon();
56     }
57     return menuItemCheckIcon;
58     }
59
60     public static Icon getMenuItemArrowIcon() {
61     if (menuItemArrowIcon == null) {
62         menuItemArrowIcon = new MenuItemArrowIcon();
63     }
64     return menuItemArrowIcon;
65     }
66
67     public static Icon getMenuArrowIcon() {
68     if (menuArrowIcon == null) {
69         menuArrowIcon = new MenuArrowIcon();
70     }
71     return menuArrowIcon;
72     }
73
74     public static Icon getCheckBoxIcon() {
75     if (checkBoxIcon == null) {
76         checkBoxIcon = new CheckBoxIcon();
77     }
78     return checkBoxIcon;
79     }
80
81     public static Icon getRadioButtonIcon() {
82     if (radioButtonIcon == null) {
83         radioButtonIcon = new RadioButtonIcon();
84     }
85     return radioButtonIcon;
86     }
87
88     public static Icon getCheckBoxMenuItemIcon() {
89     if (checkBoxMenuItemIcon == null) {
90         checkBoxMenuItemIcon = new CheckBoxMenuItemIcon();
91     }
92     return checkBoxMenuItemIcon;
93     }
94
95     public static Icon getRadioButtonMenuItemIcon() {
96     if (radioButtonMenuItemIcon == null) {
97         radioButtonMenuItemIcon = new RadioButtonMenuItemIcon();
98     }
99     return radioButtonMenuItemIcon;
100     }
101
102     static
103     synchronized VistaMenuItemCheckIconFactory getMenuItemCheckIconFactory() {
104         if (menuItemCheckIconFactory == null) {
105             menuItemCheckIconFactory =
106                 new VistaMenuItemCheckIconFactory();
107         }
108         return menuItemCheckIconFactory;
109     }
110
111     public static Icon createFrameCloseIcon() {
112     if (frame_closeIcon == null) {
113             frame_closeIcon = new FrameButtonIcon(Part.WP_CLOSEBUTTON);
114     }
115     return frame_closeIcon;
116     }
117
118     public static Icon createFrameIconifyIcon() {
119     if (frame_iconifyIcon == null) {
120             frame_iconifyIcon = new FrameButtonIcon(Part.WP_MINBUTTON);
121     }
122     return frame_iconifyIcon;
123     }
124
125     public static Icon createFrameMaximizeIcon() {
126     if (frame_maxIcon == null) {
127             frame_maxIcon = new FrameButtonIcon(Part.WP_MAXBUTTON);
128     }
129     return frame_maxIcon;
130     }
131
132     public static Icon createFrameMinimizeIcon() {
133     if (frame_minIcon == null) {
134             frame_minIcon = new FrameButtonIcon(Part.WP_RESTOREBUTTON);
135     }
136     return frame_minIcon;
137     }
138
139     public static Icon createFrameResizeIcon() {
140     if(frame_resizeIcon == null)
141         frame_resizeIcon = new ResizeIcon();
142     return frame_resizeIcon;
143     }
144
145
146     private static class FrameButtonIcon implements Icon, Serializable JavaDoc {
147         private Part part;
148
149         private FrameButtonIcon(Part part) {
150             this.part = part;
151     }
152
153     public void paintIcon(Component c, Graphics g, int x0, int y0) {
154         int width = getIconWidth();
155         int height = getIconHeight();
156
157         XPStyle xp = XPStyle.getXP();
158         if (xp != null) {
159                 Skin skin = xp.getSkin(c, part);
160         JButton b = (JButton)c;
161         ButtonModel model = b.getModel();
162
163         // Find out if frame is inactive
164
JInternalFrame jif = (JInternalFrame)SwingUtilities.
165                     getAncestorOfClass(JInternalFrame.class, b);
166                 boolean jifSelected = (jif != null && jif.isSelected());
167
168                 State state;
169                 if (jifSelected) {
170                     if (!model.isEnabled()) {
171                         state = State.DISABLED;
172                     } else if (model.isArmed() && model.isPressed()) {
173                         state = State.PUSHED;
174                     } else if (model.isRollover()) {
175                         state = State.HOT;
176                     } else {
177                         state = State.NORMAL;
178                     }
179                 } else {
180                     if (!model.isEnabled()) {
181                         state = State.INACTIVEDISABLED;
182                     } else if (model.isArmed() && model.isPressed()) {
183                         state = State.INACTIVEPUSHED;
184                     } else if (model.isRollover()) {
185                         state = State.INACTIVEHOT;
186                     } else {
187                         state = State.INACTIVENORMAL;
188                     }
189         }
190                 skin.paintSkin(g, 0, 0, width, height, state);
191         } else {
192         g.setColor(Color.black);
193         int x = width / 12 + 2;
194         int y = height / 5;
195         int h = height - y * 2 - 1;
196         int w = width * 3/4 -3;
197         int thickness2 = Math.max(height / 8, 2);
198         int thickness = Math.max(width / 15, 1);
199                 if (part == Part.WP_CLOSEBUTTON) {
200             int lineWidth;
201             if (width > 47) lineWidth = 6;
202             else if (width > 37) lineWidth = 5;
203             else if (width > 26) lineWidth = 4;
204             else if (width > 16) lineWidth = 3;
205             else if (width > 12) lineWidth = 2;
206             else lineWidth = 1;
207             y = height / 12 + 2;
208             if (lineWidth == 1) {
209             if (w % 2 == 1) { x++; w++; }
210             g.drawLine(x, y, x+w-2, y+w-2);
211             g.drawLine(x+w-2, y, x, y+w-2);
212             } else if (lineWidth == 2) {
213             if (w > 6) { x++; w--; }
214             g.drawLine(x, y, x+w-2, y+w-2);
215             g.drawLine(x+w-2, y, x, y+w-2);
216             g.drawLine(x+1, y, x+w-1, y+w-2);
217             g.drawLine(x+w-1, y, x+1, y+w-2);
218             } else {
219             x += 2; y++; w -= 2;
220             g.drawLine(x, y, x+w-1, y+w-1);
221             g.drawLine(x+w-1, y, x, y+w-1);
222             g.drawLine(x+1, y, x+w-1, y+w-2);
223             g.drawLine(x+w-2, y, x, y+w-2);
224             g.drawLine(x, y+1, x+w-2, y+w-1);
225             g.drawLine(x+w-1, y+1, x+1, y+w-1);
226             for (int i = 4; i <= lineWidth; i++) {
227                 g.drawLine(x+i-2, y, x+w-1, y+w-i+1);
228                 g.drawLine(x, y+i-2, x+w-i+1, y+w-1);
229                 g.drawLine(x+w-i+1, y, x, y+w-i+1);
230                 g.drawLine(x+w-1, y+i-2, x+i-2, y+w-1);
231             }
232             }
233                 } else if (part == Part.WP_MINBUTTON) {
234             g.fillRect(x, y+h-thickness2, w-w/3, thickness2);
235                 } else if (part == Part.WP_MAXBUTTON) {
236             g.fillRect(x, y, w, thickness2);
237             g.fillRect(x, y, thickness, h);
238             g.fillRect(x+w-thickness, y, thickness, h);
239             g.fillRect(x, y+h-thickness, w, thickness);
240                 } else if (part == Part.WP_RESTOREBUTTON) {
241             g.fillRect(x+w/3, y, w-w/3, thickness2);
242             g.fillRect(x+w/3, y, thickness, h/3);
243             g.fillRect(x+w-thickness, y, thickness, h-h/3);
244             g.fillRect(x+w-w/3, y+h-h/3-thickness, w/3, thickness);
245
246             g.fillRect(x, y+h/3, w-w/3, thickness2);
247             g.fillRect(x, y+h/3, thickness, h-h/3);
248             g.fillRect(x+w-w/3-thickness, y+h/3, thickness, h-h/3);
249             g.fillRect(x, y+h-thickness, w-w/3, thickness);
250         }
251         }
252     }
253
254     public int getIconWidth() {
255         int width;
256         if (XPStyle.getXP() != null) {
257         // Fix for XP bug where sometimes these sizes aren't updated properly
258
// Assume for now that XP buttons are always square
259
width = UIManager.getInt("InternalFrame.titleButtonHeight") -2;
260         } else {
261         width = UIManager.getInt("InternalFrame.titleButtonWidth") -2;
262         }
263         if (XPStyle.getXP() != null) {
264         width -= 2;
265         }
266         return width;
267     }
268
269     public int getIconHeight() {
270         int height = UIManager.getInt("InternalFrame.titleButtonHeight")-4;
271         return height;
272     }
273     }
274
275
276
277         private static class ResizeIcon implements Icon, Serializable JavaDoc {
278             public void paintIcon(Component c, Graphics g, int x, int y) {
279                 g.setColor(UIManager.getColor("InternalFrame.resizeIconHighlight"));
280                 g.drawLine(0, 11, 11, 0);
281                 g.drawLine(4, 11, 11, 4);
282                 g.drawLine(8, 11, 11, 8);
283
284                 g.setColor(UIManager.getColor("InternalFrame.resizeIconShadow"));
285                 g.drawLine(1, 11, 11, 1);
286                 g.drawLine(2, 11, 11, 2);
287                 g.drawLine(5, 11, 11, 5);
288                 g.drawLine(6, 11, 11, 6);
289                 g.drawLine(9, 11, 11, 9);
290                 g.drawLine(10, 11, 11, 10);
291             }
292             public int getIconWidth() { return 13; }
293             public int getIconHeight() { return 13; }
294         };
295
296     private static class CheckBoxIcon implements Icon, Serializable JavaDoc
297     {
298     final static int csize = 13;
299     public void paintIcon(Component c, Graphics g, int x, int y) {
300         JCheckBox cb = (JCheckBox) c;
301         ButtonModel model = cb.getModel();
302         XPStyle xp = XPStyle.getXP();
303
304         if (xp != null) {
305                 State state;
306         if (model.isSelected()) {
307                     state = State.CHECKEDNORMAL;
308                     if (!model.isEnabled()) {
309                         state = State.CHECKEDDISABLED;
310                     } else if (model.isPressed() && model.isArmed()) {
311                         state = State.CHECKEDPRESSED;
312                     } else if (model.isRollover()) {
313                         state = State.CHECKEDHOT;
314                     }
315                 } else {
316                     state = State.UNCHECKEDNORMAL;
317                     if (!model.isEnabled()) {
318                         state = State.UNCHECKEDDISABLED;
319                     } else if (model.isPressed() && model.isArmed()) {
320                         state = State.UNCHECKEDPRESSED;
321                     } else if (model.isRollover()) {
322                         state = State.UNCHECKEDHOT;
323                     }
324         }
325                 Part part = Part.BP_CHECKBOX;
326                 xp.getSkin(c, part).paintSkin(g, x, y, state);
327         } else {
328         // outer bevel
329
if(!cb.isBorderPaintedFlat()) {
330             // Outer top/left
331
g.setColor(UIManager.getColor("CheckBox.shadow"));
332             g.drawLine(x, y, x+11, y);
333             g.drawLine(x, y+1, x, y+11);
334
335             // Outer bottom/right
336
g.setColor(UIManager.getColor("CheckBox.highlight"));
337             g.drawLine(x+12, y, x+12, y+12);
338             g.drawLine(x, y+12, x+11, y+12);
339
340             // Inner top.left
341
g.setColor(UIManager.getColor("CheckBox.darkShadow"));
342             g.drawLine(x+1, y+1, x+10, y+1);
343             g.drawLine(x+1, y+2, x+1, y+10);
344
345             // Inner bottom/right
346
g.setColor(UIManager.getColor("CheckBox.light"));
347             g.drawLine(x+1, y+11, x+11, y+11);
348             g.drawLine(x+11, y+1, x+11, y+10);
349
350             // inside box
351
if((model.isPressed() && model.isArmed()) || !model.isEnabled()) {
352             g.setColor(UIManager.getColor("CheckBox.background"));
353             } else {
354             g.setColor(UIManager.getColor("CheckBox.interiorBackground"));
355             }
356             g.fillRect(x+2, y+2, csize-4, csize-4);
357         } else {
358             g.setColor(UIManager.getColor("CheckBox.shadow"));
359             g.drawRect(x+1, y+1, csize-3, csize-3);
360
361             if((model.isPressed() && model.isArmed()) || !model.isEnabled()) {
362             g.setColor(UIManager.getColor("CheckBox.background"));
363             } else {
364             g.setColor(UIManager.getColor("CheckBox.interiorBackground"));
365             }
366             g.fillRect(x+2, y+2, csize-4, csize-4);
367         }
368
369         if(model.isEnabled()) {
370             g.setColor(UIManager.getColor("CheckBox.darkShadow"));
371         } else {
372             g.setColor(UIManager.getColor("CheckBox.shadow"));
373         }
374
375         // paint check
376
if (model.isSelected()) {
377             g.drawLine(x+9, y+3, x+9, y+3);
378             g.drawLine(x+8, y+4, x+9, y+4);
379             g.drawLine(x+7, y+5, x+9, y+5);
380             g.drawLine(x+6, y+6, x+8, y+6);
381             g.drawLine(x+3, y+7, x+7, y+7);
382             g.drawLine(x+4, y+8, x+6, y+8);
383             g.drawLine(x+5, y+9, x+5, y+9);
384             g.drawLine(x+3, y+5, x+3, y+5);
385             g.drawLine(x+3, y+6, x+4, y+6);
386         }
387         }
388     }
389
390     public int getIconWidth() {
391         XPStyle xp = XPStyle.getXP();
392         if (xp != null) {
393                 return xp.getSkin(null, Part.BP_CHECKBOX).getWidth();
394         } else {
395         return csize;
396         }
397     }
398         
399     public int getIconHeight() {
400         XPStyle xp = XPStyle.getXP();
401         if (xp != null) {
402                 return xp.getSkin(null, Part.BP_CHECKBOX).getHeight();
403         } else {
404         return csize;
405         }
406     }
407     }
408
409     private static class RadioButtonIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc
410     {
411     public void paintIcon(Component c, Graphics g, int x, int y) {
412         AbstractButton b = (AbstractButton) c;
413         ButtonModel model = b.getModel();
414         XPStyle xp = XPStyle.getXP();
415
416         if (xp != null) {
417                 Part part = Part.BP_RADIOBUTTON;
418                 Skin skin = xp.getSkin(b, part);
419                 State state;
420         int index = 0;
421         if (model.isSelected()) {
422                     state = State.CHECKEDNORMAL;
423                     if (!model.isEnabled()) {
424                         state = State.CHECKEDDISABLED;
425                     } else if (model.isPressed() && model.isArmed()) {
426                         state = State.CHECKEDPRESSED;
427                     } else if (model.isRollover()) {
428                         state = State.CHECKEDHOT;
429                     }
430                 } else {
431                     state = State.UNCHECKEDNORMAL;
432                     if (!model.isEnabled()) {
433                         state = State.UNCHECKEDDISABLED;
434                     } else if (model.isPressed() && model.isArmed()) {
435                         state = State.UNCHECKEDPRESSED;
436                     } else if (model.isRollover()) {
437                         state = State.UNCHECKEDHOT;
438                     }
439         }
440                 skin.paintSkin(g, x, y, state);
441         } else {
442         // fill interior
443
if((model.isPressed() && model.isArmed()) || !model.isEnabled()) {
444             g.setColor(UIManager.getColor("RadioButton.background"));
445         } else {
446             g.setColor(UIManager.getColor("RadioButton.interiorBackground"));
447         }
448         g.fillRect(x+2, y+2, 8, 8);
449
450
451             // outter left arc
452
g.setColor(UIManager.getColor("RadioButton.shadow"));
453         g.drawLine(x+4, y+0, x+7, y+0);
454         g.drawLine(x+2, y+1, x+3, y+1);
455         g.drawLine(x+8, y+1, x+9, y+1);
456         g.drawLine(x+1, y+2, x+1, y+3);
457         g.drawLine(x+0, y+4, x+0, y+7);
458         g.drawLine(x+1, y+8, x+1, y+9);
459
460         // outter right arc
461
g.setColor(UIManager.getColor("RadioButton.highlight"));
462         g.drawLine(x+2, y+10, x+3, y+10);
463         g.drawLine(x+4, y+11, x+7, y+11);
464         g.drawLine(x+8, y+10, x+9, y+10);
465         g.drawLine(x+10, y+9, x+10, y+8);
466         g.drawLine(x+11, y+7, x+11, y+4);
467         g.drawLine(x+10, y+3, x+10, y+2);
468
469
470         // inner left arc
471
g.setColor(UIManager.getColor("RadioButton.darkShadow"));
472         g.drawLine(x+4, y+1, x+7, y+1);
473         g.drawLine(x+2, y+2, x+3, y+2);
474         g.drawLine(x+8, y+2, x+9, y+2);
475         g.drawLine(x+2, y+3, x+2, y+3);
476         g.drawLine(x+1, y+4, x+1, y+7);
477         g.drawLine(x+2, y+8, x+2, y+8);
478
479
480         // inner right arc
481
g.setColor(UIManager.getColor("RadioButton.light"));
482         g.drawLine(x+2, y+9, x+3, y+9);
483         g.drawLine(x+4, y+10, x+7, y+10);
484         g.drawLine(x+8, y+9, x+9, y+9);
485         g.drawLine(x+9, y+8, x+9, y+8);
486         g.drawLine(x+10, y+7, x+10, y+4);
487         g.drawLine(x+9, y+3, x+9, y+3);
488
489
490         // indicate whether selected or not
491
if(model.isSelected()) {
492             g.setColor(UIManager.getColor("RadioButton.darkShadow"));
493             g.fillRect(x+4, y+5, 4, 2);
494             g.fillRect(x+5, y+4, 2, 4);
495         }
496         }
497     }
498
499     public int getIconWidth() {
500         XPStyle xp = XPStyle.getXP();
501         if (xp != null) {
502                 return xp.getSkin(null, Part.BP_RADIOBUTTON).getWidth();
503         } else {
504         return 13;
505         }
506     }
507         
508     public int getIconHeight() {
509         XPStyle xp = XPStyle.getXP();
510         if (xp != null) {
511                 return xp.getSkin(null, Part.BP_RADIOBUTTON).getHeight();
512         } else {
513         return 13;
514         }
515     }
516     } // end class RadioButtonIcon
517

518
519     private static class CheckBoxMenuItemIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc
520     {
521     public void paintIcon(Component c, Graphics g, int x, int y) {
522         AbstractButton b = (AbstractButton) c;
523         ButtonModel model = b.getModel();
524         boolean isSelected = model.isSelected();
525         if (isSelected) {
526         y = y - getIconHeight() / 2;
527         g.drawLine(x+9, y+3, x+9, y+3);
528         g.drawLine(x+8, y+4, x+9, y+4);
529         g.drawLine(x+7, y+5, x+9, y+5);
530         g.drawLine(x+6, y+6, x+8, y+6);
531         g.drawLine(x+3, y+7, x+7, y+7);
532         g.drawLine(x+4, y+8, x+6, y+8);
533         g.drawLine(x+5, y+9, x+5, y+9);
534         g.drawLine(x+3, y+5, x+3, y+5);
535         g.drawLine(x+3, y+6, x+4, y+6);
536         }
537     }
538     public int getIconWidth() { return 9; }
539     public int getIconHeight() { return 9; }
540
541     } // End class CheckBoxMenuItemIcon
542

543     
544     private static class RadioButtonMenuItemIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc
545     {
546     public void paintIcon(Component c, Graphics g, int x, int y) {
547         AbstractButton b = (AbstractButton) c;
548         ButtonModel model = b.getModel();
549         if (b.isSelected() == true) {
550                g.fillArc(0,0,getIconWidth()-2, getIconHeight()-2, 0, 360);
551         }
552     }
553     public int getIconWidth() { return 12; }
554     public int getIconHeight() { return 12; }
555
556     } // End class RadioButtonMenuItemIcon
557

558
559     private static class MenuItemCheckIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc{
560     public void paintIcon(Component c, Graphics g, int x, int y) {
561         /* For debugging:
562            Color oldColor = g.getColor();
563         g.setColor(Color.orange);
564         g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true);
565         g.setColor(oldColor);
566         */

567     }
568     public int getIconWidth() { return 9; }
569     public int getIconHeight() { return 9; }
570
571     } // End class MenuItemCheckIcon
572

573     private static class MenuItemArrowIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc {
574     public void paintIcon(Component c, Graphics g, int x, int y) {
575         /* For debugging:
576         Color oldColor = g.getColor();
577         g.setColor(Color.green);
578         g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true);
579         g.setColor(oldColor);
580         */

581     }
582     public int getIconWidth() { return 4; }
583     public int getIconHeight() { return 8; }
584
585     } // End class MenuItemArrowIcon
586

587     private static class MenuArrowIcon implements Icon, UIResource JavaDoc, Serializable JavaDoc {
588     public void paintIcon(Component c, Graphics g, int x, int y) {
589             if (WindowsMenuItemUI.isVistaPainting()) {
590                 XPStyle xp = XPStyle.getXP();
591                 State state = State.NORMAL;
592                 if (c instanceof JMenuItem) {
593                     state = ((JMenuItem) c).getModel().isEnabled()
594                     ? State.NORMAL : State.DISABLED;
595                 }
596                 Skin skin = xp.getSkin(c, Part.MP_POPUPSUBMENU);
597                 if (WindowsGraphicsUtils.isLeftToRight(c)) {
598                     skin.paintSkin(g, x, y, state);
599                 } else {
600                     Graphics2D g2d = (Graphics2D)g.create();
601                     g2d.translate(x + skin.getWidth(), y);
602                     g2d.scale(-1, 1);
603                     skin.paintSkin(g2d, 0, 0, state);
604                     g2d.dispose();
605                 }
606             } else {
607                 g.translate(x,y);
608                 if( WindowsGraphicsUtils.isLeftToRight(c) ) {
609                     g.drawLine( 0, 0, 0, 7 );
610                     g.drawLine( 1, 1, 1, 6 );
611                     g.drawLine( 2, 2, 2, 5 );
612                     g.drawLine( 3, 3, 3, 4 );
613                 } else {
614                     g.drawLine( 4, 0, 4, 7 );
615                     g.drawLine( 3, 1, 3, 6 );
616                     g.drawLine( 2, 2, 2, 5 );
617                     g.drawLine( 1, 3, 1, 4 );
618                 }
619                 g.translate(-x,-y);
620             }
621     }
622         public int getIconWidth() {
623             if (WindowsMenuItemUI.isVistaPainting()) {
624                 Skin skin = XPStyle.getXP().getSkin(null, Part.MP_POPUPSUBMENU);
625                 return skin.getWidth();
626             } else {
627                 return 4;
628             }
629         }
630         public int getIconHeight() {
631             if (WindowsMenuItemUI.isVistaPainting()) {
632                 Skin skin = XPStyle.getXP().getSkin(null, Part.MP_POPUPSUBMENU);
633                 return skin.getHeight();
634             } else {
635                 return 8;
636             }
637         }
638     } // End class MenuArrowIcon
639

640     static class VistaMenuItemCheckIconFactory
641            implements MenuItemCheckIconFactory {
642         private static final int OFFSET = 3;
643         
644         public Icon getIcon(JMenuItem component) {
645             return new VistaMenuItemCheckIcon(component);
646         }
647         
648         public boolean isCompatible(Object JavaDoc icon, String JavaDoc prefix) {
649             return icon instanceof VistaMenuItemCheckIcon
650               && ((VistaMenuItemCheckIcon) icon).type == getType(prefix);
651         }
652         
653         public Icon getIcon(String JavaDoc type) {
654             return new VistaMenuItemCheckIcon(type);
655         }
656         
657         static int getIconWidth() {
658             return XPStyle.getXP().getSkin(null, Part.MP_POPUPCHECK).getWidth()
659                 + 2 * OFFSET;
660         }
661         
662         private static Class JavaDoc<? extends JMenuItem> getType(Component c) {
663             Class JavaDoc<? extends JMenuItem> rv = null;
664             if (c instanceof JCheckBoxMenuItem) {
665                 rv = JCheckBoxMenuItem.class;
666             } else if (c instanceof JRadioButtonMenuItem) {
667                 rv = JRadioButtonMenuItem.class;
668             } else if (c instanceof JMenu) {
669                 rv = JMenu.class;
670             } else if (c instanceof JMenuItem) {
671                 rv = JMenuItem.class;
672             }
673             return rv;
674         }
675         
676         private static Class JavaDoc<? extends JMenuItem> getType(String JavaDoc type) {
677             Class JavaDoc<? extends JMenuItem> rv = null;
678             if (type == "CheckBoxMenuItem") {
679                 rv = JCheckBoxMenuItem.class;
680             } else if (type == "RadioButtonMenuItem") {
681                 rv = JRadioButtonMenuItem.class;
682             } else if (type == "Menu") {
683                 rv = JMenu.class;
684             } else if (type == "MenuItem") {
685                 rv = JMenuItem.class;
686             } else {
687                 // this should never happen
688
rv = JMenuItem.class;
689             }
690             return rv;
691         }
692         
693         /**
694          * CheckIcon for JMenuItem, JMenu, JCheckBoxMenuItem and
695          * JRadioButtonMenuItem.
696          * Note: to be used on Vista only.
697          */

698         private static class VistaMenuItemCheckIcon
699               implements Icon, UIResource JavaDoc, Serializable JavaDoc {
700             
701             private final JMenuItem menuItem;
702             private final Class JavaDoc<? extends JMenuItem> type;
703             
704             VistaMenuItemCheckIcon(JMenuItem menuItem) {
705                 this.type = getType(menuItem);
706                 this.menuItem = menuItem;
707             }
708             VistaMenuItemCheckIcon(String JavaDoc type) {
709                 this.type = getType(type);
710                 this.menuItem = null;
711             }
712             
713             public int getIconHeight() {
714                 Icon lafIcon = getLaFIcon();
715                 if (lafIcon != null) {
716                     return lafIcon.getIconHeight();
717                 }
718                 Icon icon = getIcon();
719                 int height = 0;
720                 if (icon != null) {
721                     height = icon.getIconHeight() + 2 * OFFSET;
722                 } else {
723                     Skin skin =
724                         XPStyle.getXP().getSkin(null, Part.MP_POPUPCHECK);
725                     height = skin.getHeight() + 2 * OFFSET;
726                 }
727                 return height;
728             }
729
730             public int getIconWidth() {
731                 Icon lafIcon = getLaFIcon();
732                 if (lafIcon != null) {
733                     return lafIcon.getIconWidth();
734                 }
735                 Icon icon = getIcon();
736                 int width = 0;
737                 if (icon != null) {
738                     width = icon.getIconWidth() + 2 * OFFSET;
739                 } else {
740                     width = VistaMenuItemCheckIconFactory.getIconWidth();
741                 }
742                 return width;
743             }
744             
745             public void paintIcon(Component c, Graphics g, int x, int y) {
746                 Icon lafIcon = getLaFIcon();
747                 if (lafIcon != null) {
748                     lafIcon.paintIcon(c, g, x, y);
749                     return;
750                 }
751                 assert menuItem == null || c == menuItem;
752                 Icon icon = getIcon();
753                 if (type == JCheckBoxMenuItem.class
754                       || type == JRadioButtonMenuItem.class) {
755                     AbstractButton b = (AbstractButton) c;
756                     if (b.isSelected()) {
757                         Part backgroundPart = Part.MP_POPUPCHECKBACKGROUND;
758                         Part part = Part.MP_POPUPCHECK;
759                         State backgroundState;
760                         State state;
761                         if (isEnabled(c, null)) {
762                             backgroundState =
763                                 (icon != null) ? State.BITMAP : State.NORMAL;
764                             state = (type == JRadioButtonMenuItem.class)
765                               ? State.BULLETNORMAL
766                               : State.CHECKMARKNORMAL;
767                         } else {
768                             backgroundState = State.DISABLEDPUSHED;
769                             state =
770                                 (type == JRadioButtonMenuItem.class)
771                                   ? State.BULLETDISABLED
772                                   : State.CHECKMARKDISABLED;
773                         }
774                         Skin skin;
775                         XPStyle xp = XPStyle.getXP();
776                         skin = xp.getSkin(c, backgroundPart);
777                         skin.paintSkin(g, x, y,
778                             getIconWidth(), getIconHeight(), backgroundState);
779                         if (icon == null) {
780                             skin = xp.getSkin(c, part);
781                             skin.paintSkin(g, x + OFFSET, y + OFFSET, state);
782                         }
783                     }
784                 }
785                 if (icon != null) {
786                     icon.paintIcon(c, g, x + OFFSET, y + OFFSET);
787                 }
788             }
789             private static WindowsMenuItemUIAccessor getAccessor(
790                     JMenuItem menuItem) {
791                 WindowsMenuItemUIAccessor rv = null;
792                 ButtonUI JavaDoc uiObject = (menuItem != null) ? menuItem.getUI()
793                         : null;
794                 if (uiObject instanceof WindowsMenuItemUI) {
795                     rv = ((WindowsMenuItemUI) uiObject).accessor;
796                 } else if (uiObject instanceof WindowsMenuUI) {
797                     rv = ((WindowsMenuUI) uiObject).accessor;
798                 } else if (uiObject instanceof WindowsCheckBoxMenuItemUI) {
799                     rv = ((WindowsCheckBoxMenuItemUI) uiObject).accessor;
800                 } else if (uiObject instanceof WindowsRadioButtonMenuItemUI) {
801                     rv = ((WindowsRadioButtonMenuItemUI) uiObject).accessor;
802                 }
803                 return rv;
804             }
805             
806             private static boolean isEnabled(Component c, State state) {
807                 if (state == null && c instanceof JMenuItem) {
808                     WindowsMenuItemUIAccessor accessor =
809                         getAccessor((JMenuItem) c);
810                     if (accessor != null) {
811                         state = accessor.getState((JMenuItem) c);
812                     }
813                 }
814                 if (state == null) {
815                     if (c != null) {
816                         return c.isEnabled();
817                     } else {
818                         return true;
819                     }
820                 } else {
821                     return (state != State.DISABLED)
822                         && (state != State.DISABLEDHOT)
823                         && (state != State.DISABLEDPUSHED);
824                 }
825             }
826             private Icon getIcon() {
827                 Icon rv = null;
828                 if (menuItem == null) {
829                     return rv;
830                 }
831                 WindowsMenuItemUIAccessor accessor =
832                     getAccessor(menuItem);
833                 State state = (accessor != null) ? accessor.getState(menuItem)
834                         : null;
835                 if (isEnabled(menuItem, null)) {
836                     if (state == State.PUSHED) {
837                         rv = menuItem.getPressedIcon();
838                     } else {
839                         rv = menuItem.getIcon();
840                     }
841                 } else {
842                     rv = menuItem.getDisabledIcon();
843                 }
844                 return rv;
845             }
846             /**
847              * Check if developer changed icon in the UI table.
848              *
849              * @return the icon to use or {@code null} if the current one is to
850              * be used
851              */

852             private Icon getLaFIcon() {
853                 // use icon from the UI table if it does not match this one.
854
Icon rv = (Icon) UIManager.getDefaults().get(typeToString(type));
855                 if (rv instanceof VistaMenuItemCheckIcon
856                       && ((VistaMenuItemCheckIcon) rv).type == type) {
857                     rv = null;
858                 }
859                 return rv;
860             }
861             
862             private static String JavaDoc typeToString(
863                     Class JavaDoc<? extends JMenuItem> type) {
864                 assert type == JMenuItem.class
865                     || type == JMenu.class
866                     || type == JCheckBoxMenuItem.class
867                     || type == JRadioButtonMenuItem.class;
868                 StringBuilder JavaDoc sb = new StringBuilder JavaDoc(type.getName());
869                 // remove package name, dot and the first character
870
sb.delete(0, sb.lastIndexOf("J") + 1);
871                 sb.append(".checkIcon");
872                 return sb.toString();
873             }
874         }
875     } // End class VistaMenuItemCheckIconFactory
876
}
877
878
Popular Tags