jdk/src/share/classes/javax/swing/text/html/BlockView.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 21278 ef8a3a2a72f2
child 23010 6dadb192ad81
permissions -rw-r--r--
8032047: Fix static lint warnings in client libraries 8032048: Add static lint warning to build of jdk repository Reviewed-by: pchelko, serb, erikj
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: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, 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.SizeRequirements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.DocumentEvent;
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 a block (as a box)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * with CSS specifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class BlockView extends BoxView  {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * html box.  This can be used for a number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * @param elem the element to create a view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @param axis either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public BlockView(Element elem, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        super(elem, axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Establishes the parent view for this view.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * guaranteed to be called before any other methods if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * parent view is functioning properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * to forward to the superclass as well as call the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * {@link #setPropertiesFromAttributes()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * method to set the paragraph properties from the css
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * attributes.  The call is made at this time to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * the ability to resolve upward through the parents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * view attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param parent the new parent, or null if the view is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *  being removed from a parent it was previously added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *  to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public void setParent(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            setPropertiesFromAttributes();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Calculate the requirements of the block along the major
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * axis (i.e. the axis along with it tiles).  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * to provide the superclass behavior and then adjust it if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * CSS width or height attribute is specified and applicable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * the axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected SizeRequirements calculateMajorAxisRequirements(int axis, SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            r = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (! spanSetFromAttributes(axis, r, cssWidth, cssHeight)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            r = super.calculateMajorAxisRequirements(axis, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // Offset by the margins so that pref/min/max return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // right value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            SizeRequirements parentR = super.calculateMajorAxisRequirements(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                      axis, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            int margin = (axis == X_AXIS) ? getLeftInset() + getRightInset() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                            getTopInset() + getBottomInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            r.minimum -= margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            r.preferred -= margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            r.maximum -= margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            constrainSize(axis, r, parentR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return r;
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
     * Calculate the requirements of the block along the minor
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   109
     * axis (i.e. the axis orthogonal to the axis along with it tiles).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * to provide the superclass behavior and then adjust it if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * CSS width or height attribute is specified and applicable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * the axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            r = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (! spanSetFromAttributes(axis, r, cssWidth, cssHeight)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             * The requirements were not directly specified by attributes, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             * compute the aggregate of the requirements of the children.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
             * children that have a percentage value specified will be treated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             * as completely stretchable since that child is not limited in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             * way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            int min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            long pref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            int max = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                min = Math.max((int) v.getMinimumSpan(axis), min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                pref = Math.max((int) v.getPreferredSpan(axis), pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                max = Math.max((int) v.getMaximumSpan(axis), max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            r.preferred = (int) pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            r.minimum = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            r.maximum = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            r = super.calculateMinorAxisRequirements(axis, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // Offset by the margins so that pref/min/max return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // right value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            SizeRequirements parentR = super.calculateMinorAxisRequirements(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                      axis, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            int margin = (axis == X_AXIS) ? getLeftInset() + getRightInset() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                            getTopInset() + getBottomInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            r.minimum -= margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            r.preferred -= margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            r.maximum -= margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            constrainSize(axis, r, parentR);
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
         * Set the alignment based upon the CSS properties if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         * specified.  For X_AXIS this would be text-align, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * Y_AXIS this would be vertical-align.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            Object o = getAttributes().getAttribute(CSS.Attribute.TEXT_ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                String align = o.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (align.equals("center")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    r.alignment = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                } else if (align.equals("right")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    r.alignment = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    r.alignment = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // Y_AXIS TBD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    boolean isPercentage(int axis, AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (cssWidth != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return cssWidth.isPercentage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (cssHeight != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return cssHeight.isPercentage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return false;
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
     * Adjust the given requirements to the CSS width or height if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * it is specified along the applicable axis.  Return true if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * size is exactly specified, false if the span is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * in an attribute or the size specified is a percentage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    static boolean spanSetFromAttributes(int axis, SizeRequirements r,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                         CSS.LengthValue cssWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                         CSS.LengthValue cssHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if ((cssWidth != null) && (! cssWidth.isPercentage())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                r.minimum = r.preferred = r.maximum = (int) cssWidth.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if ((cssHeight != null) && (! cssHeight.isPercentage())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                r.minimum = r.preferred = r.maximum = (int) cssHeight.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Performs layout for the minor axis of the box (i.e. the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   221
     * axis orthogonal to the axis that it represents). The results
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * of the layout (the offset and span for each children) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * placed in the given arrays which represent the allocations to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * the children along the minor axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param targetSpan the total span given to the view, which
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   227
     *  would be used to layout the children.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param axis the axis being layed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param offsets the offsets from the origin of the view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *  each of the child views; this is a return value and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *  filled in by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param spans the span of each child view; this is a return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *  value and is filled in by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        Object key = (axis == X_AXIS) ? CSS.Attribute.WIDTH : CSS.Attribute.HEIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            int min = (int) v.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            int max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // check for percentage span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            AttributeSet a = v.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            CSS.LengthValue lv = (CSS.LengthValue) a.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if ((lv != null) && lv.isPercentage()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                // bound the span to the percentage specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                min = Math.max((int) lv.getValue(targetSpan), min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                max = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                max = (int)v.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // assign the offset and span for the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (max < targetSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                // can't make the child this wide, align it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                float align = v.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                offsets[i] = (int) ((targetSpan - max) * align);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                spans[i] = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                // make it the target width, or as small as it can get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                offsets[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                spans[i] = Math.max(min, targetSpan);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Renders using the given rendering surface and area on that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * surface.  This is implemented to delegate to the css box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * painter to paint the border and background prior to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * interior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param allocation the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public void paint(Graphics g, Shape allocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        Rectangle a = (Rectangle) allocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        painter.paint(g, a.x, a.y, a.width, a.height, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        super.paint(g, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Fetches the attributes to use when rendering.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * implemented to multiplex the attributes specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * model with a StyleSheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            StyleSheet sheet = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            attr = sheet.getViewAttributes(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Gets the resize weight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @return the weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @exception IllegalArgumentException for an invalid axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public int getResizeWeight(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Gets the alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return the alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (getViewCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            float span = getPreferredSpan(View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            View v = getView(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            float above = v.getPreferredSpan(View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            float a = (((int)span) != 0) ? (above * v.getAlignment(View.Y_AXIS)) / span: 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        super.changedUpdate(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        int pos = changes.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (pos <= getStartOffset() && (pos + changes.getLength()) >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            setPropertiesFromAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param axis may be either <code>View.X_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *           or <code>View.Y_AXIS</code>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   355
     * @return   the span the view would like to be rendered into &gt;= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *           typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *           that is returned, although there is no guarantee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *           the parent may choose to resize or break the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @exception IllegalArgumentException for an invalid axis type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return super.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Determines the minimum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param axis may be either <code>View.X_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *           or <code>View.Y_AXIS</code>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   371
     * @return  the span the view would like to be rendered into &gt;= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *           typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *           that is returned, although there is no guarantee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *           the parent may choose to resize or break the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @exception IllegalArgumentException for an invalid axis type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return super.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Determines the maximum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param axis may be either <code>View.X_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *           or <code>View.Y_AXIS</code>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   387
     * @return   the span the view would like to be rendered into &gt;= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *           typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *           that is returned, although there is no guarantee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *           the parent may choose to resize or break the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @exception IllegalArgumentException for an invalid axis type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Update any cached values that come from attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    protected void setPropertiesFromAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // update attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        StyleSheet sheet = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        attr = sheet.getViewAttributes(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // Reset the painter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        painter = sheet.getBoxPainter(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            setInsets((short) painter.getInset(TOP, this),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                      (short) painter.getInset(LEFT, this),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                      (short) painter.getInset(BOTTOM, this),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                      (short) painter.getInset(RIGHT, this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // Get the width/height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        cssWidth = (CSS.LengthValue) attr.getAttribute(CSS.Attribute.WIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        cssHeight = (CSS.LengthValue) attr.getAttribute(CSS.Attribute.HEIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    protected StyleSheet getStyleSheet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        HTMLDocument doc = (HTMLDocument) getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return doc.getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Constrains <code>want</code> to fit in the minimum size specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * by <code>min</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    private void constrainSize(int axis, SizeRequirements want,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                               SizeRequirements min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (min.minimum > want.minimum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            want.minimum = want.preferred = min.minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            want.maximum = Math.max(want.maximum, min.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    private AttributeSet attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    private StyleSheet.BoxPainter painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    private CSS.LengthValue cssWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    private CSS.LengthValue cssHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
}