Skip to main content
summaryrefslogtreecommitdiffstats
blob: 57146ee777cbf8d8b3d0a612f2b0239d5e5942a0 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (c) 2000, 2008 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.ui.texteditor.quickdiff;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.text.source.IAnnotationModel;

import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
import org.eclipse.ui.internal.texteditor.quickdiff.DocumentLineDiffer;
import org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffExtensionsRegistry;

import org.eclipse.ui.texteditor.ITextEditor;

/**
 * Access class for the quick diff reference provider extension point.
 * <p>
 * This class may be instantiated, it is not intended to be subclassed.
 * </p>
 * @since 3.0
 * @noextend This class is not intended to be subclassed by clients.
 */
public class QuickDiff {

	/**
	 * Creates a new instance.
	 */
	public QuickDiff() {
	}

	/**
	 * Returns the descriptor of the "last saved version" reference provider.
	 * <p>
	 * Clients should not cache this value because it can change when plug-ins get dynamically added or removed.
	 * </p>
	 *
	 * @return the descriptor of "last saved version" reference provider or <code>null</code> if none
	 */
	public ReferenceProviderDescriptor getDefaultProvider() {
		QuickDiffExtensionsRegistry registry= TextEditorPlugin.getDefault().getQuickDiffExtensionRegistry();
		if (registry != null)
			return registry.getDefaultProvider();

		return null;
	}

	/**
	 * Returns a non-modifiable list of <code>ReferenceProviderDescriptor</code> describing all extension
	 * to the <code>quickDiffReferenceProvider</code> extension point.
	 * <p>
	 * Clients should not cache this list because it can change when plug-ins get dynamically added or removed.
	 * </p>
	 *
	 * @return the non-modifiable list of extensions to the <code>quickDiffReferenceProvider</code> extension point.
	 */
	public List getReferenceProviderDescriptors() {
		QuickDiffExtensionsRegistry registry= TextEditorPlugin.getDefault().getQuickDiffExtensionRegistry();
		if (registry != null)
			return registry.getReferenceProviderDescriptors();

		return Collections.EMPTY_LIST;
	}

	/**
	 * Returns the quick diff reference provider registered under <code>id</code>, or the default
	 * reference provider. The returned provider gets its editor set to <code>editor</code>. If neither
	 * the requested provider nor the default provider return <code>true</code> from <code>isEnabled</code> after
	 * having the editor set, <code>null</code> is returned.
	 * <p>
	 * Clients should not cache this value because it can change when plug-ins get dynamically added or removed.
	 * </p>
	 *
	 * @param editor the editor to be installed with the returned provider
	 * @param id the id as specified in the <code>plugin.xml</code> that installs the reference provider
	 * @return the reference provider registered under <code>id</code>, or the default reference provider, or <code>null</code>
	 */
	public IQuickDiffReferenceProvider getReferenceProviderOrDefault(ITextEditor editor, String id) {
		Assert.isNotNull(editor);
		Assert.isNotNull(id);

		List descs= getReferenceProviderDescriptors();
		// try to fetch preferred provider; load if needed
		for (Iterator iter= descs.iterator(); iter.hasNext();) {
			ReferenceProviderDescriptor desc= (ReferenceProviderDescriptor) iter.next();
			if (desc.getId().equals(id)) {
				IQuickDiffReferenceProvider provider= desc.createProvider();
				if (provider != null) {
					provider.setActiveEditor(editor);
					if (provider.isEnabled())
						return provider;
					provider.dispose();
					provider= null;
				}
			}
		}

		for (ListIterator iter= descs.listIterator(descs.size()); iter.hasPrevious();) {
			ReferenceProviderDescriptor desc= (ReferenceProviderDescriptor) iter.previous();
			IQuickDiffReferenceProvider provider= desc.createProvider();
			if (provider != null) {
				provider.setActiveEditor(editor);
				if (provider.isEnabled())
					return provider;
				provider.dispose();
				provider= null;
			}
		}

		return null;
	}

	/**
	 * Creates a new line differ annotation model with its reference provider set to the reference provider
	 * obtained by calling <code>getReferenceProviderOrDefault(editor, id)</code>.
	 *
	 * @param editor the editor to be installed with the returned provider
	 * @param id the id as specified in the <code>plugin.xml</code> that installs the reference provider
	 * @return a quick diff annotation model
	 */
	public IAnnotationModel createQuickDiffAnnotationModel(ITextEditor editor, String id) {
		IQuickDiffReferenceProvider provider= getReferenceProviderOrDefault(editor, id);
		if (provider != null) {
			DocumentLineDiffer differ= new DocumentLineDiffer();
			differ.setReferenceProvider(provider);
			return differ;
		}
		return null;
	}

	/**
	 * Returns the identifier of the quick diff provider installed with the given diff annotation
	 * model, or the empty string if it is not a diff annotation model or has no configured diff
	 * provider.
	 *
	 * @param differ a diff annotation model
	 * @return the reference provider id, or the empty string for none
	 * @since 3.2
	 */
	public Object getConfiguredQuickDiffProvider(IAnnotationModel differ) {
		if (differ instanceof DocumentLineDiffer) {
			DocumentLineDiffer lineDiffer= (DocumentLineDiffer) differ;
			IQuickDiffReferenceProvider provider= lineDiffer.getReferenceProvider();
			if (provider != null)
				return provider.getId();
		}
		return ""; //$NON-NLS-1$
	}

}

Back to the top