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