Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b65a2ec732f2c9effb6c184685ffe4166a4da763 (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
/*****************************************************************************
 * Copyright (c) 2014 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.infra.types.core.registries;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.papyrus.infra.types.ElementTypeConfiguration;
import org.eclipse.papyrus.infra.types.ElementTypesConfigurationsPackage;
import org.eclipse.papyrus.infra.types.core.Activator;
import org.eclipse.papyrus.infra.types.core.extensionpoints.IElementTypeConfigurationKindExtensionPoint;
import org.eclipse.papyrus.infra.types.core.factories.IElementTypeConfigurationFactory;
import org.eclipse.papyrus.infra.types.core.factories.impl.MetamodelTypeFactory;
import org.eclipse.papyrus.infra.types.core.factories.impl.SpecializationTypeFactory;

/**
 * Registry that manages all possible elementTypeConfigurationTypes
 */
public class ElementTypeConfigurationTypeRegistry {

	private static ElementTypeConfigurationTypeRegistry registry;

	protected Map<String, IElementTypeConfigurationFactory<? extends ElementTypeConfiguration>> elementTypeConfigurationTypeToFactory = null;

	/**
	 * returns the singleton instance of this registry
	 *
	 * @return the singleton instance of this registry
	 */
	public static synchronized ElementTypeConfigurationTypeRegistry getInstance() {
		if (registry == null) {
			registry = new ElementTypeConfigurationTypeRegistry();
			registry.init();
		}
		return registry;
	}

	/**
	 * Inits the registry.
	 */
	protected void init() {
		elementTypeConfigurationTypeToFactory = new HashMap<String, IElementTypeConfigurationFactory<? extends ElementTypeConfiguration>>();
		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(IElementTypeConfigurationKindExtensionPoint.EXTENSION_POINT_ID);
		for (IConfigurationElement configurationElement : elements) {
			String configurationClass = configurationElement.getAttribute(IElementTypeConfigurationKindExtensionPoint.CONFIGURATION_CLASS);
			try {
				Object factoryClass = configurationElement.createExecutableExtension(IElementTypeConfigurationKindExtensionPoint.FACTORY_CLASS);
				if (factoryClass instanceof IElementTypeConfigurationFactory) {
					elementTypeConfigurationTypeToFactory.put(configurationClass, (IElementTypeConfigurationFactory<?>) factoryClass);
				}
			} catch (CoreException e) {
				Activator.log.error(e);
			}
		}
		// Register default interpretations
		elementTypeConfigurationTypeToFactory.put(ElementTypesConfigurationsPackage.eINSTANCE.getMetamodelTypeConfiguration().getInstanceTypeName(), new MetamodelTypeFactory());
		elementTypeConfigurationTypeToFactory.put(ElementTypesConfigurationsPackage.eINSTANCE.getSpecializationTypeConfiguration().getInstanceTypeName(), new SpecializationTypeFactory());
	}

	protected <T extends ElementTypeConfiguration> IElementTypeConfigurationFactory<T> getFactory(ElementTypeConfiguration elementTypeConfiguration) {
		String elementTypeConfigurationType = elementTypeConfiguration.eClass().getInstanceTypeName();
		// We assume here that the right factory is registered for the right ElementTypeConfiguration
		@SuppressWarnings("unchecked")
		IElementTypeConfigurationFactory<T> factory = (IElementTypeConfigurationFactory<T>) elementTypeConfigurationTypeToFactory.get(elementTypeConfigurationType);
		return factory;
	}

	public <T extends ElementTypeConfiguration> IElementType getElementType(T elementTypeConfiguration) {
		if (elementTypeConfiguration == null) {
			return null;
		} else {
			IElementTypeConfigurationFactory<T> factory = getFactory(elementTypeConfiguration);
			if (factory != null) {
				IHintedType elementType = factory.createElementType(elementTypeConfiguration);
				return elementType;
			}
		}
		return null;
	}
}

Back to the top