KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > windows > WindowsIconFactory


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.windows;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.io.Serializable JavaDoc;
36
37 import javax.swing.AbstractButton JavaDoc;
38 import javax.swing.ButtonModel JavaDoc;
39 import javax.swing.Icon JavaDoc;
40 import javax.swing.JCheckBox JavaDoc;
41 import javax.swing.UIManager JavaDoc;
42 import javax.swing.plaf.UIResource JavaDoc;
43
44 /**
45  * Factory class that vends <code>Icon</code>s used in the JGoodies Windows look&amp;feel.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.2 $
49  */

50 final class WindowsIconFactory {
51
52
53     // Accessing and Creating Icons *****************************************************
54

55     private static Icon JavaDoc checkBoxIcon;
56     private static Icon JavaDoc radioButtonIcon;
57
58
59     /**
60      * Returns an <code>Icon</code> for a <code>JCheckBox</code>.
61      */

62     static Icon JavaDoc getCheckBoxIcon() {
63         if (checkBoxIcon == null) {
64             checkBoxIcon = new CheckBoxIcon();
65         }
66         return checkBoxIcon;
67     }
68
69
70     /**
71      * Returns an <code>Icon</code> for a <code>JCheckBox</code>.
72      */

73     static Icon JavaDoc getRadioButtonIcon() {
74         if (radioButtonIcon == null) {
75             radioButtonIcon = new RadioButtonIcon();
76         }
77         return radioButtonIcon;
78     }
79
80
81     // Helper Classes *******************************************************************
82

83     // Paints the icon for a check box.
84
private static class CheckBoxIcon implements Icon JavaDoc, Serializable JavaDoc {
85         
86         private static final int SIZE = 13;
87         
88         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
89             JCheckBox JavaDoc cb = (JCheckBox JavaDoc) c;
90             ButtonModel JavaDoc model = cb.getModel();
91     
92             // outer bevel
93
if(!cb.isBorderPaintedFlat()) {
94                     // Outer top/left
95
g.setColor(UIManager.getColor("CheckBox.shadow"));
96                     g.drawLine(x, y, x+11, y);
97                     g.drawLine(x, y+1, x, y+11);
98                     
99                     // Outer bottom/right
100
g.setColor(UIManager.getColor("CheckBox.highlight"));
101                     g.drawLine(x+12, y, x+12, y+12);
102                     g.drawLine(x, y+12, x+11, y+12);
103                     
104                     // Inner top.left
105
g.setColor(UIManager.getColor("CheckBox.darkShadow"));
106                     g.drawLine(x+1, y+1, x+10, y+1);
107                     g.drawLine(x+1, y+2, x+1, y+10);
108                     
109                     // Inner bottom/right
110
g.setColor(UIManager.getColor("CheckBox.light"));
111                     g.drawLine(x+1, y+11, x+11, y+11);
112                     g.drawLine(x+11, y+1, x+11, y+10);
113     
114             } else {
115                 g.setColor(UIManager.getColor("CheckBox.shadow"));
116                 g.drawRect(x+1, y+1, SIZE-3, SIZE-3);
117             }
118             // inside box
119
g.setColor(UIManager.getColor(
120                     (model.isPressed() && model.isArmed()) || !model.isEnabled()
121                         ? "CheckBox.background"
122                         : "CheckBox.interiorBackground"));
123             g.fillRect(x+2, y+2, SIZE-4, SIZE-4);
124     
125             g.setColor(UIManager.getColor(model.isEnabled()
126                             ? "CheckBox.checkColor" // Modified by JGoodies
127
: "CheckBox.shadow"));
128     
129             // paint check
130
if (model.isSelected()) {
131                 g.drawLine(x+9, y+3, x+9, y+3);
132                 g.drawLine(x+8, y+4, x+9, y+4);
133                 g.drawLine(x+7, y+5, x+9, y+5);
134                 g.drawLine(x+6, y+6, x+8, y+6);
135                 g.drawLine(x+3, y+7, x+7, y+7);
136                 g.drawLine(x+4, y+8, x+6, y+8);
137                 g.drawLine(x+5, y+9, x+5, y+9);
138                 g.drawLine(x+3, y+5, x+3, y+5);
139                 g.drawLine(x+3, y+6, x+4, y+6);
140             }
141         }
142     
143         public int getIconWidth() { return SIZE; }
144         public int getIconHeight() { return SIZE; }
145     }
146     
147
148     // Paints the icon for a radio button.
149
private static class RadioButtonIcon implements Icon JavaDoc, UIResource JavaDoc, Serializable JavaDoc {
150     
151         private static final int SIZE = 13;
152
153         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
154             AbstractButton JavaDoc b = (AbstractButton JavaDoc) c;
155             ButtonModel JavaDoc model = b.getModel();
156     
157             // fill interior
158
g.setColor(UIManager.getColor(
159                     (model.isPressed() && model.isArmed()) || !model.isEnabled()
160                         ? "RadioButton.background"
161                         : "RadioButton.interiorBackground"));
162             g.fillRect(x+2, y+2, 8, 8);
163             
164             
165             // outter left arc
166
g.setColor(UIManager.getColor("RadioButton.shadow"));
167             g.drawLine(x+4, y+0, x+7, y+0);
168             g.drawLine(x+2, y+1, x+3, y+1);
169             g.drawLine(x+8, y+1, x+9, y+1);
170             g.drawLine(x+1, y+2, x+1, y+3);
171             g.drawLine(x+0, y+4, x+0, y+7);
172             g.drawLine(x+1, y+8, x+1, y+9);
173      
174             // outter right arc
175
g.setColor(UIManager.getColor("RadioButton.highlight"));
176             g.drawLine(x+2, y+10, x+3, y+10);
177             g.drawLine(x+4, y+11, x+7, y+11);
178             g.drawLine(x+8, y+10, x+9, y+10);
179             g.drawLine(x+10, y+9, x+10, y+8);
180             g.drawLine(x+11, y+7, x+11, y+4);
181             g.drawLine(x+10, y+3, x+10, y+2);
182      
183      
184             // inner left arc
185
g.setColor(UIManager.getColor("RadioButton.darkShadow"));
186             g.drawLine(x+4, y+1, x+7, y+1);
187             g.drawLine(x+2, y+2, x+3, y+2);
188             g.drawLine(x+8, y+2, x+9, y+2);
189             g.drawLine(x+2, y+3, x+2, y+3);
190             g.drawLine(x+1, y+4, x+1, y+7);
191             g.drawLine(x+2, y+8, x+2, y+8);
192     
193      
194             // inner right arc
195
g.setColor(UIManager.getColor("RadioButton.light"));
196             g.drawLine(x+2, y+9, x+3, y+9);
197             g.drawLine(x+4, y+10, x+7, y+10);
198             g.drawLine(x+8, y+9, x+9, y+9);
199             g.drawLine(x+9, y+8, x+9, y+8);
200             g.drawLine(x+10, y+7, x+10, y+4);
201             g.drawLine(x+9, y+3, x+9, y+3);
202      
203      
204             // indicate whether selected or not
205
if(model.isSelected()) {
206                 g.setColor(UIManager.getColor("RadioButton.checkColor")); // Modified by JGoodies
207
g.fillRect(x+4, y+5, 4, 2);
208                 g.fillRect(x+5, y+4, 2, 4);
209             }
210         }
211     
212         public int getIconWidth() { return SIZE; }
213         public int getIconHeight() { return SIZE; }
214         
215     }
216     
217 }
Popular Tags