Refactory: Remove UI from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / textmanipulation / MoveTextEdit.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 /**
12  * A text edit that moves text inside a text buffer.
13  */
14 public final class MoveTextEdit extends TextEdit {
15
16         /* package */static class TargetMark extends NopTextEdit {
17                 private MoveTextEdit fMoveTextEdit;
18
19                 public TargetMark(TextRange range, MoveTextEdit edit) {
20                         super(range);
21                         fMoveTextEdit = edit;
22                 }
23
24                 /* package */MoveTextEdit getMoveTextEdit() {
25                         return fMoveTextEdit;
26                 }
27
28                 public TextEdit perform(TextBuffer buffer) throws CoreException {
29                         fMoveTextEdit.internalPerform(buffer);
30                         return super.perform(buffer);
31                 }
32
33                 public TextEdit copy() {
34                         // Assert.isTrue(false, "This should never happen"); //$NON-NLS-1$
35                         return super.copy();
36                 }
37         }
38
39         private TextRange fTarget;
40
41         private TextRange fSource;
42
43         private int fPerformCounter;
44
45         /**
46          * Creates a new <code>MoveTextEdit</code>. The text edit doesn't support
47          * overlapping moves. So for a
48          * <code>MoveTextEdit</code> <code>destination &lt;= offset && 
49          * offset + length - 1 &lt;= destination</code>
50          * must be <code>true</code>.
51          * 
52          * @param offset
53          *            the offset of the text to be moved
54          * @param length
55          *            the text length to be moved
56          * @param destination
57          *            the destination offset
58          */
59         public MoveTextEdit(int offset, int length, int destination) {
60                 // Assert.isTrue(destination <= offset || offset + length <=
61                 // destination);
62                 fSource = new TextRange(offset, length);
63                 fTarget = new TextRange(destination);
64         }
65
66         /**
67          * Creates a new <code>MoveTextEdit</code> with the given source and
68          * target range.
69          * 
70          * @param source
71          *            the source
72          * @param target
73          *            the target
74          */
75         private MoveTextEdit(TextRange source, TextRange target) {
76                 fSource = source;
77                 fTarget = target;
78         }
79
80         /**
81          * Returns the move text edit's source range. This method returns the same
82          * range as <code>TextEdit#getTextRange()</code>
83          * 
84          * @return the edit's source range
85          */
86         public TextRange getSourceRange() {
87                 return fSource;
88         }
89
90         /**
91          * Returns the move text edit's target range.
92          * 
93          * @return the edit's target range
94          */
95         public TextRange getTargetRange() {
96                 return fTarget;
97         }
98
99         /*
100          * non Java-doc
101          * 
102          * @see TextEdit#getTextRange()
103          */
104         public TextRange getTextRange() {
105                 return fSource;
106         }
107
108         /*
109          * non Java-doc
110          * 
111          * @see TextEdit#connect(TextBufferEditor)
112          */
113         public void connect(TextBufferEditor editor) throws CoreException {
114                 editor.add(new TargetMark(fTarget, this));
115         }
116
117         /*
118          * non Java-doc
119          * 
120          * @see TextEdit#perform(TextBuffer)
121          */
122         public TextEdit perform(TextBuffer buffer) throws CoreException {
123                 internalPerform(buffer);
124                 return new MoveTextEdit(fTarget, fSource);
125         }
126
127         /*
128          * non Java-doc
129          * 
130          * @see TextEdit#copy()
131          */
132         public TextEdit copy() {
133                 TextRange source = getSourceRange();
134                 TextRange target = getTargetRange();
135                 return new MoveTextEdit(source.fOffset, source.fLength, target.fOffset);
136         }
137
138         // ---- Helper method
139         // ---------------------------------------------------------------------------------
140
141         private void internalPerform(TextBuffer buffer) throws CoreException {
142                 // Assert.isTrue(fPerformCounter < 2);
143                 if (++fPerformCounter == 2) {
144                         TextRange source = getSourceRange();
145                         TextRange target = getTargetRange();
146                         String current = buffer.getContent(source.fOffset, source.fLength);
147                         buffer.replace(source, ""); //$NON-NLS-1$
148                         buffer.replace(target, current);
149                 }
150         }
151
152         /* package */boolean isUpMove() {
153                 return fSource.fOffset < fTarget.fOffset;
154         }
155
156         /* package */boolean isDownMove() {
157                 return fSource.fOffset > fTarget.fOffset;
158         }
159
160         /* package */TextRange getChildRange() {
161                 int offset = fSource.fOffset;
162                 int length = fSource.fLength;
163                 int destination = fTarget.fOffset;
164                 if (destination <= offset)
165                         return new TextRange(destination, offset + length - destination);
166                 else
167                         return new TextRange(offset, destination - offset);
168         }
169 }