Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ac7be38d5c68b75aa59b795e3db963b843a2baf4 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*******************************************************************************
 * Copyright (c) 2000, 2015 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.decorators;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.internal.ObjectContributorManager;
import org.eclipse.ui.internal.WorkbenchMessages;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.misc.StatusUtil;
import org.eclipse.ui.internal.util.Util;

/**
 * The LightweightDecoratorManager is a decorator manager that encapsulates the
 * behavior for the lightweight decorators.
 */
public class LightweightDecoratorManager extends ObjectContributorManager {

	/**
	 * The runnable is the object used to run the decorations so that an error in
	 * someones decorator will not kill the thread. It is implemented here to
	 * prevent aborting of decoration i.e. successful decorations will still be
	 * applied.
	 */

	private static class LightweightRunnable implements ISafeRunnable {

		static class RunnableData {

			final DecorationBuilder builder;

			final LightweightDecoratorDefinition decorator;

			final Object element;

			public RunnableData(Object object, DecorationBuilder builder, LightweightDecoratorDefinition definition) {
				this.element = object;
				this.builder = builder;
				this.decorator = definition;
			}

			boolean isConsistent() {
				return builder != null && decorator != null && element != null;
			}
		}

		private volatile RunnableData data = new RunnableData(null, null, null);

		void setValues(Object object, DecorationBuilder builder, LightweightDecoratorDefinition definition) {
			data = new RunnableData(object, builder, definition);
		}

		/*
		 * @see ISafeRunnable.handleException(Throwable).
		 */
		@Override
		public void handleException(Throwable exception) {
			IStatus status = StatusUtil.newStatus(IStatus.ERROR, exception.getMessage(), exception);
			LightweightDecoratorDefinition decorator = data.decorator;
			String message;
			if (decorator == null) {
				message = WorkbenchMessages.DecoratorError;
			} else {
				String name = decorator.getName();
				if (name == null) {
					// decorator definition is not accessible anymore
					name = decorator.getId();
				}
				message = NLS.bind(WorkbenchMessages.DecoratorWillBeDisabled, name);
			}
			WorkbenchPlugin.log(message, status);
			if (decorator != null) {
				decorator.crashDisable();
			}
			clearReferences();
		}

		/*
		 * @see ISafeRunnable.run
		 */
		@Override
		public void run() throws Exception {
			// Copy to local variables, see
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=300358
			RunnableData data = this.data;
			if (data.isConsistent()) {
				data.decorator.decorate(data.element, data.builder);
			}
			clearReferences();
		}

		/**
		 * Clear all of the references in the receiver.
		 *
		 * @since 3.1
		 */
		void clearReferences() {
			data = new RunnableData(null, null, null);
		}
	}

	private LightweightRunnable runnable = new LightweightRunnable();

	// The lightweight definitions read from the registry
	private LightweightDecoratorDefinition[] lightweightDefinitions;

	private static final LightweightDecoratorDefinition[] EMPTY_LIGHTWEIGHT_DEF = new LightweightDecoratorDefinition[0];

	LightweightDecoratorManager(LightweightDecoratorDefinition[] definitions) {
		super();
		lightweightDefinitions = definitions;
		buildContributors();
	}

	/**
	 * Get the lightweight definitions for the receiver.
	 *
	 * @return LightweightDecoratorDefinition[]
	 */
	LightweightDecoratorDefinition[] getDefinitions() {
		return lightweightDefinitions;
	}

	/**
	 * Register the decorators as object contributions so that adaptable lookup can
	 * occur.
	 */
	private void buildContributors() {
		for (LightweightDecoratorDefinition decorator : lightweightDefinitions) {
			for (String type : getTargetTypes(decorator)) {
				registerContributor(decorator, type);
			}
		}
	}

	/**
	 * For dynamic UI
	 *
	 * @param decorator the definition to add
	 * @return whether the definition was added
	 * @since 3.0
	 */
	public boolean addDecorator(LightweightDecoratorDefinition decorator) {
		if (getLightweightDecoratorDefinition(decorator.getId()) == null) {
			LightweightDecoratorDefinition[] oldDefs = lightweightDefinitions;
			lightweightDefinitions = new LightweightDecoratorDefinition[lightweightDefinitions.length + 1];
			System.arraycopy(oldDefs, 0, lightweightDefinitions, 0, oldDefs.length);
			lightweightDefinitions[oldDefs.length] = decorator;
			// no reset - handled in the DecoratorManager
			String[] types = getTargetTypes(decorator);
			for (String type : types) {
				registerContributor(decorator, type);
			}
			return true;
		}
		return false;
	}

	/**
	 * Get the name of the types that a decorator is registered for.
	 *
	 * @param decorator
	 * @return String[]
	 */
	private String[] getTargetTypes(LightweightDecoratorDefinition decorator) {
		return decorator.getObjectClasses();
	}

	/**
	 * For dynamic-ui
	 *
	 * @param decorator the definition to remove
	 * @return whether the definition was removed
	 * @since 3.1
	 */
	public boolean removeDecorator(LightweightDecoratorDefinition decorator) {
		int idx = getLightweightDecoratorDefinitionIdx(decorator.getId());
		if (idx != -1) {
			LightweightDecoratorDefinition[] oldDefs = lightweightDefinitions;
			Util.arrayCopyWithRemoval(oldDefs,
					lightweightDefinitions = new LightweightDecoratorDefinition[lightweightDefinitions.length - 1],
					idx);
			// no reset - handled in the DecoratorManager
			for (String type : getTargetTypes(decorator)) {
				unregisterContributor(decorator, type);

			}
			return true;
		}
		return false;
	}

	/**
	 * Get the LightweightDecoratorDefinition with the supplied id
	 *
	 * @return LightweightDecoratorDefinition or <code>null</code> if it is not
	 *         found
	 * @param decoratorId String
	 * @since 3.0
	 */
	private LightweightDecoratorDefinition getLightweightDecoratorDefinition(String decoratorId) {
		int idx = getLightweightDecoratorDefinitionIdx(decoratorId);
		if (idx != -1) {
			return lightweightDefinitions[idx];
		}
		return null;
	}

	/**
	 * Return the index of the definition in the array.
	 *
	 * @param decoratorId the id
	 * @return the index of the definition in the array or <code>-1</code>
	 * @since 3.1
	 */
	private int getLightweightDecoratorDefinitionIdx(String decoratorId) {
		for (int i = 0; i < lightweightDefinitions.length; i++) {
			if (lightweightDefinitions[i].getId().equals(decoratorId)) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * Return the enabled lightweight decorator definitions.
	 *
	 * @return LightweightDecoratorDefinition[]
	 */
	LightweightDecoratorDefinition[] enabledDefinitions() {
		ArrayList result = new ArrayList();
		for (LightweightDecoratorDefinition lightweightDefinition : lightweightDefinitions) {
			if (lightweightDefinition.isEnabled()) {
				result.add(lightweightDefinition);
			}
		}
		LightweightDecoratorDefinition[] returnArray = new LightweightDecoratorDefinition[result.size()];
		result.toArray(returnArray);
		return returnArray;
	}

	/**
	 * Return whether there are enabled lightwieght decorators
	 *
	 * @return boolean
	 */
	boolean hasEnabledDefinitions() {
		for (LightweightDecoratorDefinition lightweightDefinition : lightweightDefinitions) {
			if (lightweightDefinition.isEnabled()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Reset any cached values.
	 */
	void reset() {
		runnable.clearReferences();
	}

	/**
	 * Shutdown the decorator manager by disabling all of the decorators so that
	 * dispose() will be called on them.
	 */
	void shutdown() {
		// Disable all fo the enabled decorators
		// so as to force a dispose of thier decorators
		for (LightweightDecoratorDefinition lightweightDefinition : lightweightDefinitions) {
			if (lightweightDefinition.isEnabled()) {
				lightweightDefinition.setEnabled(false);
			}
		}
	}

	/**
	 * Get the LightweightDecoratorDefinition with the supplied id
	 *
	 * @return LightweightDecoratorDefinition or <code>null</code> if it is not
	 *         found
	 * @param decoratorId String
	 */
	LightweightDecoratorDefinition getDecoratorDefinition(String decoratorId) {
		for (LightweightDecoratorDefinition lightweightDefinition : lightweightDefinitions) {
			if (lightweightDefinition.getId().equals(decoratorId)) {
				return lightweightDefinition;
			}
		}
		return null;
	}

	/**
	 * Get the lightweight registered for elements of this type.
	 */
	LightweightDecoratorDefinition[] getDecoratorsFor(Object element) {

		if (element == null) {
			return EMPTY_LIGHTWEIGHT_DEF;
		}

		List elements = new ArrayList(1);
		elements.add(element);
		LightweightDecoratorDefinition[] decoratorArray = EMPTY_LIGHTWEIGHT_DEF;
		List contributors = getContributors(elements);
		if (!contributors.isEmpty()) {
			Collection decorators = DecoratorManager.getDecoratorsFor(element,
					(DecoratorDefinition[]) contributors.toArray(new DecoratorDefinition[contributors.size()]));
			if (decorators.size() > 0) {
				decoratorArray = new LightweightDecoratorDefinition[decorators.size()];
				decorators.toArray(decoratorArray);
			}
		}

		return decoratorArray;
	}

	/**
	 * Fill the decoration with all of the results of the decorators.
	 *
	 * @param element    The source element
	 * @param decoration The DecorationResult we are working on. where adaptable is
	 *                   true.
	 */
	public void getDecorations(Object element, DecorationBuilder decoration) {
		for (LightweightDecoratorDefinition decorator : getDecoratorsFor(element)) {
			decoration.setCurrentDefinition(decorator);
			decorate(element, decoration, decorator);
		}
	}

	/**
	 * Decorate the element receiver in a SafeRunnable.
	 *
	 * @param element    The Object to be decorated
	 * @param decoration The object building decorations.
	 * @param decorator  The decorator being applied.
	 */
	private void decorate(Object element, DecorationBuilder decoration, LightweightDecoratorDefinition decorator) {

		runnable.setValues(element, decoration, decorator);
		SafeRunner.run(runnable);
	}

	/**
	 * Method for use by test cases
	 *
	 * @param object the object to be decorated
	 * @return the decoration result
	 */
	public DecorationResult getDecorationResult(Object object) {
		DecorationBuilder builder = new DecorationBuilder();
		getDecorations(object, builder);
		return builder.createResult();

	}

	@Override
	public void addExtension(IExtensionTracker tracker, IExtension extension) {
		// Do nothing as this is handled by the DecoratorManager
		// This is not called as canHandleExtensionTracking returns
		// false.
	}
}

Back to the top