1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.border;
  26 
  27 import java.awt.Graphics;
  28 import java.awt.Insets;
  29 import java.awt.Rectangle;
  30 import java.awt.Component;
  31 import java.io.Serializable;
  32 
  33 /**
  34  * A class that implements an empty border with no size.
  35  * This provides a convenient base class from which other border
  36  * classes can be easily derived.
  37  * <p>
  38  * <strong>Warning:</strong>
  39  * Serialized objects of this class will not be compatible with
  40  * future Swing releases. The current serialization support is
  41  * appropriate for short term storage or RMI between applications running
  42  * the same version of Swing.  As of 1.4, support for long term storage
  43  * of all JavaBeans&trade;
  44  * has been added to the <code>java.beans</code> package.
  45  * Please see {@link java.beans.XMLEncoder}.
  46  *
  47  * @author David Kloba
  48  */
  49 @SuppressWarnings("serial")
  50 public abstract class AbstractBorder implements Border, Serializable
  51 {
  52     /**
  53      * This default implementation does no painting.
  54      * @param c the component for which this border is being painted
  55      * @param g the paint graphics
  56      * @param x the x position of the painted border
  57      * @param y the y position of the painted border
  58      * @param width the width of the painted border
  59      * @param height the height of the painted border
  60      */
  61     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  62     }
  63 
  64     /**
  65      * This default implementation returns a new {@link Insets} object
  66      * that is initialized by the {@link #getBorderInsets(Component,Insets)}
  67      * method.
  68      * By default the {@code top}, {@code left}, {@code bottom},
  69      * and {@code right} fields are set to {@code 0}.
  70      *
  71      * @param c  the component for which this border insets value applies
  72      * @return a new {@link Insets} object
  73      */
  74     public Insets getBorderInsets(Component c)       {
  75         return getBorderInsets(c, new Insets(0, 0, 0, 0));
  76     }
  77 
  78     /**
  79      * Reinitializes the insets parameter with this Border's current Insets.
  80      * @param c the component for which this border insets value applies
  81      * @param insets the object to be reinitialized
  82      * @return the <code>insets</code> object
  83      */
  84     public Insets getBorderInsets(Component c, Insets insets) {
  85         insets.left = insets.top = insets.right = insets.bottom = 0;
  86         return insets;
  87     }
  88 
  89     /**
  90      * This default implementation returns false.
  91      * @return false
  92      */
  93     public boolean isBorderOpaque() { return false; }
  94 
  95     /**
  96      * This convenience method calls the static method.
  97      * @param c the component for which this border is being computed
  98      * @param x the x position of the border
  99      * @param y the y position of the border
 100      * @param width the width of the border
 101      * @param height the height of the border
 102      * @return a <code>Rectangle</code> containing the interior coordinates
 103      */
 104     public Rectangle getInteriorRectangle(Component c, int x, int y, int width, int height) {
 105         return getInteriorRectangle(c, this, x, y, width, height);
 106     }
 107 
 108     /**
 109      * Returns a rectangle using the arguments minus the
 110      * insets of the border. This is useful for determining the area
 111      * that components should draw in that will not intersect the border.
 112      * @param c the component for which this border is being computed
 113      * @param b the <code>Border</code> object
 114      * @param x the x position of the border
 115      * @param y the y position of the border
 116      * @param width the width of the border
 117      * @param height the height of the border
 118      * @return a <code>Rectangle</code> containing the interior coordinates
 119      */
 120     public static Rectangle getInteriorRectangle(Component c, Border b, int x, int y, int width, int height) {
 121         Insets insets;
 122         if(b != null)
 123             insets = b.getBorderInsets(c);
 124         else
 125             insets = new Insets(0, 0, 0, 0);
 126         return new Rectangle(x + insets.left,
 127                                     y + insets.top,
 128                                     width - insets.right - insets.left,
 129                                     height - insets.top - insets.bottom);
 130     }
 131 
 132     /**
 133      * Returns the baseline.  A return value less than 0 indicates the border
 134      * does not have a reasonable baseline.
 135      * <p>
 136      * The default implementation returns -1.  Subclasses that support
 137      * baseline should override appropriately.  If a value &gt;= 0 is
 138      * returned, then the component has a valid baseline for any
 139      * size &gt;= the minimum size and <code>getBaselineResizeBehavior</code>
 140      * can be used to determine how the baseline changes with size.
 141      *
 142      * @param c <code>Component</code> baseline is being requested for
 143      * @param width the width to get the baseline for
 144      * @param height the height to get the baseline for
 145      * @return the baseline or &lt; 0 indicating there is no reasonable
 146      *         baseline
 147      * @throws IllegalArgumentException if width or height is &lt; 0
 148      * @see java.awt.Component#getBaseline(int,int)
 149      * @see java.awt.Component#getBaselineResizeBehavior()
 150      * @since 1.6
 151      */
 152     public int getBaseline(Component c, int width, int height) {
 153         if (width < 0 || height < 0) {
 154             throw new IllegalArgumentException(
 155                     "Width and height must be >= 0");
 156         }
 157         return -1;
 158     }
 159 
 160     /**
 161      * Returns an enum indicating how the baseline of a component
 162      * changes as the size changes.  This method is primarily meant for
 163      * layout managers and GUI builders.
 164      * <p>
 165      * The default implementation returns
 166      * <code>BaselineResizeBehavior.OTHER</code>, subclasses that support
 167      * baseline should override appropriately.  Subclasses should
 168      * never return <code>null</code>; if the baseline can not be
 169      * calculated return <code>BaselineResizeBehavior.OTHER</code>.  Callers
 170      * should first ask for the baseline using
 171      * <code>getBaseline</code> and if a value &gt;= 0 is returned use
 172      * this method.  It is acceptable for this method to return a
 173      * value other than <code>BaselineResizeBehavior.OTHER</code> even if
 174      * <code>getBaseline</code> returns a value less than 0.
 175      *
 176      * @param c <code>Component</code> to return baseline resize behavior for
 177      * @return an enum indicating how the baseline changes as the border is
 178      *         resized
 179      * @see java.awt.Component#getBaseline(int,int)
 180      * @see java.awt.Component#getBaselineResizeBehavior()
 181      * @since 1.6
 182      */
 183     public Component.BaselineResizeBehavior getBaselineResizeBehavior(
 184             Component c) {
 185         if (c == null) {
 186             throw new NullPointerException("Component must be non-null");
 187         }
 188         return Component.BaselineResizeBehavior.OTHER;
 189     }
 190 
 191     /*
 192      * Convenience function for determining ComponentOrientation.
 193      * Helps us avoid having Munge directives throughout the code.
 194      */
 195     static boolean isLeftToRight( Component c ) {
 196         return c.getComponentOrientation().isLeftToRight();
 197     }
 198 
 199 }