KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17
18 /**
19  * Text edit to insert a text at a given position in a
20  * document.
21  * <p>
22  * An insert edit is equivalent to <code>ReplaceEdit(offset, 0, text)
23  * </code>
24  *
25  * @since 3.0
26  */

27 public final class InsertEdit extends TextEdit {
28
29     private String JavaDoc fText;
30
31     /**
32      * Constructs a new insert edit.
33      *
34      * @param offset the insertion offset
35      * @param text the text to insert
36      */

37     public InsertEdit(int offset, String JavaDoc text) {
38         super(offset, 0);
39         Assert.isNotNull(text);
40         fText= text;
41     }
42
43     /*
44      * Copy constructor
45      */

46     private InsertEdit(InsertEdit other) {
47         super(other);
48         fText= other.fText;
49     }
50
51     /**
52      * Returns the text to be inserted.
53      *
54      * @return the edit's text.
55      */

56     public String JavaDoc getText() {
57         return fText;
58     }
59
60     /*
61      * @see TextEdit#doCopy
62      */

63     protected TextEdit doCopy() {
64         return new InsertEdit(this);
65     }
66
67     /*
68      * @see TextEdit#accept0
69      */

70     protected void accept0(TextEditVisitor visitor) {
71         boolean visitChildren= visitor.visit(this);
72         if (visitChildren) {
73             acceptChildren(visitor);
74         }
75     }
76
77     /*
78      * @see TextEdit#performDocumentUpdating
79      */

80     int performDocumentUpdating(IDocument document) throws BadLocationException {
81         document.replace(getOffset(), getLength(), fText);
82         fDelta= fText.length() - getLength();
83         return fDelta;
84     }
85
86     /*
87      * @see TextEdit#deleteChildren
88      */

89     boolean deleteChildren() {
90         return false;
91     }
92
93     /*
94      * @see org.eclipse.text.edits.TextEdit#internalToString(java.lang.StringBuffer, int)
95      * @since 3.3
96      */

97     void internalToString(StringBuffer JavaDoc buffer, int indent) {
98         super.internalToString(buffer, indent);
99         buffer.append(" <<").append(fText); //$NON-NLS-1$
100
}
101 }
102
Popular Tags