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