KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > text > edits > TextEdit


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.text.edits;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.Region;
25
26 /**
27  * A text edit describes an elementary text manipulation operation. Edits are
28  * executed by applying them to a document (e.g. an instance of <code>IDocument
29  * </code>).
30  * <p>
31  * Text edits form a tree. Clients can navigate the tree upwards, from child to
32  * parent, as well as downwards. Newly created edits are un-parented. New edits
33  * are added to the tree by calling one of the <code>add</code> methods on a parent
34  * edit.
35  * </p>
36  * <p>
37  * An edit tree is well formed in the following sense:
38  * <ul>
39  * <li>a parent edit covers all its children</li>
40  * <li>children don't overlap</li>
41  * <li>an edit with length 0 can't have any children</li>
42  * </ul>
43  * Any manipulation of the tree that violates one of the above requirements results
44  * in a <code>MalformedTreeException</code>.
45  * </p>
46  * <p>
47  * Insert edits are represented by an edit of length 0. If more than one insert
48  * edit exists at the same offset then the edits are executed in the order in which
49  * they have been added to a parent. The following code example:
50  * <pre>
51  * IDocument document= new Document("org");
52  * MultiTextEdit edit= new MultiTextEdit();
53  * edit.addChild(new InsertEdit(0, "www."));
54  * edit.addChild(new InsertEdit(0, "eclipse."));
55  * edit.apply(document);
56  * </pre>
57  * therefore results in string: "www.eclipse.org".
58  * </p>
59  * <p>
60  * Text edits can be executed in a mode where the edit's region is updated to
61  * reflect the edit's position in the changed document. Region updating is enabled
62  * by default or can be requested by passing <code>UPDATE_REGIONS</code> to the
63  * {@link #apply(IDocument, int) apply(IDocument, int)} method. In the above example
64  * the region of the <code>InsertEdit(0, "eclipse.")</code> edit after executing
65  * the root edit is <code>[3, 8]</code>. If the region of an edit got deleted during
66  * change execution the region is set to <code>[-1, -1]</code> and the method {@link
67  * #isDeleted() isDeleted} returns <code>true</code>.
68  * </p>
69  * This class isn't intended to be subclassed outside of the edit framework. Clients
70  * are only allowed to subclass <code>MultiTextEdit</code>.
71  *
72  * @since 3.0
73  */

74 public abstract class TextEdit {
75
76     /**
77      * Flags indicating that neither <code>CREATE_UNDO</code> nor
78      * <code>UPDATE_REGIONS</code> is set.
79      */

80     public static final int NONE= 0;
81
82     /**
83      * Flags indicating that applying an edit tree to a document
84      * is supposed to create a corresponding undo edit. If not
85      * specified <code>null</code> is returned from method <code>
86      * apply</code>.
87      */

88     public static final int CREATE_UNDO= 1 << 0;
89
90     /**
91      * Flag indicating that the edit's region will be updated to
92      * reflect its position in the changed document. If not specified
93      * when applying an edit tree to a document the edit's region will
94      * be arbitrary. It is even not guaranteed that the tree is still
95      * well formed.
96      */

97     public static final int UPDATE_REGIONS= 1 << 1;
98
99     private static class InsertionComparator implements Comparator JavaDoc {
100         public int compare(Object JavaDoc o1, Object JavaDoc o2) throws MalformedTreeException {
101             TextEdit edit1= (TextEdit)o1;
102             TextEdit edit2= (TextEdit)o2;
103
104             int offset1= edit1.getOffset();
105             int length1= edit1.getLength();
106
107             int offset2= edit2.getOffset();
108             int length2= edit2.getLength();
109
110             if (offset1 == offset2 && length1 == 0 && length2 == 0) {
111                 return 0;
112             }
113             if (offset1 + length1 <= offset2) {
114                 return -1;
115             }
116             if (offset2 + length2 <= offset1) {
117                 return 1;
118             }
119             throw new MalformedTreeException(
120                     null, edit1,
121                     TextEditMessages.getString("TextEdit.overlapping")); //$NON-NLS-1$
122
}
123     }
124
125     private static final TextEdit[] EMPTY_ARRAY= new TextEdit[0];
126     private static final InsertionComparator INSERTION_COMPARATOR= new InsertionComparator();
127
128     private static final int DELETED_VALUE= -1;
129
130     private int fOffset;
131     private int fLength;
132
133     private TextEdit fParent;
134     private List JavaDoc fChildren;
135
136     int fDelta;
137
138     /**
139      * Create a new text edit. Parent is initialized to <code>
140      * null<code> and the edit doesn't have any children.
141      *
142      * @param offset the edit's offset
143      * @param length the edit's length
144      */

145     protected TextEdit(int offset, int length) {
146         Assert.isTrue(offset >= 0 && length >= 0);
147         fOffset= offset;
148         fLength= length;
149         fDelta= 0;
150     }
151
152     /**
153      * Copy constructor
154      *
155      * @param source the source to copy form
156      */

157     protected TextEdit(TextEdit source) {
158         fOffset= source.fOffset;
159         fLength= source.fLength;
160         fDelta= 0;
161     }
162
163     //---- Region management -----------------------------------------------
164

165     /**
166      * Returns the range that this edit is manipulating. The returned
167      * <code>IRegion</code> contains the edit's offset and length at
168      * the point in time when this call is made. Any subsequent changes
169      * to the edit's offset and length aren't reflected in the returned
170      * region object.
171      * <p>
172      * Creating a region for a deleted edit will result in an assertion
173      * failure.
174      *
175      * @return the manipulated region
176      */

177     public final IRegion getRegion() {
178         return new Region(getOffset(), getLength());
179     }
180
181     /**
182      * Returns the offset of the edit. An offset is a 0-based
183      * character index. Returns <code>-1</code> if the edit
184      * is marked as deleted.
185      *
186      * @return the offset of the edit
187      */

188     public int getOffset() {
189         return fOffset;
190     }
191
192     /**
193      * Returns the length of the edit. Returns <code>-1</code>
194      * if the edit is marked as deleted.
195      *
196      * @return the length of the edit
197      */

198     public int getLength() {
199         return fLength;
200     }
201
202     /**
203      * Returns the inclusive end position of this edit. The inclusive end
204      * position denotes the last character of the region manipulated by
205      * this edit. The returned value is the result of the following
206      * calculation:
207      * <pre>
208      * getOffset() + getLength() - 1;
209      * <pre>
210      *
211      * @return the inclusive end position
212      */

213     public final int getInclusiveEnd() {
214         return getOffset() + getLength() - 1;
215     }
216
217     /**
218      * Returns the exclusive end position of this edit. The exclusive end
219      * position denotes the next character of the region manipulated by
220      * this edit. The returned value is the result of the following
221      * calculation:
222      * <pre>
223      * getOffset() + getLength();
224      * </pre>
225      *
226      * @return the exclusive end position
227      */

228     public final int getExclusiveEnd() {
229         return getOffset() + getLength();
230     }
231
232     /**
233      * Returns whether this edit has been deleted or not.
234      *
235      * @return <code>true</code> if the edit has been
236      * deleted; otherwise <code>false</code> is returned.
237      */

238     public final boolean isDeleted() {
239         return fOffset == DELETED_VALUE && fLength == DELETED_VALUE;
240     }
241
242     /**
243      * Move all offsets in the tree by the given delta. This node must be a
244      * root node. The resulting offsets must be greater or equal to zero.
245      *
246      * @param delta the delta
247      * @since 3.1
248      */

249     public final void moveTree(int delta) {
250         Assert.isTrue(fParent == null);
251         Assert.isTrue(getOffset() + delta >= 0);
252         internalMoveTree(delta);
253     }
254
255     /**
256      * Returns <code>true</code> if the edit covers the given edit
257      * <code>other</code>. It is up to the concrete text edit to
258      * decide if a edit of length zero can cover another edit.
259      *
260      * @param other the other edit
261      * @return <code>true<code> if the edit covers the other edit;
262      * otherwise <code>false</code> is returned.
263      */

264     public boolean covers(TextEdit other) {
265         if (getLength() == 0 && !canZeroLengthCover())
266             return false;
267
268         if (!other.isDefined())
269             return true;
270
271         int thisOffset= getOffset();
272         int otherOffset= other.getOffset();
273         return thisOffset <= otherOffset && otherOffset + other.getLength() <= thisOffset + getLength();
274     }
275
276     /**
277      * Returns <code>true</code> if an edit with length zero can cover
278      * another edit. Returns <code>false</code> otherwise.
279      *
280      * @return whether an edit of length zero can cover another edit
281      */

282     protected boolean canZeroLengthCover() {
283         return false;
284     }
285
286     /**
287      * Returns whether the region of this edit is defined or not.
288      *
289      * @return whether the region of this edit is defined or not
290      *
291      * @since 3.1
292      */

293     boolean isDefined() {
294         return true;
295     }
296
297     //---- parent and children management -----------------------------
298

299     /**
300      * Returns the edit's parent. The method returns <code>null</code>
301      * if this edit hasn't been add to another edit.
302      *
303      * @return the edit's parent
304      */

305     public final TextEdit getParent() {
306         return fParent;
307     }
308
309     /**
310      * Returns the root edit of the edit tree.
311      *
312      * @return the root edit of the edit tree
313      * @since 3.1
314      */

315     public final TextEdit getRoot() {
316         TextEdit result= this;
317         while (result.fParent != null) {
318             result= result.fParent;
319         }
320         return result;
321     }
322
323     /**
324      * Adds the given edit <code>child</code> to this edit.
325      *
326      * @param child the child edit to add
327      * @exception MalformedTreeException is thrown if the child
328      * edit can't be added to this edit. This is the case if the child
329      * overlaps with one of its siblings or if the child edit's region
330      * isn't fully covered by this edit.
331      */

332     public final void addChild(TextEdit child) throws MalformedTreeException {
333         internalAdd(child);
334     }
335
336     /**
337      * Adds all edits in <code>edits</code> to this edit.
338      *
339      * @param edits the text edits to add
340      * @exception MalformedTreeException is thrown if one of
341      * the given edits can't be added to this edit.
342      *
343      * @see #addChild(TextEdit)
344      */

345     public final void addChildren(TextEdit[] edits) throws MalformedTreeException {
346         for (int i= 0; i < edits.length; i++) {
347             internalAdd(edits[i]);
348         }
349     }
350
351     /**
352      * Removes the edit specified by the given index from the list
353      * of children. Returns the child edit that was removed from
354      * the list of children. The parent of the returned edit is
355      * set to <code>null</code>.
356      *
357      * @param index the index of the edit to remove
358      * @return the removed edit
359      * @exception IndexOutOfBoundsException if the index
360      * is out of range
361      */

362     public final TextEdit removeChild(int index) {
363         if (fChildren == null)
364             throw new IndexOutOfBoundsException JavaDoc("Index: " + index + " Size: 0"); //$NON-NLS-1$//$NON-NLS-2$
365
TextEdit result= (TextEdit)fChildren.remove(index);
366         result.internalSetParent(null);
367         if (fChildren.isEmpty())
368             fChildren= null;
369         return result;
370     }
371
372     /**
373      * Removes the first occurrence of the given child from the list
374      * of children.
375      *
376      * @param child the child to be removed
377      * @return <code>true</code> if the edit contained the given
378      * child; otherwise <code>false</code> is returned
379      */

380     public final boolean removeChild(TextEdit child) {
381         Assert.isNotNull(child);
382         if (fChildren == null)
383             return false;
384         boolean result= fChildren.remove(child);
385         if (result) {
386             child.internalSetParent(null);
387             if (fChildren.isEmpty())
388                 fChildren= null;
389         }
390         return result;
391     }
392
393     /**
394      * Removes all child edits from and returns them. The parent
395      * of the removed edits is set to <code>null</code>.
396      *
397      * @return an array of the removed edits
398      */

399     public final TextEdit[] removeChildren() {
400         if (fChildren == null)
401             return EMPTY_ARRAY;
402         int size= fChildren.size();
403         TextEdit[] result= new TextEdit[size];
404         for (int i= 0; i < size; i++) {
405             result[i]= (TextEdit)fChildren.get(i);
406             result[i].internalSetParent(null);
407         }
408         fChildren= null;
409         return result;
410     }
411
412     /**
413      * Returns <code>true</code> if this edit has children. Otherwise
414      * <code>false</code> is returned.
415      *
416      * @return <code>true</code> if this edit has children; otherwise
417      * <code>false</code> is returned
418      */

419     public final boolean hasChildren() {
420         return fChildren != null && ! fChildren.isEmpty();
421     }
422
423     /**
424      * Returns the edit's children. If the edit doesn't have any
425      * children an empty array is returned.
426      *
427      * @return the edit's children
428      */

429     public final TextEdit[] getChildren() {
430         if (fChildren == null)
431             return EMPTY_ARRAY;
432         return (TextEdit[])fChildren.toArray(new TextEdit[fChildren.size()]);
433     }
434
435     /**
436      * Returns the size of the managed children.
437      *
438      * @return the size of the children
439      */

440     public final int getChildrenSize() {
441         if (fChildren == null)
442             return 0;
443         return fChildren.size();
444     }
445
446     /**
447      * Returns the text range spawned by the given array of text edits.
448      * The method requires that the given array contains at least one
449      * edit. If all edits passed are deleted the method returns <code>
450      * null</code>.
451      *
452      * @param edits an array of edits
453      * @return the text range spawned by the given array of edits or
454      * <code>null</code> if all edits are marked as deleted
455      */

456     public static IRegion getCoverage(TextEdit[] edits) {
457         Assert.isTrue(edits != null && edits.length > 0);
458
459         int offset= Integer.MAX_VALUE;
460         int end= Integer.MIN_VALUE;
461         int deleted= 0;
462         for (int i= 0; i < edits.length; i++) {
463             TextEdit edit= edits[i];
464             if (edit.isDeleted()) {
465                 deleted++;
466             } else {
467                 offset= Math.min(offset, edit.getOffset());
468                 end= Math.max(end, edit.getExclusiveEnd());
469             }
470         }
471         if (edits.length == deleted)
472             return null;
473
474         return new Region(offset, end - offset);
475     }
476
477     /*
478      * Hook called before this edit gets added to the passed
479      * parent.
480      */

481     void aboutToBeAdded(TextEdit parent) {
482     }
483
484     //---- Object methods ------------------------------------------------------
485

486     /**
487      * The <code>Edit</code> implementation of this <code>Object</code>
488      * method uses object identity (==).
489      *
490      * @param obj the other object
491      * @return <code>true</code> iff <code>this == obj</code>; otherwise
492      * <code>false</code> is returned
493      *
494      * @see Object#equals(java.lang.Object)
495      */

496     public final boolean equals(Object JavaDoc obj) {
497         return this == obj; // equivalent to Object.equals
498
}
499
500     /**
501      * The <code>Edit</code> implementation of this <code>Object</code>
502      * method calls uses <code>Object#hashCode()</code> to compute its
503      * hash code.
504      *
505      * @return the object's hash code value
506      *
507      * @see Object#hashCode()
508      */

509     public final int hashCode() {
510         return super.hashCode();
511     }
512
513     /*
514      * @see java.lang.Object#toString()
515      */

516     public String JavaDoc toString() {
517         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
518         toStringWithChildren(buffer, 0);
519         return buffer.toString();
520     }
521
522     /**
523      * Adds the string representation of this text edit without
524      * children information to the given string buffer.
525      *
526      * @param buffer the string buffer
527      * @param indent the indent level in number of spaces
528      * @since 3.3
529      */

530     void internalToString(StringBuffer JavaDoc buffer, int indent) {
531         for (int i= indent; i > 0; i--) {
532             buffer.append(" "); //$NON-NLS-1$
533
}
534         buffer.append("{"); //$NON-NLS-1$
535
String JavaDoc name= getClass().getName();
536         int index= name.lastIndexOf('.');
537         if (index != -1) {
538             buffer.append(name.substring(index + 1));
539         } else {
540             buffer.append(name);
541         }
542         buffer.append("} "); //$NON-NLS-1$
543
if (isDeleted()) {
544             buffer.append("[deleted]"); //$NON-NLS-1$
545
} else {
546             buffer.append("["); //$NON-NLS-1$
547
buffer.append(getOffset());
548             buffer.append(","); //$NON-NLS-1$
549
buffer.append(getLength());
550             buffer.append("]"); //$NON-NLS-1$
551
}
552     }
553
554     /**
555      * Adds the string representation for this text edit
556      * and its children to the given string buffer.
557      *
558      * @param buffer the string buffer
559      * @param indent the indent level in number of spaces
560      * @since 3.3
561      */

562     private void toStringWithChildren(StringBuffer JavaDoc buffer, int indent) {
563         internalToString(buffer, indent);
564         if (fChildren != null) {
565             for (Iterator JavaDoc iterator= fChildren.iterator(); iterator.hasNext();) {
566                 TextEdit child= (TextEdit) iterator.next();
567                 buffer.append('\n');
568                 child.toStringWithChildren(buffer, indent + 1);
569             }
570         }
571     }
572     
573     //---- Copying -------------------------------------------------------------
574

575     /**
576      * Creates a deep copy of the edit tree rooted at this
577      * edit.
578      *
579      * @return a deep copy of the edit tree
580      * @see #doCopy()
581      */

582     public final TextEdit copy() {
583         TextEditCopier copier= new TextEditCopier(this);
584         return copier.perform();
585     }
586
587     /**
588      * Creates and returns a copy of this edit. The copy method should be
589      * implemented in a way so that the copy can executed without causing
590      * any harm to the original edit. Implementors of this method are
591      * responsible for creating deep or shallow copies of referenced
592      * object to fulfill this requirement.
593      * <p>
594      * Implementers of this method should use the copy constructor <code>
595      * Edit#Edit(Edit source) to initialize the edit part of the copy.
596      * Implementors aren't responsible to actually copy the children or
597      * to set the right parent.
598      * <p>
599      * This method <b>should not be called</b> from outside the framework.
600      * Please use <code>copy</code> to create a copy of a edit tree.
601      *
602      * @return a copy of this edit.
603      * @see #copy()
604      * @see #postProcessCopy(TextEditCopier)
605      * @see TextEditCopier
606      */

607     protected abstract TextEdit doCopy();
608
609     /**
610      * This method is called on every edit of the copied tree to do some
611      * post-processing like connected an edit to a different edit in the tree.
612      * <p>
613      * This default implementation does nothing
614      *
615      * @param copier the copier that manages a map between original and
616      * copied edit.
617      * @see TextEditCopier
618      */

619     protected void postProcessCopy(TextEditCopier copier) {
620     }
621
622     //---- Visitor support -------------------------------------------------
623

624     /**
625      * Accepts the given visitor on a visit of the current edit.
626      *
627      * @param visitor the visitor object
628      * @exception IllegalArgumentException if the visitor is null
629      */

630     public final void accept(TextEditVisitor visitor) {
631         Assert.isNotNull(visitor);
632         // begin with the generic pre-visit
633
visitor.preVisit(this);
634         // dynamic dispatch to internal method for type-specific visit/endVisit
635
accept0(visitor);
636         // end with the generic post-visit
637
visitor.postVisit(this);
638     }
639
640     /**
641      * Accepts the given visitor on a type-specific visit of the current edit.
642      * This method must be implemented in all concrete text edits.
643      * <p>
644      * General template for implementation on each concrete TextEdit class:
645      * <pre>
646      * <code>
647      * boolean visitChildren= visitor.visit(this);
648      * if (visitChildren) {
649      * acceptChildren(visitor);
650      * }
651      * </code>
652      * </pre>
653      * Note that the caller (<code>accept</code>) takes care of invoking
654      * <code>visitor.preVisit(this)</code> and <code>visitor.postVisit(this)</code>.
655      * </p>
656      *
657      * @param visitor the visitor object
658      */

659     protected abstract void accept0(TextEditVisitor visitor);
660
661
662     /**
663      * Accepts the given visitor on the edits children.
664      * <p>
665      * This method must be used by the concrete implementations of
666      * <code>accept</code> to traverse list-values properties; it
667      * encapsulates the proper handling of on-the-fly changes to the list.
668      * </p>
669      *
670      * @param visitor the visitor object
671      */

672     protected final void acceptChildren(TextEditVisitor visitor) {
673         if (fChildren == null)
674             return;
675         Iterator JavaDoc iterator= fChildren.iterator();
676         while (iterator.hasNext()) {
677             TextEdit curr= (TextEdit) iterator.next();
678             curr.accept(visitor);
679         }
680     }
681
682     //---- Execution -------------------------------------------------------
683

684     /**
685      * Applies the edit tree rooted by this edit to the given document. To check
686      * if the edit tree can be applied to the document either catch <code>
687      * MalformedTreeException</code> or use <code>TextEditProcessor</code> to
688      * execute an edit tree.
689      *
690      * @param document the document to be manipulated
691      * @param style flags controlling the execution of the edit tree. Valid
692      * flags are: <code>CREATE_UNDO</code> and </code>UPDATE_REGIONS</code>.
693      * @return a undo edit, if <code>CREATE_UNDO</code> is specified. Otherwise
694      * <code>null</code> is returned.
695      *
696      * @exception MalformedTreeException is thrown if the tree isn't
697      * in a valid state. This exception is thrown before any edit
698      * is executed. So the document is still in its original state.
699      * @exception BadLocationException is thrown if one of the edits
700      * in the tree can't be executed. The state of the document is
701      * undefined if this exception is thrown.
702      *
703      * @see TextEditProcessor#performEdits()
704      */

705     public final UndoEdit apply(IDocument document, int style) throws MalformedTreeException, BadLocationException {
706         try {
707             TextEditProcessor processor= new TextEditProcessor(document, this, style);
708             return processor.performEdits();
709         } finally {
710             // disconnect from text edit processor
711
fParent= null;
712         }
713     }
714
715     /**
716      * Applies the edit tree rooted by this edit to the given document. This
717      * method is a convenience method for <code>apply(document, CREATE_UNDO | UPDATE_REGIONS)
718      * </code>
719      *
720      * @param document the document to which to apply this edit
721      * @return a undo edit, if <code>CREATE_UNDO</code> is specified. Otherwise
722      * <code>null</code> is returned.
723      * @exception MalformedTreeException is thrown if the tree isn't
724      * in a valid state. This exception is thrown before any edit
725      * is executed. So the document is still in its original state.
726      * @exception BadLocationException is thrown if one of the edits
727      * in the tree can't be executed. The state of the document is
728      * undefined if this exception is thrown.
729      * @see #apply(IDocument, int)
730      */

731     public final UndoEdit apply(IDocument document) throws MalformedTreeException, BadLocationException {
732         return apply(document, CREATE_UNDO | UPDATE_REGIONS);
733     }
734
735     UndoEdit dispatchPerformEdits(TextEditProcessor processor) throws BadLocationException {
736         return processor.executeDo();
737     }
738
739     void dispatchCheckIntegrity(TextEditProcessor processor) throws MalformedTreeException {
740         processor.checkIntegrityDo();
741     }
742
743     //---- internal state accessors ----------------------------------------------------------
744

745     void internalSetParent(TextEdit parent) {
746         if (parent != null)
747             Assert.isTrue(fParent == null);
748         fParent= parent;
749     }
750
751     void internalSetOffset(int offset) {
752         Assert.isTrue(offset >= 0);
753         fOffset= offset;
754     }
755
756     void internalSetLength(int length) {
757         Assert.isTrue(length >= 0);
758         fLength= length;
759     }
760
761     List JavaDoc internalGetChildren() {
762         return fChildren;
763     }
764
765     void internalSetChildren(List JavaDoc children) {
766         fChildren= children;
767     }
768
769     void internalAdd(TextEdit child) throws MalformedTreeException {
770         child.aboutToBeAdded(this);
771         if (child.isDeleted())
772             throw new MalformedTreeException(this, child, TextEditMessages.getString("TextEdit.deleted_edit")); //$NON-NLS-1$
773
if (!covers(child))
774             throw new MalformedTreeException(this, child, TextEditMessages.getString("TextEdit.range_outside")); //$NON-NLS-1$
775
if (fChildren == null) {
776             fChildren= new ArrayList JavaDoc(2);
777         }
778         int index= computeInsertionIndex(child);
779         fChildren.add(index, child);
780         child.internalSetParent(this);
781     }
782
783     private int computeInsertionIndex(TextEdit edit) throws MalformedTreeException {
784         int size= fChildren.size();
785         if (size == 0)
786             return 0;
787         int lastIndex= size - 1;
788         TextEdit last= (TextEdit)fChildren.get(lastIndex);
789         if (last.getExclusiveEnd() <= edit.getOffset())
790             return size;
791         try {
792
793             int index= Collections.binarySearch(fChildren, edit, INSERTION_COMPARATOR);
794
795             if (index < 0)
796                 // edit is not in fChildren
797
return -index - 1;
798
799             // edit is already in fChildren
800
// make sure that multiple insertion points at the same offset are inserted last.
801
while (index < lastIndex && INSERTION_COMPARATOR.compare(fChildren.get(index), fChildren.get(index + 1)) == 0)
802                 index++;
803
804             return index + 1;
805
806         } catch(MalformedTreeException e) {
807             e.setParent(this);
808             throw e;
809         }
810     }
811
812     //---- Offset & Length updating -------------------------------------------------
813

814     /**
815      * Adjusts the edits offset according to the given
816      * delta. This method doesn't update any children.
817      *
818      * @param delta the delta of the text replace operation
819      */

820     void adjustOffset(int delta) {
821         if (isDeleted())
822             return;
823         fOffset+= delta;
824         Assert.isTrue(fOffset >= 0);
825     }
826
827     /**
828      * Adjusts the edits length according to the given
829      * delta. This method doesn't update any children.
830      *
831      * @param delta the delta of the text replace operation
832      */

833     void adjustLength(int delta) {
834         if (isDeleted())
835             return;
836         fLength+= delta;
837         Assert.isTrue(fLength >= 0);
838     }
839
840     /**
841      * Marks the edit as deleted. This method doesn't update
842      * any children.
843      */

844     void markAsDeleted() {
845         fOffset= DELETED_VALUE;
846         fLength= DELETED_VALUE;
847     }
848
849     //---- Edit processing ----------------------------------------------
850

851     /**
852      * Traverses the edit tree to perform the consistency check.
853      *
854      * @param processor the text edit processor
855      * @param document the document to be manipulated
856      * @param sourceEdits the list of source edits to be performed before
857      * the actual tree is applied to the document
858      *
859      * @return the number of indirect move or copy target edit children
860      */

861     int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List JavaDoc sourceEdits) {
862         int result= 0;
863         if (fChildren != null) {
864             for (int i= fChildren.size() - 1; i >= 0; i--) {
865                 TextEdit child= (TextEdit)fChildren.get(i);
866                 result= Math.max(result, child.traverseConsistencyCheck(processor, document, sourceEdits));
867             }
868         }
869         if (processor.considerEdit(this)) {
870             performConsistencyCheck(processor, document);
871         }
872         return result;
873     }
874
875     void performConsistencyCheck(TextEditProcessor processor, IDocument document) {
876     }
877
878     void traverseSourceComputation(TextEditProcessor processor, IDocument document) {
879     }
880
881     void performSourceComputation(TextEditProcessor processor, IDocument document) {
882     }
883
884     int traverseDocumentUpdating(TextEditProcessor processor, IDocument document) throws BadLocationException {
885         int delta= 0;
886         if (fChildren != null) {
887             for (int i= fChildren.size() - 1; i >= 0; i--) {
888                 TextEdit child= (TextEdit)fChildren.get(i);
889                 delta+= child.traverseDocumentUpdating(processor, document);
890                 childDocumentUpdated();
891             }
892         }
893         if (processor.considerEdit(this)) {
894             if (delta != 0)
895                 adjustLength(delta);
896             int r= performDocumentUpdating(document);
897             if (r != 0)
898                 adjustLength(r);
899             delta+= r;
900         }
901         return delta;
902     }
903
904     /**
905      * Hook method called when the document updating of a child edit has been
906      * completed. When a client calls {@link #apply(IDocument)} or
907      * {@link #apply(IDocument, int)} this method is called
908      * {@link #getChildrenSize()} times.
909      * <p>
910      * May be overridden by subclasses of {@link MultiTextEdit}.
911      *
912      * @since 3.1
913      */

914     protected void childDocumentUpdated() {
915     }
916
917     abstract int performDocumentUpdating(IDocument document) throws BadLocationException;
918
919     int traverseRegionUpdating(TextEditProcessor processor, IDocument document, int accumulatedDelta, boolean delete) {
920         performRegionUpdating(accumulatedDelta, delete);
921         if (fChildren != null) {
922             boolean childDelete= delete || deleteChildren();
923             for (Iterator JavaDoc iter= fChildren.iterator(); iter.hasNext();) {
924                 TextEdit child= (TextEdit)iter.next();
925                 accumulatedDelta= child.traverseRegionUpdating(processor, document, accumulatedDelta, childDelete);
926                 childRegionUpdated();
927             }
928         }
929         return accumulatedDelta + fDelta;
930     }
931
932     /**
933      * Hook method called when the region updating of a child edit has been
934      * completed. When a client calls {@link #apply(IDocument)} this method is
935      * called {@link #getChildrenSize()} times. When calling
936      * {@link #apply(IDocument, int)} this method is called
937      * {@link #getChildrenSize()} times, when the style parameter contains the
938      * {@link #UPDATE_REGIONS} flag.
939      * <p>
940      * May be overridden by subclasses of {@link MultiTextEdit}.
941      *
942      * @since 3.1
943      */

944     protected void childRegionUpdated() {
945     }
946
947     void performRegionUpdating(int accumulatedDelta, boolean delete) {
948         if (delete)
949             markAsDeleted();
950         else
951             adjustOffset(accumulatedDelta);
952     }
953
954     abstract boolean deleteChildren();
955
956     void internalMoveTree(int delta) {
957         adjustOffset(delta);
958         if (fChildren != null) {
959             for (Iterator JavaDoc iter= fChildren.iterator(); iter.hasNext();) {
960                 ((TextEdit)iter.next()).internalMoveTree(delta);
961             }
962         }
963     }
964
965     void deleteTree() {
966         markAsDeleted();
967         if (fChildren != null) {
968             for (Iterator JavaDoc iter= fChildren.iterator(); iter.hasNext();) {
969                 TextEdit child= (TextEdit)iter.next();
970                 child.deleteTree();
971             }
972         }
973     }
974 }
975
976
Popular Tags