jdk/src/share/classes/javax/swing/text/FlowView.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 21278 ef8a3a2a72f2
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
/*
20126
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2013, 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: 1299
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: 1299
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: 1299
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
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;
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.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.SizeRequirements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A View that tries to flow it's children into some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * partially constrained space.  This can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * build things like paragraphs, pages, etc.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * flow is made up of the following pieces of functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <li>A logical set of child views, which as used as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * layout pool from which a physical view is formed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <li>A strategy for translating the logical view to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a physical (flowed) view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <li>Constraints for the strategy to work against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li>A physical structure, that represents the flow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The children of this view are where the pieces of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * of the logical views are placed to create the flow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see     View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public abstract class FlowView extends BoxView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Constructs a FlowView for the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param elem the element that this view is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public FlowView(Element elem, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        super(elem, axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        layoutSpan = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        strategy = new FlowStrategy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Fetches the axis along which views should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * flowed.  By default, this will be the axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * orthogonal to the axis along which the flow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * rows are tiled (the axis of the default flow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * rows themselves).  This is typically used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * by the <code>FlowStrategy</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public int getFlowAxis() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (getAxis() == Y_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            return X_AXIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return Y_AXIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Fetch the constraining span to flow against for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * the given child index.  This is called by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * FlowStrategy while it is updating the flow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * A flow can be shaped by providing different values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * for the row constraints.  By default, the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * span inside of the insets along the flow axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param index the index of the row being updated.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
    91
     *   This should be a value &gt;= 0 and &lt; getViewCount().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @see #getFlowStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public int getFlowSpan(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return layoutSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Fetch the location along the flow axis that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * flow span will start at.  This is called by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * FlowStrategy while it is updating the flow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * A flow can be shaped by providing different values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * for the row constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param index the index of the row being updated.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   106
     *   This should be a value &gt;= 0 and &lt; getViewCount().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see #getFlowSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public int getFlowStart(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Create a View that should be used to hold a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * a rows worth of children in a flow.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * called by the FlowStrategy when new children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * are added or removed (i.e. rows are added or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * removed) in the process of updating the flow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected abstract View createRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // ---- BoxView methods -------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Loads all of the children to initialize the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * This is called by the <code>setParent</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * This is reimplemented to not load any children directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * (as they are created in the process of formatting).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * If the layoutPool variable is null, an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * LogicalView is created to represent the logical view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * that is used in the process of formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param f the view factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (layoutPool == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            layoutPool = new LogicalView(getElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        layoutPool.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // This synthetic insertUpdate call gives the strategy a chance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // to initialize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        strategy.insertUpdate(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Fetches the child view index representing the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   150
     * @param pos the position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return  index of the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *   -1 if no view represents that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    protected int getViewIndexAtPosition(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (pos >= getStartOffset() && (pos < getEndOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            for (int counter = 0; counter < getViewCount(); counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                View v = getView(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if(pos >= v.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                   pos < v.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    return counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return -1;
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
     * Lays out the children.  If the span along the flow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * axis has changed, layout is marked as invalid which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * which will cause the superclass behavior to recalculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * the layout along the box axis.  The FlowStrategy.layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * method will be called to rebuild the flow rows as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * appropriate.  If the height of this view changes
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   174
     * (determined by the preferred size along the box axis),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * a preferenceChanged is called.  Following all of that,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * the normal box layout of the superclass is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   178
     * @param width  the width to lay out against &gt;= 0.  This is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *   the width inside of the inset area.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   180
     * @param height the height to lay out against &gt;= 0 This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *   is the height inside of the inset area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected void layout(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        final int faxis = getFlowAxis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int newSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (faxis == X_AXIS) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 673
diff changeset
   187
            newSpan = width;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 673
diff changeset
   189
            newSpan = height;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (layoutSpan != newSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            layoutChanged(faxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            layoutChanged(getAxis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            layoutSpan = newSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // repair the flow if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (! isLayoutValid(faxis)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            final int heightAxis = getAxis();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 673
diff changeset
   200
            int oldFlowHeight = (heightAxis == X_AXIS)? getWidth() : getHeight();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            strategy.layout(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            int newFlowHeight = (int) getPreferredSpan(heightAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (oldFlowHeight != newFlowHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                View p = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    p.preferenceChanged(this, (heightAxis == X_AXIS), (heightAxis == Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                // PENDING(shannonh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                // Temporary fix for 4250847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                // Can be removed when TraversalContext is added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                Component host = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    //nb idk 12/12/2001 host should not be equal to null. We need to add assertion here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    host.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
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
        super.layout(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   224
     * Calculate requirements along the minor axis.  This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * is implemented to forward the request to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * view by calling getMinimumSpan, getPreferredSpan, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * getMaximumSpan on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            r = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        float pref = layoutPool.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        float min = layoutPool.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Don't include insets, Box.getXXXSpan will include them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        r.minimum = (int)min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        r.preferred = Math.max(r.minimum, (int) pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        r.maximum = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        r.alignment = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    // ---- View methods ----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Gives notification that something was inserted into the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        layoutPool.insertUpdate(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        strategy.insertUpdate(this, changes, getInsideAllocation(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        layoutPool.removeUpdate(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        strategy.removeUpdate(this, changes, getInsideAllocation(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see View#changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        layoutPool.changedUpdate(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        strategy.changedUpdate(this, changes, getInsideAllocation(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public void setParent(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        super.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (parent == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                && layoutPool != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            layoutPool.setParent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    // --- variables -----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Default constraint against which the flow is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * created against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    protected int layoutSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * These are the views that represent the child elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * of the element this view represents (The logical view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * to translate to a physical view).  These are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * directly children of this view.  These are either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * placed into the rows directly or used for the purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * of breaking into smaller chunks, to form the physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    protected View layoutPool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * The behavior for keeping the flow updated.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * default this is a singleton shared by all instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * of FlowView (FlowStrategy is stateless).  Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * can create an alternative strategy, which might keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    protected FlowStrategy strategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Strategy for maintaining the physical form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * of the flow.  The default implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * completely stateless, and recalculates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * entire flow if the layout is invalid on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * given FlowView.  Alternative strategies can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * be implemented by subclassing, and might
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   331
     * perform incremental repair to the layout
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * or alternative breaking behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public static class FlowStrategy {
673
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   336
        Position damageStart = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Vector<View> viewBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        void addDamage(FlowView fv, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (offset >= fv.getStartOffset() && offset < fv.getEndOffset()) {
673
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   341
                if (damageStart == null || offset < damageStart.getOffset()) {
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   342
                    try {
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   343
                        damageStart = fv.getDocument().createPosition(offset);
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   344
                    } catch (BadLocationException e) {
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   345
                        // shouldn't happen since offset is inside view bounds
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   346
                        assert(false);
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   347
                    }
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   348
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        void unsetDamage() {
673
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   353
            damageStart = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * Gives notification that something was inserted into the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * in a location that the given flow view is responsible for.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * strategy should update the appropriate changed region (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         * depends upon the strategy used for repair).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         * @param alloc the current allocation of the view inside of the insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         *   This value will be null if the view has not yet been displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        public void insertUpdate(FlowView fv, DocumentEvent e, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // FlowView.loadChildren() makes a synthetic call into this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // passing null as e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                addDamage(fv, e.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                Component host = fv.getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    host.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                fv.preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         * in a location that the given flow view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * @param alloc the current allocation of the view inside of the insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        public void removeUpdate(FlowView fv, DocumentEvent e, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            addDamage(fv, e.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                Component host = fv.getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    host.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                fv.preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         * @param fv     the <code>FlowView</code> containing the changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * @param e      the <code>DocumentEvent</code> describing the changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         *               done to the Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * @param alloc  Bounds of the View
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * @see View#changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        public void changedUpdate(FlowView fv, DocumentEvent e, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            addDamage(fv, e.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (alloc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                Component host = fv.getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    host.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                fv.preferenceChanged(null, true, true);
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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * This method gives flow strategies access to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * view of the FlowView.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        protected View getLogicalView(FlowView fv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return fv.layoutPool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         * Update the flow on the given FlowView.  By default, this causes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * all of the rows (child views) to be rebuilt to match the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         * constraints for each row.  This is called by a FlowView.layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * to update the child views in the flow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * @param fv the view to reflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        public void layout(FlowView fv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            View pool = getLogicalView(fv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            int rowIndex, p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            int p1 = fv.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (fv.majorAllocValid) {
673
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   448
                if (damageStart == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                // In some cases there's no view at position damageStart, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                // step back and search again. See 6452106 for details.
673
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   453
                int offset = damageStart.getOffset();
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   454
                while ((rowIndex = fv.getViewIndexAtPosition(offset)) < 0) {
effa1ee249d6 6606443: Infinite loop in FlowView.layout when using HTML tables in JEditorPane
peterz
parents: 2
diff changeset
   455
                    offset--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (rowIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    rowIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                p0 = fv.getView(rowIndex).getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                rowIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                p0 = fv.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            reparentViews(pool, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            viewBuffer = new Vector<View>(10, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            int rowCount = fv.getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            while (p0 < p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                View row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                if (rowIndex >= rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    row = fv.createRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    fv.append(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    row = fv.getView(rowIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                p0 = layoutRow(fv, rowIndex, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                rowIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            viewBuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if (rowIndex < rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                fv.replace(rowIndex, rowCount - rowIndex, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            unsetDamage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * Creates a row of views that will fit within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * layout span of the row.  This is called by the layout method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * This is implemented to fill the row by repeatedly calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * the createView method until the available span has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * exhausted, a forced break was encountered, or the createView
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   494
         * method returned null.  If the remaining span was exhausted,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * the adjustRow method will be called to perform adjustments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         * to the row to try and make it fit into the given span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * @param rowIndex the index of the row to fill in with views.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         *   row is assumed to be empty on entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * @param pos  The current position in the children of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         *   this views element from which to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * @return the position to start the next row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        protected int layoutRow(FlowView fv, int rowIndex, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            View row = fv.getView(rowIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            float x = fv.getFlowStart(rowIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            float spanLeft = fv.getFlowSpan(rowIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            int end = fv.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            TabExpander te = (fv instanceof TabExpander) ? (TabExpander)fv : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            final int flowAxis = fv.getFlowAxis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            int breakWeight = BadBreakWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            float breakX = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            float breakSpan = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            int breakIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            viewBuffer.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            while (pos < end && spanLeft >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                View v = createView(fv, pos, (int)spanLeft, rowIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                int bw = v.getBreakWeight(flowAxis, x, spanLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                if (bw >= ForcedBreakWeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    View w = v.breakView(flowAxis, pos, x, spanLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    if (w != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        viewBuffer.add(w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    } else if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        // if the view does not break, and it is the only view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        // in a row, use the whole view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        viewBuffer.add(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                } else if (bw >= breakWeight && bw > BadBreakWeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    breakWeight = bw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    breakX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    breakSpan = spanLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    breakIndex = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                float chunkSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                if (flowAxis == X_AXIS && v instanceof TabableView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    chunkSpan = ((TabableView)v).getTabbedSpan(x, te);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    chunkSpan = v.getPreferredSpan(flowAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                if (chunkSpan > spanLeft && breakIndex >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    // row is too long, and we may break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    if (breakIndex < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        v = viewBuffer.get(breakIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    for (int i = n - 1; i >= breakIndex; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        viewBuffer.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    v = v.breakView(flowAxis, v.getStartOffset(), breakX, breakSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                spanLeft -= chunkSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                x += chunkSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                viewBuffer.add(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                pos = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            View[] views = new View[viewBuffer.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            viewBuffer.toArray(views);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            row.replace(0, row.getViewCount(), views);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            return (views.length > 0 ? row.getEndOffset() : pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         * Adjusts the given row if possible to fit within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         * layout span.  By default this will try to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * highest break weight possible nearest the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         * the row.  If a forced break is encountered, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * break will be positioned there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         * @param rowIndex the row to adjust to the current layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         *  span.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   583
         * @param desiredSpan the current layout span &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * @param x the location r starts at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        protected void adjustRow(FlowView fv, int rowIndex, int desiredSpan, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            final int flowAxis = fv.getFlowAxis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            View r = fv.getView(rowIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            int n = r.getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            int span = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            int bestWeight = BadBreakWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            int bestSpan = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            int bestIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            View v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                v = r.getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                int spanLeft = desiredSpan - span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                int w = v.getBreakWeight(flowAxis, x + span, spanLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                if ((w >= bestWeight) && (w > BadBreakWeight)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    bestWeight = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    bestIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    bestSpan = span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    if (w >= ForcedBreakWeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        // it's a forced break, so there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        // no point in searching further.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                span += v.getPreferredSpan(flowAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (bestIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                // there is nothing that can be broken, leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                // it in it's current state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            // Break the best candidate view, and patch up the row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            int spanLeft = desiredSpan - bestSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            v = r.getView(bestIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            v = v.breakView(flowAxis, v.getStartOffset(), x + bestSpan, spanLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            View[] va = new View[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            va[0] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            View lv = getLogicalView(fv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            int p0 = r.getView(bestIndex).getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            int p1 = r.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            for (int i = 0; i < lv.getViewCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                View tmpView = lv.getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                if (tmpView.getEndOffset() > p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                if (tmpView.getStartOffset() >= p0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    tmpView.setParent(lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            r.replace(bestIndex, n - bestIndex, va);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        void reparentViews(View pool, int startPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            int n = pool.getViewIndex(startPos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (n >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                for (int i = n; i < pool.getViewCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    pool.getView(i).setParent(pool);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         * Creates a view that can be used to represent the current piece
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         * of the flow.  This can be either an entire view from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * logical view, or a fragment of the logical view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         * @param fv the view holding the flow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         * @param startOffset the start location for the view being created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
         * @param spanLeft the about of span left to fill in the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         * @param rowIndex the row the view will be placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        protected View createView(FlowView fv, int startOffset, int spanLeft, int rowIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            // Get the child view that contains the given starting position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            View lv = getLogicalView(fv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            int childIndex = lv.getViewIndex(startOffset, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            View v = lv.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            if (startOffset==v.getStartOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                // return the entire view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            // return a fragment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            v = v.createFragment(startOffset, v.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * This class can be used to represent a logical view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * a flow.  It keeps the children updated to reflect the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * of the model, gives the logical child views access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * view hierarchy, and calculates a preferred span.  It doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * do any rendering, layout, or model/view translation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    static class LogicalView extends CompositeView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        LogicalView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        protected int getViewIndexAtPosition(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            if (elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return super.getViewIndexAtPosition(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                View v = new LabelView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                append(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                super.loadChildren(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         * Fetches the attributes to use when rendering.  This view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         * isn't directly responsible for an element so it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         * the outer classes attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            View p = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            return (p != null) ? p.getAttributes() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
         * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
         * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
         * @return   the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
         *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         * @see View#getPreferredSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            float maxpref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            float pref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                pref += v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                if (v.getBreakWeight(axis, 0, Integer.MAX_VALUE) >= ForcedBreakWeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    maxpref = Math.max(maxpref, pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    pref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            maxpref = Math.max(maxpref, pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            return maxpref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         * Determines the minimum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
         * axis.  The is implemented to find the minimum unbreakable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
         * span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
         * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
         * @return  the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
         *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         * @see View#getPreferredSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            float maxmin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            float min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            boolean nowrap = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                if (v.getBreakWeight(axis, 0, Integer.MAX_VALUE) == BadBreakWeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    min += v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    nowrap = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                } else if (nowrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    maxmin = Math.max(min, maxmin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    nowrap = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                if (v instanceof ComponentView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    maxmin = Math.max(maxmin, v.getMinimumSpan(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            maxmin = Math.max(maxmin, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return maxmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
         * Forward the DocumentEvent to the given child view.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         * is implemented to reparent the child to the logical view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         * (the children may have been parented by a row in the flow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         * if they fit without breaking) and then execute the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         * behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         * @param v the child view to forward the event to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         * @see #forwardUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        protected void forwardUpdateToView(View v, DocumentEvent e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                                           Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            View parent = v.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            v.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            super.forwardUpdateToView(v, e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            v.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
20126
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   799
        /** {@inheritDoc} */
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   800
        @Override
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   801
        protected void forwardUpdate(DocumentEvent.ElementChange ec,
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   802
                                          DocumentEvent e, Shape a, ViewFactory f) {
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   803
            calculateUpdateIndexes(e);
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   804
            // Send update event to all views followed by the changed place.
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   805
            lastUpdateIndex = Math.max((getViewCount() - 1), 0);
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   806
            for (int i = firstUpdateIndex; i <= lastUpdateIndex; i++) {
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   807
                View v = getView(i);
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   808
                if (v != null) {
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   809
                    Shape childAlloc = getChildAllocation(i, a);
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   810
                    forwardUpdateToView(v, e, childAlloc, f);
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   811
                }
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   812
            }
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   813
        }
e09648d4170c 8024395: Improve fix for line break calculations
dmarkov
parents: 5506
diff changeset
   814
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        // The following methods don't do anything useful, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // simply keep the class from being abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         * Renders using the given rendering surface and area on that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
         * surface.  This is implemented to do nothing, the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
         * view is never visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
         * @param allocation the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        public void paint(Graphics g, Shape allocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
         * Tests whether a point lies before the rectangle range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
         * Implemented to return false, as hit detection is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
         * performed on the logical view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
         *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   835
         * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   836
         * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * @param alloc the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * @return true if the point is before the specified range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        protected boolean isBefore(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * Tests whether a point lies after the rectangle range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         * Implemented to return false, as hit detection is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
         * performed on the logical view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   849
         * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   850
         * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
         * @param alloc the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         * @return true if the point is after the specified range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        protected boolean isAfter(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
         * Fetches the child view at the given point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
         * Implemented to return null, as hit detection is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
         * performed on the logical view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   863
         * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   864
         * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
         * @param alloc the parent's allocation on entry, which should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
         *   be changed to the child's allocation on exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
         * @return the child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        protected View getViewAtPoint(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
         * Returns the allocation for a given child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
         * Implemented to do nothing, as the logical view doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
         * perform layout on the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
         *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20126
diff changeset
   878
         * @param index the index of the child, &gt;= 0 &amp;&amp; &lt; getViewCount()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
         * @param a  the allocation to the interior of the box on entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
         *   and the allocation of the child view at the index on exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        protected void childAllocation(int index, Rectangle a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
}