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.io.Serializable;
  45: 
  46: import javax.swing.Icon;
  47: import javax.swing.JCheckBoxMenuItem;
  48: 
  49: /**
  50:  * Creates icons for the {@link BasicLookAndFeel}.
  51:  */
  52: public class BasicIconFactory implements Serializable
  53: {
  54:   static final long serialVersionUID = 5605588811185324383L;
  55: 
  56:   private static class DummyIcon 
  57:     implements Icon
  58:   {    
  59:     public int getIconHeight() 
  60:     { 
  61:       return 10; 
  62:     }
  63:     public int getIconWidth() 
  64:     { 
  65:       return 10; 
  66:     }
  67:     public void paintIcon(Component c, Graphics g, int x, int y)
  68:     {
  69:       Color save = g.getColor();
  70:       g.setColor(c.getForeground());
  71:       g.drawRect(x, y, 10, 10);
  72:       g.setColor(save);
  73:     }
  74:   }
  75: 
  76:   /**
  77:    * The icon used for CheckBoxes in the BasicLookAndFeel. This is an empty
  78:    * icon with a size of 13x13 pixels.
  79:    */
  80:   static class CheckBoxIcon
  81:     implements Icon
  82:   {
  83:     /**
  84:      * Returns the height of the icon. The BasicLookAndFeel CheckBox icon
  85:      * has a height of 13 pixels.
  86:      *
  87:      * @return the height of the icon
  88:      */
  89:     public int getIconHeight()
  90:     {
  91:       return 13;
  92:     }
  93: 
  94:     /**
  95:      * Returns the width of the icon. The BasicLookAndFeel CheckBox icon
  96:      * has a width of 13 pixels.
  97:      *
  98:      * @return the height of the icon
  99:      */
 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