Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4fb836fbd02c2581273c22a52ce06c4d7f481425 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*******************************************************************************
 * Copyright (c) 2006, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.DocumentProviderRegistry;
import org.eclipse.ui.texteditor.IDocumentProvider;

/**
 * An implementation of {@link ISharedDocumentAdapter} that provides default behavior for the
 * methods of that interface.
 * <p>
 * Clients may subclass this class.
 * </p>
 * @since 3.3
 */
public abstract class SharedDocumentAdapter implements ISharedDocumentAdapter {
	/**
	 * Returns the document provider for the given editor input.
	 *
	 * @param input the editor input
	 * @return the document provider for the given editor input
	 */
	public static IDocumentProvider getDocumentProvider(IEditorInput input) {
		return DocumentProviderRegistry.getDefault().getDocumentProvider(input);
	}

	@Override
	public void connect(IDocumentProvider provider, IEditorInput documentKey)
			throws CoreException {
		provider.connect(documentKey);
	}

	@Override
	public void disconnect(IDocumentProvider provider, IEditorInput documentKey) {
		provider.disconnect(documentKey);
	}

	/**
	 * Default implementation of {@link #getDocumentKey(Object)} that returns a
	 * {@link FileEditorInput} for the element if the element adapts to {@link IFile}.
	 * @see org.eclipse.compare.ISharedDocumentAdapter#getDocumentKey(java.lang.Object)
	 */
	@Override
	public IEditorInput getDocumentKey(Object element) {
		IFile file = getFile(element);
		if (file != null && file.exists()) {
			return new FileEditorInput(file);
		}
		return null;
	}

	private IFile getFile(Object element) {
		if (element instanceof IResourceProvider) {
			IResourceProvider rp = (IResourceProvider) element;
			IResource resource = rp.getResource();
			if (resource instanceof IFile) {
				return (IFile) resource;
			}
		}
		IFile file = Adapters.adapt(element, IFile.class);
		if (file != null) {
			return file;
		}
		IResource resource = Adapters.adapt(element, IResource.class);
		if (resource instanceof IFile) {
			return (IFile) resource;
		}
		return null;
	}

	/**
	 * A helper method to save a document.
	 *
	 * @param provider the document provider
	 * @param documentKey the document key
	 * @param document the document
	 * @param overwrite indicates whether overwrite should be performed
	 * 			while saving the given element if necessary
	 * @param monitor a progress monitor
	 * @throws CoreException
	 */
	protected void saveDocument(IDocumentProvider provider,
			IEditorInput documentKey, IDocument document, boolean overwrite,
			IProgressMonitor monitor) throws CoreException {
		try {
			provider.aboutToChange(documentKey);
			provider.saveDocument(monitor, documentKey, document, overwrite);
		} finally {
			provider.changed(documentKey);
		}
	}

	@Override
	public void disconnect(Object element) {
		IEditorInput input = getDocumentKey(element);
		if (input == null)
			return;
		IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(input);
		if (provider == null)
			return;
		disconnect(provider, input);
	}
}

Back to the top