001    /* BasicIconFactory.java --
002       Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.swing.plaf.basic;
040    
041    import java.awt.Color;
042    import java.awt.Component;
043    import java.awt.Graphics;
044    import java.io.Serializable;
045    
046    import javax.swing.Icon;
047    import javax.swing.JCheckBoxMenuItem;
048    
049    /**
050     * Creates icons for the {@link BasicLookAndFeel}.
051     */
052    public class BasicIconFactory implements Serializable
053    {
054      static final long serialVersionUID = 5605588811185324383L;
055    
056      private static class DummyIcon
057        implements Icon
058      {
059        public int getIconHeight()
060        {
061          return 10;
062        }
063        public int getIconWidth()
064        {
065          return 10;
066        }
067        public void paintIcon(Component c, Graphics g, int x, int y)
068        {
069          Color save = g.getColor();
070          g.setColor(c.getForeground());
071          g.drawRect(x, y, 10, 10);
072          g.setColor(save);
073        }
074      }
075    
076      /**
077       * The icon used for CheckBoxes in the BasicLookAndFeel. This is an empty
078       * icon with a size of 13x13 pixels.
079       */
080      static class CheckBoxIcon
081        implements Icon
082      {
083        /**
084         * Returns the height of the icon. The BasicLookAndFeel CheckBox icon
085         * has a height of 13 pixels.
086         *
087         * @return the height of the icon
088         */
089        public int getIconHeight()
090        {
091          return 13;
092        }
093    
094        /**
095         * Returns the width of the icon. The BasicLookAndFeel CheckBox icon
096         * has a width of 13 pixels.
097         *
098         * @return the height of the icon
099         */
100        public int getIconWidth()
101        {
102          return 13;
103        }
104    
105        /**
106         * Paints the icon. The BasicLookAndFeel CheckBox icon is empty and does
107         * not need to be painted.
108         *
109         * @param c the component to be painted
110         * @param g the Graphics context to be painted with
111         * @param x the x position of the icon
112         * @param y the y position of the icon
113         */
114        public void paintIcon(Component c, Graphics g, int x, int y)
115        {
116          // The icon is empty and needs no painting.
117        }
118      }
119    
120      /**
121       * The icon used for {@link JCheckBoxMenuItem}s in the
122       * {@link BasicLookAndFeel}. This icon has a size of 9x9 pixels.
123       */
124      static class CheckBoxMenuItemIcon
125        implements Icon
126      {
127        /**
128         * Returns the height of the icon in pixels.
129         *
130         * @return the height of the icon
131         */
132        public int getIconHeight()
133        {
134          return 9;
135        }
136    
137        /**
138         * Returns the width of the icon in pixels.
139         *
140         * @return the height of the icon
141         */
142        public int getIconWidth()
143        {
144          return 9;
145        }
146    
147        /**
148         * Paints the icon.
149         *
150         * @param c the component to be painted
151         * @param g the Graphics context to be painted with
152         * @param x the x position of the icon
153         * @param y the y position of the icon
154         */
155        public void paintIcon(Component c, Graphics g, int x, int y)
156        {
157          JCheckBoxMenuItem item = (JCheckBoxMenuItem) c;
158          if (item.isSelected())
159            {
160              // paint the check...
161              g.setColor(Color.black);
162              g.drawLine(x + 1, y + 3, x + 1, y + 4);
163              g.drawLine(x + 2, y + 4, x + 2, y + 5);
164              for (int i = 0; i < 5; i++)
165                g.drawLine(x + 3 + i, y + 5 - i, x + 3 + i, y + 6 - i);
166            }
167        }
168      }
169    
170      /**
171       * The icon used for RadioButtons in the BasicLookAndFeel. This is an empty
172       * icon with a size of 13x13 pixels.
173       */
174      static class RadioButtonIcon
175        implements Icon
176      {
177        /**
178         * Returns the height of the icon. The BasicLookAndFeel RadioButton icon
179         * has a height of 13 pixels.
180         *
181         * @return the height of the icon
182         */
183        public int getIconHeight()
184        {
185          return 13;
186        }
187    
188        /**
189         * Returns the width of the icon. The BasicLookAndFeel RadioButton icon
190         * has a width of 13 pixels.
191         *
192         * @return the height of the icon
193         */
194        public int getIconWidth()
195        {
196          return 13;
197        }
198    
199        /**
200         * Paints the icon. The BasicLookAndFeel RadioButton icon is empty and does
201         * not need to be painted.
202         *
203         * @param c the component to be painted
204         * @param g the Graphics context to be painted with
205         * @param x the x position of the icon
206         * @param y the y position of the icon
207         */
208        public void paintIcon(Component c, Graphics g, int x, int y)
209        {
210          // The icon is empty and needs no painting.
211        }
212      }
213      /** The cached CheckBoxIcon instance. */
214      private static CheckBoxIcon checkBoxIcon;
215    
216      /** The cached RadioButtonIcon instance. */
217      private static RadioButtonIcon radioButtonIcon;
218    
219      public static Icon getMenuItemCheckIcon()
220      {
221        return new Icon()
222        {
223          public int getIconHeight()
224          {
225            return 13;
226          }
227    
228          public int getIconWidth()
229          {
230            return 13;
231          }
232    
233          public void paintIcon(Component c, Graphics g, int x, int y)
234          {
235            Color saved = g.getColor();
236            g.setColor(Color.BLACK);
237            g.drawLine(3 + x, 5 + y, 3 + x, 9 + y);
238            g.drawLine(4 + x, 5 + y, 4 + x, 9 + y);
239            g.drawLine(5 + x, 7 + y, 9 + x, 3 + y);
240            g.drawLine(5 + x, 8 + y, 9 + x, 4 + y);
241            g.setColor(saved);
242          }
243        };
244      }
245      public static Icon getMenuItemArrowIcon()
246      {
247        return new DummyIcon();
248      }
249    
250      /**
251       * Returns a new instance of a 4 x 8 icon showing a small black triangle that
252       * points to the right.  This is displayed in menu items that have a
253       * sub menu.
254       *
255       * @return The icon.
256       */
257      public static Icon getMenuArrowIcon()
258      {
259        return new Icon()
260          {
261            public int getIconHeight()
262            {
263              return 8;
264            }
265            public int getIconWidth()
266            {
267              return 4;
268            }
269            public void paintIcon(Component c, Graphics g, int x, int y)
270            {
271              Color saved = g.getColor();
272              g.setColor(Color.BLACK);
273              for (int i = 0; i < 4; i++)
274                g.drawLine(x + i, y + i, x + i, y + 7 - i);
275              g.setColor(saved);
276            }
277          };
278      }
279    
280      /**
281       * Returns an icon for CheckBoxes in the BasicLookAndFeel. CheckBox icons
282       * in the Basic L&amp;F are empty and have a size of 13x13 pixels.
283       * This method returns a shared single instance of this icon.
284       *
285       * @return an icon for CheckBoxes in the BasicLookAndFeel
286       */
287      public static Icon getCheckBoxIcon()
288      {
289        if (checkBoxIcon == null)
290          checkBoxIcon = new CheckBoxIcon();
291        return checkBoxIcon;
292      }
293    
294      /**
295       * Returns an icon for RadioButtons in the BasicLookAndFeel. RadioButton
296       * icons in the Basic L&amp;F are empty and have a size of 13x13 pixels.
297       * This method returns a shared single instance of this icon.
298       *
299       * @return an icon for RadioButtons in the BasicLookAndFeel
300       */
301      public static Icon getRadioButtonIcon()
302      {
303        if (radioButtonIcon == null)
304          radioButtonIcon = new RadioButtonIcon();
305        return radioButtonIcon;
306      }
307    
308      /**
309       * Creates and returns an icon used when rendering {@link JCheckBoxMenuItem}
310       * components.
311       *
312       * @return An icon.
313       */
314      public static Icon getCheckBoxMenuItemIcon()
315      {
316        return new CheckBoxMenuItemIcon();
317      }
318    
319      public static Icon getRadioButtonMenuItemIcon()
320      {
321        return getRadioButtonIcon();
322      }
323    
324      public static Icon createEmptyFrameIcon()
325      {
326        return new DummyIcon();
327      }
328    } // class BasicIconFactory