KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > revisions > ChangeRegion


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.jface.internal.text.revisions;
12
13 import java.util.LinkedList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.ListIterator JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.jface.text.revisions.Revision;
20 import org.eclipse.jface.text.source.ILineRange;
21 import org.eclipse.jface.text.source.LineRange;
22
23 /**
24  * A change region describes a contiguous range of lines that was changed in the same revision of a
25  * document. Once it is adjusted to diff information, the originally contiguous range may be split
26  * into several ranges or even be empty.
27  *
28  * @since 3.2
29  */

30 public final class ChangeRegion {
31     private final Revision fRevision;
32     private final ILineRange fLines;
33     private final List JavaDoc fAdjusted= new LinkedList JavaDoc();
34     
35     /**
36      * Creates a new change region for the given revision and line range.
37      *
38      * @param revision the revision of the new region
39      * @param lines the line range of the new region
40      * @throws IndexOutOfBoundsException if the line range is not well-formed
41      */

42     public ChangeRegion(Revision revision, ILineRange lines) throws IndexOutOfBoundsException JavaDoc {
43         Assert.isLegal(revision != null);
44         Assert.isLegal(lines != null);
45         fLines= Range.copy(lines);
46         fRevision=revision;
47         clearDiff();
48     }
49     
50     /**
51      * Returns the revision that this region belongs to.
52      *
53      * @return the revision that this region belongs to
54      */

55     public Revision getRevision() {
56         return fRevision;
57     }
58
59     /**
60      * Returns the original (before applying diff information) line range of this change region.
61      *
62      * @return the original (before applying diff information) line range of this change region
63      */

64     public ILineRange getOriginalRange() {
65         return fLines;
66     }
67
68     /**
69      * Returns the list of {@link ILineRange}s of this change region for which the revision
70      * information is still valid.
71      *
72      * @return the list of adjusted line ranges
73      */

74     public List JavaDoc getAdjustedRanges() {
75         return fAdjusted;
76     }
77     
78     /**
79      * Returns the line coverage of the adjusted ranges, an empty range if the coverage is empty.
80      *
81      * @return the line coverage of the adjusted ranges
82      */

83     public ILineRange getAdjustedCoverage() {
84         if (fAdjusted.isEmpty())
85             return new LineRange(fLines.getStartLine(), 0);
86         
87         Range first= (Range) fAdjusted.get(0);
88         Range last= (Range) fAdjusted.get(fAdjusted.size() - 1);
89         
90         return Range.createAbsolute(first.start(), last.end());
91     }
92     
93     /**
94      * Clears any adjusted ranges, restoring the original range.
95      */

96     public void clearDiff() {
97         fAdjusted.clear();
98         fAdjusted.add(Range.copy(fLines));
99     }
100     
101     /**
102      * Adjusts this change region to a diff hunk. This will change the adjusted ranges.
103      *
104      * @param hunk the diff hunk to adjust to
105      */

106     public void adjustTo(Hunk hunk) {
107         for (ListIterator JavaDoc it= fAdjusted.listIterator(); it.hasNext();) {
108             Range range= (Range) it.next();
109             
110             // do we need a split?
111
int unchanged= getUnchanged(hunk, range.start());
112             if (unchanged > 0) {
113                 if (unchanged >= range.length())
114                     continue;
115                 range= range.split(unchanged);
116                 it.add(range);
117                 it.previous(); it.next(); // needed so we can remove below
118
}
119             
120             int line= range.start();
121             Assert.isTrue(hunk.line <= line);
122             
123             // by how much do we shrink?
124
int overlap= getOverlap(hunk, line);
125             if (overlap >= range.length()) {
126                 it.remove();
127                 continue;
128             }
129             
130             // by how much do we move?
131
range.moveBy(hunk.delta + overlap);
132             range.resizeBy(-overlap);
133         }
134         
135     }
136     
137     private int getUnchanged(Hunk hunk, int line) {
138         return Math.max(0, hunk.line - line);
139     }
140
141     /*
142      * Returns the number of lines after line that the hunk reports as changed.
143      */

144     private int getOverlap(Hunk hunk, int line) {
145         
146         int deltaLine= hunk.line + hunk.changed;
147         if (hunk.delta >= 0) {
148             if (deltaLine <= line)
149                 return 0;
150             return deltaLine - line;
151         }
152         
153         // hunk.delta < 0
154
int hunkEnd= deltaLine - hunk.delta;
155         int cutCount= hunkEnd - line;
156         return Math.max(0, cutCount);
157     }
158
159     /*
160      * @see java.lang.Object#toString()
161      */

162     public String JavaDoc toString() {
163         return "ChangeRegion [" + fRevision.toString() + ", [" + fLines.getStartLine() + "+" + fLines.getNumberOfLines() + ")]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
164
}
165 }
166
Popular Tags