0N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text;
0N/A
0N/Aimport java.util.Vector;
0N/Aimport java.awt.*;
0N/Aimport javax.swing.event.*;
0N/A
0N/A/**
0N/A * ZoneView is a View implementation that creates zones for which
0N/A * the child views are not created or stored until they are needed
0N/A * for display or model/view translations. This enables a substantial
0N/A * reduction in memory consumption for situations where the model
0N/A * being represented is very large, by building view objects only for
0N/A * the region being actively viewed/edited. The size of the children
0N/A * can be estimated in some way, or calculated asynchronously with
0N/A * only the result being saved.
0N/A * <p>
0N/A * ZoneView extends BoxView to provide a box that implements
0N/A * zones for its children. The zones are special View implementations
0N/A * (the children of an instance of this class) that represent only a
0N/A * portion of the model that an instance of ZoneView is responsible
0N/A * for. The zones don't create child views until an attempt is made
0N/A * to display them. A box shaped view is well suited to this because:
0N/A * <ul>
0N/A * <li>
0N/A * Boxes are a heavily used view, and having a box that
0N/A * provides this behavior gives substantial opportunity
0N/A * to plug the behavior into a view hierarchy from the
0N/A * view factory.
0N/A * <li>
0N/A * Boxes are tiled in one direction, so it is easy to
0N/A * divide them into zones in a reliable way.
0N/A * <li>
0N/A * Boxes typically have a simple relationship to the model (i.e. they
0N/A * create child views that directly represent the child elements).
0N/A * <li>
0N/A * Boxes are easier to estimate the size of than some other shapes.
0N/A * </ul>
0N/A * <p>
0N/A * The default behavior is controled by two properties, maxZoneSize
0N/A * and maxZonesLoaded. Setting maxZoneSize to Integer.MAX_VALUE would
0N/A * have the effect of causing only one zone to be created. This would
0N/A * effectively turn the view into an implementation of the decorator
0N/A * pattern. Setting maxZonesLoaded to a value of Integer.MAX_VALUE would
0N/A * cause zones to never be unloaded. For simplicity, zones are created on
0N/A * boundaries represented by the child elements of the element the view is
0N/A * responsible for. The zones can be any View implementation, but the
0N/A * default implementation is based upon AsyncBoxView which supports fairly
0N/A * large zones efficiently.
0N/A *
0N/A * @author Timothy Prinzing
0N/A * @see View
0N/A * @since 1.3
0N/A */
0N/Apublic class ZoneView extends BoxView {
0N/A
0N/A int maxZoneSize = 8 * 1024;
0N/A int maxZonesLoaded = 3;
611N/A Vector<View> loadedZones;
0N/A
0N/A /**
0N/A * Constructs a ZoneView.
0N/A *
0N/A * @param elem the element this view is responsible for
0N/A * @param axis either View.X_AXIS or View.Y_AXIS
0N/A */
0N/A public ZoneView(Element elem, int axis) {
0N/A super(elem, axis);
611N/A loadedZones = new Vector<View>();
0N/A }
0N/A
0N/A /**
0N/A * Get the current maximum zone size.
0N/A */
0N/A public int getMaximumZoneSize() {
0N/A return maxZoneSize;
0N/A }
0N/A
0N/A /**
0N/A * Set the desired maximum zone size. A
0N/A * zone may get larger than this size if
0N/A * a single child view is larger than this
0N/A * size since zones are formed on child view
0N/A * boundaries.
0N/A *
0N/A * @param size the number of characters the zone
0N/A * may represent before attempting to break
0N/A * the zone into a smaller size.
0N/A */
0N/A public void setMaximumZoneSize(int size) {
0N/A maxZoneSize = size;
0N/A }
0N/A
0N/A /**
0N/A * Get the current setting of the number of zones
0N/A * allowed to be loaded at the same time.
0N/A */
0N/A public int getMaxZonesLoaded() {
0N/A return maxZonesLoaded;
0N/A }
0N/A
0N/A /**
0N/A * Sets the current setting of the number of zones
0N/A * allowed to be loaded at the same time. This will throw an
0N/A * <code>IllegalArgumentException</code> if <code>mzl</code> is less
0N/A * than 1.
0N/A *
0N/A * @param mzl the desired maximum number of zones
0N/A * to be actively loaded, must be greater than 0
0N/A * @exception IllegalArgumentException if <code>mzl</code> is < 1
0N/A */
0N/A public void setMaxZonesLoaded(int mzl) {
0N/A if (mzl < 1) {
0N/A throw new IllegalArgumentException("ZoneView.setMaxZonesLoaded must be greater than 0.");
0N/A }
0N/A maxZonesLoaded = mzl;
0N/A unloadOldZones();
0N/A }
0N/A
0N/A /**
0N/A * Called by a zone when it gets loaded. This happens when
0N/A * an attempt is made to display or perform a model/view
0N/A * translation on a zone that was in an unloaded state.
0N/A * This is imlemented to check if the maximum number of
0N/A * zones was reached and to unload the oldest zone if so.
0N/A *
0N/A * @param zone the child view that was just loaded.
0N/A */
0N/A protected void zoneWasLoaded(View zone) {
0N/A //System.out.println("loading: " + zone.getStartOffset() + "," + zone.getEndOffset());
0N/A loadedZones.addElement(zone);
0N/A unloadOldZones();
0N/A }
0N/A
0N/A void unloadOldZones() {
0N/A while (loadedZones.size() > getMaxZonesLoaded()) {
611N/A View zone = loadedZones.elementAt(0);
0N/A loadedZones.removeElementAt(0);
0N/A unloadZone(zone);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Unload a zone (Convert the zone to its memory saving state).
0N/A * The zones are expected to represent a subset of the
0N/A * child elements of the element this view is responsible for.
0N/A * Therefore, the default implementation is to simple remove
0N/A * all the children.
0N/A *
0N/A * @param zone the child view desired to be set to an
0N/A * unloaded state.
0N/A */
0N/A protected void unloadZone(View zone) {
0N/A //System.out.println("unloading: " + zone.getStartOffset() + "," + zone.getEndOffset());
0N/A zone.removeAll();
0N/A }
0N/A
0N/A /**
0N/A * Determine if a zone is in the loaded state.
0N/A * The zones are expected to represent a subset of the
0N/A * child elements of the element this view is responsible for.
0N/A * Therefore, the default implementation is to return
0N/A * true if the view has children.
0N/A */
0N/A protected boolean isZoneLoaded(View zone) {
0N/A return (zone.getViewCount() > 0);
0N/A }
0N/A
0N/A /**
0N/A * Create a view to represent a zone for the given
0N/A * range within the model (which should be within
0N/A * the range of this objects responsibility). This
0N/A * is called by the zone management logic to create
0N/A * new zones. Subclasses can provide a different
0N/A * implementation for a zone by changing this method.
0N/A *
0N/A * @param p0 the start of the desired zone. This should
0N/A * be >= getStartOffset() and < getEndOffset(). This
0N/A * value should also be < p1.
0N/A * @param p1 the end of the desired zone. This should
0N/A * be > getStartOffset() and <= getEndOffset(). This
0N/A * value should also be > p0.
0N/A */
0N/A protected View createZone(int p0, int p1) {
0N/A Document doc = getDocument();
611N/A View zone;
0N/A try {
0N/A zone = new Zone(getElement(),
0N/A doc.createPosition(p0),
0N/A doc.createPosition(p1));
0N/A } catch (BadLocationException ble) {
0N/A // this should puke in some way.
0N/A throw new StateInvariantError(ble.getMessage());
0N/A }
0N/A return zone;
0N/A }
0N/A
0N/A /**
0N/A * Loads all of the children to initialize the view.
0N/A * This is called by the <code>setParent</code> method.
0N/A * This is reimplemented to not load any children directly
0N/A * (as they are created by the zones). This method creates
0N/A * the initial set of zones. Zones don't actually get
0N/A * populated however until an attempt is made to display
0N/A * them or to do model/view coordinate translation.
0N/A *
0N/A * @param f the view factory
0N/A */
0N/A protected void loadChildren(ViewFactory f) {
0N/A // build the first zone.
0N/A Document doc = getDocument();
0N/A int offs0 = getStartOffset();
0N/A int offs1 = getEndOffset();
0N/A append(createZone(offs0, offs1));
0N/A handleInsert(offs0, offs1 - offs0);
0N/A }
0N/A
0N/A /**
0N/A * Returns the child view index representing the given position in
0N/A * the model.
0N/A *
0N/A * @param pos the position >= 0
0N/A * @return index of the view representing the given position, or
0N/A * -1 if no view represents that position
0N/A */
0N/A protected int getViewIndexAtPosition(int pos) {
0N/A // PENDING(prinz) this could be done as a binary
0N/A // search, and probably should be.
0N/A int n = getViewCount();
0N/A if (pos == getEndOffset()) {
0N/A return n - 1;
0N/A }
0N/A for(int i = 0; i < n; i++) {
0N/A View v = getView(i);
0N/A if(pos >= v.getStartOffset() &&
0N/A pos < v.getEndOffset()) {
0N/A return i;
0N/A }
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A void handleInsert(int pos, int length) {
0N/A int index = getViewIndex(pos, Position.Bias.Forward);
0N/A View v = getView(index);
0N/A int offs0 = v.getStartOffset();
0N/A int offs1 = v.getEndOffset();
0N/A if ((offs1 - offs0) > maxZoneSize) {
0N/A splitZone(index, offs0, offs1);
0N/A }
0N/A }
0N/A
0N/A void handleRemove(int pos, int length) {
0N/A // IMPLEMENT
0N/A }
0N/A
0N/A /**
0N/A * Break up the zone at the given index into pieces
0N/A * of an acceptable size.
0N/A */
0N/A void splitZone(int index, int offs0, int offs1) {
0N/A // divide the old zone into a new set of bins
0N/A Element elem = getElement();
0N/A Document doc = elem.getDocument();
611N/A Vector<View> zones = new Vector<View>();
0N/A int offs = offs0;
0N/A do {
0N/A offs0 = offs;
0N/A offs = Math.min(getDesiredZoneEnd(offs0), offs1);
0N/A zones.addElement(createZone(offs0, offs));
0N/A } while (offs < offs1);
0N/A View oldZone = getView(index);
0N/A View[] newZones = new View[zones.size()];
0N/A zones.copyInto(newZones);
0N/A replace(index, 1, newZones);
0N/A }
0N/A
0N/A /**
0N/A * Returns the zone position to use for the
0N/A * end of a zone that starts at the given
0N/A * position. By default this returns something
0N/A * close to half the max zone size.
0N/A */
0N/A int getDesiredZoneEnd(int pos) {
0N/A Element elem = getElement();
0N/A int index = elem.getElementIndex(pos + (maxZoneSize / 2));
0N/A Element child = elem.getElement(index);
0N/A int offs0 = child.getStartOffset();
0N/A int offs1 = child.getEndOffset();
0N/A if ((offs1 - pos) > maxZoneSize) {
0N/A if (offs0 > pos) {
0N/A return offs0;
0N/A }
0N/A }
0N/A return offs1;
0N/A }
0N/A
0N/A // ---- View methods ----------------------------------------------------
0N/A
0N/A /**
0N/A * The superclass behavior will try to update the child views
0N/A * which is not desired in this case, since the children are
0N/A * zones and not directly effected by the changes to the
0N/A * associated element. This is reimplemented to do nothing
0N/A * and return false.
0N/A */
0N/A protected boolean updateChildren(DocumentEvent.ElementChange ec,
0N/A DocumentEvent e, ViewFactory f) {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Gives notification that something was inserted into the document
0N/A * in a location that this view is responsible for. This is largely
0N/A * delegated to the superclass, but is reimplemented to update the
0N/A * relevant zone (i.e. determine if a zone needs to be split into a
0N/A * set of 2 or more zones).
0N/A *
0N/A * @param changes the change information from the associated document
0N/A * @param a the current allocation of the view
0N/A * @param f the factory to use to rebuild if the view has children
0N/A * @see View#insertUpdate
0N/A */
0N/A public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
0N/A handleInsert(changes.getOffset(), changes.getLength());
0N/A super.insertUpdate(changes, a, f);
0N/A }
0N/A
0N/A /**
0N/A * Gives notification that something was removed from the document
0N/A * in a location that this view is responsible for. This is largely
0N/A * delegated to the superclass, but is reimplemented to update the
0N/A * relevant zones (i.e. determine if zones need to be removed or
0N/A * joined with another zone).
0N/A *
0N/A * @param changes the change information from the associated document
0N/A * @param a the current allocation of the view
0N/A * @param f the factory to use to rebuild if the view has children
0N/A * @see View#removeUpdate
0N/A */
0N/A public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
0N/A handleRemove(changes.getOffset(), changes.getLength());
0N/A super.removeUpdate(changes, a, f);
0N/A }
0N/A
0N/A /**
0N/A * Internally created view that has the purpose of holding
0N/A * the views that represent the children of the ZoneView
0N/A * that have been arranged in a zone.
0N/A */
0N/A class Zone extends AsyncBoxView {
0N/A
0N/A private Position start;
0N/A private Position end;
0N/A
0N/A public Zone(Element elem, Position start, Position end) {
0N/A super(elem, ZoneView.this.getAxis());
0N/A this.start = start;
0N/A this.end = end;
0N/A }
0N/A
0N/A /**
0N/A * Creates the child views and populates the
0N/A * zone with them. This is done by translating
0N/A * the positions to child element index locations
0N/A * and building views to those elements. If the
0N/A * zone is already loaded, this does nothing.
0N/A */
0N/A public void load() {
0N/A if (! isLoaded()) {
0N/A setEstimatedMajorSpan(true);
0N/A Element e = getElement();
0N/A ViewFactory f = getViewFactory();
0N/A int index0 = e.getElementIndex(getStartOffset());
0N/A int index1 = e.getElementIndex(getEndOffset());
0N/A View[] added = new View[index1 - index0 + 1];
0N/A for (int i = index0; i <= index1; i++) {
0N/A added[i - index0] = f.create(e.getElement(i));
0N/A }
0N/A replace(0, 0, added);
0N/A
0N/A zoneWasLoaded(this);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes the child views and returns to a
0N/A * state of unloaded.
0N/A */
0N/A public void unload() {
0N/A setEstimatedMajorSpan(true);
0N/A removeAll();
0N/A }
0N/A
0N/A /**
0N/A * Determines if the zone is in the loaded state
0N/A * or not.
0N/A */
0N/A public boolean isLoaded() {
0N/A return (getViewCount() != 0);
0N/A }
0N/A
0N/A /**
0N/A * This method is reimplemented to not build the children
0N/A * since the children are created when the zone is loaded
0N/A * rather then when it is placed in the view hierarchy.
0N/A * The major span is estimated at this point by building
0N/A * the first child (but not storing it), and calling
0N/A * setEstimatedMajorSpan(true) followed by setSpan for
0N/A * the major axis with the estimated span.
0N/A */
0N/A protected void loadChildren(ViewFactory f) {
0N/A // mark the major span as estimated
0N/A setEstimatedMajorSpan(true);
0N/A
0N/A // estimate the span
0N/A Element elem = getElement();
0N/A int index0 = elem.getElementIndex(getStartOffset());
0N/A int index1 = elem.getElementIndex(getEndOffset());
0N/A int nChildren = index1 - index0;
0N/A
0N/A // replace this with something real
0N/A //setSpan(getMajorAxis(), nChildren * 10);
0N/A
0N/A View first = f.create(elem.getElement(index0));
0N/A first.setParent(this);
0N/A float w = first.getPreferredSpan(X_AXIS);
0N/A float h = first.getPreferredSpan(Y_AXIS);
0N/A if (getMajorAxis() == X_AXIS) {
0N/A w *= nChildren;
0N/A } else {
0N/A h += nChildren;
0N/A }
0N/A
0N/A setSize(w, h);
0N/A }
0N/A
0N/A /**
0N/A * Publish the changes in preferences upward to the parent
0N/A * view.
0N/A * <p>
0N/A * This is reimplemented to stop the superclass behavior
0N/A * if the zone has not yet been loaded. If the zone is
0N/A * unloaded for example, the last seen major span is the
0N/A * best estimate and a calculated span for no children
0N/A * is undesirable.
0N/A */
0N/A protected void flushRequirementChanges() {
0N/A if (isLoaded()) {
0N/A super.flushRequirementChanges();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the child view index representing the given position in
0N/A * the model. Since the zone contains a cluster of the overall
0N/A * set of child elements, we can determine the index fairly
0N/A * quickly from the model by subtracting the index of the
0N/A * start offset from the index of the position given.
0N/A *
0N/A * @param pos the position >= 0
0N/A * @return index of the view representing the given position, or
0N/A * -1 if no view represents that position
0N/A * @since 1.3
0N/A */
0N/A public int getViewIndex(int pos, Position.Bias b) {
0N/A boolean isBackward = (b == Position.Bias.Backward);
0N/A pos = (isBackward) ? Math.max(0, pos - 1) : pos;
0N/A Element elem = getElement();
0N/A int index1 = elem.getElementIndex(pos);
0N/A int index0 = elem.getElementIndex(getStartOffset());
0N/A return index1 - index0;
0N/A }
0N/A
0N/A protected boolean updateChildren(DocumentEvent.ElementChange ec,
0N/A DocumentEvent e, ViewFactory f) {
0N/A // the structure of this element changed.
0N/A Element[] removedElems = ec.getChildrenRemoved();
0N/A Element[] addedElems = ec.getChildrenAdded();
0N/A Element elem = getElement();
0N/A int index0 = elem.getElementIndex(getStartOffset());
0N/A int index1 = elem.getElementIndex(getEndOffset()-1);
0N/A int index = ec.getIndex();
0N/A if ((index >= index0) && (index <= index1)) {
0N/A // The change is in this zone
0N/A int replaceIndex = index - index0;
0N/A int nadd = Math.min(index1 - index0 + 1, addedElems.length);
0N/A int nremove = Math.min(index1 - index0 + 1, removedElems.length);
0N/A View[] added = new View[nadd];
0N/A for (int i = 0; i < nadd; i++) {
0N/A added[i] = f.create(addedElems[i]);
0N/A }
0N/A replace(replaceIndex, nremove, added);
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A // --- View methods ----------------------------------
0N/A
0N/A /**
0N/A * Fetches the attributes to use when rendering. This view
0N/A * isn't directly responsible for an element so it returns
0N/A * the outer classes attributes.
0N/A */
0N/A public AttributeSet getAttributes() {
0N/A return ZoneView.this.getAttributes();
0N/A }
0N/A
0N/A /**
0N/A * Renders using the given rendering surface and area on that
0N/A * surface. This is implemented to load the zone if its not
0N/A * already loaded, and then perform the superclass behavior.
0N/A *
0N/A * @param g the rendering surface to use
0N/A * @param a the allocated region to render into
0N/A * @see View#paint
0N/A */
0N/A public void paint(Graphics g, Shape a) {
0N/A load();
0N/A super.paint(g, a);
0N/A }
0N/A
0N/A /**
0N/A * Provides a mapping from the view coordinate space to the logical
0N/A * coordinate space of the model. This is implemented to first
0N/A * make sure the zone is loaded before providing the superclass
0N/A * behavior.
0N/A *
0N/A * @param x x coordinate of the view location to convert >= 0
0N/A * @param y y coordinate of the view location to convert >= 0
0N/A * @param a the allocated region to render into
0N/A * @return the location within the model that best represents the
0N/A * given point in the view >= 0
0N/A * @see View#viewToModel
0N/A */
0N/A public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
0N/A load();
0N/A return super.viewToModel(x, y, a, bias);
0N/A }
0N/A
0N/A /**
0N/A * Provides a mapping from the document model coordinate space
0N/A * to the coordinate space of the view mapped to it. This is
0N/A * implemented to provide the superclass behavior after first
0N/A * making sure the zone is loaded (The zone must be loaded to
0N/A * make this calculation).
0N/A *
0N/A * @param pos the position to convert
0N/A * @param a the allocated region to render into
0N/A * @return the bounding box of the given position
0N/A * @exception BadLocationException if the given position does not represent a
0N/A * valid location in the associated document
0N/A * @see View#modelToView
0N/A */
0N/A public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
0N/A load();
0N/A return super.modelToView(pos, a, b);
0N/A }
0N/A
0N/A /**
0N/A * Start of the zones range.
0N/A *
0N/A * @see View#getStartOffset
0N/A */
0N/A public int getStartOffset() {
0N/A return start.getOffset();
0N/A }
0N/A
0N/A /**
0N/A * End of the zones range.
0N/A */
0N/A public int getEndOffset() {
0N/A return end.getOffset();
0N/A }
0N/A
0N/A /**
0N/A * Gives notification that something was inserted into
0N/A * the document in a location that this view is responsible for.
0N/A * If the zone has been loaded, the superclass behavior is
0N/A * invoked, otherwise this does nothing.
0N/A *
0N/A * @param e the change information from the associated document
0N/A * @param a the current allocation of the view
0N/A * @param f the factory to use to rebuild if the view has children
0N/A * @see View#insertUpdate
0N/A */
0N/A public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
0N/A if (isLoaded()) {
0N/A super.insertUpdate(e, a, f);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gives notification that something was removed from the document
0N/A * in a location that this view is responsible for.
0N/A * If the zone has been loaded, the superclass behavior is
0N/A * invoked, otherwise this does nothing.
0N/A *
0N/A * @param e the change information from the associated document
0N/A * @param a the current allocation of the view
0N/A * @param f the factory to use to rebuild if the view has children
0N/A * @see View#removeUpdate
0N/A */
0N/A public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
0N/A if (isLoaded()) {
0N/A super.removeUpdate(e, a, f);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gives notification from the document that attributes were changed
0N/A * in a location that this view is responsible for.
0N/A * If the zone has been loaded, the superclass behavior is
0N/A * invoked, otherwise this does nothing.
0N/A *
0N/A * @param e the change information from the associated document
0N/A * @param a the current allocation of the view
0N/A * @param f the factory to use to rebuild if the view has children
0N/A * @see View#removeUpdate
0N/A */
0N/A public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
0N/A if (isLoaded()) {
0N/A super.changedUpdate(e, a, f);
0N/A }
0N/A }
0N/A
0N/A }
0N/A}