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;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.HashSet;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtension;
22 import org.eclipse.core.runtime.IExtensionPoint;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
25 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
26 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
27 import org.eclipse.core.runtime.dynamichelpers.IFilter;
28 import org.simantics.browsing.ui.common.Activator;
29 import org.simantics.browsing.ui.common.extension.internal.EvaluatorBindingExtension;
30 import org.simantics.browsing.ui.common.extension.internal.EvaluatorImplementationBinding;
31 import org.simantics.browsing.ui.common.extension.internal.EvaluatorReferenceBinding;
32 import org.simantics.utils.strings.StringUtils;
35 * @author Tuukka Lehtonen
37 public class EvaluatorBindingExtensionManager implements IExtensionChangeHandler {
39 private final static String NAMESPACE = Activator.PLUGIN_ID;
41 private final static String EP_NAME = "evaluatorBinding";
43 private ExtensionTracker tracker;
45 private EvaluatorBindingExtension[] extensions = new EvaluatorBindingExtension[0];
47 private static EvaluatorBindingExtensionManager INSTANCE;
50 public static synchronized EvaluatorBindingExtensionManager getInstance() {
52 INSTANCE = new EvaluatorBindingExtensionManager();
56 public static synchronized void dispose() {
57 if (INSTANCE != null) {
64 private EvaluatorBindingExtensionManager() {
65 tracker = new ExtensionTracker();
67 // Cache defined actions
68 IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
69 loadExtensions(expt.getConfigurationElements());
71 // Start tracking for new and removed extensions
72 IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
73 tracker.registerHandler(this, filter);
76 private void close() {
79 extensions = new EvaluatorBindingExtension[0];
82 public EvaluatorBindingExtension[] getExtensions() {
86 private void loadExtensions(IConfigurationElement[] elements) {
88 Set<EvaluatorBindingExtension> newExtensions = new HashSet<EvaluatorBindingExtension>(Arrays.asList(extensions));
90 for (IConfigurationElement el : elements) {
92 String browseContext = StringUtils.safeString(el.getAttribute("browseContext"));
94 for(IConfigurationElement child : el.getChildren("reference")) {
96 String factoryId = StringUtils.safeString(child.getAttribute("id"));
97 EvaluatorBindingExtension ext = new EvaluatorReferenceBinding(browseContext, factoryId);
99 // Start tracking the new extension object, its removal will be notified of
100 // with removeExtension(extension, Object[]).
101 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
103 newExtensions.add(ext);
107 for(IConfigurationElement child : el.getChildren("implementation")) {
111 EvaluatorFactory factory = (EvaluatorFactory)child.createExecutableExtension("class");
112 EvaluatorBindingExtension ext = new EvaluatorImplementationBinding(browseContext, factory);
114 // Start tracking the new extension object, its removal will be notified of
115 // with removeExtension(extension, Object[]).
116 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
118 newExtensions.add(ext);
120 } catch (CoreException e) {
122 System.out.println(" == Could not load ViewpointContributionFactory '" + child.getAttribute("class") + "' due to the following error: " + e.getMessage() );
131 this.extensions = newExtensions.toArray(new EvaluatorBindingExtension[newExtensions.size()]);
135 public void addExtension(IExtensionTracker tracker, IExtension extension) {
136 loadExtensions(extension.getConfigurationElements());
140 public void removeExtension(IExtension extension, Object[] objects) {
141 Set<EvaluatorBindingExtension> newExtensions = new HashSet<EvaluatorBindingExtension>(Arrays.asList(extensions));
143 for (Object o : objects) {
144 tracker.unregisterObject(extension, o);
145 newExtensions.remove(o);
149 this.extensions = newExtensions.toArray(new EvaluatorBindingExtension[newExtensions.size()]);
152 public Collection<EvaluatorFactory> getBoundFactories(Set<String> browseContexts) {
154 // ArrayList<EvaluatorFactory> result = new ArrayList<EvaluatorFactory>();
155 // for(EvaluatorBindingExtension binding : getExtensions()) {
156 // if(browseContexts.contains(binding.browserId())) {
157 // EvaluatorFactory factory = EvaluatorFactoryExtensionManager.getFactory(binding.factoryId());
158 // result.add(factory);
163 HashSet<EvaluatorFactory> result = new HashSet<EvaluatorFactory>();
165 for(EvaluatorBindingExtension binding : getExtensions()) {
166 if(browseContexts.contains(binding.getBrowseContext())) {
167 EvaluatorFactory factory = binding.getFactory();
168 if(factory != null) {
169 // System.out.println("----------- Plugin contribution " + binding.getFactory());
172 // System.out.println("FAILED: ----------- No factory for " + binding);