Source for javax.swing.border.MatteBorder

   1: /* MatteBorder.java -- 
   2:    Copyright (C) 2003, 2004  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.border;
  40: 
  41: import java.awt.Color;
  42: import java.awt.Component;
  43: import java.awt.Graphics;
  44: import java.awt.Insets;
  45: 
  46: import javax.swing.Icon;
  47: 
  48: /**
  49:  * A border that is filled with either a solid color or with repeated
  50:  * icon tiles.
  51:  *
  52:  * <p><img src="doc-files/MatteBorder-1.png" width="500" height="150"
  53:  * alt="[Two MatteBorders]" />
  54:  *
  55:  * @author Sascha Brawer (brawer@dandelis.ch)
  56:  */
  57: public class MatteBorder extends EmptyBorder
  58: {
  59:   /**
  60:    * Determined using the <code>serialver</code> tool
  61:    * of Apple/Sun JDK 1.3.1 on MacOS X 10.1.5.
  62:    */
  63:   static final long serialVersionUID = 4422248989617298224L;
  64:   
  65:   
  66:   /**
  67:    * The color that is used for filling the border, or
  68:    * <code>null</code> if the border is filled with repetitions of a
  69:    * tile icon.
  70:    *
  71:    * @see #tileIcon
  72:    */
  73:   protected Color color;
  74:   
  75:   
  76:   /**
  77:    * The icon is used for filling the border with a tile, or
  78:    * <code>null</code> if the border is filled with a solid
  79:    * color.
  80:    *
  81:    * @see #color
  82:    */
  83:   protected Icon tileIcon;
  84:   
  85:   
  86:   /**
  87:    * Constructs a MatteBorder given the width on each side
  88:    * and a fill color.
  89:    *
  90:    * <p><img src="doc-files/MatteBorder-2.png" width="500" height="150"
  91:    * alt="[A picture of a MatteBorder made by this constructor]" />
  92:    *
  93:    * @param top the width of the border at its top edge.
  94:    * @param left the width of the border at its left edge.
  95:    * @param bottom the width of the border at its bottom edge.
  96:    * @param right the width of the border at its right edge.
  97:    * @param matteColor the color for filling the border.
  98:    */
  99:   public MatteBorder(int top, int left, int bottom, int right,
 100:                      Color matteColor)
 101:   {
 102:     super(top, left, bottom, right);
 103: 
 104:     if (matteColor == null)
 105:       throw new IllegalArgumentException();
 106: 
 107:     this.color = matteColor;
 108:   }
 109: 
 110: 
 111:   /**
 112:    * Constructs a MatteBorder given its insets and fill color.
 113:    *
 114:    * <p><img src="doc-files/MatteBorder-3.png" width="500" height="150"
 115:    * alt="[A picture of a MatteBorder made by this constructor]" />
 116:    *
 117:    * @param borderInsets an Insets object whose <code>top</code>,
 118:    *        <code>left</code>, <code>bottom</code> and <code>right</code>
 119:    *        fields indicate the with of the border at the respective
 120:    *        edge.
 121:    *
 122:    * @param matteColor the color for filling the border.
 123:    */
 124:   public MatteBorder(Insets borderInsets, Color matteColor)
 125:   {
 126:     this(borderInsets.top, borderInsets.left,
 127:          borderInsets.bottom, borderInsets.right,
 128:          matteColor);
 129:   }
 130: 
 131: 
 132:   /**
 133:    * Constructs a MatteBorder given the width on each side
 134:    * and an icon for tiling the border area.
 135:    *
 136:    * <p><img src="doc-files/MatteBorder-4.png" width="500" height="150"
 137:    * alt="[A picture of a MatteBorder made by this constructor]" />
 138:    *
 139:    * @param top the width of the border at its top edge.
 140:    * @param left the width of the border at its left edge.
 141:    * @param bottom the width of the border at its bottom edge.
 142:    * @param right the width of the border at its right edge.
 143:    * @param tileIcon an icon for tiling the border area.
 144:    */
 145:   public MatteBorder(int top, int left, int bottom, int right,
 146:                      Icon tileIcon)
 147:   {
 148:     super(top, left, bottom, right);
 149: 
 150:     if (tileIcon == null)
 151:       throw new IllegalArgumentException();
 152: 
 153:     this.tileIcon = tileIcon;
 154:   }
 155: 
 156: 
 157:   /**
 158:    * Constructs a MatteBorder given its insets and an icon
 159:    * for tiling the border area.
 160:    *
 161:    * <p><img src="doc-files/MatteBorder-5.png" width="500" height="150"
 162:    * alt="[A picture of a MatteBorder made by this constructor]" />
 163:    *
 164:    * @param borderInsets an Insets object whose <code>top</code>,
 165:    *        <code>left</code>, <code>bottom</code> and <code>right</code>
 166:    *        fields indicate the with of the border at the respective
 167:    *        edge.
 168:    *
 169:    * @param tileIcon an icon for tiling the border area.
 170:    */
 171:   public MatteBorder(Insets borderInsets, Icon tileIcon)
 172:   {
 173:     this(borderInsets.top, borderInsets.left,
 174:          borderInsets.bottom, borderInsets.right,
 175:          tileIcon);
 176:   }
 177:   
 178:   
 179:   /**
 180:    * Constructs a MatteBorder given an icon for tiling the
 181:    * border area. The icon width is used for the border insets
 182:    * at the left and right edge, the icon height for the top and
 183:    * bottom edge.
 184:    *
 185:    * <p><img src="doc-files/MatteBorder-6.png" width="379" height="150"
 186:    * alt="[A picture of a MatteBorder made by this constructor]" />
 187:    *
 188:    * @param tileIcon an icon for tiling the border area.
 189:    */
 190:   public MatteBorder(Icon tileIcon)
 191:   {
 192:     this(-1, -1, -1, -1, tileIcon);
 193:   }
 194:   
 195:   
 196:   /**
 197:    * Paints the border for a given component.
 198:    *
 199:    * @param c the component whose border is to be painted.
 200:    * @param g the graphics for painting.
 201:    * @param x the horizontal position for painting the border.
 202:    * @param y the vertical position for painting the border.
 203:    * @param width the width of the available area for painting the border.
 204:    * @param height the height of the available area for painting the border.
 205:    */
 206:   public void paintBorder(Component c, Graphics  g,
 207:                           int x, int y, int width, int height)
 208:   {
 209:     Insets i = getBorderInsets();
 210:     paintEdge(c, g, x, y, width, i.top, 0, 0);      // top edge
 211:     paintEdge(c, g, x, y + height - i.bottom,       // bottom edge
 212:               width, i.bottom,
 213:               0, height - i.bottom);
 214:     paintEdge(c, g, x, y + i.top,                   // left edge
 215:               i.left, height - i.top,
 216:               0, i.top);
 217:     paintEdge(c, g, x + width - i.right, y + i.top, // right edge
 218:               i.right, height - i.bottom,
 219:               width - i.right, i.top);
 220:   }
 221:   
 222:   
 223:   /**
 224:    * Measures the width of this border.
 225:    *
 226:    * @param c the component whose border is to be measured.
 227:    *
 228:    * @return an Insets object whose <code>left</code>, <code>right</code>,
 229:    *         <code>top</code> and <code>bottom</code> fields indicate the
 230:    *         width of the border at the respective edge.
 231:    *
 232:    * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
 233:    */
 234:   public Insets getBorderInsets(Component c)
 235:   {
 236:     /* There is no obvious reason for overriding this method, but we
 237:      * try to have exactly the same API as the Sun reference
 238:      * implementation.
 239:      */
 240:     return this.getBorderInsets(c, null);
 241:   }
 242:   
 243:   
 244:   /**
 245:    * Measures the width of this border, storing the results into a
 246:    * pre-existing Insets object.
 247:    *
 248:    * @param insets an Insets object for holding the result values.
 249:    *        After invoking this method, the <code>left</code>,
 250:    *        <code>right</code>, <code>top</code> and
 251:    *        <code>bottom</code> fields indicate the width of the
 252:    *        border at the respective edge.
 253:    *
 254:    * @return the same object that was passed for <code>insets</code>.
 255:    *
 256:    * @see #getBorderInsets()
 257:    */
 258:   public Insets getBorderInsets(Component c, Insets insets)
 259:   {
 260:     if (insets == null)
 261:       insets = new Insets(0, 0, 0, 0);
 262: 
 263:     if ((tileIcon != null)
 264:         && (top < 0) && (left < 0)
 265:         && (right < 0) && (bottom < 0))
 266:     {
 267:       insets.left = insets.right = tileIcon.getIconWidth();
 268:       insets.top = insets.bottom = tileIcon.getIconHeight();
 269:       return insets;
 270:     }
 271: 
 272:     /* Copy top, left, bottom and right into the respective
 273:      * field of insets.
 274:      */
 275:     return super.getBorderInsets(c, insets);
 276:   }
 277:   
 278:   
 279:   /**
 280:    * Measures the width of this border.
 281:    *
 282:    * @return an Insets object whose <code>left</code>, <code>right</code>,
 283:    *         <code>top</code> and <code>bottom</code> fields indicate the
 284:    *         width of the border at the respective edge.
 285:    *
 286:    * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
 287:    */
 288:   public Insets getBorderInsets()
 289:   {
 290:     /* The inherited implementation of EmptyBorder.isBorderOpaque()
 291:      * would do the same. It is not clear why this is overriden in the
 292:      * Sun implementation, at least not from just reading the JavaDoc.
 293:      */
 294:     return this.getBorderInsets(null, null);
 295:   }
 296:   
 297:   
 298:   /**
 299:    * Returns the color that is used for filling the border, or
 300:    * <code>null</code> if the border is filled with repetitions of a
 301:    * tile icon.
 302:    */
 303:   public Color getMatteColor()
 304:   {
 305:     return color;
 306:   }
 307:   
 308:   
 309:   /**
 310:    * Returns the icon is used for tiling the border, or
 311:    * <code>null</code> if the border is filled with a color instead of
 312:    * an icon.
 313:    */
 314:   public Icon getTileIcon()
 315:   {
 316:     return tileIcon;
 317:   }
 318:   
 319:   
 320:   /**
 321:    * Determines whether this border fills every pixel in its area
 322:    * when painting.
 323:    *
 324:    * @return <code>true</code> if the border is filled with an
 325:    *         opaque color; <code>false</code> if it is filled with
 326:    *         a semi-transparent color or with an icon.
 327:    */
 328:   public boolean isBorderOpaque()
 329:   {
 330:     return (color != null) && (color.getAlpha() == 255);
 331:   }
 332:   
 333: 
 334:   /**
 335:    * Paints a rectangular area of the border. This private helper
 336:    * method is called once for each of the border edges
 337:    * by {@link #paintBorder}.
 338:    *
 339:    * @param c the component whose border is being painted.
 340:    * @param g the graphics for painting.
 341:    * @param x the horizontal position of the rectangular area.
 342:    * @param y the vertical position of the rectangular area.
 343:    * @param width the width of the rectangular area.
 344:    * @param height the height of the rectangular area.
 345:    * @param dx the x displacement for repeating the tile.
 346:    * @param dy the y displacement for repeating the tile.
 347:    */
 348:   private void paintEdge(Component c, Graphics g,
 349:                          int x, int y, int width, int height,
 350:                          int dx, int dy)
 351:   {
 352:     Color oldColor;
 353:     int iconWidth, iconHeight;
 354:     Graphics clipped;
 355: 
 356:     if ((width <= 0) || (height <= 0))
 357:       return;
 358: 
 359:     /* Paint a colored rectangle if desired. */
 360:     if (color != null)
 361:     {
 362:       oldColor = g.getColor();
 363:       try
 364:       {
 365:         g.setColor(color);
 366:         g.fillRect(x, y, width, height);
 367:       }
 368:       finally
 369:       {
 370:         g.setColor(oldColor);
 371:       }
 372:       return;
 373:     }
 374: 
 375:     /* Determine the width and height of the icon. Some icons return
 376:      * -1 if it is an image whose dimensions have not yet been
 377:      * retrieved. There is not much we can do about this, but we
 378:      * should at least avoid entering the paint loop below
 379:      * with negative increments.
 380:      */
 381:     iconWidth = tileIcon.getIconWidth();
 382:     iconHeight = tileIcon.getIconHeight();
 383:     if ((iconWidth <= 0) || (iconHeight <= 0))
 384:       return;
 385: 
 386:     dx = dx % iconWidth;
 387:     dy = dy % iconHeight;
 388: 
 389:     clipped = g.create();
 390:     try
 391:     {
 392:       clipped.setClip(x, y, width, height);
 393:       for (int ty = y - dy; ty < y + height; ty += iconHeight)
 394:         for (int tx = x - dx; tx < x + width; tx += iconWidth)
 395:           tileIcon.paintIcon(c, clipped, tx, ty);
 396:     }
 397:     finally
 398:     {
 399:       clipped.dispose();
 400:     }
 401:   }
 402: }