1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import java.util.Vector;
  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      */
  69     public StringContent(int initialLength) {
  70         if (initialLength < 1) {
  71             initialLength = 1;
  72         }
  73         data = new char[initialLength];
  74         data[0] = '\n';
  75         count = 1;
  76     }
  77 
  78     /**
  79      * Returns the length of the content.
  80      *
  81      * @return the length &gt;= 1
  82      * @see AbstractDocument.Content#length
  83      */
  84     public int length() {
  85         return count;
  86     }
  87 
  88     /**
  89      * Inserts a string into the content.
  90      *
  91      * @param where the starting position &gt;= 0 &amp;&amp; &lt; length()
  92      * @param str the non-null string to insert
  93      * @return an UndoableEdit object for undoing
  94      * @exception BadLocationException if the specified position is invalid
  95      * @see AbstractDocument.Content#insertString
  96      */
  97     public UndoableEdit insertString(int where, String str) throws BadLocationException {
  98         if (where >= count || where < 0) {
  99             throw new BadLocationException("Invalid location", count);
 100         }
 101         char[] chars = str.toCharArray();
 102         replace(where, 0, chars, 0, chars.length);
 103         if (marks != null) {
 104             updateMarksForInsert(where, str.length());
 105         }
 106         return new InsertUndo(where, str.length());
 107     }
 108 
 109     /**
 110      * Removes part of the content.  where + nitems must be &lt; length().
 111      *
 112      * @param where the starting position &gt;= 0
 113      * @param nitems the number of characters to remove &gt;= 0
 114      * @return an UndoableEdit object for undoing
 115      * @exception BadLocationException if the specified position is invalid
 116      * @see AbstractDocument.Content#remove
 117      */
 118     public UndoableEdit remove(int where, int nitems) throws BadLocationException {
 119         if (where + nitems >= count) {
 120             throw new BadLocationException("Invalid range", count);
 121         }
 122         String removedString = getString(where, nitems);
 123         UndoableEdit edit = new RemoveUndo(where, removedString);
 124         replace(where, nitems, empty, 0, 0);
 125         if (marks != null) {
 126             updateMarksForRemove(where, nitems);
 127         }
 128         return edit;
 129 
 130     }
 131 
 132     /**
 133      * Retrieves a portion of the content.  where + len must be &lt;= length().
 134      *
 135      * @param where the starting position &gt;= 0
 136      * @param len the length to retrieve &gt;= 0
 137      * @return a string representing the content; may be empty
 138      * @exception BadLocationException if the specified position is invalid
 139      * @see AbstractDocument.Content#getString
 140      */
 141     public String getString(int where, int len) throws BadLocationException {
 142         if (where + len > count) {
 143             throw new BadLocationException("Invalid range", count);
 144         }
 145         return new String(data, where, len);
 146     }
 147 
 148     /**
 149      * Retrieves a portion of the content.  where + len must be &lt;= length()
 150      *
 151      * @param where the starting position &gt;= 0
 152      * @param len the number of characters to retrieve &gt;= 0
 153      * @param chars the Segment object to return the characters in
 154      * @exception BadLocationException if the specified position is invalid
 155      * @see AbstractDocument.Content#getChars
 156      */
 157     public void getChars(int where, int len, Segment chars) throws BadLocationException {
 158         if (where + len > count) {
 159             throw new BadLocationException("Invalid location", count);
 160         }
 161         chars.array = data;
 162         chars.offset = where;
 163         chars.count = len;
 164     }
 165 
 166     /**
 167      * Creates a position within the content that will
 168      * track change as the content is mutated.
 169      *
 170      * @param offset the offset to create a position for &gt;= 0
 171      * @return the position
 172      * @exception BadLocationException if the specified position is invalid
 173      */
 174     public Position createPosition(int offset) throws BadLocationException {
 175         // some small documents won't have any sticky positions
 176         // at all, so the buffer is created lazily.
 177         if (marks == null) {
 178             marks = new Vector<PosRec>();
 179         }
 180         return new StickyPosition(offset);
 181     }
 182 
 183     // --- local methods ---------------------------------------
 184 
 185     /**
 186      * Replaces some of the characters in the array
 187      * @param offset  offset into the array to start the replace
 188      * @param length  number of characters to remove
 189      * @param replArray replacement array
 190      * @param replOffset offset into the replacement array
 191      * @param replLength number of character to use from the
 192      *   replacement array.
 193      */
 194     void replace(int offset, int length,
 195                  char[] replArray, int replOffset, int replLength) {
 196         int delta = replLength - length;
 197         int src = offset + length;
 198         int nmove = count - src;
 199         int dest = src + delta;
 200         if ((count + delta) >= data.length) {
 201             // need to grow the array
 202             int newLength = Math.max(2*data.length, count + delta);
 203             char[] newData = new char[newLength];
 204             System.arraycopy(data, 0, newData, 0, offset);
 205             System.arraycopy(replArray, replOffset, newData, offset, replLength);
 206             System.arraycopy(data, src, newData, dest, nmove);
 207             data = newData;
 208         } else {
 209             // patch the existing array
 210             System.arraycopy(data, src, data, dest, nmove);
 211             System.arraycopy(replArray, replOffset, data, offset, replLength);
 212         }
 213         count = count + delta;
 214     }
 215 
 216     void resize(int ncount) {
 217         char[] ndata = new char[ncount];
 218         System.arraycopy(data, 0, ndata, 0, Math.min(ncount, count));
 219         data = ndata;
 220     }
 221 
 222     synchronized void updateMarksForInsert(int offset, int length) {
 223         if (offset == 0) {
 224             // zero is a special case where we update only
 225             // marks after it.
 226             offset = 1;
 227         }
 228         int n = marks.size();
 229         for (int i = 0; i < n; i++) {
 230             PosRec mark = marks.elementAt(i);
 231             if (mark.unused) {
 232                 // this record is no longer used, get rid of it
 233                 marks.removeElementAt(i);
 234                 i -= 1;
 235                 n -= 1;
 236             } else if (mark.offset >= offset) {
 237                 mark.offset += length;
 238             }
 239         }
 240     }
 241 
 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];
 316     private char[] data;
 317     private int count;
 318     transient Vector<PosRec> marks;
 319 
 320     /**
 321      * holds the data for a mark... separately from
 322      * the real mark so that the real mark can be
 323      * collected if there are no more references to
 324      * it.... the update table holds only a reference
 325      * to this grungy thing.
 326      */
 327     final class PosRec {
 328 
 329         PosRec(int offset) {
 330             this.offset = offset;
 331         }
 332 
 333         int offset;
 334         boolean unused;
 335     }
 336 
 337     /**
 338      * This really wants to be a weak reference but
 339      * in 1.1 we don't have a 100% pure solution for
 340      * this... so this class trys to hack a solution
 341      * to causing the marks to be collected.
 342      */
 343     final class StickyPosition implements Position {
 344 
 345         StickyPosition(int offset) {
 346             rec = new PosRec(offset);
 347             marks.addElement(rec);
 348         }
 349 
 350         public int getOffset() {
 351             return rec.offset;
 352         }
 353 
 354         protected void finalize() throws Throwable {
 355             // schedule the record to be removed later
 356             // on another thread.
 357             rec.unused = true;
 358         }
 359 
 360         public String toString() {
 361             return Integer.toString(getOffset());
 362         }
 363 
 364         PosRec rec;
 365     }
 366 
 367     /**
 368      * Used to hold a reference to a Position that is being reset as the
 369      * result of removing from the content.
 370      */
 371     final class UndoPosRef {
 372         UndoPosRef(PosRec rec) {
 373             this.rec = rec;
 374             this.undoLocation = rec.offset;
 375         }
 376 
 377         /**
 378          * Resets the location of the Position to the offset when the
 379          * receiver was instantiated.
 380          */
 381         protected void resetLocation() {
 382             rec.offset = undoLocation;
 383         }
 384 
 385         /** Location to reset to when resetLocatino is invoked. */
 386         protected int undoLocation;
 387         /** Position to reset offset. */
 388         protected PosRec rec;
 389     }
 390 
 391     /**
 392      * UnoableEdit created for inserts.
 393      */
 394     class InsertUndo extends AbstractUndoableEdit {
 395         protected InsertUndo(int offset, int length) {
 396             super();
 397             this.offset = offset;
 398             this.length = length;
 399         }
 400 
 401         public void undo() throws CannotUndoException {
 402             super.undo();
 403             try {
 404                 synchronized(StringContent.this) {
 405                     // Get the Positions in the range being removed.
 406                     if(marks != null)
 407                         posRefs = getPositionsInRange(null, offset, length);
 408                     string = getString(offset, length);
 409                     remove(offset, length);
 410                 }
 411             } catch (BadLocationException bl) {
 412               throw new CannotUndoException();
 413             }
 414         }
 415 
 416         public void redo() throws CannotRedoException {
 417             super.redo();
 418             try {
 419                 synchronized(StringContent.this) {
 420                     insertString(offset, string);
 421                     string = null;
 422                     // Update the Positions that were in the range removed.
 423                     if(posRefs != null) {
 424                         updateUndoPositions(posRefs);
 425                         posRefs = null;
 426                     }
 427               }
 428             } catch (BadLocationException bl) {
 429               throw new CannotRedoException();
 430             }
 431         }
 432 
 433         // Where the string goes.
 434         protected int offset;
 435         // Length of the string.
 436         protected int length;
 437         // The string that was inserted. To cut down on space needed this
 438         // will only be valid after an undo.
 439         protected String string;
 440         // An array of instances of UndoPosRef for the Positions in the
 441         // range that was removed, valid after undo.
 442         @SuppressWarnings("rawtypes") // UndoPosRef type cannot be exposed
 443         protected Vector posRefs;
 444     }
 445 
 446 
 447     /**
 448      * UndoableEdit created for removes.
 449      */
 450     class RemoveUndo extends AbstractUndoableEdit {
 451         @SuppressWarnings("unchecked")
 452         protected RemoveUndo(int offset, String string) {
 453             super();
 454             this.offset = offset;
 455             this.string = string;
 456             this.length = string.length();
 457             if(marks != null)
 458                 posRefs = getPositionsInRange(null, offset, length);
 459         }
 460 
 461         public void undo() throws CannotUndoException {
 462             super.undo();
 463             try {
 464                 synchronized(StringContent.this) {
 465                     insertString(offset, string);
 466                     // Update the Positions that were in the range removed.
 467                     if(posRefs != null) {
 468                         updateUndoPositions(posRefs);
 469                         posRefs = null;
 470                     }
 471                     string = null;
 472                 }
 473             } catch (BadLocationException bl) {
 474               throw new CannotUndoException();
 475             }
 476         }
 477 
 478         @SuppressWarnings("unchecked")
 479         public void redo() throws CannotRedoException {
 480             super.redo();
 481             try {
 482                 synchronized(StringContent.this) {
 483                     string = getString(offset, length);
 484                     // Get the Positions in the range being removed.
 485                     if(marks != null)
 486                         posRefs = getPositionsInRange(null, offset, length);
 487                     remove(offset, length);
 488                 }
 489             } catch (BadLocationException bl) {
 490               throw new CannotRedoException();
 491             }
 492         }
 493 
 494         // Where the string goes.
 495         protected int offset;
 496         // Length of the string.
 497         protected int length;
 498         // The string that was inserted. This will be null after an undo.
 499         protected String string;
 500         // An array of instances of UndoPosRef for the Positions in the
 501         // range that was removed, valid before undo.
 502         protected Vector<UndoPosRef> posRefs;
 503     }
 504 }