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