jdk/src/share/classes/javax/swing/border/AbstractBorder.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 23010 6dadb192ad81
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 11268
diff changeset
    43
 * of all JavaBeans&trade;
2
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
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
    49
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public abstract class AbstractBorder implements Border, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * This default implementation does no painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @param c the component for which this border is being painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param g the paint graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param x the x position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param y the y position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param width the width of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param height the height of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * This default implementation returns a new {@link Insets} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * that is initialized by the {@link #getBorderInsets(Component,Insets)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * By default the {@code top}, {@code left}, {@code bottom},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * and {@code right} fields are set to {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param c  the component for which this border insets value applies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @return a new {@link Insets} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public Insets getBorderInsets(Component c)       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return getBorderInsets(c, new Insets(0, 0, 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Reinitializes the insets parameter with this Border's current Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param c the component for which this border insets value applies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param insets the object to be reinitialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @return the <code>insets</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        insets.left = insets.top = insets.right = insets.bottom = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * This default implementation returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public boolean isBorderOpaque() { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * This convenience method calls the static method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param c the component for which this border is being computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param x the x position of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param y the y position of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param width the width of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param height the height of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @return a <code>Rectangle</code> containing the interior coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public Rectangle getInteriorRectangle(Component c, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return getInteriorRectangle(c, this, x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns a rectangle using the arguments minus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * insets of the border. This is useful for determining the area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * that components should draw in that will not intersect the border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param c the component for which this border is being computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param b the <code>Border</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param x the x position of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param y the y position of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param width the width of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param height the height of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return a <code>Rectangle</code> containing the interior coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static Rectangle getInteriorRectangle(Component c, Border b, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Insets insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if(b != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            insets = b.getBorderInsets(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            insets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return new Rectangle(x + insets.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                    y + insets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                    width - insets.right - insets.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                    height - insets.top - insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns the baseline.  A return value less than 0 indicates the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * does not have a reasonable baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * The default implementation returns -1.  Subclasses that support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * baseline should override appropriately.  If a value &gt;= 0 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * returned, then the component has a valid baseline for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * size &gt;= the minimum size and <code>getBaselineResizeBehavior</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * can be used to determine how the baseline changes with size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param c <code>Component</code> baseline is being requested for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param width the width to get the baseline for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param height the height to get the baseline for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @return the baseline or &lt; 0 indicating there is no reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *         baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @throws IllegalArgumentException if width or height is &lt; 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @see java.awt.Component#getBaseline(int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see java.awt.Component#getBaselineResizeBehavior()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public int getBaseline(Component c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (width < 0 || height < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    "Width and height must be >= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Returns an enum indicating how the baseline of a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * changes as the size changes.  This method is primarily meant for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * layout managers and GUI builders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <code>BaselineResizeBehavior.OTHER</code>, subclasses that support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * baseline should override appropriately.  Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * never return <code>null</code>; if the baseline can not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * calculated return <code>BaselineResizeBehavior.OTHER</code>.  Callers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * should first ask for the baseline using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>getBaseline</code> and if a value &gt;= 0 is returned use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * this method.  It is acceptable for this method to return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * value other than <code>BaselineResizeBehavior.OTHER</code> even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <code>getBaseline</code> returns a value less than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param c <code>Component</code> to return baseline resize behavior for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return an enum indicating how the baseline changes as the border is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *         resized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @see java.awt.Component#getBaseline(int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see java.awt.Component#getBaselineResizeBehavior()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new NullPointerException("Component must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Convenience function for determining ComponentOrientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Helps us avoid having Munge directives throughout the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static boolean isLeftToRight( Component c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return c.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}