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