Source for javax.swing.border.CompoundBorder

   1: /* CompoundBorder.java -- 
   2:    Copyright (C) 2003 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.Component;
  42: import java.awt.Graphics;
  43: import java.awt.Insets;
  44: 
  45: /**
  46:  * A Border that is composed of an interior and an exterior border,
  47:  * where the interior border is tightly nested into the exterior.
  48:  *
  49:  * @author Sascha Brawer (brawer@dandelis.ch)
  50:  */
  51: public class CompoundBorder extends AbstractBorder
  52: {
  53:   /**
  54:    * Determined using the <code>serialver</code> tool
  55:    * of Apple/Sun JDK 1.3.1 on MacOS X 10.1.5.
  56:    */
  57:   static final long serialVersionUID = 9054540377030555103L;
  58: 
  59:   /**
  60:    * The inside border, which is painted between the bordered
  61:    * Component and the outside border. It is valid for
  62:    * <code>insideBorder</code> to be <code>null</code>.
  63:    */
  64:   protected Border insideBorder;
  65: 
  66:   /**
  67:    * The outside border, which is painted outside both the
  68:    * bordered Component and the inside border. It is valid for
  69:    * <code>outsideBorder</code> to be <code>null</code>.
  70:    */
  71:   protected Border outsideBorder;
  72: 
  73:   /**
  74:    * Constructs a CompoundBorder whose inside and outside borders
  75:    * are both <code>null</code>. While this does not really make
  76:    * any sense (there exists a class EmptyBorder as well, and not
  77:    * every Component needs to have a border at all), the API
  78:    * specification requires the existence of this constructor.
  79:    *
  80:    * @see EmptyBorder
  81:    */
  82:   public CompoundBorder()
  83:   {
  84:     this (null, null);
  85:   }
  86: 
  87:   /**
  88:    * Constructs a CompoundBorder with the specified inside and
  89:    * outside borders.
  90:    *
  91:    * @param outsideBorder the outside border, which is painted to the
  92:    *        outside of both <code>insideBorder</code> and the enclosed
  93:    *        component. It is acceptable to pass <code>null</code>, in
  94:    *        which case no outside border is painted.
  95:    *
  96:    * @param insideBorder the inside border, which is painted to
  97:    *        between <code>outsideBorder</code> and the enclosed
  98:    *        component. It is acceptable to pass <code>null</code>, in
  99:    *        which case no inside border is painted.
 100:    */
 101:   public CompoundBorder(Border outsideBorder, Border insideBorder)
 102:   {
 103:     this.outsideBorder = outsideBorder;
 104:     this.insideBorder = insideBorder;
 105:   }
 106: 
 107:   /**
 108:    * Determines whether or not this border is opaque. An opaque
 109:    * border fills every pixel in its area when painting. Partially
 110:    * translucent borders must return <code>false</code>, or ugly
 111:    * artifacts can appear on screen.
 112:    *
 113:    * @return <code>true</code> if both the inside and outside borders
 114:    *         are opaque, or <code>false</code> otherwise.
 115:    */
 116:   public boolean isBorderOpaque()
 117:   {
 118:     // While it would be safe to assume true for the opacity of
 119:     // a null border, this behavior would not be according to
 120:     // the API specification. Also, it is pathological to have
 121:     // null borders anyway.
 122:     if ((insideBorder == null) || (outsideBorder == null))
 123:       return false;
 124: 
 125:     return insideBorder.isBorderOpaque()
 126:       && outsideBorder.isBorderOpaque();
 127:   }
 128: 
 129:   /**
 130:    * Paints the compound border by first painting the outside border,
 131:    * then painting the inside border tightly nested into the outside. 
 132:    *
 133:    * @param c the component whose border is to be painted.
 134:    * @param g the graphics for painting.
 135:    * @param x the horizontal position for painting the border.
 136:    * @param y the vertical position for painting the border.
 137:    * @param width the width of the available area for painting the border.
 138:    * @param height the height of the available area for painting the border.
 139:    */
 140:   public void paintBorder(Component c, Graphics g,
 141:                           int x, int y, int width, int height)
 142:   {
 143:     // If there is an outside border, paint it and reduce the
 144:     // bounding box by its insets.
 145:     //
 146:     if (outsideBorder != null)
 147:     {
 148:       Insets outsideInsets;
 149: 
 150:       outsideBorder.paintBorder(c, g, x, y, width, height);
 151:       outsideInsets = outsideBorder.getBorderInsets(c);
 152: 
 153:       x += outsideInsets.left;
 154:       y += outsideInsets.top;
 155: 
 156:       // Reduce width and height by the respective extent of the
 157:       // outside border.
 158:       width -= outsideInsets.left + outsideInsets.right;
 159:       height -= outsideInsets.top + outsideInsets.bottom;
 160:     }
 161: 
 162:     if (insideBorder != null)
 163:       insideBorder.paintBorder(c, g, x, y, width, height);
 164:   }
 165: 
 166:   /**
 167:    * Changes the specified insets to the insets of this border,
 168:    * which is the sum of the insets of the inside and the outside
 169:    * border.
 170:    *
 171:    * @param c the component in the center of this border.
 172:    * @param insets an Insets object for holding the added insets.
 173:    *
 174:    * @return the <code>insets</code> object.
 175:    */
 176:   public Insets getBorderInsets(Component c, Insets insets)
 177:   {
 178:     Insets borderInsets;
 179: 
 180:     if (insets == null)
 181:       insets = new Insets(0, 0, 0, 0);
 182:     else
 183:       insets.left = insets.right = insets.top = insets.bottom = 0;
 184: 
 185:     // If there is an outside border, add it to insets.
 186:     if (outsideBorder != null)
 187:     {
 188:       borderInsets = outsideBorder.getBorderInsets(c);
 189:       insets.left += borderInsets.left;
 190:       insets.right += borderInsets.right;
 191:       insets.top += borderInsets.top;
 192:       insets.bottom += borderInsets.bottom;
 193:     }
 194: 
 195:     // If there is an inside border, add it to insets.
 196:     if (insideBorder != null)
 197:     {
 198:       borderInsets = insideBorder.getBorderInsets(c);
 199:       insets.left += borderInsets.left;
 200:       insets.right += borderInsets.right;
 201:       insets.top += borderInsets.top;
 202:       insets.bottom += borderInsets.bottom;
 203:     }
 204: 
 205:     return insets;
 206:   }
 207: 
 208:   /**
 209:    * Determines the insets of this border, which is the sum of the
 210:    * insets of the inside and the outside border.
 211:    *
 212:    * @param c the component in the center of this border.
 213:    */
 214:   public Insets getBorderInsets(Component c)
 215:   {
 216:     // It is not clear why CompoundBorder does not simply inherit
 217:     // the implementation from AbstractBorder. However, we want
 218:     // to be compatible with the API specification, which overrides
 219:     // the getBorderInsets(Component) method.
 220:     return getBorderInsets(c, null);
 221:   }
 222: 
 223:   /**
 224:    * Returns the outside border, which is painted outside both the
 225:    * bordered Component and the inside border. It is valid for the
 226:    * result to be <code>null</code>.
 227:    * 
 228:    * @return The outside border (possibly <code>null</code>).
 229:    */
 230:   public Border getOutsideBorder()
 231:   {
 232:     return outsideBorder;
 233:   }
 234: 
 235:   /**
 236:    * Returns the inside border, which is painted between the bordered
 237:    * Component and the outside border. It is valid for the result to
 238:    * be <code>null</code>.
 239:    * 
 240:    * @return The inside border (possibly <code>null</code>).
 241:    */
 242:   public Border getInsideBorder()
 243:   {
 244:     return insideBorder;
 245:   }
 246: }