1 11 package org.eclipse.compare.structuremergeviewer; 12 13 import java.io.UnsupportedEncodingException ; 14 15 import org.eclipse.compare.*; 16 import org.eclipse.compare.contentmergeviewer.IDocumentRange; 17 import org.eclipse.compare.internal.CompareUIPlugin; 18 import org.eclipse.compare.internal.Utilities; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.jface.text.*; 23 import org.eclipse.ui.IEditorInput; 24 import org.eclipse.ui.services.IDisposable; 25 import org.eclipse.ui.texteditor.IDocumentProvider; 26 27 36 public abstract class StructureCreator implements IStructureCreator2 { 37 38 41 public IStructureComparator getStructure(Object input) { 42 String contents= null; 43 IDocument doc= CompareUI.getDocument(input); 44 if (doc == null) { 45 if (input instanceof IStreamContentAccessor) { 46 IStreamContentAccessor sca= (IStreamContentAccessor) input; 47 try { 48 contents= Utilities.readString(sca); 49 } catch (CoreException e) { 50 CompareUIPlugin.log(e); 52 return null; 53 } 54 } 55 56 if (contents == null) { 57 return null; 59 } 60 61 doc= new Document(contents); 62 setupDocument(doc); 63 } 64 65 try { 66 return createStructureComparator(input, doc, null, null); 67 } catch (CoreException e) { 68 CompareUIPlugin.log(e); 69 return null; 70 } 71 } 72 73 76 public IStructureComparator createStructure(final Object element, final IProgressMonitor monitor) throws CoreException { 77 final IStructureComparator[] result = new IStructureComparator[] { null }; 78 Runnable runnable = new Runnable () { 79 public void run() { 80 result[0] = internalCreateStructure(element, monitor); 81 } 82 }; 83 Utilities.runInUIThread(runnable); 84 return result[0]; 85 } 86 87 90 private IStructureComparator internalCreateStructure(Object element, 91 IProgressMonitor monitor) { 92 final ISharedDocumentAdapter sda = SharedDocumentAdapterWrapper.getAdapter(element); 93 if (sda != null) { 94 final IEditorInput input = sda.getDocumentKey(element); 95 if (input != null) { 96 final IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(input); 97 if (provider != null) { 98 try { 99 sda.connect(provider, input); 100 IDocument document = provider.getDocument(input); 101 setupDocument(document); 102 return createStructureComparator(element, document, wrapSharedDocumentAdapter(sda, element, document), monitor); 103 } catch (CoreException e) { 104 CompareUIPlugin.log(e); 107 } 108 } 109 } 110 } 111 return getStructure(element); 112 } 113 114 141 protected abstract IStructureComparator createStructureComparator( 142 final Object element, IDocument document, 143 final ISharedDocumentAdapter sharedDocumentAdapter, 144 IProgressMonitor monitor) throws CoreException; 145 146 152 protected void setupDocument(IDocument document) { 153 String partitioning = getDocumentPartitioning(); 154 if (partitioning == null || !(document instanceof IDocumentExtension3)) { 155 if (document.getDocumentPartitioner() == null) { 156 IDocumentPartitioner partitioner= getDocumentPartitioner(); 157 if (partitioner != null) { 158 document.setDocumentPartitioner(partitioner); 159 partitioner.connect(document); 160 } 161 } 162 } else { 163 IDocumentExtension3 ex3 = (IDocumentExtension3) document; 164 if (ex3.getDocumentPartitioner(partitioning) == null) { 165 IDocumentPartitioner partitioner= getDocumentPartitioner(); 166 if (partitioner != null) { 167 ex3.setDocumentPartitioner(partitioning, partitioner); 168 partitioner.connect(document); 169 } 170 } 171 } 172 } 173 174 180 protected IDocumentPartitioner getDocumentPartitioner() { 181 return null; 182 } 183 184 192 protected String getDocumentPartitioning() { 193 return null; 194 } 195 196 206 public void save(IStructureComparator node, Object input) { 207 if (node instanceof IDocumentRange && input instanceof IEditableContent) { 208 IDocument document= ((IDocumentRange)node).getDocument(); 209 final ISharedDocumentAdapter sda = SharedDocumentAdapterWrapper.getAdapter(input); 211 if (sda != null) { 212 IEditorInput key = sda.getDocumentKey(input); 213 if (key != null) { 214 IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(key); 215 if (provider != null) { 216 IDocument providerDoc = provider.getDocument(key); 217 if (providerDoc != null && providerDoc == document) { 219 if (save(provider, document, input, sda, key)) 220 return; 221 } 222 } 223 } 224 } 225 IEditableContent bca= (IEditableContent) input; 226 String contents= document.get(); 227 String encoding= null; 228 if (input instanceof IEncodedStreamContentAccessor) { 229 try { 230 encoding= ((IEncodedStreamContentAccessor)input).getCharset(); 231 } catch (CoreException e1) { 232 } 234 } 235 if (encoding == null) 236 encoding= ResourcesPlugin.getEncoding(); 237 byte[] bytes; 238 try { 239 bytes= contents.getBytes(encoding); 240 } catch (UnsupportedEncodingException e) { 241 bytes= contents.getBytes(); 242 } 243 bca.setContent(bytes); 244 } 245 } 246 247 private boolean save(final IDocumentProvider provider, final IDocument document, 248 final Object input, final ISharedDocumentAdapter sda, final IEditorInput key) { 249 try { 250 sda.flushDocument(provider, key, document, false); 251 return true; 252 } catch (CoreException e) { 253 CompareUIPlugin.log(e); 254 } 255 return false; 256 } 257 258 266 private final ISharedDocumentAdapter wrapSharedDocumentAdapter(ISharedDocumentAdapter elementAdapter, final Object input, final IDocument document) { 267 return new SharedDocumentAdapterWrapper(elementAdapter) { 269 public IEditorInput getDocumentKey(Object element) { 270 if (hasSameDocument(element)) { 271 return super.getDocumentKey(input); 272 } 273 return super.getDocumentKey(element); 274 } 275 private boolean hasSameDocument(Object element) { 276 if (element instanceof DocumentRangeNode) { 277 DocumentRangeNode drn = (DocumentRangeNode) element; 278 return drn.getDocument() == document; 279 } 280 return false; 281 } 282 }; 283 } 284 285 297 public ITypedElement createElement(Object element, Object input, IProgressMonitor monitor) 298 throws CoreException { 299 String [] path= getPath(element, input); 300 if (path == null) { 301 IStructureComparator locate = locate(element, input); 303 if (locate instanceof ITypedElement) { 304 return (ITypedElement)locate; 305 } 306 return null; 307 } 308 309 IStructureComparator structure= createStructure(input, monitor); 311 if (structure == null) return null; 314 return findElement(structure, path); 316 } 317 318 328 public IStructureComparator locate(Object element, Object input) { 329 String [] path= getPath(element, input); 330 if (path == null) 331 return null; 332 IStructureComparator structure= getStructure(input); 334 if (structure == null) return null; 337 return (IStructureComparator)findElement(structure, path); 339 } 340 341 350 protected ITypedElement findElement(IStructureComparator structure, String [] path) { 351 return (ITypedElement)find(structure, path, 0); 352 } 353 354 357 private IStructureComparator find(IStructureComparator tree, String [] path, int index) { 358 if (tree != null) { 359 Object [] children= tree.getChildren(); 360 if (children != null) { 361 for (int i= 0; i < children.length; i++) { 362 IStructureComparator child= (IStructureComparator) children[i]; 363 if (child instanceof ITypedElement && child instanceof DocumentRangeNode) { 364 String n1= null; 365 if (child instanceof DocumentRangeNode) 366 n1= ((DocumentRangeNode)child).getId(); 367 if (n1 == null) 368 n1= ((ITypedElement)child).getName(); 369 String n2= path[index]; 370 if (n1.equals(n2)) { 371 if (index == path.length-1) 372 return child; 373 IStructureComparator result= find(child, path, index+1); 374 if (result != null) 375 return result; 376 } 377 } 378 } 379 } 380 } 381 return null; 382 } 383 384 396 protected String [] getPath(Object element, Object input) { 397 return null; 398 } 399 400 403 public void destroy(Object object) { 404 IDisposable disposable = getDisposable(object); 405 if (disposable != null) 406 disposable.dispose(); 407 } 408 409 private IDisposable getDisposable(Object object) { 410 if (object instanceof IDisposable) { 411 return (IDisposable) object; 412 } 413 if (object instanceof DocumentRangeNode) { 414 DocumentRangeNode node = (DocumentRangeNode) object; 415 return getDisposable(node.getParentNode()); 416 } 417 return null; 418 } 419 } 420 | Popular Tags |