jdk/src/share/classes/javax/swing/text/html/InlineView.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 java.text.BreakIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.event.DocumentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Displays the <dfn>inline element</dfn> styles
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * based upon css attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class InlineView extends LabelView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Constructs a new view wrapped on an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public InlineView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        StyleSheet sheet = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        attr = sheet.getViewAttributes(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Gives notification that something was inserted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * the document in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * If either parameter is <code>null</code>, behavior of this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * implementation dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super.insertUpdate(e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * If either parameter is <code>null</code>, behavior of this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * implementation dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        super.removeUpdate(e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @see View#changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        super.changedUpdate(e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        StyleSheet sheet = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        attr = sheet.getViewAttributes(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Fetches the attributes to use when rendering.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * implemented to multiplex the attributes specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * model with a StyleSheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Determines how attractive a break opportunity in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * this view is.  This can be used for determining which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * view is the most attractive to call <code>breakView</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * on in the process of formatting.  A view that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * text that has whitespace in it might be more attractive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * than a view that has no whitespace, for example.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * higher the weight, the more attractive the break.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * value equal to or lower than <code>BadBreakWeight</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * should not be considered for a break.  A value greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * than or equal to <code>ForcedBreakWeight</code> should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * be broken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * This is implemented to provide the default behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * of returning <code>BadBreakWeight</code> unless the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * is greater than the length of the view in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * entire view represents the fragment.  Unless a view has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * been written to support breaking behavior, it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * attractive to try and break the view.  An example of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * a view that does support breaking is <code>LabelView</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * An example of a view that uses break weight is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>ParagraphView</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param pos the potential location of the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *   broken view >= 0.  This may be useful for calculating tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *   positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param len specifies the relative length from <em>pos</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *   where a potential break is desired >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return the weight, which should be a value between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *   ForcedBreakWeight and BadBreakWeight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see LabelView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @see ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see javax.swing.text.View#BadBreakWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @see javax.swing.text.View#GoodBreakWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see javax.swing.text.View#ExcellentBreakWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see javax.swing.text.View#ForcedBreakWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public int getBreakWeight(int axis, float pos, float len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (nowrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return BadBreakWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return super.getBreakWeight(axis, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Tries to break this view on the given axis. Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * {@link javax.swing.text.View#breakView} for a complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * description of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <p>Behavior of this method is unspecified in case <code>axis</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * is neither <code>View.X_AXIS</code> nor <code>View.Y_AXIS</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * in case <code>offset</code>, <code>pos</code>, or <code>len</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param axis may be either <code>View.X_AXIS</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *          <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param offset the location in the document model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *   that a broken fragment would occupy >= 0.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *   would be the starting offset of the fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *   returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param pos the position along the axis that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *  broken view would occupy >= 0.  This may be useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *  things like tab calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param len specifies the distance along the axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *  where a potential break is desired >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return the fragment of the view that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *  given span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see javax.swing.text.View#breakView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public View breakView(int axis, int offset, float pos, float len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return super.breakView(axis, offset, pos, len);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Set the cached properties from the attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected void setPropertiesFromAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        super.setPropertiesFromAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        AttributeSet a = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Object decor = a.getAttribute(CSS.Attribute.TEXT_DECORATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        boolean u = (decor != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
          (decor.toString().indexOf("underline") >= 0) : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        setUnderline(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        boolean s = (decor != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
          (decor.toString().indexOf("line-through") >= 0) : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        setStrikeThrough(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Object vAlign = a.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        s = (vAlign != null) ? (vAlign.toString().indexOf("sup") >= 0) : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        setSuperscript(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        s = (vAlign != null) ? (vAlign.toString().indexOf("sub") >= 0) : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        setSubscript(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        Object whitespace = a.getAttribute(CSS.Attribute.WHITE_SPACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if ((whitespace != null) && whitespace.equals("nowrap")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            nowrap = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            nowrap = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        HTMLDocument doc = (HTMLDocument)getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // fetches background color from stylesheet if specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Color bg = doc.getBackground(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (bg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            setBackground(bg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    protected StyleSheet getStyleSheet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        HTMLDocument doc = (HTMLDocument) getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return doc.getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private boolean nowrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private AttributeSet attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}