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