< prev index next >

src/java.desktop/share/classes/javax/swing/text/StringContent.java

Print this page




  28 import java.io.Serializable;
  29 import javax.swing.undo.*;
  30 import javax.swing.SwingUtilities;
  31 
  32 /**
  33  * An implementation of the AbstractDocument.Content interface that is
  34  * a brute force implementation that is useful for relatively small
  35  * documents and/or debugging.  It manages the character content
  36  * as a simple character array.  It is also quite inefficient.
  37  * <p>
  38  * It is generally recommended that the gap buffer or piece table
  39  * implementations be used instead.  This buffer does not scale up
  40  * to large sizes.
  41  * <p>
  42  * <strong>Warning:</strong>
  43  * Serialized objects of this class will not be compatible with
  44  * future Swing releases. The current serialization support is
  45  * appropriate for short term storage or RMI between applications running
  46  * the same version of Swing.  As of 1.4, support for long term storage
  47  * of all JavaBeans&trade;
  48  * has been added to the <code>java.beans</code> package.
  49  * Please see {@link java.beans.XMLEncoder}.
  50  *
  51  * @author  Timothy Prinzing
  52  */
  53 @SuppressWarnings("serial") // Same-version serialization only
  54 public final class StringContent implements AbstractDocument.Content, Serializable {
  55 
  56     /**
  57      * Creates a new StringContent object.  Initial size defaults to 10.
  58      */
  59     public StringContent() {
  60         this(10);
  61     }
  62 
  63     /**
  64      * Creates a new StringContent object, with the initial
  65      * size specified.  If the length is &lt; 1, a size of 1 is used.
  66      *
  67      * @param initialLength the initial size
  68      */


 242     synchronized void updateMarksForRemove(int offset, int length) {
 243         int n = marks.size();
 244         for (int i = 0; i < n; i++) {
 245             PosRec mark = marks.elementAt(i);
 246             if (mark.unused) {
 247                 // this record is no longer used, get rid of it
 248                 marks.removeElementAt(i);
 249                 i -= 1;
 250                 n -= 1;
 251             } else if (mark.offset >= (offset + length)) {
 252                 mark.offset -= length;
 253             } else if (mark.offset >= offset) {
 254                 mark.offset = offset;
 255             }
 256         }
 257     }
 258 
 259     /**
 260      * Returns a Vector containing instances of UndoPosRef for the
 261      * Positions in the range
 262      * <code>offset</code> to <code>offset</code> + <code>length</code>.
 263      * If <code>v</code> is not null the matching Positions are placed in
 264      * there. The vector with the resulting Positions are returned.
 265      * <p>
 266      * This is meant for internal usage, and is generally not of interest
 267      * to subclasses.
 268      *
 269      * @param v the Vector to use, with a new one created on null
 270      * @param offset the starting offset &gt;= 0
 271      * @param length the length &gt;= 0
 272      * @return the set of instances
 273      */
 274     @SuppressWarnings({"rawtypes", "unchecked"}) // UndoPosRef type cannot be exposed
 275     protected Vector getPositionsInRange(Vector v, int offset,
 276                                          int length) {
 277         int n = marks.size();
 278         int end = offset + length;
 279         Vector placeIn = (v == null) ? new Vector() : v;
 280         for (int i = 0; i < n; i++) {
 281             PosRec mark = marks.elementAt(i);
 282             if (mark.unused) {
 283                 // this record is no longer used, get rid of it
 284                 marks.removeElementAt(i);
 285                 i -= 1;
 286                 n -= 1;
 287             } else if(mark.offset >= offset && mark.offset <= end)
 288                 placeIn.addElement(new UndoPosRef(mark));
 289         }
 290         return placeIn;
 291     }
 292 
 293     /**
 294      * Resets the location for all the UndoPosRef instances
 295      * in <code>positions</code>.
 296      * <p>
 297      * This is meant for internal usage, and is generally not of interest
 298      * to subclasses.
 299      *
 300      * @param positions the positions of the instances
 301      */
 302     @SuppressWarnings("rawtypes") // UndoPosRef type cannot be exposed
 303     protected void updateUndoPositions(Vector positions) {
 304         for(int counter = positions.size() - 1; counter >= 0; counter--) {
 305             UndoPosRef ref = (UndoPosRef) positions.elementAt(counter);
 306             // Check if the Position is still valid.
 307             if(ref.rec.unused) {
 308                 positions.removeElementAt(counter);
 309             }
 310             else
 311                 ref.resetLocation();
 312         }
 313     }
 314 
 315     private static final char[] empty = new char[0];




  28 import java.io.Serializable;
  29 import javax.swing.undo.*;
  30 import javax.swing.SwingUtilities;
  31 
  32 /**
  33  * An implementation of the AbstractDocument.Content interface that is
  34  * a brute force implementation that is useful for relatively small
  35  * documents and/or debugging.  It manages the character content
  36  * as a simple character array.  It is also quite inefficient.
  37  * <p>
  38  * It is generally recommended that the gap buffer or piece table
  39  * implementations be used instead.  This buffer does not scale up
  40  * to large sizes.
  41  * <p>
  42  * <strong>Warning:</strong>
  43  * Serialized objects of this class will not be compatible with
  44  * future Swing releases. The current serialization support is
  45  * appropriate for short term storage or RMI between applications running
  46  * the same version of Swing.  As of 1.4, support for long term storage
  47  * of all JavaBeans&trade;
  48  * has been added to the {@code java.beans} package.
  49  * Please see {@link java.beans.XMLEncoder}.
  50  *
  51  * @author  Timothy Prinzing
  52  */
  53 @SuppressWarnings("serial") // Same-version serialization only
  54 public final class StringContent implements AbstractDocument.Content, Serializable {
  55 
  56     /**
  57      * Creates a new StringContent object.  Initial size defaults to 10.
  58      */
  59     public StringContent() {
  60         this(10);
  61     }
  62 
  63     /**
  64      * Creates a new StringContent object, with the initial
  65      * size specified.  If the length is &lt; 1, a size of 1 is used.
  66      *
  67      * @param initialLength the initial size
  68      */


 242     synchronized void updateMarksForRemove(int offset, int length) {
 243         int n = marks.size();
 244         for (int i = 0; i < n; i++) {
 245             PosRec mark = marks.elementAt(i);
 246             if (mark.unused) {
 247                 // this record is no longer used, get rid of it
 248                 marks.removeElementAt(i);
 249                 i -= 1;
 250                 n -= 1;
 251             } else if (mark.offset >= (offset + length)) {
 252                 mark.offset -= length;
 253             } else if (mark.offset >= offset) {
 254                 mark.offset = offset;
 255             }
 256         }
 257     }
 258 
 259     /**
 260      * Returns a Vector containing instances of UndoPosRef for the
 261      * Positions in the range
 262      * {@code offset} to {@code offset} + {@code length}.
 263      * If {@code v} is not null the matching Positions are placed in
 264      * there. The vector with the resulting Positions are returned.
 265      * <p>
 266      * This is meant for internal usage, and is generally not of interest
 267      * to subclasses.
 268      *
 269      * @param v the Vector to use, with a new one created on null
 270      * @param offset the starting offset &gt;= 0
 271      * @param length the length &gt;= 0
 272      * @return the set of instances
 273      */
 274     @SuppressWarnings({"rawtypes", "unchecked"}) // UndoPosRef type cannot be exposed
 275     protected Vector getPositionsInRange(Vector v, int offset,
 276                                          int length) {
 277         int n = marks.size();
 278         int end = offset + length;
 279         Vector placeIn = (v == null) ? new Vector() : v;
 280         for (int i = 0; i < n; i++) {
 281             PosRec mark = marks.elementAt(i);
 282             if (mark.unused) {
 283                 // this record is no longer used, get rid of it
 284                 marks.removeElementAt(i);
 285                 i -= 1;
 286                 n -= 1;
 287             } else if(mark.offset >= offset && mark.offset <= end)
 288                 placeIn.addElement(new UndoPosRef(mark));
 289         }
 290         return placeIn;
 291     }
 292 
 293     /**
 294      * Resets the location for all the UndoPosRef instances
 295      * in {@code positions}.
 296      * <p>
 297      * This is meant for internal usage, and is generally not of interest
 298      * to subclasses.
 299      *
 300      * @param positions the positions of the instances
 301      */
 302     @SuppressWarnings("rawtypes") // UndoPosRef type cannot be exposed
 303     protected void updateUndoPositions(Vector positions) {
 304         for(int counter = positions.size() - 1; counter >= 0; counter--) {
 305             UndoPosRef ref = (UndoPosRef) positions.elementAt(counter);
 306             // Check if the Position is still valid.
 307             if(ref.rec.unused) {
 308                 positions.removeElementAt(counter);
 309             }
 310             else
 311                 ref.resetLocation();
 312         }
 313     }
 314 
 315     private static final char[] empty = new char[0];


< prev index next >