Source for javax.swing.border.AbstractBorder

   1: /* AbstractBorder.java --
   2:    Copyright (C) 2003, 2006, 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: import java.awt.Rectangle;
  45: import java.io.Serializable;
  46: 
  47: 
  48: /**
  49:  * An invisible zero-width border, serving as a base class for
  50:  * implementing more interesting borders.
  51:  *
  52:  * @author Sascha Brawer (brawer@dandelis.ch)
  53:  * @author Ronald Veldema (rveldema@cs.vu.nl)
  54:  */
  55: public abstract class AbstractBorder implements Border, Serializable
  56: {
  57:   static final long serialVersionUID = -545885975315191844L;
  58: 
  59:   /**
  60:    * Constructs a new AbstractBorder.
  61:    */
  62:   public AbstractBorder()
  63:   {
  64:     // Nothing to do here.
  65:   }
  66: 
  67:   /**
  68:    * Performs nothing, because the default implementation provided by
  69:    * this class is an invisible, zero-width border. Subclasses will
  70:    * likely want to override this method, but they are not required
  71:    * to do so.
  72:    *
  73:    * @param c the component whose border is to be painted.
  74:    * @param g the graphics for painting.
  75:    * @param x the horizontal position for painting the border.
  76:    * @param y the vertical position for painting the border.
  77:    * @param width the width of the available area for painting the border.
  78:    * @param height the height of the available area for painting the border.
  79:    */
  80:   public void paintBorder(Component c, Graphics g, int x, int y, int width,
  81:                           int height)
  82:   {
  83:     // A previous version of Classpath had emitted a warning when
  84:     // this method was called. The warning was removed because it is
  85:     // perfectly legal for a subclass to not override the paintBorder
  86:     // method. An example would be EmptyBorder.
  87:   }
  88: 
  89:   /**
  90:    * Returns the insets required for drawing this border around the specified
  91:    * component.
  92:    *
  93:    * @param c  the component that the border applies to (ignored here,
  94:    *     subclasses may use it).
  95:    *
  96:    * @return an Insets object whose <code>left</code>, <code>right</code>,
  97:    *         <code>top</code> and <code>bottom</code> fields indicate the
  98:    *         width of the border at the respective edge, which is zero
  99:    *         for the default implementation provided by AbstractButton.
 100:    *
 101:    * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
 102:    */
 103:   public Insets getBorderInsets(Component c)
 104:   {
 105:     return new Insets(0, 0, 0, 0);
 106:   }
 107: 
 108:   /**
 109:    * Returns the insets required for drawing this border around the specified
 110:    * component.  The default implementation provided here sets the
 111:    * <code>left</code>, <code>right</code>, <code>top</code> and
 112:    * <code>bottom</code> fields of the passed <code>insets</code> parameter to
 113:    * zero.
 114:    *
 115:    * @param c  the component that the border applies to (ignored here,
 116:    *     subclasses may use it).
 117:    * @param insets  an instance that will be overwritten and returned as the
 118:    *     result (<code>null</code> not permitted).
 119:    *
 120:    * @return The border insets (the same object that was passed as the
 121:    *     <code>insets</code> argument).
 122:    *
 123:    * @see #getBorderInsets(Component)
 124:    *
 125:    * @throws NullPointerException if <code>insets</code> is <code>null</code>.
 126:    */
 127:   public Insets getBorderInsets(Component c, Insets insets)
 128:   {
 129:     insets.left = insets.right = insets.top = insets.bottom = 0;
 130:     return insets;
 131:   }
 132: 
 133:   /**
 134:    * Determines whether or not this border is opaque. An opaque border
 135:    * fills every pixel in its area when painting. Partially
 136:    * translucent borders must return <code>false</code>, or ugly
 137:    * artifacts can appear on screen. The default implementation
 138:    * provided here always returns <code>false</code>.
 139:    *
 140:    * @return <code>false</code>.
 141:    */
 142:   public boolean isBorderOpaque()
 143:   {
 144:     return false;
 145:   }
 146: 
 147:   /**
 148:    * Returns a rectangle that covers the specified area minus the insets
 149:    * required to draw this border.  Components that wish to determine an area
 150:    * into which they can safely draw without intersecting with a border might
 151:    * want to use this helper method.
 152:    *
 153:    * @param c the component in the center of this border.
 154:    * @param x the horizontal position of the border.
 155:    * @param y the vertical position of the border.
 156:    * @param width the width of the available area for the border.
 157:    * @param height the height of the available area for the border.
 158:    *
 159:    * @return The interior rectangle.
 160:    */
 161:   public Rectangle getInteriorRectangle(Component c, int x, int y, int width,
 162:                                         int height)
 163:   {
 164:     return getInteriorRectangle(c, this, x, y, width, height);
 165:   }
 166: 
 167:   /**
 168:    * Returns a rectangle that covers the specified area minus the insets
 169:    * required to draw the specified border (if the border is <code>null</code>,
 170:    * zero insets are assumed).  Components that wish to determine an area into
 171:    * which they can safely draw without intersecting with a border might want
 172:    * to use this helper method.
 173:    *
 174:    * @param c the component in the center of this border.
 175:    * @param b the border (<code>null</code> permitted).
 176:    * @param x the horizontal position of the border.
 177:    * @param y the vertical position of the border.
 178:    * @param width the width of the available area for the border.
 179:    * @param height the height of the available area for the border.
 180:    *
 181:    * @return The interior rectangle.
 182:    */
 183:   public static Rectangle getInteriorRectangle(Component c, Border b, int x,
 184:                                                int y, int width, int height)
 185:   {
 186:     Insets borderInsets;
 187: 
 188:     if (b != null)
 189:     {
 190:       borderInsets = b.getBorderInsets(c);
 191:       x += borderInsets.left;
 192:       y += borderInsets.top;
 193:       width -= borderInsets.left + borderInsets.right;
 194:       height -= borderInsets.top + borderInsets.bottom;
 195:     }
 196: 
 197:     return new Rectangle(x, y, width, height);
 198:   }
 199: }