KickJava   Java API By Example, From Geeks To Geeks.

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


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 replace a range in a document with a different
20  * string.
21  *
22  * @since 3.0
23  */

24 public final class ReplaceEdit extends TextEdit {
25
26     private String JavaDoc fText;
27
28     /**
29      * Constructs a new replace edit.
30      *
31      * @param offset the offset of the range to replace
32      * @param length the length of the range to replace
33      * @param text the new text
34      */

35     public ReplaceEdit(int offset, int length, String JavaDoc text) {
36         super(offset, length);
37         Assert.isNotNull(text);
38         fText= text;
39     }
40
41     /*
42      * Copy constructor
43      *
44      * @param other the edit to copy from
45      */

46     private ReplaceEdit(ReplaceEdit other) {
47         super(other);
48         fText= other.fText;
49     }
50
51     /**
52      * Returns the new text replacing the text denoted
53      * by the edit.
54      *
55      * @return the edit's text.
56      */

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

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

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

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

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

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