e5a19beb660c4156ea27869d5da18988f74c6c68
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / textmanipulation / SimpleTextEdit.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.textmanipulation;
6
7 import org.eclipse.core.runtime.CoreException;
8
9 // import net.sourceforge.phpdt.internal.corext.Assert;
10
11 public abstract class SimpleTextEdit extends TextEdit {
12
13         private TextRange fRange;
14
15         private String fText;
16
17         public static SimpleTextEdit createReplace(int offset, int length,
18                         String text) {
19                 return new SimpleTextEditImpl(offset, length, text);
20         }
21
22         public static SimpleTextEdit createInsert(int offset, String text) {
23                 return new SimpleTextEditImpl(offset, 0, text);
24         }
25
26         public static SimpleTextEdit createDelete(int offset, int length) {
27                 return new SimpleTextEditImpl(offset, length, ""); //$NON-NLS-1$
28         }
29
30         private final static class SimpleTextEditImpl extends SimpleTextEdit {
31                 protected SimpleTextEditImpl(TextRange range, String text) {
32                         super(range, text);
33                 }
34
35                 protected SimpleTextEditImpl(int offset, int length, String text) {
36                         super(offset, length, text);
37                 }
38
39                 public TextEdit copy() {
40                         return new SimpleTextEditImpl(getTextRange().copy(), getText());
41                 }
42         }
43
44         protected SimpleTextEdit() {
45                 this(TextRange.UNDEFINED, ""); //$NON-NLS-1$
46         }
47
48         protected SimpleTextEdit(int offset, int length, String text) {
49                 this(new TextRange(offset, length), text);
50         }
51
52         protected SimpleTextEdit(TextRange range, String text) {
53                 // Assert.isNotNull(range);
54                 // Assert.isNotNull(text);
55                 fRange = range;
56                 fText = text;
57         }
58
59         /**
60          * Returns the text edit's text
61          * 
62          * @return the text edit's text
63          */
64         public String getText() {
65                 return fText;
66         }
67
68         /**
69          * Sets the text edit's text
70          * <p>
71          * This method should only be called from within the <code>
72          * connect</code>
73          * method.
74          * 
75          * @param text
76          *            the text edit's text
77          */
78         protected final void setText(String text) {
79                 fText = text;
80                 // Assert.isNotNull(fText);
81         }
82
83         /**
84          * Sets the text edit's range.
85          * <p>
86          * This method should only be called from within the <code>
87          * connect</code>
88          * method.
89          * 
90          * @param range
91          *            the text edit's range.
92          */
93         protected void setTextRange(TextRange range) {
94                 fRange = range;
95                 // Assert.isNotNull(fRange);
96         }
97
98         /*
99          * non Java-doc
100          * 
101          * @see TextEdit#getTextRange
102          */
103         public TextRange getTextRange() {
104                 return fRange;
105         }
106
107         /*
108          * non Java-doc
109          * 
110          * @see TextEdit#doPerform
111          */
112         public final TextEdit perform(TextBuffer buffer) throws CoreException {
113                 String current = buffer.getContent(fRange.fOffset, fRange.fLength);
114                 buffer.replace(fRange, fText);
115                 return new SimpleTextEditImpl(fRange, current);
116         }
117 }