1000537d65807d7a522cd5d3bf4161ee6ade928a
[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 org.eclipse.jdt.internal.corext.Assert;
10
11 public abstract class SimpleTextEdit extends TextEdit {
12
13         private TextRange fRange;
14         private String fText;
15
16         public static SimpleTextEdit createReplace(int offset, int length, String text) {
17                 return new SimpleTextEditImpl(offset, length, text);
18         }
19
20         public static SimpleTextEdit createInsert(int offset, String text) {
21                 return new SimpleTextEditImpl(offset, 0, text);
22         }
23         
24         public static SimpleTextEdit createDelete(int offset, int length) {
25                 return new SimpleTextEditImpl(offset, length, ""); //$NON-NLS-1$
26         }
27         
28         private final static class SimpleTextEditImpl extends SimpleTextEdit {
29                 protected SimpleTextEditImpl(TextRange range, String text) {
30                         super(range, text);
31                 }
32                 protected SimpleTextEditImpl(int offset, int length, String text) {
33                         super(offset, length, text);
34                 }
35                 public TextEdit copy() {
36                         return new SimpleTextEditImpl(getTextRange().copy(), getText());
37                 }       
38         }
39         
40         protected SimpleTextEdit() {
41                 this(TextRange.UNDEFINED, ""); //$NON-NLS-1$
42         }
43         
44         protected SimpleTextEdit(int offset, int length, String text) {
45                 this(new TextRange(offset, length), text);
46         }
47         protected SimpleTextEdit(TextRange range, String text) {
48         //      Assert.isNotNull(range);
49         //      Assert.isNotNull(text);
50                 fRange= range;
51                 fText= text;
52         }
53         
54         /**
55          * Returns the text edit's text
56          * 
57          * @return the text edit's text
58          */
59         public String getText() {
60                 return fText;
61         }
62                 
63         /**
64          * Sets the text edit's text
65          * <p>
66          * This method should only be called from within the <code>
67          * connect</code> method.
68          * 
69          * @param text the text edit's text
70          */     
71         protected final void setText(String text) {
72                 fText= text;
73 //              Assert.isNotNull(fText);
74         }
75         
76         /**
77          * Sets the text edit's range.
78          * <p>
79          * This method should only be called from within the <code>
80          * connect</code> method.
81          * 
82          * @param range the text edit's range.
83          */     
84         protected void setTextRange(TextRange range) {
85                 fRange= range;
86         //      Assert.isNotNull(fRange);
87         }
88         
89         /* non Java-doc
90          * @see TextEdit#getTextRange
91          */
92         public TextRange getTextRange() {
93                 return fRange;
94         }
95         
96         /* non Java-doc
97          * @see TextEdit#doPerform
98          */
99         public final TextEdit perform(TextBuffer buffer) throws CoreException {
100                 String current= buffer.getContent(fRange.fOffset, fRange.fLength);
101                 buffer.replace(fRange, fText);
102                 return new SimpleTextEditImpl(fRange, current);
103         }       
104 }
105