KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

63     private CopyTargetEdit(CopyTargetEdit 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 CopySourceEdit 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(CopySourceEdit edit) throws MalformedTreeException {
86         Assert.isNotNull(edit);
87         if (fSource != edit) {
88             fSource= edit;
89             fSource.setTargetEdit(this);
90             TextEdit parent= getParent();
91             while (parent != null) {
92                 if (parent == fSource)
93                     throw new MalformedTreeException(parent, this, TextEditMessages.getString("CopyTargetEdit.wrong_parent")); //$NON-NLS-1$
94
parent= parent.getParent();
95             }
96         }
97     }
98
99     /*
100      * @see TextEdit#doCopy
101      */

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

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

121     protected void accept0(TextEditVisitor visitor) {
122         boolean visitChildren= visitor.visit(this);
123         if (visitChildren) {
124             acceptChildren(visitor);
125         }
126     }
127
128     /*
129      * @see TextEdit#traverseConsistencyCheck
130      */

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

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

148     int performDocumentUpdating(IDocument document) throws BadLocationException {
149         String JavaDoc source= fSource.getContent();
150         document.replace(getOffset(), getLength(), source);
151         fDelta= source.length() - getLength();
152         fSource.clearContent();
153         return fDelta;
154     }
155
156     /*
157      * @see TextEdit#deleteChildren
158      */

159     boolean deleteChildren() {
160         return false;
161     }
162 }
163
Popular Tags