1 11 package org.eclipse.ui.internal.texteditor.quickdiff; 12 13 import java.util.List ; 14 import java.util.ListIterator ; 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 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 fList; 38 39 private final IDocument fDocument; 40 41 49 public DiffRegion(RangeDifference difference, int offset, List differences, IDocument source) { 50 super("org.eclipse.ui.workbench.texteditor.quickdiffChange", false, null); fOffset= offset; 52 fDifference= difference; 53 fList= differences; 54 fDocument= source; 55 } 56 57 60 public String getType() { 61 switch (getChangeType()) { 64 case CHANGED: 65 return "org.eclipse.ui.workbench.texteditor.quickdiffChange"; case ADDED: 67 return "org.eclipse.ui.workbench.texteditor.quickdiffAddition"; case UNCHANGED: 69 return "org.eclipse.ui.workbench.texteditor.quickdiffUnchanged"; default: 71 return TYPE_UNKNOWN; 72 } 73 } 74 75 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 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 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 114 public int getRemovedLinesAbove() { 115 if (getChangeType() == UNCHANGED && fOffset == 0) { 116 synchronized (fList) { 117 for (ListIterator 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 134 public boolean hasChanges() { 135 return getChangeType() != UNCHANGED || getRemovedLinesAbove() > 0 || getRemovedLinesBelow() > 0; 136 } 137 138 141 public String [] getOriginalText() { 142 IDocument doc= fDocument; 143 if (doc != null) { 144 int startLine= fDifference.leftStart() + fOffset; 145 if (startLine >= fDifference.leftEnd()) 146 return new String [0]; 149 int endLine= startLine + getRemovedLinesBelow(); 150 if (getChangeType() == UNCHANGED) 151 startLine++; 152 String [] ret= new String [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]= ""; } 159 } 160 return ret; 161 } 162 163 return new String [0]; 165 } 166 167 170 public String 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 changed= c > 0 ? NLSUtility.format(QuickDiffMessages.quickdiff_annotation_changed, new Integer (c)) : null; 176 String added; 177 if (a > 0) 178 added= NLSUtility.format(QuickDiffMessages.quickdiff_annotation_added, new Integer (a)); 179 else if (a < 0) 180 added= NLSUtility.format(QuickDiffMessages.quickdiff_annotation_deleted, new Integer (-a)); 181 else 182 added= null; 183 String line= c > 1 || c == 0 && Math.abs(a) > 1 ? QuickDiffMessages.quickdiff_annotation_line_plural : QuickDiffMessages.quickdiff_annotation_line_singular; 184 185 String ret= (changed != null ? changed : "") + (changed != null ? " " + line : "") + (changed != null && added != null ? ", " : " ") + (added != null ? added : "") + (added != null && changed == null ? " " + line : ""); return ret; 189 } 190 191 194 public RangeDifference getDifference() { 195 return fDifference; 196 } 197 198 201 public int getOffset() { 202 return fOffset; 203 } 204 } 205 | Popular Tags |