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
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.List;
|
|
18
|
|
19 import dwtx.core.runtime.Assert;
|
|
20 import dwtx.jface.text.BadLocationException;
|
|
21 import dwtx.jface.text.IDocument;
|
|
22
|
|
23 /**
|
|
24 * A copy target edit denotes the target of a copy operation. Copy
|
|
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 the 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 * Copy target edits can't be used as a parent for other edits.
|
|
32 * Trying to add an edit to a copy target edit results in a <code>
|
|
33 * MalformedTreeException</code> as well.
|
|
34 *
|
|
35 * @see dwtx.text.edits.CopySourceEdit
|
|
36 *
|
|
37 * @since 3.0
|
|
38 */
|
|
39 public final class CopyTargetEdit : TextEdit {
|
|
40
|
|
41 private CopySourceEdit fSource;
|
|
42
|
|
43 /**
|
|
44 * Constructs a new copy target edit
|
|
45 *
|
|
46 * @param offset the edit's offset
|
|
47 */
|
|
48 public CopyTargetEdit(int offset) {
|
|
49 super(offset, 0);
|
|
50 }
|
|
51
|
|
52 /**
|
|
53 * Constructs an new copy target edit
|
|
54 *
|
|
55 * @param offset the edit's offset
|
|
56 * @param source the corresponding source edit
|
|
57 */
|
130
|
58 public this(int offset, CopySourceEdit source) {
|
129
|
59 this(offset);
|
|
60 setSourceEdit(source);
|
|
61 }
|
|
62
|
|
63 /*
|
|
64 * Copy constructor
|
|
65 */
|
130
|
66 private this(CopyTargetEdit other) {
|
129
|
67 super(other);
|
|
68 }
|
|
69
|
|
70 /**
|
|
71 * Returns the associated source edit or <code>null</code>
|
|
72 * if no source edit is associated yet.
|
|
73 *
|
|
74 * @return the source edit or <code>null</code>
|
|
75 */
|
|
76 public CopySourceEdit getSourceEdit() {
|
|
77 return fSource;
|
|
78 }
|
|
79
|
|
80 /**
|
|
81 * Sets the source edit.
|
|
82 *
|
|
83 * @param edit the source edit
|
|
84 *
|
|
85 * @exception MalformedTreeException is thrown if the target edit
|
|
86 * is a direct or indirect child of the source edit
|
|
87 */
|
|
88 public void setSourceEdit(CopySourceEdit edit) throws MalformedTreeException {
|
|
89 Assert.isNotNull(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("CopyTargetEdit.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 CopyTargetEdit(this);
|
|
107 }
|
|
108
|
|
109 /*
|
|
110 * @see TextEdit#postProcessCopy
|
|
111 */
|
|
112 protected void postProcessCopy(TextEditCopier copier) {
|
|
113 if (fSource !is null) {
|
|
114 CopyTargetEdit target= (CopyTargetEdit)copier.getCopy(this);
|
|
115 CopySourceEdit source= (CopySourceEdit)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 /*
|
|
132 * @see TextEdit#traverseConsistencyCheck
|
|
133 */
|
|
134 int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List sourceEdits) {
|
|
135 return super.traverseConsistencyCheck(processor, document, sourceEdits) + 1;
|
|
136 }
|
|
137
|
|
138 /*
|
|
139 * @see TextEdit#performConsistencyCheck
|
|
140 */
|
|
141 void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
|
|
142 if (fSource is null)
|
|
143 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopyTargetEdit.no_source")); //$NON-NLS-1$
|
|
144 if (fSource.getTargetEdit() !is this)
|
|
145 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopyTargetEdit.different_target")); //$NON-NLS-1$
|
|
146 }
|
|
147
|
|
148 /*
|
|
149 * @see TextEdit#performDocumentUpdating
|
|
150 */
|
|
151 int performDocumentUpdating(IDocument document) throws BadLocationException {
|
|
152 String source= fSource.getContent();
|
|
153 document.replace(getOffset(), getLength(), source);
|
|
154 fDelta= source.length() - getLength();
|
|
155 fSource.clearContent();
|
|
156 return fDelta;
|
|
157 }
|
|
158
|
|
159 /*
|
|
160 * @see TextEdit#deleteChildren
|
|
161 */
|
|
162 bool deleteChildren() {
|
|
163 return false;
|
|
164 }
|
|
165 }
|