1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.common.extension.internal;
\r
14 import java.util.Arrays;
\r
15 import java.util.HashSet;
\r
16 import java.util.Set;
\r
18 import org.eclipse.core.runtime.CoreException;
\r
19 import org.eclipse.core.runtime.IConfigurationElement;
\r
20 import org.eclipse.core.runtime.IExtension;
\r
21 import org.eclipse.core.runtime.IExtensionPoint;
\r
22 import org.eclipse.core.runtime.Platform;
\r
23 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
\r
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
\r
25 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
\r
26 import org.eclipse.core.runtime.dynamichelpers.IFilter;
\r
27 import org.simantics.browsing.ui.common.Activator;
\r
28 import org.simantics.browsing.ui.common.ErrorLogger;
\r
29 import org.simantics.browsing.ui.common.extension.EvaluatorFactory;
\r
32 * @author Tuukka Lehtonen
\r
34 public class EvaluatorFactoryExtensionManager implements IExtensionChangeHandler {
\r
36 private final static String NAMESPACE = Activator.PLUGIN_ID;
\r
38 private final static String EP_NAME = "evaluator";
\r
40 private ExtensionTracker tracker;
\r
42 private IEvaluatorFactoryExtension[] extensions = new IEvaluatorFactoryExtension[0];
\r
44 private static EvaluatorFactoryExtensionManager INSTANCE;
\r
47 public static synchronized EvaluatorFactoryExtensionManager getInstance() {
\r
48 if (INSTANCE == null)
\r
49 INSTANCE = new EvaluatorFactoryExtensionManager();
\r
53 public static synchronized void dispose() {
\r
54 if (INSTANCE != null) {
\r
61 private EvaluatorFactoryExtensionManager() {
\r
62 tracker = new ExtensionTracker();
\r
64 // Cache defined actions
\r
65 IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
\r
66 loadExtensions(expt.getConfigurationElements());
\r
68 // Start tracking for new and removed extensions
\r
69 IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
\r
70 tracker.registerHandler(this, filter);
\r
73 private void close() {
\r
76 extensions = new IEvaluatorFactoryExtension[0];
\r
79 public IEvaluatorFactoryExtension[] getExtensions() {
\r
83 private void loadExtensions(IConfigurationElement[] elements) {
\r
85 Set<IEvaluatorFactoryExtension> newExtensions = new HashSet<IEvaluatorFactoryExtension>(Arrays.asList(extensions));
\r
87 for (IConfigurationElement el : elements) {
\r
90 EvaluatorFactory factory = (EvaluatorFactory)el.createExecutableExtension("class");
\r
91 IEvaluatorFactoryExtension ext = new EvaluatorFactoryExtension(factory);
\r
93 // Start tracking the new extension object, its removal will be notified of
\r
94 // with removeExtension(extension, Object[]).
\r
95 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
\r
97 newExtensions.add(ext);
\r
98 } catch (CoreException e) {
\r
99 ErrorLogger.defaultLogError("Failed to initialize " + EP_NAME + " extension \"" + el.getName()
\r
100 + "\" with name \"" + el.getAttribute("name") + "\": "
\r
101 + e.getMessage(), e);
\r
105 // Atomic assignment
\r
106 this.extensions = newExtensions.toArray(new IEvaluatorFactoryExtension[newExtensions.size()]);
\r
110 public void addExtension(IExtensionTracker tracker, IExtension extension) {
\r
111 loadExtensions(extension.getConfigurationElements());
\r
115 public void removeExtension(IExtension extension, Object[] objects) {
\r
116 Set<IEvaluatorFactoryExtension> newExtensions = new HashSet<IEvaluatorFactoryExtension>(Arrays.asList(extensions));
\r
118 for (Object o : objects) {
\r
119 tracker.unregisterObject(extension, o);
\r
120 newExtensions.remove(o);
\r
123 // Atomic assignment
\r
124 this.extensions = newExtensions.toArray(new IEvaluatorFactoryExtension[newExtensions.size()]);
\r
127 public static EvaluatorFactory getFactory(String id) {
\r
128 for(IEvaluatorFactoryExtension ext : getInstance().extensions) {
\r
129 if(ext.factory().getClass().getCanonicalName().equals(id)) return ext.factory();
\r