jdk/src/share/classes/javax/swing/text/html/HRuleView.java
author yan
Mon, 14 Oct 2013 11:47:27 +0400
changeset 21254 f6d36ee3f269
parent 5506 202f599c92aa
permissions -rw-r--r--
8025824: [cleanup] Fix tidy errors and warnings in preformatted HTML files related to 2d/awt/swing Reviewed-by: anthony, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 1997, 2008, 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.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.event.DocumentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.Integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A view implementation to display an html horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * rule.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Sara Swanson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class HRuleView extends View  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * Creates a new view that represents an <hr> element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * @param elem the element to create a view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public HRuleView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        setPropertiesFromAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Update any cached values that come from attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected void setPropertiesFromAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        StyleSheet sheet = ((HTMLDocument)getDocument()).getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        AttributeSet eAttr = getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        attr = sheet.getViewAttributes(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        alignment = StyleConstants.ALIGN_CENTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        noshade = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        widthValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            // getAlignment() returns ALIGN_LEFT by default, and HR should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            // use ALIGN_CENTER by default, so we check if the alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // attribute is actually defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (attr.getAttribute(StyleConstants.Alignment) != null) {
1306
036c9e2dfd78 5062055: JEditorPane HTML: HR-tag with attribute size=1px causes NumberFormatException
peterz
parents: 2
diff changeset
    70
                alignment = StyleConstants.getAlignment(attr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            noshade = (String)eAttr.getAttribute(HTML.Attribute.NOSHADE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            Object value = eAttr.getAttribute(HTML.Attribute.SIZE);
1306
036c9e2dfd78 5062055: JEditorPane HTML: HR-tag with attribute size=1px causes NumberFormatException
peterz
parents: 2
diff changeset
    75
            if (value != null && (value instanceof String)) {
036c9e2dfd78 5062055: JEditorPane HTML: HR-tag with attribute size=1px causes NumberFormatException
peterz
parents: 2
diff changeset
    76
                try {
036c9e2dfd78 5062055: JEditorPane HTML: HR-tag with attribute size=1px causes NumberFormatException
peterz
parents: 2
diff changeset
    77
                    size = Integer.parseInt((String)value);
036c9e2dfd78 5062055: JEditorPane HTML: HR-tag with attribute size=1px causes NumberFormatException
peterz
parents: 2
diff changeset
    78
                } catch (NumberFormatException e) {
036c9e2dfd78 5062055: JEditorPane HTML: HR-tag with attribute size=1px causes NumberFormatException
peterz
parents: 2
diff changeset
    79
                    size = 1;
036c9e2dfd78 5062055: JEditorPane HTML: HR-tag with attribute size=1px causes NumberFormatException
peterz
parents: 2
diff changeset
    80
                }
036c9e2dfd78 5062055: JEditorPane HTML: HR-tag with attribute size=1px causes NumberFormatException
peterz
parents: 2
diff changeset
    81
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            value = attr.getAttribute(CSS.Attribute.WIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (value != null && (value instanceof CSS.LengthValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                widthValue = (CSS.LengthValue)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            topMargin = getLength(CSS.Attribute.MARGIN_TOP, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            bottomMargin = getLength(CSS.Attribute.MARGIN_BOTTOM, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            leftMargin = getLength(CSS.Attribute.MARGIN_LEFT, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            rightMargin = getLength(CSS.Attribute.MARGIN_RIGHT, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            topMargin = bottomMargin = leftMargin = rightMargin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        size = Math.max(2, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // This will be removed and centralized at some point, need to unify this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // and avoid private classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private float getLength(CSS.Attribute key, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        CSS.LengthValue lv = (CSS.LengthValue) a.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        float len = (lv != null) ? lv.getValue() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // --- View methods ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Paints the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param a the allocation region for the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                          a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int y = alloc.y + SPACE_ABOVE + (int)topMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int width = alloc.width - (int)(leftMargin + rightMargin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (widthValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            width = (int)widthValue.getValue((float)width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int height = alloc.height - (SPACE_ABOVE + SPACE_BELOW +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                     (int)topMargin + (int)bottomMargin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (size > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                height = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Align the rule horizontally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        switch (alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        case StyleConstants.ALIGN_CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            x = alloc.x + (alloc.width / 2) - (width / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        case StyleConstants.ALIGN_RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            x = alloc.x + alloc.width - width - (int)rightMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        case StyleConstants.ALIGN_LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            x = alloc.x + (int)leftMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Paint either a shaded rule or a solid line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (noshade != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            g.fillRect(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            Color bg = getContainer().getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            Color bottom, top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (bg == null || bg.equals(Color.white)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                top = Color.darkGray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                bottom = Color.lightGray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                top = Color.darkGray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                bottom = Color.white;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            g.setColor(bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            g.drawLine(x + width - 1, y, x + width - 1, y + height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            g.setColor(top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            g.drawLine(x, y, x + width - 1, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            g.drawLine(x, y, x, y + height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Calculates the desired shape of the rule... this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * basically the preferred size of the border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return the desired span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see View#getPreferredSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return size + SPACE_ABOVE + SPACE_BELOW + topMargin +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    bottomMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                if (noshade != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    return 2 + SPACE_ABOVE + SPACE_BELOW + topMargin +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        bottomMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    return SPACE_ABOVE + SPACE_BELOW + topMargin +bottomMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Gets the resize weight for the axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * The rule is: rigid vertically and flexible horizontally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return the weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public int getResizeWeight(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (axis == View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else if (axis == View.Y_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Determines how attractive a break opportunity in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * this view is.  This is implemented to request a forced break.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param pos the potential location of the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *   broken view (greater than or equal to zero).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *   This may be useful for calculating tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *   positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param len specifies the relative length from <em>pos</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *   where a potential break is desired. The value must be greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *   than or equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @return the weight, which should be a value between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *   ForcedBreakWeight and BadBreakWeight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public int getBreakWeight(int axis, float pos, float len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return ForcedBreakWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return BadBreakWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public View breakView(int axis, int offset, float pos, float len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param pos the position to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int p1 = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if ((pos >= p0) && (pos <= p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            Rectangle r = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (pos == p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                r.x += r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            r.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param x the X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param y the Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *  given point of view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        Rectangle alloc = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (x < alloc.x + (alloc.width / 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        bias[0] = Position.Bias.Backward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return getEndOffset();
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
     * Fetches the attributes to use when rendering.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * implemented to multiplex the attributes specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * model with a StyleSheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        super.changedUpdate(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int pos = changes.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (pos <= getStartOffset() && (pos + changes.getLength()) >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            setPropertiesFromAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    // --- variables ------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private float topMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private float bottomMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private float leftMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private float rightMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private int alignment = StyleConstants.ALIGN_CENTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private String noshade = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private CSS.LengthValue widthValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private static final int SPACE_ABOVE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private static final int SPACE_BELOW = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /** View Attributes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private AttributeSet attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
}