1 11 package org.eclipse.compare.structuremergeviewer; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.InputStream ; 15 import java.util.ArrayList ; 16 17 import org.eclipse.compare.*; 18 import org.eclipse.compare.contentmergeviewer.IDocumentRange; 19 import org.eclipse.compare.internal.CompareUIPlugin; 20 import org.eclipse.compare.internal.Utilities; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.jface.text.*; 23 import org.eclipse.swt.widgets.Shell; 24 25 26 50 public class DocumentRangeNode 51 implements IDocumentRange, IStructureComparator, IEditableContent, IEncodedStreamContentAccessor, IAdaptable, IEditableContentExtension { 52 53 private static final String UTF_16= "UTF-16"; 55 private IDocument fBaseDocument; 56 private Position fRange; private int fTypeCode; 58 private String fID; 59 private Position fAppendPosition; private ArrayList fChildren; 61 private final DocumentRangeNode fParent; 62 63 75 public DocumentRangeNode(int typeCode, String id, IDocument document, int start, int length) { 76 this(null, typeCode, id, document, start, length); 77 } 78 79 93 public DocumentRangeNode(DocumentRangeNode parent, int typeCode, String id, IDocument document, int start, int length) { 94 fParent = parent; 95 fTypeCode= typeCode; 96 fID= id; 97 fBaseDocument= document; 98 registerPositionUpdater(start, length); 99 } 100 101 private void registerPositionUpdater(int start, int length) { 102 fBaseDocument.addPositionCategory(RANGE_CATEGORY); 103 fRange= new Position(start, length); 104 try { 105 fBaseDocument.addPosition(RANGE_CATEGORY, fRange); 106 } catch (BadPositionCategoryException ex) { 107 CompareUIPlugin.log(ex); 108 } catch (BadLocationException ex) { 109 CompareUIPlugin.log(ex); 110 } 111 } 112 113 116 public IDocument getDocument() { 117 return fBaseDocument; 118 } 119 120 123 public Position getRange() { 124 return fRange; 125 } 126 127 133 public int getTypeCode() { 134 return fTypeCode; 135 } 136 137 143 public String getId() { 144 return fID; 145 } 146 147 153 public void setId(String id) { 154 fID= id; 155 } 156 157 162 public void addChild(DocumentRangeNode node) { 163 if (fChildren == null) 164 fChildren= new ArrayList (); 165 fChildren.add(node); 166 } 167 168 171 public Object [] getChildren() { 172 if (fChildren != null) 173 return fChildren.toArray(); 174 return new Object [0]; 175 } 176 177 182 public void setLength(int length) { 183 getRange().setLength(length); 184 } 185 186 196 public void setAppendPosition(int pos) { 197 if (fAppendPosition != null) 198 try { 199 fBaseDocument.removePosition(RANGE_CATEGORY, fAppendPosition); 200 } catch (BadPositionCategoryException e) { 201 } 203 try { 204 if (pos <= getDocument().getLength()) { 206 Position p= new Position(pos); 207 fBaseDocument.addPosition(RANGE_CATEGORY, p); 208 fAppendPosition= p; 209 } 210 } catch (BadPositionCategoryException ex) { 211 } catch (BadLocationException ex) { 213 } 215 } 216 217 225 public Position getAppendPosition() { 226 if (fAppendPosition == null) { 227 try { 228 Position p= new Position(fBaseDocument.getLength()); 229 fBaseDocument.addPosition(RANGE_CATEGORY, p); 230 fAppendPosition= p; 231 return fAppendPosition; 232 } catch (BadPositionCategoryException ex) { 233 } catch (BadLocationException ex) { 235 } 237 } 238 return new Position(fBaseDocument.getLength()); 239 } 240 241 246 public boolean equals(Object other) { 247 if (other != null && other.getClass() == getClass()) { 248 DocumentRangeNode tn= (DocumentRangeNode) other; 249 return fTypeCode == tn.fTypeCode && fID.equals(tn.fID); 250 } 251 return super.equals(other); 252 } 253 254 258 public int hashCode() { 259 return fID.hashCode(); 260 } 261 262 265 private Position findCorrespondingPosition(DocumentRangeNode otherParent, DocumentRangeNode child) { 266 267 269 if (child != null && fChildren != null) { 270 int ix= otherParent.fChildren.indexOf(child); 271 if (ix >= 0) { 272 273 for (int i= ix - 1; i >= 0; i--) { 274 DocumentRangeNode c1= (DocumentRangeNode) otherParent.fChildren.get(i); 275 int i2= fChildren.indexOf(c1); 276 if (i2 >= 0) { 277 DocumentRangeNode c= (DocumentRangeNode) fChildren.get(i2); 278 Position p= c.fRange; 280 281 Position po= new Position(p.getOffset() + p.getLength() + 1, 0); 283 return po; 285 } 289 } 290 291 for (int i= ix; i < otherParent.fChildren.size(); i++) { 292 DocumentRangeNode c1= (DocumentRangeNode) otherParent.fChildren.get(i); 293 int i2= fChildren.indexOf(c1); 294 if (i2 >= 0) { 295 DocumentRangeNode c= (DocumentRangeNode) fChildren.get(i2); 296 Position p= c.fRange; 298 Position po= new Position(p.getOffset(), 0); 300 return po; 302 } 306 } 307 308 } 309 } 310 return getAppendPosition(); 311 } 312 313 private void add(String s, DocumentRangeNode parent, DocumentRangeNode child) { 314 315 Position p= findCorrespondingPosition(parent, child); 316 if (p != null) { 317 try { 318 fBaseDocument.replace(p.getOffset(), p.getLength(), s); 319 } catch (BadLocationException ex) { 320 CompareUIPlugin.log(ex); 321 } 322 } 323 } 324 325 328 public InputStream getContents() { 329 String s; 330 try { 331 s= fBaseDocument.get(fRange.getOffset(), fRange.getLength()); 332 } catch (BadLocationException ex) { 333 s= ""; } 335 return new ByteArrayInputStream (Utilities.getBytes(s, UTF_16)); 336 } 337 338 339 344 public boolean isEditable() { 345 if (fParent != null) 346 return fParent.isEditable(); 347 return true; 348 } 349 350 353 public ITypedElement replace(ITypedElement child, ITypedElement other) { 354 355 if (fParent == null) { 356 DocumentRangeNode SRC= null; 360 String srcContents= ""; 362 if (other != null) { 363 src= (DocumentRangeNode) child; 364 365 if (other instanceof IStreamContentAccessor) { 366 try { 367 srcContents= Utilities.readString((IStreamContentAccessor)other); 368 } catch(CoreException ex) { 369 CompareUIPlugin.log(ex); 371 } 372 } 373 } 374 375 if (child == null) add(srcContents, null, src); 377 } 378 nodeChanged(this); 379 return child; 380 } 381 382 389 public void setContent(byte[] content) { 390 internalSetContents(content); 391 nodeChanged(this); 392 } 393 394 400 protected void internalSetContents(byte[] content) { 401 403 } 404 405 408 public String getCharset() { 409 return UTF_16; 410 } 411 412 418 protected void nodeChanged(DocumentRangeNode node) { 419 if (fParent != null) 420 fParent.nodeChanged(node); 421 } 422 423 434 public Object getAdapter(Class adapter) { 435 if (adapter == ISharedDocumentAdapter.class && fParent != null) 436 return fParent.getAdapter(adapter); 437 438 return Platform.getAdapterManager().getAdapter(this, adapter); 439 } 440 441 444 public boolean isReadOnly() { 445 if (fParent != null) 446 return fParent.isReadOnly(); 447 return false; 448 } 449 450 453 public IStatus validateEdit(Shell shell) { 454 if (fParent != null) 455 return fParent.validateEdit(shell); 456 return Status.OK_STATUS; 457 } 458 459 464 public Object getParentNode() { 465 return fParent; 466 } 467 } 468 469 | Popular Tags |