jdk/src/share/classes/javax/swing/border/EtchedBorder.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 22574 7f8ce0c8c20a
child 24500 399b77799d66
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
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, 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.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A class which implements a simple etched border which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * either be etched-in or etched-out.  If no highlight/shadow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * colors are initialized when the border is created, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * these colors will be dynamically derived from the background
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * color of the component argument passed into the paintBorder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * 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: 5506
diff changeset
    47
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author David Kloba
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    54
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class EtchedBorder extends AbstractBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** Raised etched type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final int RAISED  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Lowered etched type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final int LOWERED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected int etchType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected Color highlight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected Color shadow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates a lowered etched border whose colors will be derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * from the background color of the component passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * the paintBorder method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public EtchedBorder()    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this(LOWERED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Creates an etched border with the specified etch-type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * whose colors will be derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * from the background color of the component passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * the paintBorder method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param etchType the type of etch to be drawn by the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public EtchedBorder(int etchType)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this(etchType, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Creates a lowered etched border with the specified highlight and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * shadow colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param highlight the color to use for the etched highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param shadow the color to use for the etched shadow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public EtchedBorder(Color highlight, Color shadow)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this(LOWERED, highlight, shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Creates an etched border with the specified etch-type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * highlight and shadow colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param etchType the type of etch to be drawn by the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param highlight the color to use for the etched highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param shadow the color to use for the etched shadow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    @ConstructorProperties({"etchType", "highlightColor", "shadowColor"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public EtchedBorder(int etchType, Color highlight, Color shadow)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.etchType = etchType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.highlight = highlight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.shadow = shadow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Paints the border for the specified component with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * specified position and size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param c the component for which this border is being painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param g the paint graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param x the x position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param y the y position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param width the width of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param height the height of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int w = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int h = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        g.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        g.setColor(etchType == LOWERED? getShadowColor(c) : getHighlightColor(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        g.drawRect(0, 0, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        g.setColor(etchType == LOWERED? getHighlightColor(c) : getShadowColor(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        g.drawLine(1, h-3, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        g.drawLine(1, 1, w-3, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        g.drawLine(0, h-1, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        g.drawLine(w-1, h-1, w-1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        g.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Reinitialize the insets parameter with this Border's current Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param c the component for which this border insets value applies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param insets the object to be reinitialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        insets.set(2, 2, 2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Returns whether or not the border is opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public boolean isBorderOpaque() { return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Returns which etch-type is set on the etched border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public int getEtchType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return etchType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Returns the highlight color of the etched border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * when rendered on the specified component.  If no highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * color was specified at instantiation, the highlight color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * is derived from the specified component's background color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param c the component for which the highlight may be derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public Color getHighlightColor(Component c)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return highlight != null? highlight :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                       c.getBackground().brighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns the highlight color of the etched border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Will return null if no highlight color was specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * at instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public Color getHighlightColor()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return highlight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns the shadow color of the etched border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * when rendered on the specified component.  If no shadow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * color was specified at instantiation, the shadow color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * is derived from the specified component's background color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param c the component for which the shadow may be derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public Color getShadowColor(Component c)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return shadow != null? shadow : c.getBackground().darker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Returns the shadow color of the etched border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Will return null if no shadow color was specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * at instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public Color getShadowColor()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return shadow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}