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