1 11 package org.eclipse.compare; 12 13 import org.eclipse.compare.internal.Utilities; 14 import org.eclipse.core.resources.IFile; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.jface.text.IDocument; 19 import org.eclipse.ui.IEditorInput; 20 import org.eclipse.ui.part.FileEditorInput; 21 import org.eclipse.ui.texteditor.DocumentProviderRegistry; 22 import org.eclipse.ui.texteditor.IDocumentProvider; 23 24 32 public abstract class SharedDocumentAdapter implements ISharedDocumentAdapter { 33 34 39 public static IDocumentProvider getDocumentProvider(IEditorInput input) { 40 return DocumentProviderRegistry.getDefault().getDocumentProvider(input); 41 } 42 43 46 public void connect(IDocumentProvider provider, IEditorInput documentKey) 47 throws CoreException { 48 provider.connect(documentKey); 49 } 50 51 54 public void disconnect(IDocumentProvider provider, IEditorInput documentKey) { 55 provider.disconnect(documentKey); 56 } 57 58 63 public IEditorInput getDocumentKey(Object element) { 64 IFile file = getFile(element); 65 if (file != null && file.exists()) { 66 return new FileEditorInput(file); 67 } 68 return null; 69 } 70 71 private IFile getFile(Object element) { 72 if (element instanceof IResourceProvider) { 73 IResourceProvider rp = (IResourceProvider) element; 74 IResource resource = rp.getResource(); 75 if (resource instanceof IFile) { 76 return (IFile) resource; 77 } 78 } 79 IFile file = (IFile)Utilities.getAdapter(element, IFile.class); 80 if (file != null) { 81 return file; 82 } 83 IResource resource = (IResource)Utilities.getAdapter(element, IResource.class); 84 if (resource instanceof IFile) { 85 return (IFile) resource; 86 } 87 return null; 88 } 89 90 101 protected void saveDocument(IDocumentProvider provider, 102 IEditorInput documentKey, IDocument document, boolean overwrite, 103 IProgressMonitor monitor) throws CoreException { 104 try { 105 provider.aboutToChange(documentKey); 106 provider.saveDocument(monitor, documentKey, document, overwrite); 107 } finally { 108 provider.changed(documentKey); 109 } 110 } 111 112 115 public void disconnect(Object element) { 116 IEditorInput input = getDocumentKey(element); 117 if (input == null) 118 return; 119 IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(input); 120 if (provider == null) 121 return; 122 disconnect(provider, input); 123 } 124 125 } 126 | Popular Tags |