Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: af63d2e1ad4161f6d4f2ff46458c0645c82874a7 (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
/**
 * Copyright (c) 2012 Mia-Software.
 *  
 * 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:
 *  	Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
 *  	Grégoire Dupé (Mia-Software) - Bug 387470 - [EFacet][Custom] Editors
 */
package org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.query;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.InvalidRegistryObjectException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.Activator;
import org.eclipse.papyrus.emf.facet.efacet.sdk.ui.internal.exported.IQueryDialogFactoryStrategy;
import org.eclipse.papyrus.emf.facet.util.core.Logger;
import org.eclipse.osgi.util.NLS;

/**
 * Implementation of the {@link IQueryWidgetCompositeFactoryFactory}.
 */
public class QueryWidgetCompositeFactoryImpl implements
		IQueryWidgetCompositeFactoryFactory {

	private static final String EXTENSION_POINT = "org.eclipse.papyrus.emf.facet.efacet.sdk.ui.queryFactoryDialogRegistration"; //$NON-NLS-1$
	private static final String DIALOG_FACTORY = "dialogFactory"; //$NON-NLS-1$
	private static final String MANAGED_TYPE_NAME = "managedQueryTypeName"; //$NON-NLS-1$

	private Map<String, IQueryDialogFactoryStrategy> extensions;

	public IQueryDialogFactoryStrategy getQueryDialogFactoryStrategy(
			final String managedTypeName) {
		// Lazy Loading, we only create the necessary instance.
		for (final IConfigurationElement element : Platform
				.getExtensionRegistry().getConfigurationElementsFor(
						QueryWidgetCompositeFactoryImpl.EXTENSION_POINT)) {
			final String elementName = element
					.getAttribute(QueryWidgetCompositeFactoryImpl.MANAGED_TYPE_NAME);
			if (elementName.equals(managedTypeName)) {
				try {
					final Object object = element
							.createExecutableExtension(QueryWidgetCompositeFactoryImpl.DIALOG_FACTORY);
					if (object instanceof IQueryDialogFactoryStrategy) {
						final IQueryDialogFactoryStrategy strategy = (IQueryDialogFactoryStrategy) object;
						this.extensions.put(elementName, strategy);
					} else {
						final String errorMsg = NLS
								.bind("The attribut {0} must contains a class implementing {1}", //$NON-NLS-1$
										QueryWidgetCompositeFactoryImpl.DIALOG_FACTORY,
										IQueryDialogFactoryStrategy.class
												.getName());
						Logger.logError(errorMsg, Activator.getDefault());
					}
				} catch (final InvalidRegistryObjectException e) {
					Logger.logError(e, Activator.getDefault());
				} catch (final CoreException e) {
					Logger.logError(e, Activator.getDefault());
				}
			}
		}
		return this.extensions.get(managedTypeName);
	}

	public List<String> getRegisteredQueryWidgetsComposite() {
		if (this.extensions == null) {
			this.extensions = new HashMap<String, IQueryDialogFactoryStrategy>();

			for (final IConfigurationElement element : Platform
					.getExtensionRegistry().getConfigurationElementsFor(
							QueryWidgetCompositeFactoryImpl.EXTENSION_POINT)) {
				this.extensions
						.put(element
								.getAttribute(QueryWidgetCompositeFactoryImpl.MANAGED_TYPE_NAME),
								null);
			}
		}

		return new LinkedList<String>(this.extensions.keySet());
	}

}

Back to the top