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 org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.jface.text.BadLocationException;
015: import org.eclipse.jface.text.IDocument;
016:
017: /**
018: * Text edit to replace a range in a document with a different
019: * string.
020: *
021: * @since 3.0
022: */
023: public final class ReplaceEdit extends TextEdit {
024:
025: private String fText;
026:
027: /**
028: * Constructs a new replace edit.
029: *
030: * @param offset the offset of the range to replace
031: * @param length the length of the range to replace
032: * @param text the new text
033: */
034: public ReplaceEdit(int offset, int length, String text) {
035: super (offset, length);
036: Assert.isNotNull(text);
037: fText = text;
038: }
039:
040: /*
041: * Copy constructor
042: *
043: * @param other the edit to copy from
044: */
045: private ReplaceEdit(ReplaceEdit other) {
046: super (other);
047: fText = other.fText;
048: }
049:
050: /**
051: * Returns the new text replacing the text denoted
052: * by the edit.
053: *
054: * @return the edit's text.
055: */
056: public String getText() {
057: return fText;
058: }
059:
060: /*
061: * @see TextEdit#doCopy
062: */
063: protected TextEdit doCopy() {
064: return new ReplaceEdit(this );
065: }
066:
067: /*
068: * @see TextEdit#accept0
069: */
070: protected void accept0(TextEditVisitor visitor) {
071: boolean visitChildren = visitor.visit(this );
072: if (visitChildren) {
073: acceptChildren(visitor);
074: }
075: }
076:
077: /*
078: * @see TextEdit#performDocumentUpdating
079: */
080: int performDocumentUpdating(IDocument document)
081: throws BadLocationException {
082: document.replace(getOffset(), getLength(), fText);
083: fDelta = fText.length() - getLength();
084: return fDelta;
085: }
086:
087: /*
088: * @see TextEdit#deleteChildren
089: */
090: boolean deleteChildren() {
091: return true;
092: }
093:
094: /*
095: * @see org.eclipse.text.edits.TextEdit#internalToString(java.lang.StringBuffer, int)
096: * @since 3.3
097: */
098: void internalToString(StringBuffer buffer, int indent) {
099: super .internalToString(buffer, indent);
100: buffer.append(" <<").append(fText); //$NON-NLS-1$
101: }
102: }
|