KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18
19 /**
20  * A move target edit denotes the target of a move operation. Move
21  * target edits are only valid inside an edit tree if they have a
22  * corresponding source edit. Furthermore a target edit can't
23  * can't be a direct or indirect child of its associated source edit.
24  * Violating one of two requirements will result in a <code>
25  * MalformedTreeException</code> when executing the edit tree.
26  * <p>
27  * Move target edits can't be used as a parent for other edits.
28  * Trying to add an edit to a move target edit results in a <code>
29  * MalformedTreeException</code> as well.
30  *
31  * @see org.eclipse.text.edits.MoveSourceEdit
32  * @see org.eclipse.text.edits.CopyTargetEdit
33  *
34  * @since 3.0
35  */

36 public final class MoveTargetEdit extends TextEdit {
37
38     private MoveSourceEdit fSource;
39
40     /**
41      * Constructs a new move target edit
42      *
43      * @param offset the edit's offset
44      */

45     public MoveTargetEdit(int offset) {
46         super(offset, 0);
47     }
48
49     /**
50      * Constructs an new move target edit
51      *
52      * @param offset the edit's offset
53      * @param source the corresponding source edit
54      */

55     public MoveTargetEdit(int offset, MoveSourceEdit source) {
56         this(offset);
57         setSourceEdit(source);
58     }
59
60     /*
61      * Copy constructor
62      */

63     private MoveTargetEdit(MoveTargetEdit other) {
64         super(other);
65     }
66
67     /**
68      * Returns the associated source edit or <code>null</code>
69      * if no source edit is associated yet.
70      *
71      * @return the source edit or <code>null</code>
72      */

73     public MoveSourceEdit getSourceEdit() {
74         return fSource;
75     }
76
77     /**
78      * Sets the source edit.
79      *
80      * @param edit the source edit
81      *
82      * @exception MalformedTreeException is thrown if the target edit
83      * is a direct or indirect child of the source edit
84      */

85     public void setSourceEdit(MoveSourceEdit edit) {
86         if (fSource != edit) {
87             fSource= edit;
88             fSource.setTargetEdit(this);
89             TextEdit parent= getParent();
90             while (parent != null) {
91                 if (parent == fSource)
92                     throw new MalformedTreeException(parent, this, TextEditMessages.getString("MoveTargetEdit.wrong_parent")); //$NON-NLS-1$
93
parent= parent.getParent();
94             }
95         }
96     }
97
98     /*
99      * @see TextEdit#doCopy
100      */

101     protected TextEdit doCopy() {
102         return new MoveTargetEdit(this);
103     }
104
105     /*
106      * @see TextEdit#postProcessCopy
107      */

108     protected void postProcessCopy(TextEditCopier copier) {
109         if (fSource != null) {
110             MoveTargetEdit target= (MoveTargetEdit)copier.getCopy(this);
111             MoveSourceEdit source= (MoveSourceEdit)copier.getCopy(fSource);
112             if (target != null && source != null)
113                 target.setSourceEdit(source);
114         }
115     }
116
117     /*
118      * @see TextEdit#accept0
119      */

120     protected void accept0(TextEditVisitor visitor) {
121         boolean visitChildren= visitor.visit(this);
122         if (visitChildren) {
123             acceptChildren(visitor);
124         }
125     }
126
127     //---- consistency check ----------------------------------------------------------
128

129     /*
130      * @see TextEdit#traverseConsistencyCheck
131      */

132     int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List JavaDoc sourceEdits) {
133         return super.traverseConsistencyCheck(processor, document, sourceEdits) + 1;
134     }
135
136     /*
137      * @see TextEdit#performConsistencyCheck
138      */

139     void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
140         if (fSource == null)
141             throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveTargetEdit.no_source")); //$NON-NLS-1$
142
if (fSource.getTargetEdit() != this)
143             throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveTargetEdit.different_target")); //$NON-NLS-1$
144
}
145
146     //---- document updating ----------------------------------------------------------------
147

148     /*
149      * @see TextEdit#performDocumentUpdating
150      */

151     int performDocumentUpdating(IDocument document) throws BadLocationException {
152         String JavaDoc source= fSource.getContent();
153         document.replace(getOffset(), getLength(), source);
154         fDelta= source.length() - getLength();
155
156         MultiTextEdit sourceRoot= fSource.getSourceRoot();
157         if (sourceRoot != null) {
158             sourceRoot.internalMoveTree(getOffset());
159             TextEdit[] sourceChildren= sourceRoot.removeChildren();
160             List JavaDoc children= new ArrayList JavaDoc(sourceChildren.length);
161             for (int i= 0; i < sourceChildren.length; i++) {
162                 TextEdit child= sourceChildren[i];
163                 child.internalSetParent(this);
164                 children.add(child);
165             }
166             internalSetChildren(children);
167         }
168         fSource.clearContent();
169         return fDelta;
170     }
171
172     //---- region updating --------------------------------------------------------------
173

174     /*
175      * @see org.eclipse.text.edits.TextEdit#traversePassThree
176      */

177     int traverseRegionUpdating(TextEditProcessor processor, IDocument document, int accumulatedDelta, boolean delete) {
178         // the children got already updated / normalized while they got removed
179
// from the source edit. So we only have to adjust the offset computed to
180
// far.
181
if (delete) {
182             deleteTree();
183         } else {
184             internalMoveTree(accumulatedDelta);
185         }
186         return accumulatedDelta + fDelta;
187     }
188
189     boolean deleteChildren() {
190         return false;
191     }
192 }
193
Popular Tags