001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.text.edits;
011:
012: import java.util.List;
013:
014: import org.eclipse.core.runtime.Assert;
015:
016: import org.eclipse.jface.text.BadLocationException;
017: import org.eclipse.jface.text.IDocument;
018:
019: /**
020: * A copy target edit denotes the target of a copy operation. Copy
021: * target edits are only valid inside an edit tree if they have a
022: * corresponding source edit. Furthermore a target edit can't
023: * can't be a direct or indirect child of the associated source edit.
024: * Violating one of two requirements will result in a <code>
025: * MalformedTreeException</code> when executing the edit tree.
026: * <p>
027: * Copy target edits can't be used as a parent for other edits.
028: * Trying to add an edit to a copy target edit results in a <code>
029: * MalformedTreeException</code> as well.
030: *
031: * @see org.eclipse.text.edits.CopySourceEdit
032: *
033: * @since 3.0
034: */
035: public final class CopyTargetEdit extends TextEdit {
036:
037: private CopySourceEdit fSource;
038:
039: /**
040: * Constructs a new copy target edit
041: *
042: * @param offset the edit's offset
043: */
044: public CopyTargetEdit(int offset) {
045: super (offset, 0);
046: }
047:
048: /**
049: * Constructs an new copy target edit
050: *
051: * @param offset the edit's offset
052: * @param source the corresponding source edit
053: */
054: public CopyTargetEdit(int offset, CopySourceEdit source) {
055: this (offset);
056: setSourceEdit(source);
057: }
058:
059: /*
060: * Copy constructor
061: */
062: private CopyTargetEdit(CopyTargetEdit other) {
063: super (other);
064: }
065:
066: /**
067: * Returns the associated source edit or <code>null</code>
068: * if no source edit is associated yet.
069: *
070: * @return the source edit or <code>null</code>
071: */
072: public CopySourceEdit getSourceEdit() {
073: return fSource;
074: }
075:
076: /**
077: * Sets the source edit.
078: *
079: * @param edit the source edit
080: *
081: * @exception MalformedTreeException is thrown if the target edit
082: * is a direct or indirect child of the source edit
083: */
084: public void setSourceEdit(CopySourceEdit edit)
085: throws MalformedTreeException {
086: Assert.isNotNull(edit);
087: if (fSource != edit) {
088: fSource = edit;
089: fSource.setTargetEdit(this );
090: TextEdit parent = getParent();
091: while (parent != null) {
092: if (parent == fSource)
093: throw new MalformedTreeException(
094: parent,
095: this ,
096: TextEditMessages
097: .getString("CopyTargetEdit.wrong_parent")); //$NON-NLS-1$
098: parent = parent.getParent();
099: }
100: }
101: }
102:
103: /*
104: * @see TextEdit#doCopy
105: */
106: protected TextEdit doCopy() {
107: return new CopyTargetEdit(this );
108: }
109:
110: /*
111: * @see TextEdit#postProcessCopy
112: */
113: protected void postProcessCopy(TextEditCopier copier) {
114: if (fSource != null) {
115: CopyTargetEdit target = (CopyTargetEdit) copier
116: .getCopy(this );
117: CopySourceEdit source = (CopySourceEdit) copier
118: .getCopy(fSource);
119: if (target != null && source != null)
120: target.setSourceEdit(source);
121: }
122: }
123:
124: /*
125: * @see TextEdit#accept0
126: */
127: protected void accept0(TextEditVisitor visitor) {
128: boolean visitChildren = visitor.visit(this );
129: if (visitChildren) {
130: acceptChildren(visitor);
131: }
132: }
133:
134: /*
135: * @see TextEdit#traverseConsistencyCheck
136: */
137: int traverseConsistencyCheck(TextEditProcessor processor,
138: IDocument document, List sourceEdits) {
139: return super .traverseConsistencyCheck(processor, document,
140: sourceEdits) + 1;
141: }
142:
143: /*
144: * @see TextEdit#performConsistencyCheck
145: */
146: void performConsistencyCheck(TextEditProcessor processor,
147: IDocument document) throws MalformedTreeException {
148: if (fSource == null)
149: throw new MalformedTreeException(getParent(), this ,
150: TextEditMessages
151: .getString("CopyTargetEdit.no_source")); //$NON-NLS-1$
152: if (fSource.getTargetEdit() != this )
153: throw new MalformedTreeException(
154: getParent(),
155: this ,
156: TextEditMessages
157: .getString("CopyTargetEdit.different_target")); //$NON-NLS-1$
158: }
159:
160: /*
161: * @see TextEdit#performDocumentUpdating
162: */
163: int performDocumentUpdating(IDocument document)
164: throws BadLocationException {
165: String source = fSource.getContent();
166: document.replace(getOffset(), getLength(), source);
167: fDelta = source.length() - getLength();
168: fSource.clearContent();
169: return fDelta;
170: }
171:
172: /*
173: * @see TextEdit#deleteChildren
174: */
175: boolean deleteChildren() {
176: return false;
177: }
178: }
|