KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticIconFactory


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.plastic;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Component JavaDoc;
35 import java.awt.Graphics JavaDoc;
36 import java.io.Serializable JavaDoc;
37
38 import javax.swing.ButtonModel JavaDoc;
39 import javax.swing.Icon JavaDoc;
40 import javax.swing.JCheckBox JavaDoc;
41 import javax.swing.JComponent JavaDoc;
42 import javax.swing.JMenuItem JavaDoc;
43 import javax.swing.plaf.UIResource JavaDoc;
44 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
45
46 /**
47  * Factory class that vends <code>Icon</code>s for the
48  * JGoodies Plastic look and feel.
49  * These icons are used extensively in Plastic via the defaults mechanism.
50  * While other look and feels often use GIFs for icons, creating icons
51  * in code facilitates switching to other themes.
52  * <p>
53  * Each method in this class returns either an <code>Icon</code> or <code>null</code>,
54  * where <code>null</code> implies that there is no default icon.
55  *
56  * @author Karsten Lentzsch
57  * @version $Revision: 1.2 $
58  */

59
60 final class PlasticIconFactory {
61
62
63     // Helper method utilized by the CheckBoxIcon and the CheckBoxMenuItemIcon.
64
private static void drawCheck(Graphics JavaDoc g, int x, int y) {
65         g.translate(x, y);
66         g.drawLine(3, 5, 3, 5);
67         g.fillRect(3, 6, 2, 2);
68         g.drawLine(4, 8, 9, 3);
69         g.drawLine(5, 8, 9, 4);
70         g.drawLine(5, 9, 9, 5);
71         g.translate(-x, -y);
72     }
73     
74     
75     private static class CheckBoxIcon implements Icon JavaDoc, UIResource JavaDoc, Serializable JavaDoc {
76
77         private static final int SIZE = 13;
78
79         public int getIconWidth() { return SIZE; }
80         public int getIconHeight() { return SIZE; }
81
82         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
83             JCheckBox JavaDoc cb = (JCheckBox JavaDoc) c;
84             ButtonModel JavaDoc model = cb.getModel();
85
86             if (model.isEnabled()) {
87                 if (cb.isBorderPaintedFlat()) {
88                     g.setColor(PlasticLookAndFeel.getControlDarkShadow());
89                     g.drawRect(x, y, SIZE - 2, SIZE - 2);
90                     // inside box
91
g.setColor(PlasticLookAndFeel.getControlHighlight());
92                     g.fillRect(x+1, y+1, SIZE-3, SIZE-3);
93                 } else if (model.isPressed() && model.isArmed()) {
94                     g.setColor(MetalLookAndFeel.getControlShadow());
95                     g.fillRect(x, y, SIZE - 1, SIZE - 1);
96                     PlasticUtils.drawPressed3DBorder(g, x, y, SIZE, SIZE);
97                 } else {
98                     PlasticUtils.drawFlush3DBorder(g, x, y, SIZE, SIZE);
99                 }
100                 g.setColor(MetalLookAndFeel.getControlInfo());
101             } else {
102                 g.setColor(MetalLookAndFeel.getControlShadow());
103                 g.drawRect(x, y, SIZE - 2, SIZE - 2);
104             }
105
106             if (model.isSelected()) {
107                 drawCheck(g, x, y);
108             }
109         }
110
111     }
112
113
114     private static class CheckBoxMenuItemIcon implements Icon JavaDoc, UIResource JavaDoc, Serializable JavaDoc {
115         
116         private static final int SIZE = 13;
117         
118         public int getIconWidth() { return SIZE; }
119         public int getIconHeight() { return SIZE; }
120
121         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
122             JMenuItem JavaDoc b = (JMenuItem JavaDoc) c;
123             if (b.isSelected()) {
124                 drawCheck(g, x, y + 1);
125             }
126         }
127     }
128     
129     
130     private static class RadioButtonMenuItemIcon implements Icon JavaDoc, UIResource JavaDoc, Serializable JavaDoc {
131         
132         private static final int SIZE = 13;
133         
134         public int getIconWidth() { return SIZE; }
135         public int getIconHeight() { return SIZE; }
136
137         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
138             JMenuItem JavaDoc b = (JMenuItem JavaDoc) c;
139             if (b.isSelected()) {
140                 drawDot(g, x, y);
141             }
142         }
143         
144         private void drawDot(Graphics JavaDoc g, int x, int y) {
145             g.translate(x, y);
146             g.drawLine(5, 4, 8, 4);
147             g.fillRect(4, 5, 6, 4);
148             g.drawLine(5, 9, 8, 9);
149             g.translate(-x, -y);
150         }
151     }
152     
153     
154     private static class MenuArrowIcon implements Icon JavaDoc, UIResource JavaDoc, Serializable JavaDoc {
155
156         private static final int WIDTH = 4;
157         private static final int HEIGHT = 8;
158
159         public void paintIcon( Component JavaDoc c, Graphics JavaDoc g, int x, int y ) {
160             JMenuItem JavaDoc b = (JMenuItem JavaDoc) c;
161     
162             g.translate( x, y );
163             if( PlasticUtils.isLeftToRight(b) ) {
164                 g.drawLine( 0, 0, 0, 7 );
165                 g.drawLine( 1, 1, 1, 6 );
166                 g.drawLine( 2, 2, 2, 5 );
167                 g.drawLine( 3, 3, 3, 4 );
168             } else {
169                 g.drawLine( 4, 0, 4, 7 );
170                 g.drawLine( 3, 1, 3, 6 );
171                 g.drawLine( 2, 2, 2, 5 );
172                 g.drawLine( 1, 3, 1, 4 );
173             }
174             g.translate( -x, -y );
175         }
176
177         public int getIconWidth() { return WIDTH; }
178         public int getIconHeight() { return HEIGHT; }
179
180     }
181     
182
183     /**
184      * The minus sign button icon used in trees
185      */

186     private static class ExpandedTreeIcon implements Icon JavaDoc, Serializable JavaDoc {
187         
188         protected static final int SIZE = 9;
189         protected static final int HALF_SIZE = 4;
190
191         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
192             Color JavaDoc backgroundColor = c.getBackground();
193
194             g.setColor(backgroundColor != null ? backgroundColor : Color.WHITE);
195             g.fillRect(x, y, SIZE - 1, SIZE - 1);
196             g.setColor(Color.GRAY);
197             g.drawRect(x, y, SIZE - 1, SIZE - 1);
198             g.setColor(Color.BLACK);
199             g.drawLine(x + 2, y + HALF_SIZE, x + (SIZE - 3), y + HALF_SIZE);
200         }
201         
202         public int getIconWidth() { return SIZE; }
203         public int getIconHeight() { return SIZE; }
204     }
205     
206
207     /**
208      * The plus sign button icon used in trees.
209      */

210     private static class CollapsedTreeIcon extends ExpandedTreeIcon {
211         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
212             super.paintIcon(c, g, x, y);
213             g.drawLine(x + HALF_SIZE, y + 2, x + HALF_SIZE, y + (SIZE - 3));
214         }
215     }
216     
217     
218     /**
219      * The arrow button used in comboboxes.
220      */

221     private static class ComboBoxButtonIcon implements Icon JavaDoc, Serializable JavaDoc {
222
223         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
224             JComponent JavaDoc component = (JComponent JavaDoc)c;
225             int iconWidth = getIconWidth();
226         
227             g.translate(x, y);
228         
229             g.setColor( component.isEnabled()
230                         ? MetalLookAndFeel.getControlInfo()
231                         : MetalLookAndFeel.getControlShadow() );
232             g.drawLine( 0, 0, iconWidth - 1, 0 );
233             g.drawLine( 1, 1, 1 + (iconWidth - 3), 1 );
234             g.drawLine( 2, 2, 2 + (iconWidth - 5), 2 );
235             g.drawLine( 3, 3, 3 + (iconWidth - 7), 3 );
236             
237 /*
238         int startY = (((h + 1) - arrowHeight) / 2) + arrowHeight - 1;
239         int startX = (w / 2);
240         
241         // System.out.println( "startX2 :" + startX + " startY2 :"+startY);
242         
243         for (int line = 0; line < arrowHeight; line++) {
244             g.drawLine(
245                 startX - line,
246                 startY - line,
247                 startX + line + 1,
248                 startY - line);
249         }*/

250             g.translate( -x, -y );
251         }
252     
253         public int getIconWidth() { return 8; }
254         public int getIconHeight() { return 4; }
255     }
256     
257
258     // Cached Access to Icons ***********************************************************
259

260     private static Icon JavaDoc checkBoxIcon;
261     private static Icon JavaDoc checkBoxMenuItemIcon;
262     private static Icon JavaDoc radioButtonMenuItemIcon;
263     private static Icon JavaDoc menuArrowIcon;
264     private static Icon JavaDoc expandedTreeIcon;
265     private static Icon JavaDoc collapsedTreeIcon;
266     
267
268     /**
269      * Answers an <code>Icon</code> used for <code>JCheckBox</code>es.
270      */

271     static Icon JavaDoc getCheckBoxIcon() {
272         if (checkBoxIcon == null) {
273             checkBoxIcon = new CheckBoxIcon();
274         }
275         return checkBoxIcon;
276     }
277
278
279     /**
280      * Answers an <code>Icon</code> used for <code>JCheckButtonMenuItem</code>s.
281      */

282     static Icon JavaDoc getCheckBoxMenuItemIcon() {
283         if (checkBoxMenuItemIcon == null) {
284             checkBoxMenuItemIcon = new CheckBoxMenuItemIcon();
285         }
286         return checkBoxMenuItemIcon;
287     }
288
289
290     /**
291      * Answers an <code>Icon</code> used for <code>JRadioButtonMenuItem</code>s.
292      */

293     static Icon JavaDoc getRadioButtonMenuItemIcon() {
294         if (radioButtonMenuItemIcon == null) {
295             radioButtonMenuItemIcon = new RadioButtonMenuItemIcon();
296         }
297         return radioButtonMenuItemIcon;
298     }
299     
300     
301     /**
302      * Answers an <code>Icon</code> used for arrows in <code>JMenu</code>s.
303      */

304     static Icon JavaDoc getMenuArrowIcon() {
305         if (menuArrowIcon == null) {
306             menuArrowIcon = new MenuArrowIcon();
307         }
308         return menuArrowIcon;
309     }
310     
311     
312     /**
313      * Answers an <code>Icon</code> used in <code>JTree</code>s.
314      */

315     static Icon JavaDoc getExpandedTreeIcon() {
316         if (expandedTreeIcon == null) {
317             expandedTreeIcon = new ExpandedTreeIcon();
318         }
319         return expandedTreeIcon;
320     }
321
322     /**
323      * Answers an <code>Icon</code> used in <code>JTree</code>s.
324      */

325     static Icon JavaDoc getCollapsedTreeIcon() {
326         if (collapsedTreeIcon == null) {
327             collapsedTreeIcon = new CollapsedTreeIcon();
328         }
329         return collapsedTreeIcon;
330     }
331
332     /**
333      * Answers an <code>Icon</code> used in <code>JComboBox</code>es.
334      */

335     static Icon JavaDoc getComboBoxButtonIcon() {
336         return new ComboBoxButtonIcon();
337     }
338
339
340 }
Popular Tags