jdk/src/java.desktop/share/classes/javax/swing/text/html/CSSBorder.java
author prr
Sat, 19 Sep 2015 15:45:59 -0700
changeset 32865 f9cb6e427f9e
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136783: Run blessed-modifier-order script on java.desktop Reviewed-by: martin, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 9035
diff changeset
     2
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.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
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 9035
diff changeset
    53
@SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
class CSSBorder extends AbstractBorder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Indices for the attribute groups.  */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    57
    static final int COLOR = 0, STYLE = 1, WIDTH = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Indices for the box sides within the attribute group.  */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    60
    static final int TOP = 0, RIGHT = 1, BOTTOM = 2, LEFT = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** The attribute groups.  */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    63
    static final Attribute[][] ATTRIBUTES = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        { Attribute.BORDER_TOP_COLOR, Attribute.BORDER_RIGHT_COLOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
          Attribute.BORDER_BOTTOM_COLOR, Attribute.BORDER_LEFT_COLOR, },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        { Attribute.BORDER_TOP_STYLE, Attribute.BORDER_RIGHT_STYLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
          Attribute.BORDER_BOTTOM_STYLE, Attribute.BORDER_LEFT_STYLE, },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        { Attribute.BORDER_TOP_WIDTH, Attribute.BORDER_RIGHT_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
          Attribute.BORDER_BOTTOM_WIDTH, Attribute.BORDER_LEFT_WIDTH, },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /** Parsers for the border properties.  */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    73
    static final CssValue PARSERS[] = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        new ColorValue(), new BorderStyle(), new BorderWidthValue(null, 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /** Default values for the border properties.  */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    78
    static final Object[] DEFAULTS = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Attribute.BORDER_COLOR, // marker: value will be computed on request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        PARSERS[1].parseCssValue(Attribute.BORDER_STYLE.getDefaultValue()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        PARSERS[2].parseCssValue(Attribute.BORDER_WIDTH.getDefaultValue()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /** Attribute set containing border properties.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    final AttributeSet attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Initialize the attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    CSSBorder(AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.attrs = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Return the border color for the given side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private Color getBorderColor(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Object o = attrs.getAttribute(ATTRIBUTES[COLOR][side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ColorValue cv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (o instanceof ColorValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            cv = (ColorValue) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // Marker for the default value.  Use 'color' property value as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // computed value of the 'border-color' property (CSS2 8.5.2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            cv = (ColorValue) attrs.getAttribute(Attribute.COLOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (cv == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                cv = (ColorValue) PARSERS[COLOR].parseCssValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                            Attribute.COLOR.getDefaultValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return cv.getValue();
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
     * Return the border width for the given side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private int getBorderWidth(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        BorderStyle bs = (BorderStyle) attrs.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                                    ATTRIBUTES[STYLE][side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if ((bs != null) && (bs.getValue() != Value.NONE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // The 'border-style' value of "none" forces the computed value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // of 'border-width' to be 0 (CSS2 8.5.3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            LengthValue bw = (LengthValue) attrs.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                                    ATTRIBUTES[WIDTH][side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (bw == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                bw = (LengthValue) DEFAULTS[WIDTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            width = (int) bw.getValue(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Return an array of border widths in the TOP, RIGHT, BOTTOM, LEFT order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private int[] getWidths() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int[] widths = new int[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (int i = 0; i < widths.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            widths[i] = getBorderWidth(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return widths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Return the border style for the given side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private Value getBorderStyle(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        BorderStyle style =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    (BorderStyle) attrs.getAttribute(ATTRIBUTES[STYLE][side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            style = (BorderStyle) DEFAULTS[STYLE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return style.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Return border shape for {@code side} as if the border has zero interior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * length.  Shape start is at (0,0); points are added clockwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private Polygon getBorderShape(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Polygon shape = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int[] widths = getWidths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (widths[side] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            shape = new Polygon(new int[4], new int[4], 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            shape.addPoint(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            shape.addPoint(-widths[(side + 3) % 4], -widths[side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            shape.addPoint(widths[(side + 1) % 4], -widths[side]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            shape.addPoint(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Return the border painter appropriate for the given side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private BorderPainter getBorderPainter(int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Value style = getBorderStyle(side);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return borderPainters.get(style);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Return the color with brightness adjusted by the specified factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * The factor values are between 0.0 (no change) and 1.0 (turn into white).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Negative factor values decrease brigthness (ie, 1.0 turns into black).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static Color getAdjustedColor(Color c, double factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        double f = 1 - Math.min(Math.abs(factor), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        double inc = (factor > 0 ? 255 * (1 - f) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return new Color((int) (c.getRed() * f + inc),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                         (int) (c.getGreen() * f + inc),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                         (int) (c.getBlue() * f + inc));
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
    /* The javax.swing.border.Border methods.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int[] widths = getWidths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        insets.set(widths[TOP], widths[LEFT], widths[BOTTOM], widths[RIGHT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public void paintBorder(Component c, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                        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
   207
        if (!(g instanceof Graphics2D)) {
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   208
            return;
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
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   211
        Graphics2D g2 = (Graphics2D) g.create();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int[] widths = getWidths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // Position and size of the border interior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int intX = x + widths[LEFT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int intY = y + widths[TOP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int intWidth = width - (widths[RIGHT] + widths[LEFT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int intHeight = height - (widths[TOP] + widths[BOTTOM]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // Coordinates of the interior corners, from NW clockwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int[][] intCorners = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            { intX, intY },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            { intX + intWidth, intY },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            { intX + intWidth, intY + intHeight },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            { intX, intY + intHeight, },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // Draw the borders for all sides.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            Value style = getBorderStyle(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            Polygon shape = getBorderShape(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if ((style != Value.NONE) && (shape != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                int sideLength = (i % 2 == 0 ? intWidth : intHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                // "stretch" the border shape by the interior area dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                shape.xpoints[2] += sideLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                shape.xpoints[3] += sideLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                Color color = getBorderColor(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                BorderPainter painter = getBorderPainter(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                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
   243
                g2.setClip(g.getClip()); // Restore initial clip
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                g2.translate(intCorners[i][0], intCorners[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                g2.rotate(angle);
8530
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   246
                g2.clip(shape);
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   247
                painter.paint(shape, g2, color, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                g2.rotate(-angle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                g2.translate(-intCorners[i][0], -intCorners[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
8530
cfe337a5776e 6796710: Html content in JEditorPane is overlapping on swing components while resizing the application.
rupashka
parents: 5506
diff changeset
   252
        g2.dispose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /* Border painters.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    interface BorderPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * The painter should paint the border as if it were at the top and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * coordinates of the NW corner of the interior area is (0, 0).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * caller is responsible for the appropriate affine transformations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * Clip is set by the caller to the exact border shape so it's safe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         * simply draw into the shape's bounding rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        void paint(Polygon shape, Graphics g, Color color, int side);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Painter for the "none" and "hidden" CSS border styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    static class NullPainter implements BorderPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // Do nothing.
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Painter for the "solid" CSS border style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    static class SolidPainter implements BorderPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            g.fillPolygon(shape);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Defines a method for painting strokes in the specified direction using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * the given length and color patterns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    abstract static class StrokePainter implements BorderPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * Paint strokes repeatedly using the given length and color patterns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        void paintStrokes(Rectangle r, Graphics g, int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                int[] lengthPattern, Color[] colorPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            boolean xAxis = (axis == View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int end = (xAxis ? r.width : r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            while (start < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                for (int i = 0; i < lengthPattern.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    if (start >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    int length = lengthPattern[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    Color c = colorPattern[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        int x = r.x + (xAxis ? start : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        int y = r.y + (xAxis ? 0 : start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        int width = xAxis ? length : r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        int height = xAxis ? r.height : length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        g.setColor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        g.fillRect(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    start += length;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Painter for the "double" CSS border style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static class DoublePainter extends StrokePainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            Rectangle r = shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            int length = Math.max(r.height / 3, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            int[] lengthPattern = { length, length };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            Color[] colorPattern = { color, null };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            paintStrokes(r, g, View.Y_AXIS, lengthPattern, colorPattern);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Painter for the "dotted" and "dashed" CSS border styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    static class DottedDashedPainter extends StrokePainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        final int factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        DottedDashedPainter(int factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            this.factor = factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            Rectangle r = shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            int length = r.height * factor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            int[] lengthPattern = { length, length };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            Color[] colorPattern = { color, null };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            paintStrokes(r, g, View.X_AXIS, lengthPattern, colorPattern);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Painter that defines colors for "shadow" and "light" border sides.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    abstract static class ShadowLightPainter extends StrokePainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         * Return the "shadow" border side color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        static Color getShadowColor(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return CSSBorder.getAdjustedColor(c, -0.3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * Return the "light" border side color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        static Color getLightColor(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return CSSBorder.getAdjustedColor(c, 0.7);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Painter for the "groove" and "ridge" CSS border styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    static class GrooveRidgePainter extends ShadowLightPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        final Value type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        GrooveRidgePainter(Value type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            Rectangle r = shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            int length = Math.max(r.height / 2, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            int[] lengthPattern = { length, length };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            Color[] colorPattern =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                             ((side + 1) % 4 < 2) == (type == Value.GROOVE) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                new Color[] { getShadowColor(color), getLightColor(color) } :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                new Color[] { getLightColor(color), getShadowColor(color) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            paintStrokes(r, g, View.Y_AXIS, lengthPattern, colorPattern);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Painter for the "inset" and "outset" CSS border styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    static class InsetOutsetPainter extends ShadowLightPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        Value type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        InsetOutsetPainter(Value type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        public void paint(Polygon shape, Graphics g, Color color, int side) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            g.setColor(((side + 1) % 4 < 2) == (type == Value.INSET) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                getShadowColor(color) : getLightColor(color));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            g.fillPolygon(shape);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Add the specified painter to the painters map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    static void registerBorderPainter(Value style, BorderPainter painter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        borderPainters.put(style, painter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /** Map the border style values to the border painter objects.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    static Map<Value, BorderPainter> borderPainters =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                                        new HashMap<Value, BorderPainter>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /* Initialize the border painters map with the pre-defined values.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        registerBorderPainter(Value.NONE, new NullPainter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        registerBorderPainter(Value.HIDDEN, new NullPainter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        registerBorderPainter(Value.SOLID, new SolidPainter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        registerBorderPainter(Value.DOUBLE, new DoublePainter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        registerBorderPainter(Value.DOTTED, new DottedDashedPainter(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        registerBorderPainter(Value.DASHED, new DottedDashedPainter(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        registerBorderPainter(Value.GROOVE, new GrooveRidgePainter(Value.GROOVE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        registerBorderPainter(Value.RIDGE, new GrooveRidgePainter(Value.RIDGE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        registerBorderPainter(Value.INSET, new InsetOutsetPainter(Value.INSET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        registerBorderPainter(Value.OUTSET, new InsetOutsetPainter(Value.OUTSET));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
}