129
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.text.edits.MoveTargetEdit;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.ArrayList;
|
|
18 import java.util.List;
|
|
19
|
|
20 import dwtx.jface.text.BadLocationException;
|
|
21 import dwtx.jface.text.IDocument;
|
|
22
|
|
23 /**
|
|
24 * A move target edit denotes the target of a move operation. Move
|
|
25 * target edits are only valid inside an edit tree if they have a
|
|
26 * corresponding source edit. Furthermore a target edit can't
|
|
27 * can't be a direct or indirect child of its associated source edit.
|
|
28 * Violating one of two requirements will result in a <code>
|
|
29 * MalformedTreeException</code> when executing the edit tree.
|
|
30 * <p>
|
|
31 * Move target edits can't be used as a parent for other edits.
|
|
32 * Trying to add an edit to a move target edit results in a <code>
|
|
33 * MalformedTreeException</code> as well.
|
|
34 *
|
|
35 * @see dwtx.text.edits.MoveSourceEdit
|
|
36 * @see dwtx.text.edits.CopyTargetEdit
|
|
37 *
|
|
38 * @since 3.0
|
|
39 */
|
|
40 public final class MoveTargetEdit : TextEdit {
|
|
41
|
|
42 private MoveSourceEdit fSource;
|
|
43
|
|
44 /**
|
|
45 * Constructs a new move target edit
|
|
46 *
|
|
47 * @param offset the edit's offset
|
|
48 */
|
|
49 public MoveTargetEdit(int offset) {
|
|
50 super(offset, 0);
|
|
51 }
|
|
52
|
|
53 /**
|
|
54 * Constructs an new move target edit
|
|
55 *
|
|
56 * @param offset the edit's offset
|
|
57 * @param source the corresponding source edit
|
|
58 */
|
|
59 public MoveTargetEdit(int offset, MoveSourceEdit source) {
|
|
60 this(offset);
|
|
61 setSourceEdit(source);
|
|
62 }
|
|
63
|
|
64 /*
|
|
65 * Copy constructor
|
|
66 */
|
|
67 private MoveTargetEdit(MoveTargetEdit other) {
|
|
68 super(other);
|
|
69 }
|
|
70
|
|
71 /**
|
|
72 * Returns the associated source edit or <code>null</code>
|
|
73 * if no source edit is associated yet.
|
|
74 *
|
|
75 * @return the source edit or <code>null</code>
|
|
76 */
|
|
77 public MoveSourceEdit getSourceEdit() {
|
|
78 return fSource;
|
|
79 }
|
|
80
|
|
81 /**
|
|
82 * Sets the source edit.
|
|
83 *
|
|
84 * @param edit the source edit
|
|
85 *
|
|
86 * @exception MalformedTreeException is thrown if the target edit
|
|
87 * is a direct or indirect child of the source edit
|
|
88 */
|
|
89 public void setSourceEdit(MoveSourceEdit edit) {
|
|
90 if (fSource !is edit) {
|
|
91 fSource= edit;
|
|
92 fSource.setTargetEdit(this);
|
|
93 TextEdit parent= getParent();
|
|
94 while (parent !is null) {
|
|
95 if (parent is fSource)
|
|
96 throw new MalformedTreeException(parent, this, TextEditMessages.getString("MoveTargetEdit.wrong_parent")); //$NON-NLS-1$
|
|
97 parent= parent.getParent();
|
|
98 }
|
|
99 }
|
|
100 }
|
|
101
|
|
102 /*
|
|
103 * @see TextEdit#doCopy
|
|
104 */
|
|
105 protected TextEdit doCopy() {
|
|
106 return new MoveTargetEdit(this);
|
|
107 }
|
|
108
|
|
109 /*
|
|
110 * @see TextEdit#postProcessCopy
|
|
111 */
|
|
112 protected void postProcessCopy(TextEditCopier copier) {
|
|
113 if (fSource !is null) {
|
|
114 MoveTargetEdit target= (MoveTargetEdit)copier.getCopy(this);
|
|
115 MoveSourceEdit source= (MoveSourceEdit)copier.getCopy(fSource);
|
|
116 if (target !is null && source !is null)
|
|
117 target.setSourceEdit(source);
|
|
118 }
|
|
119 }
|
|
120
|
|
121 /*
|
|
122 * @see TextEdit#accept0
|
|
123 */
|
|
124 protected void accept0(TextEditVisitor visitor) {
|
|
125 bool visitChildren= visitor.visit(this);
|
|
126 if (visitChildren) {
|
|
127 acceptChildren(visitor);
|
|
128 }
|
|
129 }
|
|
130
|
|
131 //---- consistency check ----------------------------------------------------------
|
|
132
|
|
133 /*
|
|
134 * @see TextEdit#traverseConsistencyCheck
|
|
135 */
|
|
136 int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List sourceEdits) {
|
|
137 return super.traverseConsistencyCheck(processor, document, sourceEdits) + 1;
|
|
138 }
|
|
139
|
|
140 /*
|
|
141 * @see TextEdit#performConsistencyCheck
|
|
142 */
|
|
143 void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
|
|
144 if (fSource is null)
|
|
145 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveTargetEdit.no_source")); //$NON-NLS-1$
|
|
146 if (fSource.getTargetEdit() !is this)
|
|
147 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveTargetEdit.different_target")); //$NON-NLS-1$
|
|
148 }
|
|
149
|
|
150 //---- document updating ----------------------------------------------------------------
|
|
151
|
|
152 /*
|
|
153 * @see TextEdit#performDocumentUpdating
|
|
154 */
|
|
155 int performDocumentUpdating(IDocument document) throws BadLocationException {
|
|
156 String source= fSource.getContent();
|
|
157 document.replace(getOffset(), getLength(), source);
|
|
158 fDelta= source.length() - getLength();
|
|
159
|
|
160 MultiTextEdit sourceRoot= fSource.getSourceRoot();
|
|
161 if (sourceRoot !is null) {
|
|
162 sourceRoot.internalMoveTree(getOffset());
|
|
163 TextEdit[] sourceChildren= sourceRoot.removeChildren();
|
|
164 List children= new ArrayList(sourceChildren.length);
|
|
165 for (int i= 0; i < sourceChildren.length; i++) {
|
|
166 TextEdit child= sourceChildren[i];
|
|
167 child.internalSetParent(this);
|
|
168 children.add(child);
|
|
169 }
|
|
170 internalSetChildren(children);
|
|
171 }
|
|
172 fSource.clearContent();
|
|
173 return fDelta;
|
|
174 }
|
|
175
|
|
176 //---- region updating --------------------------------------------------------------
|
|
177
|
|
178 /*
|
|
179 * @see dwtx.text.edits.TextEdit#traversePassThree
|
|
180 */
|
|
181 int traverseRegionUpdating(TextEditProcessor processor, IDocument document, int accumulatedDelta, bool delete) {
|
|
182 // the children got already updated / normalized while they got removed
|
|
183 // from the source edit. So we only have to adjust the offset computed to
|
|
184 // far.
|
|
185 if (delete) {
|
|
186 deleteTree();
|
|
187 } else {
|
|
188 internalMoveTree(accumulatedDelta);
|
|
189 }
|
|
190 return accumulatedDelta + fDelta;
|
|
191 }
|
|
192
|
|
193 bool deleteChildren() {
|
|
194 return false;
|
|
195 }
|
|
196 }
|