jdk/src/share/classes/javax/swing/border/MatteBorder.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 23010 6dadb192ad81
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
/*
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.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.Icon;
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 provides a matte-like border of either a solid color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * or a tiled icon.
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 Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5597
diff changeset
    49
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class MatteBorder extends EmptyBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected Color color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected Icon tileIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Creates a matte border with the specified insets and color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param top the top inset of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param left the left inset of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param bottom the bottom inset of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param right the right inset of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param matteColor the color rendered for the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public MatteBorder(int top, int left, int bottom, int right, Color matteColor)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super(top, left, bottom, right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.color = matteColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Creates a matte border with the specified insets and color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param borderInsets the insets of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param matteColor the color rendered for the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public MatteBorder(Insets borderInsets, Color matteColor)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(borderInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.color = matteColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Creates a matte border with the specified insets and tile icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param top the top inset of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param left the left inset of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param bottom the bottom inset of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param right the right inset of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param tileIcon the icon to be used for tiling the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super(top, left, bottom, right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.tileIcon = tileIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Creates a matte border with the specified insets and tile icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param borderInsets the insets of the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param tileIcon the icon to be used for tiling the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public MatteBorder(Insets borderInsets, Icon tileIcon)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        super(borderInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.tileIcon = tileIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Creates a matte border with the specified tile icon.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * insets will be calculated dynamically based on the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * the tile icon, where the top and bottom will be equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * tile icon's height, and the left and right will be equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * the tile icon's width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param tileIcon the icon to be used for tiling the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public MatteBorder(Icon tileIcon)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this(-1,-1,-1,-1, tileIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Paints the matte border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Insets insets = getBorderInsets(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Color oldColor = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        g.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // If the tileIcon failed loading, paint as gray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (tileIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            color = (tileIcon.getIconWidth() == -1) ? Color.gray : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            g.fillRect(0, 0, width - insets.right, insets.top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            g.fillRect(0, insets.top, insets.left, height - insets.top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            g.fillRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else if (tileIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int tileW = tileIcon.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int tileH = tileIcon.getIconHeight();
5584
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   138
            paintEdge(c, g, 0, 0, width - insets.right, insets.top, tileW, tileH);
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   139
            paintEdge(c, g, 0, insets.top, insets.left, height - insets.top, tileW, tileH);
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   140
            paintEdge(c, g, insets.left, height - insets.bottom, width - insets.left, insets.bottom, tileW, tileH);
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   141
            paintEdge(c, g, width - insets.right, 0, insets.right, height - insets.bottom, tileW, tileH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        g.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
5584
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   148
    private void paintEdge(Component c, Graphics g, int x, int y, int width, int height, int tileW, int tileH) {
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   149
        g = g.create(x, y, width, height);
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   150
        int sY = -(y % tileH);
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   151
        for (x = -(x % tileW); x < width; x += tileW) {
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   152
            for (y = sY; y < height; y += tileH) {
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   153
                this.tileIcon.paintIcon(c, g, x, y);
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   154
            }
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   155
        }
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   156
        g.dispose();
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   157
    }
b91509fa019d 6910490: MatteBorder JScrollpane interaction
malenkov
parents: 2
diff changeset
   158
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Reinitialize the insets parameter with this Border's current Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param c the component for which this border insets value applies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param insets the object to be reinitialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return computeInsets(insets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Returns the insets of the border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public Insets getBorderInsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return computeInsets(new Insets(0,0,0,0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /* should be protected once api changes area allowed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private Insets computeInsets(Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (tileIcon != null && top == -1 && bottom == -1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            left == -1 && right == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int w = tileIcon.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            int h = tileIcon.getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            insets.top = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            insets.right = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            insets.bottom = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            insets.left = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            insets.left = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            insets.top = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            insets.right = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            insets.bottom = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return insets;
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 color used for tiling the border or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * if a tile icon is being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public Color getMatteColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns the icon used for tiling the border or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * if a solid color is being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public Icon getTileIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return tileIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Returns whether or not the border is opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public boolean isBorderOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // If a tileIcon is set, then it may contain transparent bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return color != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}