Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3707acf01d8faa1f23f6a71d1ebfbd031f06c724 (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
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.texteditor.quickdiff;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;

import org.eclipse.ui.internal.texteditor.TextEditorPlugin;

import org.eclipse.ui.texteditor.quickdiff.ReferenceProviderDescriptor;

/**
 * Access class for the quick diff reference provider extension point.
 *
 * @since 3.0
 */
public class QuickDiffExtensionsRegistry {

	/** The default reference provider's descriptor. */
	private ReferenceProviderDescriptor fDefaultDescriptor;
	/** The list returned to callers of <code>getExtensions</code>. */
	private List<ReferenceProviderDescriptor> fDescriptors;

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

	/**
	 * Returns the default provider, which is the last saved version.
	 *
	 * @return the descriptor of the default reference provider.
	 */
	public synchronized ReferenceProviderDescriptor getDefaultProvider() {
		ensureRegistered();
		return fDefaultDescriptor;
	}

	/**
	 * Returns a non-modifiable list of <code>ReferenceProviderDescriptor</code> describing all extension
	 * to the <code>quickDiffReferenceProvider</code> extension point.
	 *
	 * @return the list of extensions to the <code>quickDiffReferenceProvider</code> extension point.
	 */
	public synchronized List<ReferenceProviderDescriptor> getReferenceProviderDescriptors() {
		ensureRegistered();
		return fDescriptors;
	}

	/**
	 * Ensures that the extensions are read and stored in <code>fDescriptors</code>.
	 */
	private void ensureRegistered() {
		if (fDescriptors == null)
			reloadExtensions();
	}

	/**
	 * Reads all extensions.
	 * <p>
	 * This method can be called more than once in
	 * order to reload from a changed extension registry.
	 * </p>
	 */
	public synchronized void reloadExtensions() {
		fDefaultDescriptor= null;
		IExtensionRegistry registry= Platform.getExtensionRegistry();
		List<ReferenceProviderDescriptor> list= new ArrayList<>();

		IConfigurationElement[] elements= registry.getConfigurationElementsFor(TextEditorPlugin.PLUGIN_ID, TextEditorPlugin.REFERENCE_PROVIDER_EXTENSION_POINT);
		for (int i= 0; i < elements.length; i++) {
			ReferenceProviderDescriptor desc= new ReferenceProviderDescriptor(elements[i]);
			if (desc.getId().equals("org.eclipse.ui.internal.editors.quickdiff.LastSaveReferenceProvider")) //$NON-NLS-1$
				fDefaultDescriptor= desc;
			list.add(desc);
		}

		// make sure the default is the first one in the list
		if (fDefaultDescriptor != null) {
			list.remove(fDefaultDescriptor);
			list.add(0, fDefaultDescriptor);
		}

		fDescriptors= Collections.unmodifiableList(list);
	}
}

Back to the top