jdk/src/share/classes/javax/swing/text/html/CSSBorder.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8530 cfe337a5776e
child 23697 e556a715949f
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8530
diff changeset
     2
 * Copyright (c) 2007, 2011, 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.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Polygon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.border.AbstractBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.text.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.text.html.CSS.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.text.html.CSS.BorderStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.text.html.CSS.BorderWidthValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.text.html.CSS.ColorValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.text.html.CSS.CssValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.text.html.CSS.LengthValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.swing.text.html.CSS.Value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * CSS-style borders for HTML elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Sergey Groznyh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
class CSSBorder extends AbstractBorder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** Indices for the attribute groups.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    final static int COLOR = 0, STYLE = 1, WIDTH = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** Indices for the box sides within the attribute group.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    final static int TOP = 0, RIGHT = 1, BOTTOM = 2, LEFT = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** The attribute groups.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    final static Attribute[][] ATTRIBUTES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        { Attribute.BORDER_TOP_COLOR, Attribute.BORDER_RIGHT_COLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
          Attribute.BORDER_BOTTOM_COLOR, Attribute.BORDER_LEFT_COLOR, },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        { Attribute.BORDER_TOP_STYLE, Attribute.BORDER_RIGHT_STYLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
          Attribute.BORDER_BOTTOM_STYLE, Attribute.BORDER_LEFT_STYLE, },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        { Attribute.BORDER_TOP_WIDTH, Attribute.BORDER_RIGHT_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
          Attribute.BORDER_BOTTOM_WIDTH, Attribute.BORDER_LEFT_WIDTH, },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** Parsers for the border properties.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    final static CssValue PARSERS[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        new ColorValue(), new BorderStyle(), new BorderWidthValue(null, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** Default values for the border properties.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    final static Object[] DEFAULTS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Attribute.BORDER_COLOR, // marker: value will be computed on request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        PARSERS[1].parseCssValue(Attribute.BORDER_STYLE.getDefaultValue()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        PARSERS[2].parseCssValue(Attribute.BORDER_WIDTH.getDefaultValue()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /** Attribute set containing border properties.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    final AttributeSet attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Initialize the attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    CSSBorder(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.attrs = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Return the border color for the given side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private Color getBorderColor(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Object o = attrs.getAttribute(ATTRIBUTES[COLOR][side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        ColorValue cv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (o instanceof ColorValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            cv = (ColorValue) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // Marker for the default value.  Use 'color' property value as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // computed value of the 'border-color' property (CSS2 8.5.2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            cv = (ColorValue) attrs.getAttribute(Attribute.COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (cv == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                cv = (ColorValue) PARSERS[COLOR].parseCssValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                            Attribute.COLOR.getDefaultValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return cv.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Return the border width for the given side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private int getBorderWidth(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        BorderStyle bs = (BorderStyle) attrs.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                                    ATTRIBUTES[STYLE][side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if ((bs != null) && (bs.getValue() != Value.NONE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // The 'border-style' value of "none" forces the computed value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // of 'border-width' to be 0 (CSS2 8.5.3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            LengthValue bw = (LengthValue) attrs.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                                    ATTRIBUTES[WIDTH][side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (bw == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                bw = (LengthValue) DEFAULTS[WIDTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            width = (int) bw.getValue(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Return an array of border widths in the TOP, RIGHT, BOTTOM, LEFT order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private int[] getWidths() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int[] widths = new int[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        for (int i = 0; i < widths.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            widths[i] = getBorderWidth(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return widths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Return the border style for the given side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private Value getBorderStyle(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        BorderStyle style =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    (BorderStyle) attrs.getAttribute(ATTRIBUTES[STYLE][side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            style = (BorderStyle) DEFAULTS[STYLE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return style.getValue();
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
     * Return border shape for {@code side} as if the border has zero interior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * length.  Shape start is at (0,0); points are added clockwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private Polygon getBorderShape(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Polygon shape = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int[] widths = getWidths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (widths[side] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            shape = new Polygon(new int[4], new int[4], 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            shape.addPoint(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            shape.addPoint(-widths[(side + 3) % 4], -widths[side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            shape.addPoint(widths[(side + 1) % 4], -widths[side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            shape.addPoint(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Return the border painter appropriate for the given side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private BorderPainter getBorderPainter(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        Value style = getBorderStyle(side);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return borderPainters.get(style);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Return the color with brightness adjusted by the specified factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * The factor values are between 0.0 (no change) and 1.0 (turn into white).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Negative factor values decrease brigthness (ie, 1.0 turns into black).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    static Color getAdjustedColor(Color c, double factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        double f = 1 - Math.min(Math.abs(factor), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        double inc = (factor > 0 ? 255 * (1 - f) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return new Color((int) (c.getRed() * f + inc),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                         (int) (c.getGreen() * f + inc),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                         (int) (c.getBlue() * f + inc));
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
    /* The javax.swing.border.Border methods.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int[] widths = getWidths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        insets.set(widths[TOP], widths[LEFT], widths[BOTTOM], widths[RIGHT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public void paintBorder(Component c, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                        int x, int y, int width, int height) {
8530
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   206
        if (!(g instanceof Graphics2D)) {
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   207
            return;
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   208
        }
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   209
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   210
        Graphics2D g2 = (Graphics2D) g.create();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int[] widths = getWidths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // Position and size of the border interior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        int intX = x + widths[LEFT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int intY = y + widths[TOP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int intWidth = width - (widths[RIGHT] + widths[LEFT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int intHeight = height - (widths[TOP] + widths[BOTTOM]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // Coordinates of the interior corners, from NW clockwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int[][] intCorners = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            { intX, intY },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            { intX + intWidth, intY },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            { intX + intWidth, intY + intHeight },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            { intX, intY + intHeight, },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Draw the borders for all sides.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            Value style = getBorderStyle(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            Polygon shape = getBorderShape(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if ((style != Value.NONE) && (shape != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                int sideLength = (i % 2 == 0 ? intWidth : intHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                // "stretch" the border shape by the interior area dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                shape.xpoints[2] += sideLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                shape.xpoints[3] += sideLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                Color color = getBorderColor(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                BorderPainter painter = getBorderPainter(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                double angle = i * Math.PI / 2;
8530
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   242
                g2.setClip(g.getClip()); // Restore initial clip
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                g2.translate(intCorners[i][0], intCorners[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                g2.rotate(angle);
8530
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   245
                g2.clip(shape);
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   246
                painter.paint(shape, g2, color, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                g2.rotate(-angle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                g2.translate(-intCorners[i][0], -intCorners[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
8530
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   251
        g2.dispose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /* Border painters.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    interface BorderPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * The painter should paint the border as if it were at the top and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * coordinates of the NW corner of the interior area is (0, 0).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * caller is responsible for the appropriate affine transformations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         * Clip is set by the caller to the exact border shape so it's safe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * simply draw into the shape's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        void paint(Polygon shape, Graphics g, Color color, int side);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Painter for the "none" and "hidden" CSS border styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    static class NullPainter implements BorderPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // Do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Painter for the "solid" CSS border style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    static class SolidPainter implements BorderPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            g.fillPolygon(shape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Defines a method for painting strokes in the specified direction using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * the given length and color patterns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    abstract static class StrokePainter implements BorderPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         * Paint strokes repeatedly using the given length and color patterns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        void paintStrokes(Rectangle r, Graphics g, int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                int[] lengthPattern, Color[] colorPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            boolean xAxis = (axis == View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            int end = (xAxis ? r.width : r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            while (start < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                for (int i = 0; i < lengthPattern.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    if (start >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    int length = lengthPattern[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    Color c = colorPattern[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        int x = r.x + (xAxis ? start : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        int y = r.y + (xAxis ? 0 : start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        int width = xAxis ? length : r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        int height = xAxis ? r.height : length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        g.setColor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        g.fillRect(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    start += length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Painter for the "double" CSS border style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static class DoublePainter extends StrokePainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            Rectangle r = shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            int length = Math.max(r.height / 3, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            int[] lengthPattern = { length, length };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            Color[] colorPattern = { color, null };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            paintStrokes(r, g, View.Y_AXIS, lengthPattern, colorPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Painter for the "dotted" and "dashed" CSS border styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    static class DottedDashedPainter extends StrokePainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        final int factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        DottedDashedPainter(int factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            this.factor = factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            Rectangle r = shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            int length = r.height * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            int[] lengthPattern = { length, length };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            Color[] colorPattern = { color, null };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            paintStrokes(r, g, View.X_AXIS, lengthPattern, colorPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Painter that defines colors for "shadow" and "light" border sides.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    abstract static class ShadowLightPainter extends StrokePainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * Return the "shadow" border side color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        static Color getShadowColor(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return CSSBorder.getAdjustedColor(c, -0.3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * Return the "light" border side color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        static Color getLightColor(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return CSSBorder.getAdjustedColor(c, 0.7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Painter for the "groove" and "ridge" CSS border styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    static class GrooveRidgePainter extends ShadowLightPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        final Value type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        GrooveRidgePainter(Value type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            Rectangle r = shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            int length = Math.max(r.height / 2, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            int[] lengthPattern = { length, length };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            Color[] colorPattern =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                             ((side + 1) % 4 < 2) == (type == Value.GROOVE) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                new Color[] { getShadowColor(color), getLightColor(color) } :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                new Color[] { getLightColor(color), getShadowColor(color) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            paintStrokes(r, g, View.Y_AXIS, lengthPattern, colorPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Painter for the "inset" and "outset" CSS border styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    static class InsetOutsetPainter extends ShadowLightPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        Value type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        InsetOutsetPainter(Value type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            g.setColor(((side + 1) % 4 < 2) == (type == Value.INSET) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                getShadowColor(color) : getLightColor(color));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            g.fillPolygon(shape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Add the specified painter to the painters map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    static void registerBorderPainter(Value style, BorderPainter painter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        borderPainters.put(style, painter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /** Map the border style values to the border painter objects.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    static Map<Value, BorderPainter> borderPainters =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                        new HashMap<Value, BorderPainter>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /* Initialize the border painters map with the pre-defined values.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        registerBorderPainter(Value.NONE, new NullPainter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        registerBorderPainter(Value.HIDDEN, new NullPainter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        registerBorderPainter(Value.SOLID, new SolidPainter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        registerBorderPainter(Value.DOUBLE, new DoublePainter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        registerBorderPainter(Value.DOTTED, new DottedDashedPainter(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        registerBorderPainter(Value.DASHED, new DottedDashedPainter(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        registerBorderPainter(Value.GROOVE, new GrooveRidgePainter(Value.GROOVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        registerBorderPainter(Value.RIDGE, new GrooveRidgePainter(Value.RIDGE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        registerBorderPainter(Value.INSET, new InsetOutsetPainter(Value.INSET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        registerBorderPainter(Value.OUTSET, new InsetOutsetPainter(Value.OUTSET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
}