1 /*
2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
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
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
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  *
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.
24  */
25 
26 package javax.swing.plaf.basic;
27 
28 import javax.swing.*;
29 import javax.swing.plaf.UIResource;
30 
31 import java.awt.Graphics;
32 import java.awt.Color;
33 import java.awt.Component;
34 import java.awt.Polygon;
35 import java.io.Serializable;
36 
37 /**
38  * Factory object that can vend Icons appropriate for the basic L & F.
39  * <p>
40  * <strong>Warning:</strong>
41  * Serialized objects of this class will not be compatible with
42  * future Swing releases. The current serialization support is
43  * appropriate for short term storage or RMI between applications running
44  * the same version of Swing.  As of 1.4, support for long term storage
45  * of all JavaBeans&trade;
46  * has been added to the <code>java.beans</code> package.
47  * Please see {@link java.beans.XMLEncoder}.
48  *
49  * @author David Kloba
50  * @author Georges Saab
51  */
52 @SuppressWarnings("serial") // Same-version serialization only
53 public class BasicIconFactory implements Serializable
54 {
55     private static Icon frame_icon;
56     private static Icon checkBoxIcon;
57     private static Icon radioButtonIcon;
58     private static Icon checkBoxMenuItemIcon;
59     private static Icon radioButtonMenuItemIcon;
60     private static Icon menuItemCheckIcon;
61     private static Icon menuItemArrowIcon;
62     private static Icon menuArrowIcon;
63 
64     /**
65      * Returns a menu item check icon.
66      *
67      * @return a menu item check icon
68      */
getMenuItemCheckIcon()69     public static Icon getMenuItemCheckIcon() {
70         if (menuItemCheckIcon == null) {
71             menuItemCheckIcon = new MenuItemCheckIcon();
72         }
73         return menuItemCheckIcon;
74     }
75 
76     /**
77      * Returns a menu item arrow icon.
78      *
79      * @return a menu item arrow icon
80      */
getMenuItemArrowIcon()81     public static Icon getMenuItemArrowIcon() {
82         if (menuItemArrowIcon == null) {
83             menuItemArrowIcon = new MenuItemArrowIcon();
84         }
85         return menuItemArrowIcon;
86     }
87 
88     /**
89      * Returns a menu arrow icon.
90      *
91      * @return a menu arrow icon
92      */
getMenuArrowIcon()93     public static Icon getMenuArrowIcon() {
94         if (menuArrowIcon == null) {
95             menuArrowIcon = new MenuArrowIcon();
96         }
97         return menuArrowIcon;
98     }
99 
100     /**
101      * Returns a check box icon.
102      *
103      * @return a check box icon
104      */
getCheckBoxIcon()105     public static Icon getCheckBoxIcon() {
106         if (checkBoxIcon == null) {
107             checkBoxIcon = new CheckBoxIcon();
108         }
109         return checkBoxIcon;
110     }
111 
112     /**
113      * Returns a radio button icon.
114      *
115      * @return a radio button icon
116      */
getRadioButtonIcon()117     public static Icon getRadioButtonIcon() {
118         if (radioButtonIcon == null) {
119             radioButtonIcon = new RadioButtonIcon();
120         }
121         return radioButtonIcon;
122     }
123 
124     /**
125      * Returns a check box menu item icon.
126      *
127      * @return a check box menu item icon
128      */
getCheckBoxMenuItemIcon()129     public static Icon getCheckBoxMenuItemIcon() {
130         if (checkBoxMenuItemIcon == null) {
131             checkBoxMenuItemIcon = new CheckBoxMenuItemIcon();
132         }
133         return checkBoxMenuItemIcon;
134     }
135 
136     /**
137      * Returns a radio button menu item icon.
138      *
139      * @return a radio button menu item icon
140      */
getRadioButtonMenuItemIcon()141     public static Icon getRadioButtonMenuItemIcon() {
142         if (radioButtonMenuItemIcon == null) {
143             radioButtonMenuItemIcon = new RadioButtonMenuItemIcon();
144         }
145         return radioButtonMenuItemIcon;
146     }
147 
148     /**
149      * Returns an empty frame icon.
150      *
151      * @return an empty frame icon
152      */
createEmptyFrameIcon()153     public static Icon createEmptyFrameIcon() {
154         if(frame_icon == null)
155             frame_icon = new EmptyFrameIcon();
156         return frame_icon;
157     }
158 
159     private static class EmptyFrameIcon implements Icon, Serializable {
160         int height = 16;
161         int width = 14;
paintIcon(Component c, Graphics g, int x, int y)162         public void paintIcon(Component c, Graphics g, int x, int y) {
163         }
getIconWidth()164         public int getIconWidth() { return width; }
getIconHeight()165         public int getIconHeight() { return height; }
166     };
167 
168     private static class CheckBoxIcon implements Icon, Serializable
169     {
170         static final int csize = 13;
paintIcon(Component c, Graphics g, int x, int y)171         public void paintIcon(Component c, Graphics g, int x, int y) {
172         }
173 
getIconWidth()174         public int getIconWidth() {
175             return csize;
176         }
177 
getIconHeight()178         public int getIconHeight() {
179             return csize;
180         }
181     }
182 
183     private static class RadioButtonIcon implements Icon, UIResource, Serializable
184     {
paintIcon(Component c, Graphics g, int x, int y)185         public void paintIcon(Component c, Graphics g, int x, int y) {
186         }
187 
getIconWidth()188         public int getIconWidth() {
189             return 13;
190         }
191 
getIconHeight()192         public int getIconHeight() {
193             return 13;
194         }
195     } // end class RadioButtonIcon
196 
197 
198     private static class CheckBoxMenuItemIcon implements Icon, UIResource, Serializable
199     {
paintIcon(Component c, Graphics g, int x, int y)200         public void paintIcon(Component c, Graphics g, int x, int y) {
201             AbstractButton b = (AbstractButton) c;
202             ButtonModel model = b.getModel();
203             boolean isSelected = model.isSelected();
204             if (isSelected) {
205                 g.drawLine(x+7, y+1, x+7, y+3);
206                 g.drawLine(x+6, y+2, x+6, y+4);
207                 g.drawLine(x+5, y+3, x+5, y+5);
208                 g.drawLine(x+4, y+4, x+4, y+6);
209                 g.drawLine(x+3, y+5, x+3, y+7);
210                 g.drawLine(x+2, y+4, x+2, y+6);
211                 g.drawLine(x+1, y+3, x+1, y+5);
212             }
213         }
getIconWidth()214         public int getIconWidth() { return 9; }
getIconHeight()215         public int getIconHeight() { return 9; }
216 
217     } // End class CheckBoxMenuItemIcon
218 
219 
220     private static class RadioButtonMenuItemIcon implements Icon, UIResource, Serializable
221     {
paintIcon(Component c, Graphics g, int x, int y)222         public void paintIcon(Component c, Graphics g, int x, int y) {
223             AbstractButton b = (AbstractButton) c;
224             ButtonModel model = b.getModel();
225             if (b.isSelected() == true) {
226                 g.fillOval(x+1, y+1, getIconWidth(), getIconHeight());
227             }
228         }
getIconWidth()229         public int getIconWidth() { return 6; }
getIconHeight()230         public int getIconHeight() { return 6; }
231 
232     } // End class RadioButtonMenuItemIcon
233 
234 
235     private static class MenuItemCheckIcon implements Icon, UIResource, Serializable{
paintIcon(Component c, Graphics g, int x, int y)236         public void paintIcon(Component c, Graphics g, int x, int y) {
237         }
getIconWidth()238         public int getIconWidth() { return 9; }
getIconHeight()239         public int getIconHeight() { return 9; }
240 
241     } // End class MenuItemCheckIcon
242 
243     private static class MenuItemArrowIcon implements Icon, UIResource, Serializable {
paintIcon(Component c, Graphics g, int x, int y)244         public void paintIcon(Component c, Graphics g, int x, int y) {
245         }
getIconWidth()246         public int getIconWidth() { return 4; }
getIconHeight()247         public int getIconHeight() { return 8; }
248 
249     } // End class MenuItemArrowIcon
250 
251     private static class MenuArrowIcon implements Icon, UIResource, Serializable {
paintIcon(Component c, Graphics g, int x, int y)252         public void paintIcon(Component c, Graphics g, int x, int y) {
253             Polygon p = new Polygon();
254             p.addPoint(x, y);
255             p.addPoint(x+getIconWidth(), y+getIconHeight()/2);
256             p.addPoint(x, y+getIconHeight());
257             g.fillPolygon(p);
258 
259         }
getIconWidth()260         public int getIconWidth() { return 4; }
getIconHeight()261         public int getIconHeight() { return 8; }
262     } // End class MenuArrowIcon
263 }
264