jdk/src/java.desktop/share/classes/javax/swing/text/html/LineView.java
author prr
Tue, 16 Dec 2014 09:58:02 -0800
changeset 28236 610561ed1847
parent 28231 b608ffcaed74
parent 28059 e576535359cc
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 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.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A view implementation to display an unwrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * preformatted line.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This subclasses ParagraphView, but this really only contains one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Row of text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class LineView extends ParagraphView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /** Last place painted at. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    int tabBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Creates a LineView object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @param elem the element to wrap in a view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public LineView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Preformatted lines are not suppressed if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * have only whitespace, so they are always visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public boolean isVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Determines the minimum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * axis.  The preformatted line should refuse to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * sized less than the preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param axis may be either <code>View.X_AXIS</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *  <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @return  the minimum span the view can be rendered into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @see View#getPreferredSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Gets the resize weight for the specified axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return the weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public int getResizeWeight(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new IllegalArgumentException("Invalid axis: " + axis);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Gets the alignment for an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @return the alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (axis == View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return super.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Lays out the children.  If the layout span has changed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * the rows are rebuilt.  The superclass functionality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * is called after checking and possibly rebuilding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * rows.  If the height has changed, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <code>preferenceChanged</code> method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * on the parent since the vertical preference is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * rigid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param width  the width to lay out against >= 0.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *   the width inside of the inset area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param height the height to lay out against >= 0 (not used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *   by paragraph, but used by the superclass).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *   is the height inside of the inset area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    protected void layout(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        super.layout(Integer.MAX_VALUE - 1, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Returns the next tab stop position given a reference position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * This view implements the tab coordinate system, and calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>getTabbedSpan</code> on the logical children in the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * of layout to determine the desired span of the children.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * logical children can delegate their tab expansion upward to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * the paragraph which knows how to expand tabs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <code>LabelView</code> is an example of a view that delegates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * its tab expansion needs upward to the paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * This is implemented to try and locate a <code>TabSet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * in the paragraph element's attribute set.  If one can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * found, its settings will be used, otherwise a default expansion
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   139
     * will be provided.  The base location for tab expansion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * is the left inset from the paragraphs most recent allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * (which is what the layout of the children is based upon).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param x the X reference position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param tabOffset the position within the text stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *   that the tab occurred at >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the trailing end of the tab expansion >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @see TabSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @see TabStop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see LabelView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public float nextTabStop(float x, int tabOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // If the text isn't left justified, offset by 10 pixels!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (getTabSet() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            StyleConstants.getAlignment(getAttributes()) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            StyleConstants.ALIGN_LEFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return getPreTab(x, tabOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return super.nextTabStop(x, tabOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Returns the location for the tab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 25859
diff changeset
   164
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected float getPreTab(float x, int tabOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Document d = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        View v = getViewAtPosition(tabOffset, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if ((d instanceof StyledDocument) && v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // Assume f is fixed point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            Font f = ((StyledDocument)d).getFont(v.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            FontMetrics fm = (c != null) ? c.getFontMetrics(f) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                Toolkit.getDefaultToolkit().getFontMetrics(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            int width = getCharactersPerTab() * fm.charWidth('W');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int tb = (int)getTabBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return (float)((((int)x - tb) / width + 1) * width + tb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return 10.0f + x;
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 number of characters per tab, 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    protected int getCharactersPerTab() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}