Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1c2663c82a094c1f9d85b556103d7d92cd701732 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*******************************************************************************
 * 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
 *     Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide extension point for CodeMining - Bug 528419
 *******************************************************************************/
package org.eclipse.ui.internal.texteditor;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.osgi.framework.BundleContext;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IRegistryChangeEvent;
import org.eclipse.core.runtime.IRegistryChangeListener;
import org.eclipse.core.runtime.Platform;

import org.eclipse.jface.action.IAction;

import org.eclipse.ui.internal.texteditor.codemining.CodeMiningProviderRegistry;
import org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffExtensionsRegistry;
import org.eclipse.ui.internal.texteditor.spelling.SpellingEngineRegistry;
import org.eclipse.ui.plugin.AbstractUIPlugin;

/**
 * The plug-in runtime class for the text editor UI plug-in (id <code>"org.eclipse.ui.workbench.texteditor"</code>).
 * This class provides static methods for:
 * <ul>
 *  <li> getting the last editor position.</li>
 * </ul>
 * <p>
 * This class provides static methods and fields only; it is not intended to be
 * instantiated or subclassed by clients.
 * </p>
 *
 * @since 2.1
 */
public final class TextEditorPlugin extends AbstractUIPlugin implements IRegistryChangeListener {

	/** The plug-in instance */
	private static TextEditorPlugin fgPlugin;

	/** The last edit position */
	private EditPosition fLastEditPosition;
	/** The action which goes to the last edit position */
	private Set<IAction> fLastEditPositionDependentActions;

	/**
	 * The quick diff extension registry.
	 * @since 3.0
	 */
	private QuickDiffExtensionsRegistry fQuickDiffExtensionRegistry;

	/**
	 * The spelling engine registry
	 * @since 3.1
	 */
	private SpellingEngineRegistry fSpellingEngineRegistry;

	/**
	 * The codemining provider registry
	 *
	 * @since 3.10
	 */
	private CodeMiningProviderRegistry fCodeMiningProviderRegistry;

	/**
	 * Creates a plug-in instance.
	 */
	public TextEditorPlugin() {
		super();
		Assert.isTrue(fgPlugin == null);
		fgPlugin= this;
	}

	/**
	 * Returns the plug-in instance.
	 *
	 * @return the text editor plug-in instance
	 * @since 3.0
	 */
	public static TextEditorPlugin getDefault() {
		return fgPlugin;
	}

	/**
	 * Text editor UI plug-in Id (value <code>"org.eclipse.ui.workbench.texteditor"</code>).
	 */
	public static final String PLUGIN_ID= "org.eclipse.ui.workbench.texteditor"; //$NON-NLS-1$

	/**
	 * Extension Id of quick diff reference provider extension point.
	 * (value <code>"quickDiffReferenceProvider"</code>).
	 * @since 3.0
	 */
	public static final String REFERENCE_PROVIDER_EXTENSION_POINT= "quickDiffReferenceProvider"; //$NON-NLS-1$

	/**
	 * Returns the last edit position.
	 *
	 * @return	the last edit position or <code>null</code>	if there is no last edit position
	 * @see EditPosition
	 */
	public EditPosition getLastEditPosition() {
		return fLastEditPosition;
	}

	/**
	 * Sets the last edit position.
	 *
	 * @param lastEditPosition	the last edit position
	 * @see EditPosition
	 */
	public void setLastEditPosition(EditPosition lastEditPosition) {
		fLastEditPosition= lastEditPosition;
		if (fLastEditPosition != null && fLastEditPositionDependentActions != null) {
			Iterator<IAction> iter= fLastEditPositionDependentActions.iterator();
			while (iter.hasNext())
				iter.next().setEnabled(true);
			fLastEditPositionDependentActions= null;
		}
	}

	/**
	 * Adds the given action to the last edit position dependent actions.
	 *
	 * @param action the goto last edit position action
	 */
	public void addLastEditPositionDependentAction(IAction action) {
		if (fLastEditPosition != null)
			return;
		if (fLastEditPositionDependentActions == null)
			fLastEditPositionDependentActions= new HashSet<>();
		fLastEditPositionDependentActions.add(action);
	}

	/**
	 * Removes the given action from the last edit position dependent actions.
	 *
	 * @param action the action that depends on the last edit position
	 */
	public void removeLastEditPositionDependentAction(IAction action) {
		if (fLastEditPosition != null)
			return;
		if (fLastEditPositionDependentActions != null)
			fLastEditPositionDependentActions.remove(action);
	}


	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		fQuickDiffExtensionRegistry= new QuickDiffExtensionsRegistry();
		fSpellingEngineRegistry= new SpellingEngineRegistry();
		fCodeMiningProviderRegistry = new CodeMiningProviderRegistry();
		Platform.getExtensionRegistry().addRegistryChangeListener(this, PLUGIN_ID);
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		Platform.getExtensionRegistry().removeRegistryChangeListener(this);
		fQuickDiffExtensionRegistry= null;
		fSpellingEngineRegistry= null;
		fCodeMiningProviderRegistry = null;
		super.stop(context);
	}

	@Override
	public void registryChanged(IRegistryChangeEvent event) {
		if (fQuickDiffExtensionRegistry != null && event.getExtensionDeltas(PLUGIN_ID, REFERENCE_PROVIDER_EXTENSION_POINT).length > 0)
			fQuickDiffExtensionRegistry.reloadExtensions();
		if (fSpellingEngineRegistry != null && event.getExtensionDeltas(PLUGIN_ID, SpellingEngineRegistry.SPELLING_ENGINE_EXTENSION_POINT).length > 0)
			fSpellingEngineRegistry.reloadExtensions();
		if (fCodeMiningProviderRegistry != null && event.getExtensionDeltas(PLUGIN_ID,
				CodeMiningProviderRegistry.CODEMINING_PROVIDERS_EXTENSION_POINT).length > 0)
			fCodeMiningProviderRegistry.reloadExtensions();
	}

	/**
	 * Returns this plug-ins quick diff extension registry.
	 *
	 * @return the quick diff extension registry or <code>null</code> if this plug-in has been shutdown
	 * @since 3.0
	 */
	public QuickDiffExtensionsRegistry getQuickDiffExtensionRegistry() {
		return fQuickDiffExtensionRegistry;
	}

	/**
	 * Returns this plug-ins spelling engine registry.
	 *
	 * @return the spelling engine registry or <code>null</code> if this plug-in has been shutdown
	 * @since 3.1
	 */
	public SpellingEngineRegistry getSpellingEngineRegistry() {
		return fSpellingEngineRegistry;
	}

	/**
	 * Returns this plug-ins codemining provider registry.
	 *
	 * @return the codemining provider registry or <code>null</code> if this plug-in
	 *         has been shutdown
	 * @since 3.10
	 */
	public CodeMiningProviderRegistry getCodeMiningProviderRegistry() {
		return fCodeMiningProviderRegistry;
	}
}

Back to the top