Source for javax.swing.plaf.basic.BasicIconFactory

   1: /* BasicIconFactory.java --
   2:    Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing.plaf.basic;
  40: 
  41: import java.awt.Color;
  42: import java.awt.Component;
  43: import java.awt.Graphics;
  44: import java.awt.Polygon;
  45: import java.io.Serializable;
  46: 
  47: import javax.swing.Icon;
  48: import javax.swing.JCheckBoxMenuItem;
  49: 
  50: /**
  51:  * Creates icons for the {@link BasicLookAndFeel}.
  52:  */
  53: public class BasicIconFactory implements Serializable
  54: {
  55:   static final long serialVersionUID = 5605588811185324383L;
  56: 
  57:   private static class DummyIcon 
  58:     implements Icon
  59:   {    
  60:     public int getIconHeight() { return 10; }
  61:     public int getIconWidth() { return 10; }
  62:     public void paintIcon(Component c, Graphics g, int x, int y)
  63:     {
  64:       Color save = g.getColor();
  65:       g.setColor(c.getForeground());
  66:       g.drawRect(x, y, 10, 10);
  67:       g.setColor(save);
  68:     }
  69:   }
  70: 
  71:   /**
  72:    * The icon used for CheckBoxes in the BasicLookAndFeel. This is an empty
  73:    * icon with a size of 13x13 pixels.
  74:    */
  75:   static class CheckBoxIcon
  76:     implements Icon
  77:   {
  78:     /**
  79:      * Returns the height of the icon. The BasicLookAndFeel CheckBox icon
  80:      * has a height of 13 pixels.
  81:      *
  82:      * @return the height of the icon
  83:      */
  84:     public int getIconHeight()
  85:     {
  86:       return 13;
  87:     }
  88: 
  89:     /**
  90:      * Returns the width of the icon. The BasicLookAndFeel CheckBox icon
  91:      * has a width of 13 pixels.
  92:      *
  93:      * @return the height of the icon
  94:      */
  95:     public int getIconWidth()
  96:     {
  97:       return 13;
  98:     }
  99: 
 100:     /**
 101:      * Paints the icon. The BasicLookAndFeel CheckBox icon is empty and does
 102:      * not need to be painted.
 103:      *
 104:      * @param c the component to be painted
 105:      * @param g the Graphics context to be painted with
 106:      * @param x the x position of the icon
 107:      * @param y the y position of the icon
 108:      */
 109:     public void paintIcon(Component c, Graphics g, int x, int y)
 110:     {
 111:       // The icon is empty and needs no painting.
 112:     }
 113:   }
 114: 
 115:   /**
 116:    * The icon used for {@link JCheckBoxMenuItem}s in the 
 117:    * {@link BasicLookAndFeel}. This icon has a size of 9x9 pixels.
 118:    */
 119:   static class CheckBoxMenuItemIcon
 120:     implements Icon
 121:   {
 122:     /**
 123:      * Returns the height of the icon in pixels.
 124:      *
 125:      * @return the height of the icon
 126:      */
 127:     public int getIconHeight()
 128:     {
 129:       return 9;
 130:     }
 131: 
 132:     /**
 133:      * Returns the width of the icon in pixels.
 134:      *
 135:      * @return the height of the icon
 136:      */
 137:     public int getIconWidth()
 138:     {
 139:       return 9;
 140:     }
 141: 
 142:     /**
 143:      * Paints the icon.
 144:      *
 145:      * @param c the component to be painted
 146:      * @param g the Graphics context to be painted with
 147:      * @param x the x position of the icon
 148:      * @param y the y position of the icon
 149:      */
 150:     public void paintIcon(Component c, Graphics g, int x, int y)
 151:     {
 152:       JCheckBoxMenuItem item = (JCheckBoxMenuItem) c;
 153:       if (item.isSelected()) 
 154:         {
 155:           // paint the check...
 156:           g.setColor(Color.black);
 157:           g.drawLine(x + 1, y + 3, x + 1, y + 4);
 158:           g.drawLine(x + 2, y + 4, x + 2, y + 5);
 159:           for (int i = 0; i < 5; i++)
 160:             g.drawLine(x + 3 + i, y + 5 - i, x + 3 + i, y + 6 - i);    
 161:         }
 162:     }
 163:   }
 164: 
 165:   /**
 166:    * The icon used for RadioButtons in the BasicLookAndFeel. This is an empty
 167:    * icon with a size of 13x13 pixels.
 168:    */
 169:   static class RadioButtonIcon
 170:     implements Icon
 171:   {
 172:     /**
 173:      * Returns the height of the icon. The BasicLookAndFeel RadioButton icon
 174:      * has a height of 13 pixels.
 175:      *
 176:      * @return the height of the icon
 177:      */
 178:     public int getIconHeight()
 179:     {
 180:       return 13;
 181:     }
 182: 
 183:     /**
 184:      * Returns the width of the icon. The BasicLookAndFeel RadioButton icon
 185:      * has a width of 13 pixels.
 186:      *
 187:      * @return the height of the icon
 188:      */
 189:     public int getIconWidth()
 190:     {
 191:       return 13;
 192:     }
 193: 
 194:     /**
 195:      * Paints the icon. The BasicLookAndFeel RadioButton icon is empty and does
 196:      * not need to be painted.
 197:      *
 198:      * @param c the component to be painted
 199:      * @param g the Graphics context to be painted with
 200:      * @param x the x position of the icon
 201:      * @param y the y position of the icon
 202:      */
 203:     public void paintIcon(Component c, Graphics g, int x, int y)
 204:     {
 205:       // The icon is empty and needs no painting.
 206:     }
 207:   }
 208:   /** The cached CheckBoxIcon instance. */
 209:   private static CheckBoxIcon checkBoxIcon;
 210:   
 211:   /** The cached RadioButtonIcon instance. */
 212:   private static RadioButtonIcon radioButtonIcon;
 213: 
 214:   public static Icon getMenuItemCheckIcon()
 215:   {
 216:     return new Icon()
 217:     {
 218:       public int getIconHeight()
 219:       {
 220:         return 13;
 221:       }
 222: 
 223:       public int getIconWidth()
 224:       {
 225:         return 13;
 226:       }
 227: 
 228:       public void paintIcon(Component c, Graphics g, int x, int y)
 229:       {
 230:         Color saved = g.getColor();
 231:         g.setColor(Color.BLACK);
 232:         g.drawLine(3 + x, 5 + y, 3 + x, 9 + y);
 233:         g.drawLine(4 + x, 5 + y, 4 + x, 9 + y);
 234:         g.drawLine(5 + x, 7 + y, 9 + x, 3 + y);
 235:         g.drawLine(5 + x, 8 + y, 9 + x, 4 + y);
 236:         g.setColor(saved);
 237:       }
 238:     };
 239:   }
 240:   public static Icon getMenuItemArrowIcon()
 241:   {
 242:     return new DummyIcon();
 243:   }
 244:   public static Icon getMenuArrowIcon()
 245:   {
 246:     return new Icon()
 247:       {
 248:     public int getIconHeight()
 249:     {
 250:       return 12;
 251:     }
 252: 
 253:     public int getIconWidth()
 254:     {
 255:       return 12;
 256:     }
 257: 
 258:     public void paintIcon(Component c, Graphics g, int x, int y)
 259:     {
 260:       g.translate(x, y);
 261: 
 262:       Color saved = g.getColor();
 263: 
 264:       g.setColor(Color.BLACK);
 265: 
 266:       g.fillPolygon(new Polygon(new int[] { 3, 9, 3 },
 267:                                   new int[] { 2, 6, 10 },
 268:                                   3));
 269: 
 270:       g.setColor(saved);
 271:       g.translate(-x, -y);
 272:     }
 273:       };
 274:   }
 275: 
 276:   /**
 277:    * Returns an icon for CheckBoxes in the BasicLookAndFeel. CheckBox icons
 278:    * in the Basic L&amp;F are empty and have a size of 13x13 pixels.
 279:    * This method returns a shared single instance of this icon.
 280:    *
 281:    * @return an icon for CheckBoxes in the BasicLookAndFeel
 282:    */
 283:   public static Icon getCheckBoxIcon()
 284:   {
 285:     if (checkBoxIcon == null)
 286:       checkBoxIcon = new CheckBoxIcon();
 287:     return checkBoxIcon;
 288:   }
 289: 
 290:   /**
 291:    * Returns an icon for RadioButtons in the BasicLookAndFeel. RadioButton
 292:    * icons in the Basic L&amp;F are empty and have a size of 13x13 pixels.
 293:    * This method returns a shared single instance of this icon.
 294:    *
 295:    * @return an icon for RadioButtons in the BasicLookAndFeel
 296:    */
 297:   public static Icon getRadioButtonIcon()
 298:   {
 299:     if (radioButtonIcon == null)
 300:       radioButtonIcon = new RadioButtonIcon();
 301:     return radioButtonIcon;
 302:   }
 303: 
 304:   /**
 305:    * Creates and returns an icon used when rendering {@link JCheckBoxMenuItem}
 306:    * components.
 307:    * 
 308:    * @return An icon.
 309:    */
 310:   public static Icon getCheckBoxMenuItemIcon()
 311:   {
 312:     return new CheckBoxMenuItemIcon();
 313:   }
 314:   
 315:   public static Icon getRadioButtonMenuItemIcon()
 316:   {
 317:     return getRadioButtonIcon();
 318:   }
 319:   
 320:   public static Icon createEmptyFrameIcon()
 321:   {
 322:     return new DummyIcon();
 323:   }
 324: } // class BasicIconFactory