Source for gnu.javax.swing.text.html.ImageViewIconFactory

   1: package gnu.javax.swing.text.html;
   2: 
   3: 
   4: import java.awt.Color;
   5: import java.awt.Component;
   6: import java.awt.Graphics;
   7: import java.io.Serializable;
   8: 
   9: import javax.swing.Icon;
  10: import javax.swing.plaf.metal.MetalLookAndFeel;
  11: 
  12: /**
  13:  * Creates icons for ImageView. The icons reflect the basic ideas of the Sun's
  14:  * icons as they would be described in the text (sheet of paper with image and
  15:  * broken sheet of paper with image). They are not pixel to pixel identical and
  16:  * contain elements from the metal icon factory.
  17:  *
  18:  * @author Audrius Meskauskas (audriusa@bioinformatics.org)
  19:  */
  20: public class ImageViewIconFactory
  21: {
  22:   private static Icon noImageIcon;
  23: 
  24:   private static Icon loadingImageIcon;
  25: 
  26:   /**
  27:    * This icon reflects the general concept (broken sheet of paper with
  28:    * image), but is currently not pixel to pixel identical with the Sun's
  29:    * implementation.
  30:    */
  31:   public static class NoImageIcon implements Icon, Serializable
  32:   {
  33:     /**
  34:      * Creates a new icon.
  35:      */
  36:     public NoImageIcon()
  37:     {
  38:       // Nothing to do here.
  39:     }
  40: 
  41:     /**
  42:      * Returns the width of the icon, in pixels.
  43:      *
  44:      * @return The width of the icon.
  45:      */
  46:     public int getIconWidth()
  47:     {
  48:       return 38;
  49:     }
  50: 
  51:     /**
  52:      * Returns the height of the icon, in pixels.
  53:      *
  54:      * @return The height of the icon.
  55:      */
  56:     public int getIconHeight()
  57:     {
  58:       return 38;
  59:     }
  60: 
  61:     /**
  62:      * Paints the icon using colors from the {@link MetalLookAndFeel}.
  63:      *
  64:      * @param c
  65:      *          the component (ignored).
  66:      * @param g
  67:      *          the graphics device.
  68:      * @param x
  69:      *          the x-coordinate for the top-left of the icon.
  70:      * @param y
  71:      *          the y-coordinate for the top-left of the icon.
  72:      */
  73:     public void paintIcon(Component c, Graphics g, int x, int y)
  74:     {
  75:       // frame
  76:       Color savedColor = g.getColor();
  77: 
  78:       g.setColor(MetalLookAndFeel.getBlack());
  79: 
  80:       g.drawLine(x, y, x + 19, y);
  81: 
  82:       g.drawLine(x, y + 1, x, y + 5);
  83:       g.drawLine(x, y + 13, x, y + 25);
  84: 
  85:       g.drawLine(x, y + 25, x + 22, y + 25);
  86: 
  87:       g.drawLine(x + 22, y + 25, x + 22, y + 21);
  88:       g.drawLine(x + 22, y + 13, x + 22, y + 6);
  89: 
  90:       g.drawLine(x + 22, y + 6, x + 19, y);
  91: 
  92:       g.drawLine(x + 17, y + 2, x + 21, y + 6);
  93: 
  94:       g.drawLine(x + 18, y + 1, x + 19, y + 1);
  95: 
  96:       g.setColor(MetalLookAndFeel.getControlShadow());
  97: 
  98:       g.drawLine(x + 1, y + 1, x + 17, y + 1);
  99: 
 100:       g.drawLine(x + 1, y + 1, x + 1, y + 5);
 101:       g.drawLine(x + 1, y + 13, x + 1, y + 24);
 102: 
 103:       g.drawLine(x + 1, y + 24, x + 21, y + 24);
 104: 
 105:       g.drawLine(x + 21, y + 24, x + 21, y + 21);
 106:       g.drawLine(x + 21, y + 13, x + 21, y + 7);
 107: 
 108:       g.drawLine(x + 18, y + 2, x + 20, y + 4);
 109: 
 110:       // Breaking line
 111: 
 112:       // Shadow
 113:       g.drawLine(x + 1, y + 6, x + 20, y + 13);
 114:       g.drawLine(x + 1, y + 13, x + 20, y + 20);
 115: 
 116:       // Edge
 117:       g.setColor(MetalLookAndFeel.getBlack());
 118:       g.drawLine(x, y + 6, x + 21, y + 14);
 119:       g.drawLine(x, y + 12, x + 21, y + 20);
 120: 
 121:       // Picture
 122: 
 123:       y += 1;
 124:       x += 3;
 125: 
 126:       g.setColor(MetalLookAndFeel.getBlack());
 127: 
 128:       // roof
 129:       g.drawLine(x + 4, y + 5, x + 8, y + 1);
 130:       g.drawLine(x + 8, y + 1, x + 15, y + 8);
 131: 
 132:       // chimney
 133:       g.drawLine(x + 11, y + 2, x + 11, y + 4);
 134:       g.drawLine(x + 12, y + 2, x + 12, y + 5);
 135: 
 136:       g.setColor(MetalLookAndFeel.getControlDarkShadow());
 137: 
 138:       // roof paint
 139:       int xx = x + 8;
 140:       for (int i = 0; i < 4; i++)
 141:         g.drawLine(xx - i, y + 2 + i, xx + i, y + 2 + i);
 142:       g.fillRect(x + 4, y + 6, 9, 2);
 143: 
 144:       // base of house
 145:       g.drawLine(x + 3, y + 14, x + 3, y + 18);
 146:       g.drawLine(x + 3, y + 18, x + 13, y + 18);
 147: 
 148:       g.setColor(savedColor);
 149:     }
 150:   }
 151: 
 152:   /**
 153:    * This icon reflects the general concept (sheet of paper with image), but is
 154:    * currently not pixel to pixel identical with the Sun's implementation.
 155:    */
 156:   public static class LoadingImageIcon implements Icon, Serializable
 157:   {
 158: 
 159:     /**
 160:      * Creates a new icon.
 161:      */
 162:     public LoadingImageIcon()
 163:     {
 164:       // Nothing to do here.
 165:     }
 166: 
 167:     /**
 168:      * Returns the width of the icon, in pixels.
 169:      *
 170:      * @return The width of the icon.
 171:      */
 172:     public int getIconWidth()
 173:     {
 174:       return 38;
 175:     }
 176: 
 177:     /**
 178:      * Returns the height of the icon, in pixels.
 179:      *
 180:      * @return The height of the icon.
 181:      */
 182:     public int getIconHeight()
 183:     {
 184:       return 38;
 185:     }
 186: 
 187:     /**
 188:      * Paints the icon using colors from the {@link MetalLookAndFeel}.
 189:      *
 190:      * @param c
 191:      *          the component (ignored).
 192:      * @param g
 193:      *          the graphics device.
 194:      * @param x
 195:      *          the x-coordinate for the top-left of the icon.
 196:      * @param y
 197:      *          the y-coordinate for the top-left of the icon.
 198:      */
 199:     public void paintIcon(Component c, Graphics g, int x, int y)
 200:     {
 201:       // frame
 202:       Color savedColor = g.getColor();
 203: 
 204:       g.setColor(Color.black);
 205:       g.drawLine(x, y, x + 19, y);
 206:       g.drawLine(x, y + 1, x, y + 25);
 207:       g.drawLine(x, y + 25, x + 22, y + 25);
 208:       g.drawLine(x + 22, y + 25, x + 22, y + 6);
 209:       g.drawLine(x + 22, y + 6, x + 19, y);
 210: 
 211:       g.drawLine(x + 17, y + 2, x + 21, y + 6);
 212:       g.drawLine(x + 18, y + 1, x + 19, y + 1);
 213: 
 214:       g.setColor(new Color(204, 204, 255));
 215: 
 216:       g.drawLine(x + 1, y + 1, x + 17, y + 1);
 217:       g.drawLine(x + 1, y + 1, x + 1, y + 24);
 218:       g.drawLine(x + 1, y + 24, x + 21, y + 24);
 219:       g.drawLine(x + 21, y + 24, x + 21, y + 7);
 220:       g.drawLine(x + 18, y + 2, x + 20, y + 4);
 221: 
 222:       // Picture (house)
 223: 
 224:       y += 3;
 225:       x += 3;
 226: 
 227:       g.setColor(MetalLookAndFeel.getBlack());
 228: 
 229:       // roof
 230:       g.drawLine(x + 1, y + 8, x + 8, y + 1);
 231:       g.drawLine(x + 8, y + 1, x + 15, y + 8);
 232: 
 233:       // base of house
 234:       g.drawLine(x + 3, y + 6, x + 3, y + 15);
 235:       g.drawLine(x + 3, y + 15, x + 13, y + 15);
 236:       g.drawLine(x + 13, y + 6, x + 13, y + 15);
 237: 
 238:       // door frame
 239:       g.drawLine(x + 6, y + 9, x + 6, y + 15);
 240:       g.drawLine(x + 6, y + 9, x + 10, y + 9);
 241:       g.drawLine(x + 10, y + 9, x + 10, y + 15);
 242: 
 243:       // chimney
 244:       g.drawLine(x + 11, y + 2, x + 11, y + 4);
 245:       g.drawLine(x + 12, y + 2, x + 12, y + 5);
 246: 
 247:       g.setColor(MetalLookAndFeel.getControlDarkShadow());
 248: 
 249:       // roof paint
 250:       int xx = x + 8;
 251:       for (int i = 0; i < 4; i++)
 252:         g.drawLine(xx - i, y + 2 + i, xx + i, y + 2 + i);
 253:       g.fillRect(x + 4, y + 6, 9, 2);
 254: 
 255:       // door knob
 256:       g.drawLine(x + 9, y + 12, x + 9, y + 12);
 257: 
 258:       // house paint
 259:       g.setColor(MetalLookAndFeel.getPrimaryControl());
 260:       g.drawLine(x + 4, y + 8, x + 12, y + 8);
 261:       g.fillRect(x + 4, y + 9, 2, 6);
 262:       g.fillRect(x + 11, y + 9, 2, 6);
 263: 
 264:       g.setColor(savedColor);
 265:     }
 266:   }
 267: 
 268:   public static Icon getNoImageIcon()
 269:   {
 270:     if (noImageIcon == null)
 271:       noImageIcon = new NoImageIcon();
 272:     return noImageIcon;
 273:   }
 274: 
 275:   public static Icon getLoadingImageIcon()
 276:   {
 277:     if (loadingImageIcon == null)
 278:       loadingImageIcon = new LoadingImageIcon();
 279:     return loadingImageIcon;
 280:   }
 281: 
 282: }