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