Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3646383642c3ffcca3fafcca5ab40dcc274f2cf4 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*******************************************************************************
 * Copyright (c) 2010 CEA LIST.
 * 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:
 *     CEA LIST - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.extensionpoints.editors.definition;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.extensionpoints.editors.Activator;
import org.eclipse.papyrus.extensionpoints.editors.configuration.DefaultDirectEditorConfiguration;
import org.eclipse.papyrus.extensionpoints.editors.configuration.IAdvancedEditorConfiguration;
import org.eclipse.papyrus.extensionpoints.editors.configuration.IDirectEditorConfiguration;
import org.eclipse.papyrus.extensionpoints.editors.utils.IDirectEditorsIds;
import org.eclipse.papyrus.infra.constraints.constraints.JavaQuery;
import org.eclipse.swt.graphics.Image;

public class DirectEditorExtensionPoint {

	/** Array that stores registered transformations */
	private static DirectEditorExtensionPoint[] configurations;

	private static DirectEditorRegistry directEditorProvider;

	/** value of the language attribute */
	private String language;

	/** value of the object to edit attribute */
	private String objectToEdit;

	/** value of the editor configuration attribute */
	private IDirectEditorConfiguration directEditorConfiguration;

	private Class objectClassToEdit;

	/** the current priority of the direct editor, can be null **/
	private Integer priority;




	/**
	 * Returns the set of transformations registered in the platform
	 *
	 * @return the set of transformations registered in the platform
	 */
	public static DirectEditorExtensionPoint[] getDirectEditorConfigurations() {

		// Computed only once
		if (configurations != null) {
			return configurations;
		}

		// It was not already computed,
		// returns the new Collection of DirectEditorExtensionPoint
		List<DirectEditorExtensionPoint> directEditorExtensionPoints = new ArrayList<DirectEditorExtensionPoint>();

		// Reading data from plug-ins
		IConfigurationElement[] configElements = Platform.getExtensionRegistry().getConfigurationElementsFor(IDirectEditorConfigurationIds.DIRECT_EDITOR_CONFIGURATION_EXTENSION_ID);

		// Read configuration elements for the current extension
		for (IConfigurationElement configElement : configElements) {
			try {
				DirectEditorExtensionPoint proxy = parseDirectEditorConfiguration(configElement);

				if (proxy != null) {
					directEditorExtensionPoints.add(proxy);
				}
			} catch (Throwable ex) {
				Activator.log.error(ex);
				continue;
			}
		} // end of configElements loop

		configurations = directEditorExtensionPoints.toArray(new DirectEditorExtensionPoint[directEditorExtensionPoints.size()]);
		directEditorProvider = new DirectEditorRegistry();
		directEditorProvider.init(configurations);

		return configurations;
	}

	public static DirectEditorRegistry getDirectEditorProvider() {
		if (directEditorProvider != null) {
			return directEditorProvider;
		} else {
			directEditorProvider = new DirectEditorRegistry();
			directEditorProvider.init(getDirectEditorConfigurations());
			return directEditorProvider;
		}
	}

	/**
	 * Retrieves the preferred editor configuration for the specified type
	 *
	 * @param class_
	 *            the type of element to edit
	 * @return the preferred editor configuration for the specified type or <code>null</code>
	 */
	public static DirectEditorExtensionPoint getDefautDirectEditorConfiguration(Class class_) {
		// retrieves preference for this element
		String language = Activator.getDefault().getPreferenceStore().getString(IDirectEditorsIds.EDITOR_FOR_ELEMENT + class_.getCanonicalName());
		if (language == null || IDirectEditorsIds.SIMPLE_DIRECT_EDITOR.equals(language)) {
			return null;
		}

		Collection<DirectEditorExtensionPoint> configs = getDirectEditorConfigurations(class_);
		for (DirectEditorExtensionPoint configuration : configs) {
			if (language.equals(configuration.getLanguage())) {
				return configuration;
			}
		}

		return null;
	}

	/**
	 * Returns the set of transformations registered in the platform for the specified kind of
	 * element
	 *
	 * @param the
	 *            type of element to be edited
	 * @return the set of transformations registered in the platform for the specified kind of
	 *         element
	 */
	public static Collection<DirectEditorExtensionPoint> getDirectEditorConfigurations(Class<?> elementClass) {
		// list of configuration to be returned. They correspond to
		// configuration to edit the
		// specified type
		final ArrayList<DirectEditorExtensionPoint> elementConfigurations = new ArrayList<DirectEditorExtensionPoint>();

		// check each configuration in the platform and select corresponding
		// ones.
		for (DirectEditorExtensionPoint configuration : getDirectEditorConfigurations()) {
			// both class are compatibles ?
			if (configuration.getObjectClassToEdit() != null) {
				if (configuration.objectClassToEdit.isAssignableFrom(elementClass)) {
					elementConfigurations.add(configuration);
				}
			}
		}
		return elementConfigurations;
	}

	/**
	 * Returns a configuration, given elements from the ConfigurationElement
	 *
	 * @param configElt
	 *            the element that declares the extension
	 * @return a new configuration, given the information of the specified configElt
	 */
	public static DirectEditorExtensionPoint parseDirectEditorConfiguration(IConfigurationElement configElt) {

		// check that the ConfigElement is a transformation
		if (!IDirectEditorConfigurationIds.TAG_DIRECT_EDITOR_CONFIGURATION.equals(configElt.getName())) {
			return null;
		}
		// this is a transformation, tries to parse extension, and create the
		// java corresponding
		// class
		try {
			return new DirectEditorExtensionPoint(configElt);
		} catch (Exception e) {
			Activator.log(e);
			return null;
		}
	}

	/**
	 * Creates a new DirectEditorExtensionPoint, according to the ConfigurationElement
	 *
	 * @param configElt
	 *            the configuration element corresponding to the configuration
	 */
	public DirectEditorExtensionPoint(IConfigurationElement configElt) {
		language = getAttribute(configElt, IDirectEditorConfigurationIds.ATT_LANGUAGE, "undefined", true); // should
		// already
		// be
		// a
		// string
		objectToEdit = getAttribute(configElt, IDirectEditorConfigurationIds.ATT_OBJECT_TO_EDIT, "java.lang.Object", true); // should already be a string
		directEditorConfiguration = getDirectEditorConfigurationClass(configElt);
		// the constraint maybe null!

		priority = getPriority(configElt);
		if (directEditorConfiguration == null) {
			directEditorConfiguration = getAdvancedDirectEditorConfigurationClass(configElt);
		}
		// Block added for the case of popup editors
		if (directEditorConfiguration == null) {
			directEditorConfiguration = getPopupDirectEditorConfigurationClass(configElt);
		}
		directEditorConfiguration.setLanguage(language);

		// retrieve the bundle loader of the plugin that declares the extension
		try {
			String pluginID = configElt.getContributor().getName();
			objectClassToEdit = Platform.getBundle(pluginID).loadClass(objectToEdit);
		} catch (ClassNotFoundException e) {
			Activator.log(e);
		}

	}

	protected static IDirectEditorConfiguration getDirectEditorConfigurationClass(IConfigurationElement configElement) {
		IDirectEditorConfiguration configuration = null;
		try {
			if (configElement.getChildren(IDirectEditorConfigurationIds.TAG_SIMPLE_EDITOR).length > 0) {
				Object config = configElement.getChildren(IDirectEditorConfigurationIds.TAG_SIMPLE_EDITOR)[0].createExecutableExtension(IDirectEditorConfigurationIds.ATT_EDITOR_CONFIGURATION);
				if (config instanceof IDirectEditorConfiguration) {
					configuration = (IDirectEditorConfiguration) config;
				}
			}
		} catch (CoreException e) {
			Activator.log(e);
			configuration = new DefaultDirectEditorConfiguration();
		}
		return configuration;
	}



	/**
	 * Try to load a javaQuery defined in the extension point
	 *
	 * @param configElement
	 *            the config element see {@link IConfigurationElement}
	 * @return the java query class see {@link JavaQuery}, can return null because this attribute is optional
	 */
	protected static JavaQuery getJavaQueryClass(IConfigurationElement configElement) {
		JavaQuery javaQuery = null;
		try {
			if (configElement.getAttribute(IDirectEditorConfigurationIds.ATT_CONSTRAINT) == null) {
				return null;
			}
			Object config = configElement.createExecutableExtension(IDirectEditorConfigurationIds.ATT_CONSTRAINT);
			if (config instanceof JavaQuery) {
				javaQuery = (JavaQuery) config;
			}
		} catch (CoreException e) {
			Activator.log(e);
		}
		return javaQuery;
	}

	/**
	 * This priority policy has been defined so that:
	 * - an external contribution is NOT the default editor with the classic configuration (LOW)
	 * - an external contribution can be set to default editor(use High or Highest)
	 *
	 * To achieve that:
	 * - the default priority is set to low
	 * - Papyrus editors priority is set to medium
	 * - Papyrus editors, defined to be the default ones, their priority is set at medium
	 */
	protected static Integer getPriority(IConfigurationElement configElement) {
		try {
			for (IConfigurationElement childConfigElement : configElement.getChildren(IDirectEditorConfigurationIds.ATT_PRIORITY)) {

				String config = getAttribute(childConfigElement, IDirectEditorConfigurationIds.ATT_PRIORITY_NAME, null, true);
				if (config.equals(IDirectEditorConfigurationIds.PRIORITY_HIGHEST)) {
					return new Integer(0);
				}
				if (config.equals(IDirectEditorConfigurationIds.PRIORITY_HIGH)) {
					return new Integer(1);
				}
				if (config.equals(IDirectEditorConfigurationIds.PRIORITY_MEDIUM)) {
					return new Integer(2);
				}
				if (config.equals(IDirectEditorConfigurationIds.PRIORITY_LOW)) {
					return new Integer(3);
				}
				if (config.equals(IDirectEditorConfigurationIds.PRIORITY_LOWEST)) {
					return new Integer(4);
				}
			}
		} catch (Exception e) {
			Activator.log.error(e);
		}
		return new Integer(3); // PRIORITY_LOW
	}


	protected static IAdvancedEditorConfiguration getAdvancedDirectEditorConfigurationClass(IConfigurationElement configElement) {
		IAdvancedEditorConfiguration configuration = null;
		try {
			for (IConfigurationElement childConfigElement : configElement.getChildren(IDirectEditorConfigurationIds.TAG_ADVANCED_EDITOR)) {
				for (String attname : childConfigElement.getAttributeNames()) {
					Activator.log.debug(attname);
				}

				Object config = childConfigElement.createExecutableExtension(IDirectEditorConfigurationIds.ATT_EDITOR_CONFIGURATION);
				if (config instanceof IAdvancedEditorConfiguration) {
					configuration = (IAdvancedEditorConfiguration) config;
				}
			}

		} catch (CoreException e) {
			Activator.log.error(e);
			configuration = null;
		}
		return configuration;
	}

	// /////////////////////////////// TODO:(done) Method added for the case of popup editors
	protected static IDirectEditorConfiguration getPopupDirectEditorConfigurationClass(IConfigurationElement configElement) {
		IDirectEditorConfiguration configuration = null;
		try {
			for (IConfigurationElement childConfigElement : configElement.getChildren(IDirectEditorConfigurationIds.TAG_POPUP_EDITOR)) {
				for (String attname : childConfigElement.getAttributeNames()) {
					Activator.log.debug(attname);
				}

				Object config = childConfigElement.createExecutableExtension(IDirectEditorConfigurationIds.ATT_EDITOR_CONFIGURATION);
				if (config instanceof IDirectEditorConfiguration) {
					configuration = (IDirectEditorConfiguration) config;
				}
			}

		} catch (CoreException e) {
			Activator.log(e);
			configuration = null;
		}
		return configuration;
	}

	// ///////////////////////////////////

	/**
	 * Returns the value of the attribute that has the given name, for the given configuration
	 * element.
	 * <p>
	 * if the attribute has no value, and if default value is not <code>null</code>, it returns defaultValue.
	 * <p>
	 * if it has no value, no default value, and if the attribute is required, it throws an exception.
	 *
	 * @param defaultValue
	 *            the default value (if exists) of the attribute
	 * @param isRequired
	 *            boolean that indicates if this attribute is required
	 * @param configElt
	 *            configuration element that reflects the content of the extension
	 * @param name
	 *            the name of the attribute to read
	 * @return the attribute value
	 */
	protected static String getAttribute(IConfigurationElement configElt, String name, String defaultValue, boolean isRequired) {
		String value = configElt.getAttribute(name);

		if (value != null) {
			return value;
		} else if (defaultValue != null) {
			return defaultValue;
		}

		if (isRequired) {
			throw new IllegalArgumentException("Missing " + name + " attribute"); //$NON-NLS-1$ //$NON-NLS-2$
		}

		return null;
	}

	/**
	 * Icon getter
	 *
	 * @return the icon which path is in extension
	 */
	// @unused
	protected Image getImage(String iconPath, IConfigurationElement configElement) {

		// no image associated to this plug-in
		if (iconPath == null) {
			return null;
		}
		IExtension extension = configElement.getDeclaringExtension();
		String extensionPluginId = extension.getContributor().getName();

		return Activator.getImage(extensionPluginId, iconPath);
	}

	/**
	 * Returns the language edited by this direct editor
	 *
	 * @return the language edited by this direct editor
	 */
	public String getLanguage() {
		return language;
	}

	/**
	 * Sets the language edited by this direct editor
	 *
	 * @param language
	 *            the language edited by this direct editor
	 */
	// @unused
	public void setLanguage(String language) {
		this.language = language;
	}

	/**
	 * Returns the type of object to edit
	 *
	 * @return the type of object to edit
	 */
	public String getObjectToEdit() {
		return objectToEdit;
	}

	/**
	 * Returns the class of object to edit
	 *
	 * @return the class of object to edit
	 */
	public Class getObjectClassToEdit() {
		return objectClassToEdit;
	}

	/**
	 * Sets the type of object to edit
	 *
	 * @param objectToEdit
	 *            the type of object to edit
	 */
	// @unused
	public void setObjectToEdit(String objectToEdit) {
		this.objectToEdit = objectToEdit;
	}

	/**
	 * Returns the configuration for the dialog window
	 *
	 * @return the configuration for the dialog window
	 */
	public IDirectEditorConfiguration getDirectEditorConfiguration() {
		return directEditorConfiguration;
	}

	/**
	 * Sets the configuration for the dialog window
	 *
	 * @param directEditorConfiguration
	 *            the configuration for the dialog window
	 */
	// @unused
	public void setDirectEditorConfiguration(IDirectEditorConfiguration directEditorConfiguration) {
		this.directEditorConfiguration = directEditorConfiguration;
	}

	/**
	 * Gets the priority.
	 *
	 * @return the priority
	 */
	public Integer getPriority() {
		return priority;
	}


	/**
	 * Sets the priority.
	 *
	 * @param priority
	 *            the new priority
	 */
	public void setPriority(Integer priority) {
		this.priority = priority;
	}





}

Back to the top