jdk/src/java.desktop/share/classes/javax/swing/text/ZoneView.java
author serb
Mon, 06 Mar 2017 22:52:45 +0300
changeset 44155 1bf93336ea45
parent 30462 507bcb03c954
permissions -rw-r--r--
8158209: Editing in TableView breaks the layout, when the document is I18n Reviewed-by: serb, alexsch Contributed-by: Abossolo Foh Guy <guy.abossolo.foh@scientificware.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * ZoneView is a View implementation that creates zones for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the child views are not created or stored until they are needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * for display or model/view translations.  This enables a substantial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * reduction in memory consumption for situations where the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * being represented is very large, by building view objects only for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the region being actively viewed/edited.  The size of the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * can be estimated in some way, or calculated asynchronously with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * only the result being saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * ZoneView extends BoxView to provide a box that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * zones for its children.  The zones are special View implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * (the children of an instance of this class) that represent only a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * portion of the model that an instance of ZoneView is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * for.  The zones don't create child views until an attempt is made
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * to display them. A box shaped view is well suited to this because:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   Boxes are a heavily used view, and having a box that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *   provides this behavior gives substantial opportunity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   to plug the behavior into a view hierarchy from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   view factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   Boxes are tiled in one direction, so it is easy to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   divide them into zones in a reliable way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   Boxes typically have a simple relationship to the model (i.e. they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   create child views that directly represent the child elements).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   Boxes are easier to estimate the size of than some other shapes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
    63
 * The default behavior is controlled by two properties, maxZoneSize
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * and maxZonesLoaded.  Setting maxZoneSize to Integer.MAX_VALUE would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * have the effect of causing only one zone to be created.  This would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * effectively turn the view into an implementation of the decorator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * pattern.  Setting maxZonesLoaded to a value of Integer.MAX_VALUE would
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * cause zones to never be unloaded.  For simplicity, zones are created on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * boundaries represented by the child elements of the element the view is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * responsible for.  The zones can be any View implementation, but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * default implementation is based upon AsyncBoxView which supports fairly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * large zones efficiently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @see     View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public class ZoneView extends BoxView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    int maxZoneSize = 8 * 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int maxZonesLoaded = 3;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    82
    Vector<View> loadedZones;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Constructs a ZoneView.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param elem the element this view is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param axis either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public ZoneView(Element elem, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        super(elem, axis);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    92
        loadedZones = new Vector<View>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Get the current maximum zone size.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
    97
     * @return the current maximum zone size
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public int getMaximumZoneSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return maxZoneSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Set the desired maximum zone size.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * zone may get larger than this size if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * a single child view is larger than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * size since zones are formed on child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param size the number of characters the zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * may represent before attempting to break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * the zone into a smaller size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void setMaximumZoneSize(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        maxZoneSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Get the current setting of the number of zones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * allowed to be loaded at the same time.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
   121
     * @return current setting of the number of zones
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
   122
     * allowed to be loaded at the same time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public int getMaxZonesLoaded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return maxZonesLoaded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Sets the current setting of the number of zones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * allowed to be loaded at the same time. This will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>IllegalArgumentException</code> if <code>mzl</code> is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * than 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param mzl the desired maximum number of zones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *  to be actively loaded, must be greater than 0
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   136
     * @exception IllegalArgumentException if <code>mzl</code> is &lt; 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void setMaxZonesLoaded(int mzl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (mzl < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new IllegalArgumentException("ZoneView.setMaxZonesLoaded must be greater than 0.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        maxZonesLoaded = mzl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        unloadOldZones();
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
     * Called by a zone when it gets loaded.  This happens when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * an attempt is made to display or perform a model/view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * translation on a zone that was in an unloaded state.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   150
     * This is implemented to check if the maximum number of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * zones was reached and to unload the oldest zone if so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param zone the child view that was just loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected void zoneWasLoaded(View zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        //System.out.println("loading: " + zone.getStartOffset() + "," + zone.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        loadedZones.addElement(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        unloadOldZones();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    void unloadOldZones() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        while (loadedZones.size() > getMaxZonesLoaded()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   163
            View zone = loadedZones.elementAt(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            loadedZones.removeElementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            unloadZone(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Unload a zone (Convert the zone to its memory saving state).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * The zones are expected to represent a subset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * child elements of the element this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Therefore, the default implementation is to simple remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * all the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param zone the child view desired to be set to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *  unloaded state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    protected void unloadZone(View zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        //System.out.println("unloading: " + zone.getStartOffset() + "," + zone.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        zone.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Determine if a zone is in the loaded state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * The zones are expected to represent a subset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * child elements of the element this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Therefore, the default implementation is to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * true if the view has children.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
   190
     * param zone the child view
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
   191
     * @param zone the zone
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
   192
     * @return whether or not the zone is in the loaded state.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    protected boolean isZoneLoaded(View zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return (zone.getViewCount() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Create a view to represent a zone for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * range within the model (which should be within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * the range of this objects responsibility).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * is called by the zone management logic to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * new zones.  Subclasses can provide a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * implementation for a zone by changing this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param p0 the start of the desired zone.  This should
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   207
     *  be &gt;= getStartOffset() and &lt; getEndOffset().  This
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   208
     *  value should also be &lt; p1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param p1 the end of the desired zone.  This should
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   210
     *  be &gt; getStartOffset() and &lt;= getEndOffset().  This
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   211
     *  value should also be &gt; p0.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
   212
     * @return a view to represent a zone for the given range within
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 25859
diff changeset
   213
     * the model
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    protected View createZone(int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        Document doc = getDocument();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   217
        View zone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            zone = new Zone(getElement(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            doc.createPosition(p0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                            doc.createPosition(p1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // this should puke in some way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new StateInvariantError(ble.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return zone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Loads all of the children to initialize the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * This is called by the <code>setParent</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * This is reimplemented to not load any children directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * (as they are created by the zones).  This method creates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * the initial set of zones.  Zones don't actually get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * populated however until an attempt is made to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * them or to do model/view coordinate translation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param f the view factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // build the first zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int offs0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int offs1 = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        append(createZone(offs0, offs1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        handleInsert(offs0, offs1 - offs0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Returns the child view index representing the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   253
     * @param pos the position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return  index of the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *   -1 if no view represents that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    protected int getViewIndexAtPosition(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // PENDING(prinz) this could be done as a binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // search, and probably should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (pos == getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return n - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        for(int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if(pos >= v.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
               pos < v.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    void handleInsert(int pos, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int index = getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        View v = getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int offs0 = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int offs1 = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if ((offs1 - offs0) > maxZoneSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            splitZone(index, offs0, offs1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    void handleRemove(int pos, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // IMPLEMENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Break up the zone at the given index into pieces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * of an acceptable size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    void splitZone(int index, int offs0, int offs1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // divide the old zone into a new set of bins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        Document doc = elem.getDocument();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   296
        Vector<View> zones = new Vector<View>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int offs = offs0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            offs0 = offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            offs = Math.min(getDesiredZoneEnd(offs0), offs1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            zones.addElement(createZone(offs0, offs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } while (offs < offs1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        View oldZone = getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        View[] newZones = new View[zones.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        zones.copyInto(newZones);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        replace(index, 1, newZones);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns the zone position to use for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * end of a zone that starts at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * position.  By default this returns something
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * close to half the max zone size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    int getDesiredZoneEnd(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        int index = elem.getElementIndex(pos + (maxZoneSize / 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        Element child = elem.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int offs0 = child.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int offs1 = child.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if ((offs1 - pos) > maxZoneSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (offs0 > pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                return offs0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return offs1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    // ---- View methods ----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * The superclass behavior will try to update the child views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * which is not desired in this case, since the children are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * zones and not directly effected by the changes to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * associated element.  This is reimplemented to do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * and return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    protected boolean updateChildren(DocumentEvent.ElementChange ec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                     DocumentEvent e, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Gives notification that something was inserted into the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * in a location that this view is responsible for.  This is largely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * delegated to the superclass, but is reimplemented to update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * relevant zone (i.e. determine if a zone needs to be split into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * set of 2 or more zones).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        handleInsert(changes.getOffset(), changes.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        super.insertUpdate(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * in a location that this view is responsible for.  This is largely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * delegated to the superclass, but is reimplemented to update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * relevant zones (i.e. determine if zones need to be removed or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * joined with another zone).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        handleRemove(changes.getOffset(), changes.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        super.removeUpdate(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Internally created view that has the purpose of holding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * the views that represent the children of the ZoneView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * that have been arranged in a zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    class Zone extends AsyncBoxView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        private Position start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        private Position end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        public Zone(Element elem, Position start, Position end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            super(elem, ZoneView.this.getAxis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            this.start = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            this.end = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         * Creates the child views and populates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         * zone with them.  This is done by translating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * the positions to child element index locations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * and building views to those elements.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * zone is already loaded, this does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        public void load() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (! isLoaded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                setEstimatedMajorSpan(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                Element e = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                ViewFactory f = getViewFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                int index0 = e.getElementIndex(getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                int index1 = e.getElementIndex(getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                View[] added = new View[index1 - index0 + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                for (int i = index0; i <= index1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    added[i - index0] = f.create(e.getElement(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                replace(0, 0, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                zoneWasLoaded(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         * Removes the child views and returns to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
         * state of unloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        public void unload() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            setEstimatedMajorSpan(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            removeAll();
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
         * Determines if the zone is in the loaded state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        public boolean isLoaded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return (getViewCount() != 0);
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
         * This method is reimplemented to not build the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * since the children are created when the zone is loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         * rather then when it is placed in the view hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * The major span is estimated at this point by building
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         * the first child (but not storing it), and calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * setEstimatedMajorSpan(true) followed by setSpan for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * the major axis with the estimated span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            // mark the major span as estimated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            setEstimatedMajorSpan(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            // estimate the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            int index0 = elem.getElementIndex(getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            int index1 = elem.getElementIndex(getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            int nChildren = index1 - index0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            // replace this with something real
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            //setSpan(getMajorAxis(), nChildren * 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            View first = f.create(elem.getElement(index0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            first.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            float w = first.getPreferredSpan(X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            float h = first.getPreferredSpan(Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (getMajorAxis() == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                w *= nChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                h += nChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            setSize(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         * Publish the changes in preferences upward to the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         * This is reimplemented to stop the superclass behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         * if the zone has not yet been loaded.  If the zone is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         * unloaded for example, the last seen major span is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         * best estimate and a calculated span for no children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * is undesirable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        protected void flushRequirementChanges() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (isLoaded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                super.flushRequirementChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         * Returns the child view index representing the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * the model.  Since the zone contains a cluster of the overall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * set of child elements, we can determine the index fairly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * quickly from the model by subtracting the index of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * start offset from the index of the position given.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * @param pos the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * @return  index of the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         *   -1 if no view represents that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        public int getViewIndex(int pos, Position.Bias b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            boolean isBackward = (b == Position.Bias.Backward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            pos = (isBackward) ? Math.max(0, pos - 1) : pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            int index1 = elem.getElementIndex(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            int index0 = elem.getElementIndex(getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            return index1 - index0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        protected boolean updateChildren(DocumentEvent.ElementChange ec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                         DocumentEvent e, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            // the structure of this element changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            Element[] removedElems = ec.getChildrenRemoved();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            Element[] addedElems = ec.getChildrenAdded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            int index0 = elem.getElementIndex(getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            int index1 = elem.getElementIndex(getEndOffset()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            int index = ec.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if ((index >= index0) && (index <= index1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                // The change is in this zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                int replaceIndex = index - index0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                int nadd = Math.min(index1 - index0 + 1, addedElems.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                int nremove = Math.min(index1 - index0 + 1, removedElems.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                View[] added = new View[nadd];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                for (int i = 0; i < nadd; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    added[i] = f.create(addedElems[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                replace(replaceIndex, nremove, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        // --- View methods ----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         * Fetches the attributes to use when rendering.  This view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         * isn't directly responsible for an element so it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         * the outer classes attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return ZoneView.this.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * Renders using the given rendering surface and area on that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * surface.  This is implemented to load the zone if its not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         * already loaded, and then perform the superclass behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
         * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            super.paint(g, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
         * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
         * coordinate space of the model.  This is implemented to first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
         * make sure the zone is loaded before providing the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
         * behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * @param x   x coordinate of the view location to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * @param y   y coordinate of the view location to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         *  given point in the view >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            return super.viewToModel(x, y, a, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
         * to the coordinate space of the view mapped to it.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         * implemented to provide the superclass behavior after first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         * making sure the zone is loaded (The zone must be loaded to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * make this calculation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * @param pos the position to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * @exception BadLocationException  if the given position does not represent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         *   valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            return super.modelToView(pos, a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * Start of the zones range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * @see View#getStartOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            return start.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         * End of the zones range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            return end.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * Gives notification that something was inserted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * the document in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * If the zone has been loaded, the superclass behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * invoked, otherwise this does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (isLoaded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                super.insertUpdate(e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * If the zone has been loaded, the superclass behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * invoked, otherwise this does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if (isLoaded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                super.removeUpdate(e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * If the zone has been loaded, the superclass behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         * invoked, otherwise this does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            if (isLoaded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                super.changedUpdate(e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
}