KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > quickdiff > DiffRegion


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.ui.internal.texteditor.quickdiff;
12
13 import java.util.List JavaDoc;
14 import java.util.ListIterator JavaDoc;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.source.Annotation;
19 import org.eclipse.jface.text.source.ILineDiffInfo;
20
21 import org.eclipse.ui.internal.texteditor.NLSUtility;
22 import org.eclipse.ui.internal.texteditor.quickdiff.compare.rangedifferencer.RangeDifference;
23
24
25 /**
26  * The local implementation of <code>ILineDiffInfo</code>. As instances are
27  * also <code>Annotation</code>s, they can be used in
28  * <code>DocumentLineDiffer</code>s <code>IAnnotationModel</code> protocol.
29  *
30  * @since 3.0
31  */

32 public final class DiffRegion extends Annotation implements ILineDiffInfo {
33     private final RangeDifference fDifference;
34
35     private final int fOffset;
36
37     private final List JavaDoc fList;
38
39     private final IDocument fDocument;
40
41     /**
42      * Creates a new diff region.
43      *
44      * @param difference
45      * @param offset
46      * @param differences
47      * @param source
48      */

49     public DiffRegion(RangeDifference difference, int offset, List JavaDoc differences, IDocument source) {
50         super("org.eclipse.ui.workbench.texteditor.quickdiffChange", false, null); //$NON-NLS-1$
51
fOffset= offset;
52         fDifference= difference;
53         fList= differences;
54         fDocument= source;
55     }
56
57     /*
58      * @see org.eclipse.jface.text.source.Annotation#getType()
59      */

60     public String JavaDoc getType() {
61         // we return unknown for unchanged regions to avoid
62
// them getting displayed.
63
switch (getChangeType()) {
64             case CHANGED:
65                 return "org.eclipse.ui.workbench.texteditor.quickdiffChange"; //$NON-NLS-1$
66
case ADDED:
67                 return "org.eclipse.ui.workbench.texteditor.quickdiffAddition"; //$NON-NLS-1$
68
case UNCHANGED:
69                 return "org.eclipse.ui.workbench.texteditor.quickdiffUnchanged"; //$NON-NLS-1$
70
default:
71                 return TYPE_UNKNOWN;
72         }
73     }
74
75     /*
76      * @see org.eclipse.jface.text.source.ILineDiffInfo#getRemovedLinesBelow()
77      */

78     public int getRemovedLinesBelow() {
79         if (fOffset == fDifference.rightLength() - 1) {
80
81             if (getChangeType() != UNCHANGED)
82                 return Math.max(fDifference.leftLength() - fDifference.rightLength(), 0);
83
84             synchronized (fList) {
85                 for (ListIterator JavaDoc it= fList.listIterator(); it.hasNext();) {
86                     if (fDifference.equals(it.next())) {
87                         if (it.hasNext()) {
88                             RangeDifference next= (RangeDifference) it.next();
89                             if (next.rightLength() == 0)
90                                 return Math.max(next.leftLength() - next.rightLength(), 0);
91                         }
92                         break;
93                     }
94                 }
95             }
96         }
97         return 0;
98     }
99
100     /*
101      * @see org.eclipse.jface.text.source.ILineDiffInfo#getChangeType()
102      */

103     public int getChangeType() {
104         if (fDifference.kind() == RangeDifference.NOCHANGE)
105             return UNCHANGED;
106         if (fOffset >= fDifference.leftLength())
107             return ADDED;
108         return CHANGED;
109     }
110
111     /*
112      * @see org.eclipse.jface.text.source.ILineDiffInfo#getRemovedLinesAbove()
113      */

114     public int getRemovedLinesAbove() {
115         if (getChangeType() == UNCHANGED && fOffset == 0) {
116             synchronized (fList) {
117                 for (ListIterator JavaDoc it= fList.listIterator(fList.size()); it.hasPrevious();) {
118                     if (fDifference.equals(it.previous())) {
119                         if (it.hasPrevious()) {
120                             RangeDifference previous= (RangeDifference) it.previous();
121                             return Math.max(previous.leftLength() - previous.rightLength(), 0);
122                         }
123                         break;
124                     }
125                 }
126             }
127         }
128         return 0;
129     }
130
131     /*
132      * @see org.eclipse.jface.text.source.ILineDiffInfo#hasChanges()
133      */

134     public boolean hasChanges() {
135         return getChangeType() != UNCHANGED || getRemovedLinesAbove() > 0 || getRemovedLinesBelow() > 0;
136     }
137
138     /*
139      * @see org.eclipse.jface.text.source.ILineDiffInfo#getOriginalText()
140      */

141     public String JavaDoc[] getOriginalText() {
142         IDocument doc= fDocument;
143         if (doc != null) {
144             int startLine= fDifference.leftStart() + fOffset;
145             if (startLine >= fDifference.leftEnd())
146                 return new String JavaDoc[0]; // original text of an added line is
147
// empty
148

149             int endLine= startLine + getRemovedLinesBelow();
150             if (getChangeType() == UNCHANGED)
151                 startLine++;
152             String JavaDoc[] ret= new String JavaDoc[endLine - startLine + 1];
153             for (int i= 0; i < ret.length; i++) {
154                 try {
155                     ret[i]= doc.get(doc.getLineOffset(startLine + i), doc.getLineLength(startLine + i));
156                 } catch (BadLocationException e) {
157                     ret[i]= ""; //$NON-NLS-1$
158
}
159             }
160             return ret;
161         }
162
163         // in initialization phase?
164
return new String JavaDoc[0];
165     }
166
167     /*
168      * @see org.eclipse.jface.text.source.Annotation#getText()
169      */

170     public String JavaDoc getText() {
171         int r= fDifference.rightLength();
172         int l= fDifference.leftLength();
173         int c= Math.min(r, l);
174         int a= r - l;
175         String JavaDoc changed= c > 0 ? NLSUtility.format(QuickDiffMessages.quickdiff_annotation_changed, new Integer JavaDoc(c)) : null;
176         String JavaDoc added;
177         if (a > 0)
178             added= NLSUtility.format(QuickDiffMessages.quickdiff_annotation_added, new Integer JavaDoc(a));
179         else if (a < 0)
180             added= NLSUtility.format(QuickDiffMessages.quickdiff_annotation_deleted, new Integer JavaDoc(-a));
181         else
182             added= null;
183         String JavaDoc line= c > 1 || c == 0 && Math.abs(a) > 1 ? QuickDiffMessages.quickdiff_annotation_line_plural : QuickDiffMessages.quickdiff_annotation_line_singular;
184
185         String JavaDoc ret= (changed != null ? changed : "") + (changed != null ? " " + line : "") //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
186
+ (changed != null && added != null ? ", " : " ") + (added != null ? added : "") //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
187
+ (added != null && changed == null ? " " + line : ""); //$NON-NLS-1$//$NON-NLS-2$
188
return ret;
189     }
190
191     /**
192      * @return Returns the difference.
193      */

194     public RangeDifference getDifference() {
195         return fDifference;
196     }
197
198     /**
199      * @return Returns the offset.
200      */

201     public int getOffset() {
202         return fOffset;
203     }
204 }
205
Popular Tags