Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c240a45330965c7a169988f87fe025730c71f031 (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
/*******************************************************************************
 * 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.structuremergeviewer;

import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.SharedDocumentAdapter;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;

/**
 * An implementation of {@link ISharedDocumentAdapter} that wraps another
 * shared document adapter.
 * <p>
 * Clients may subclass this class.
 * </p>
 * @since 3.3
 */
public class SharedDocumentAdapterWrapper implements ISharedDocumentAdapter {
	private ISharedDocumentAdapter wrappedAdapter;
	
	/**
	 * Helper method that returns the shared document adapter for the
	 * given typed element or <code>null</code> if there isn't one.
	 * @param element the typed element
	 * @return the shared document adapter for the given typed element 
	 *    or <code>null</code>
	 */
	public static ISharedDocumentAdapter getAdapter(Object element) {
		return (ISharedDocumentAdapter)Utilities.getAdapter(element, ISharedDocumentAdapter.class, true);
	}
	
	/**
	 * Create a shared document adapter that wraps the given adapter.
	 * @param wrappedAdapter the wrapped adapter
	 */
	public SharedDocumentAdapterWrapper(ISharedDocumentAdapter wrappedAdapter) {
		super();
		this.wrappedAdapter = wrappedAdapter;
	}
	
	@Override
	public void connect(IDocumentProvider provider, IEditorInput documentKey)
			throws CoreException {
		wrappedAdapter.connect(provider, documentKey);
	}

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

	@Override
	public IEditorInput getDocumentKey(Object element) {
		return wrappedAdapter.getDocumentKey(element);
	}

	@Override
	public void flushDocument(IDocumentProvider provider,
			IEditorInput documentKey, IDocument document, boolean overwrite) throws CoreException {
		wrappedAdapter.flushDocument(provider, documentKey, document, overwrite);
	}

	/**
	 * Return the wrapped adapter.
	 * @return the wrapped adapter
	 */
	public final ISharedDocumentAdapter getWrappedAdapter() {
		return wrappedAdapter;
	}

	@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