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() { return 10; }
  60:     public int getIconWidth() { return 10; }
  61:     public void paintIcon(Component c, Graphics g, int x, int y)
  62:     {
  63:       Color save = g.getColor();
  64:       g.setColor(c.getForeground());
  65:       g.drawRect(x, y, 10, 10);
  66:       g.setColor(save);
  67:     }
  68:   }
  69: 
  70:   /**
  71:    * The icon used for CheckBoxes in the BasicLookAndFeel. This is an empty
  72:    * icon with a size of 13x13 pixels.
  73:    */
  74:   static class CheckBoxIcon
  75:     implements Icon
  76:   {
  77:     /**
  78:      * Returns the height of the icon. The BasicLookAndFeel CheckBox icon
  79:      * has a height of 13 pixels.
  80:      *
  81:      * @return the height of the icon
  82:      */
  83:     public int getIconHeight()
  84:     {
  85:       return 13;
  86:     }
  87: 
  88:     /**
  89:      * Returns the width of the icon. The BasicLookAndFeel CheckBox icon
  90:      * has a width of 13 pixels.
  91:      *
  92:      * @return the height of the icon
  93:      */
  94:     public int getIconWidth()
  95:     {
  96:       return 13;
  97:     }
  98: 
  99:     /**
 100:      * Paints the icon. The BasicLookAndFeel CheckBox icon is empty and does
 101:      * not need to be painted.
 102:      *
 103:      * @param c the component to be painted
 104:      * @param g the Graphics context to be painted with
 105:      * @param x the x position of the icon
 106:      * @param y the y position of the icon
 107:      */
 108:     public void paintIcon(Component c, Graphics g, int x, int y)
 109:     {
 110:       // The icon is empty and needs no painting.
 111:     }
 112:   }
 113: 
 114:   /**
 115:    * The icon used for {@link JCheckBoxMenuItem}s in the 
 116:    * {@link BasicLookAndFeel}. This icon has a size of 9x9 pixels.
 117:    */
 118:   static class CheckBoxMenuItemIcon
 119:     implements Icon
 120:   {
 121:     /**
 122:      * Returns the height of the icon in pixels.
 123:      *
 124:      * @return the height of the icon
 125:      */
 126:     public int getIconHeight()
 127:     {
 128:       return 9;
 129:     }
 130: 
 131:     /**
 132:      * Returns the width of the icon in pixels.
 133:      *
 134:      * @return the height of the icon
 135:      */
 136:     public int getIconWidth()
 137:     {
 138:       return 9;
 139:     }
 140: 
 141:     /**
 142:      * Paints the icon.
 143:      *
 144:      * @param c the component to be painted
 145:      * @param g the Graphics context to be painted with
 146:      * @param x the x position of the icon
 147:      * @param y the y position of the icon
 148:      */
 149:     public void paintIcon(Component c, Graphics g, int x, int y)
 150:     {
 151:       JCheckBoxMenuItem item = (JCheckBoxMenuItem) c;
 152:       if (item.isSelected()) 
 153:         {
 154:           // paint the check...
 155:           g.setColor(Color.black);
 156:           g.drawLine(x + 1, y + 3, x + 1, y + 4);
 157:           g.drawLine(x + 2, y + 4, x + 2, y + 5);
 158:           for (int i = 0; i < 5; i++)
 159:             g.drawLine(x + 3 + i, y + 5 - i, x + 3 + i, y + 6 - i);    
 160:         }
 161:     }
 162:   }
 163: 
 164:   /**
 165:    * The icon used for RadioButtons in the BasicLookAndFeel. This is an empty
 166:    * icon with a size of 13x13 pixels.
 167:    */
 168:   static class RadioButtonIcon
 169:     implements Icon
 170:   {
 171:     /**
 172:      * Returns the height of the icon. The BasicLookAndFeel RadioButton icon
 173:      * has a height of 13 pixels.
 174:      *
 175:      * @return the height of the icon
 176:      */
 177:     public int getIconHeight()
 178:     {
 179:       return 13;
 180:     }
 181: 
 182:     /**
 183:      * Returns the width of the icon. The BasicLookAndFeel RadioButton icon
 184:      * has a width of 13 pixels.
 185:      *
 186:      * @return the height of the icon
 187:      */
 188:     public int getIconWidth()
 189:     {
 190:       return 13;
 191:     }
 192: 
 193:     /**
 194:      * Paints the icon. The BasicLookAndFeel RadioButton icon is empty and does
 195:      * not need to be painted.
 196:      *
 197:      * @param c the component to be painted
 198:      * @param g the Graphics context to be painted with
 199:      * @param x the x position of the icon
 200:      * @param y the y position of the icon
 201:      */
 202:     public void paintIcon(Component c, Graphics g, int x, int y)
 203:     {
 204:       // The icon is empty and needs no painting.
 205:     }
 206:   }
 207:   /** The cached CheckBoxIcon instance. */
 208:   private static CheckBoxIcon checkBoxIcon;
 209:   
 210:   /** The cached RadioButtonIcon instance. */
 211:   private static RadioButtonIcon radioButtonIcon;
 212: 
 213:   public static Icon getMenuItemCheckIcon()
 214:   {
 215:     return new Icon()
 216:     {
 217:       public int getIconHeight()
 218:       {
 219:         return 13;
 220:       }
 221: 
 222:       public int getIconWidth()
 223:       {
 224:         return 13;
 225:       }
 226: 
 227:       public void paintIcon(Component c, Graphics g, int x, int y)
 228:       {
 229:         Color saved = g.getColor();
 230:         g.setColor(Color.BLACK);
 231:         g.drawLine(3 + x, 5 + y, 3 + x, 9 + y);
 232:         g.drawLine(4 + x, 5 + y, 4 + x, 9 + y);
 233:         g.drawLine(5 + x, 7 + y, 9 + x, 3 + y);
 234:         g.drawLine(5 + x, 8 + y, 9 + x, 4 + y);
 235:         g.setColor(saved);
 236:       }
 237:     };
 238:   }
 239:   public static Icon getMenuItemArrowIcon()
 240:   {
 241:     return new DummyIcon();
 242:   }
 243:   
 244:   /**
 245:    * Returns a new instance of a 4 x 8 icon showing a small black triangle that
 246:    * points to the right.  This is displayed in menu items that have a 
 247:    * sub menu.
 248:    * 
 249:    * @return The icon.
 250:    */
 251:   public static Icon getMenuArrowIcon()
 252:   {
 253:     return new Icon()
 254:       {
 255:     public int getIconHeight()
 256:     {
 257:       return 8;
 258:     }
 259:     public int getIconWidth()
 260:     {
 261:       return 4;
 262:     }
 263:     public void paintIcon(Component c, Graphics g, int x, int y)
 264:     {
 265:       Color saved = g.getColor();
 266:       g.setColor(Color.BLACK);
 267:           for (int i = 0; i < 4; i++)
 268:             g.drawLine(x + i, y + i, x + i, y + 7 - i);
 269:       g.setColor(saved);
 270:     }
 271:       };
 272:   }
 273: 
 274:   /**
 275:    * Returns an icon for CheckBoxes in the BasicLookAndFeel. CheckBox icons
 276:    * in the Basic L&amp;F are empty and have a size of 13x13 pixels.
 277:    * This method returns a shared single instance of this icon.
 278:    *
 279:    * @return an icon for CheckBoxes in the BasicLookAndFeel
 280:    */
 281:   public static Icon getCheckBoxIcon()
 282:   {
 283:     if (checkBoxIcon == null)
 284:       checkBoxIcon = new CheckBoxIcon();
 285:     return checkBoxIcon;
 286:   }
 287: 
 288:   /**
 289:    * Returns an icon for RadioButtons in the BasicLookAndFeel. RadioButton
 290:    * icons in the Basic L&amp;F are empty and have a size of 13x13 pixels.
 291:    * This method returns a shared single instance of this icon.
 292:    *
 293:    * @return an icon for RadioButtons in the BasicLookAndFeel
 294:    */
 295:   public static Icon getRadioButtonIcon()
 296:   {
 297:     if (radioButtonIcon == null)
 298:       radioButtonIcon = new RadioButtonIcon();
 299:     return radioButtonIcon;
 300:   }
 301: 
 302:   /**
 303:    * Creates and returns an icon used when rendering {@link JCheckBoxMenuItem}
 304:    * components.
 305:    * 
 306:    * @return An icon.
 307:    */
 308:   public static Icon getCheckBoxMenuItemIcon()
 309:   {
 310:     return new CheckBoxMenuItemIcon();
 311:   }
 312:   
 313:   public static Icon getRadioButtonMenuItemIcon()
 314:   {
 315:     return getRadioButtonIcon();
 316:   }
 317:   
 318:   public static Icon createEmptyFrameIcon()
 319:   {
 320:     return new DummyIcon();
 321:   }
 322: } // class BasicIconFactory