jdk/src/share/classes/javax/swing/border/CompoundBorder.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: 21982
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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A composite Border class used to compose two Border objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * into a single border by nesting an inside Border object within
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the insets of an outside Border object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * For example, this class may be used to add blank margin space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * to a component with an existing decorative border:
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 20458
diff changeset
    39
 *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 11268
diff changeset
    40
 * <pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *    Border border = comp.getBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *    Border margin = new EmptyBorder(10,10,10,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *    comp.setBorder(new CompoundBorder(border, margin));
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 11268
diff changeset
    44
 * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * 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: 20428
diff changeset
    51
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author David Kloba
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
    57
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class CompoundBorder extends AbstractBorder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected Border outsideBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected Border insideBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Creates a compound border with null outside and inside borders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public CompoundBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.outsideBorder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.insideBorder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Creates a compound border with the specified outside and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * inside borders.  Either border may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param outsideBorder the outside border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param insideBorder the inside border to be nested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    @ConstructorProperties({"outsideBorder", "insideBorder"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public CompoundBorder(Border outsideBorder, Border insideBorder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.outsideBorder = outsideBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.insideBorder = insideBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
1286
2c43fda67ddf 4778988: CompoundBorder.isBorderOpaque() has incorrect documentation.
malenkov
parents: 2
diff changeset
    83
     * Returns whether or not the compound border is opaque.
2c43fda67ddf 4778988: CompoundBorder.isBorderOpaque() has incorrect documentation.
malenkov
parents: 2
diff changeset
    84
     *
2c43fda67ddf 4778988: CompoundBorder.isBorderOpaque() has incorrect documentation.
malenkov
parents: 2
diff changeset
    85
     * @return {@code true} if the inside and outside borders
2c43fda67ddf 4778988: CompoundBorder.isBorderOpaque() has incorrect documentation.
malenkov
parents: 2
diff changeset
    86
     *         are each either {@code null} or opaque;
2c43fda67ddf 4778988: CompoundBorder.isBorderOpaque() has incorrect documentation.
malenkov
parents: 2
diff changeset
    87
     *         or {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
1286
2c43fda67ddf 4778988: CompoundBorder.isBorderOpaque() has incorrect documentation.
malenkov
parents: 2
diff changeset
    89
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public boolean isBorderOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return (outsideBorder == null || outsideBorder.isBorderOpaque()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
               (insideBorder == null || insideBorder.isBorderOpaque());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Paints the compound border by painting the outside border
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * with the specified position and size and then painting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * inside border at the specified position and size offset by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * the insets of the outside border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param c the component for which this border is being painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param g the paint graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param x the x position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param y the y position of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param width the width of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param height the height of the painted border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        Insets  nextInsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int px, py, pw, ph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        px = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        py = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        pw = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ph = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if(outsideBorder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            outsideBorder.paintBorder(c, g, px, py, pw, ph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            nextInsets = outsideBorder.getBorderInsets(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            px += nextInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            py += nextInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            pw = pw - nextInsets.right - nextInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            ph = ph - nextInsets.bottom - nextInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if(insideBorder != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            insideBorder.paintBorder(c, g, px, py, pw, ph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Reinitialize the insets parameter with this Border's current Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param c the component for which this border insets value applies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param insets the object to be reinitialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        Insets  nextInsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        insets.top = insets.left = insets.right = insets.bottom = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if(outsideBorder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            nextInsets = outsideBorder.getBorderInsets(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            insets.top += nextInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            insets.left += nextInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            insets.right += nextInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            insets.bottom += nextInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if(insideBorder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            nextInsets = insideBorder.getBorderInsets(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            insets.top += nextInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            insets.left += nextInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            insets.right += nextInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            insets.bottom += nextInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Returns the outside border object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public Border getOutsideBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return outsideBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns the inside border object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public Border getInsideBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return insideBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}