KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > SharedDocumentAdapter


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.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 /**
25  * An implementation of {@link ISharedDocumentAdapter} that provides default behavior for the
26  * methods of that interface.
27  * <p>
28  * Clients may subclass this class.
29  * </p>
30  * @since 3.3
31  */

32 public abstract class SharedDocumentAdapter implements ISharedDocumentAdapter {
33
34     /**
35      * Return the document provider for the given editor input.
36      * @param input the editor input
37      * @return the document provider for the given editor input
38      */

39     public static IDocumentProvider getDocumentProvider(IEditorInput input) {
40         return DocumentProviderRegistry.getDefault().getDocumentProvider(input);
41     }
42     
43     /* (non-Javadoc)
44      * @see org.eclipse.compare.ISharedDocumentAdapter#connect(org.eclipse.ui.texteditor.IDocumentProvider, org.eclipse.ui.IEditorInput)
45      */

46     public void connect(IDocumentProvider provider, IEditorInput documentKey)
47             throws CoreException {
48         provider.connect(documentKey);
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.compare.ISharedDocumentAdapter#disconnect(org.eclipse.ui.texteditor.IDocumentProvider, org.eclipse.ui.IEditorInput)
53      */

54     public void disconnect(IDocumentProvider provider, IEditorInput documentKey) {
55         provider.disconnect(documentKey);
56     }
57
58     /**
59      * Default implementation of {@link #getDocumentKey(Object)} that returns a
60      * {@link FileEditorInput} for the element if the element adapts to {@link IFile}.
61      * @see org.eclipse.compare.ISharedDocumentAdapter#getDocumentKey(java.lang.Object)
62      */

63     public IEditorInput getDocumentKey(Object JavaDoc 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 JavaDoc 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     /**
91      * A helper method to save a document.
92      *
93      * @param provider the document provider
94      * @param documentKey the document key
95      * @param document the document
96      * @param overwrite indicates whether overwrite should be performed
97      * while saving the given element if necessary
98      * @param monitor a progress monitor
99      * @throws CoreException
100      */

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     /* (non-Javadoc)
113      * @see org.eclipse.compare.ISharedDocumentAdapter#disconnect(java.lang.Object)
114      */

115     public void disconnect(Object JavaDoc 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