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.selectionview;
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.scl.reflection.OntologyVersions;
29 import org.simantics.utils.strings.StringUtils;
32 * @author Tuukka Lehtonen
34 public class SelectionProcessorBindingExtensionManager implements IExtensionChangeHandler {
36 private final static String NAMESPACE = Activator.PLUGIN_ID;
38 private final static String EP_NAME = "selectionProcessorBinding";
40 private ExtensionTracker tracker;
42 private SelectionProcessorBindingExtension[] extensions = new SelectionProcessorBindingExtension[0];
44 SelectionProcessorBindingExtensionManager() {
45 tracker = new ExtensionTracker();
47 // Cache defined actions
48 IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
49 loadExtensions(expt.getConfigurationElements());
51 // Start tracking for new and removed extensions
52 IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
53 tracker.registerHandler(this, filter);
59 extensions = new SelectionProcessorBindingExtension[0];
62 public SelectionProcessorBindingExtension[] getExtensions() {
66 SelectionProcessorReferenceBinding createReferenceBinding(String browseContext, String factoryId) {
67 return new SelectionProcessorReferenceBinding(browseContext, factoryId);
71 private void loadExtensions(IConfigurationElement[] elements) {
73 Set<SelectionProcessorBindingExtension> newExtensions = new HashSet<SelectionProcessorBindingExtension>(Arrays.asList(extensions));
75 for (IConfigurationElement el : elements) {
77 String browseContext = OntologyVersions.getInstance().currentVersion(StringUtils.safeString(el.getAttribute("browseContext")));
79 for (IConfigurationElement child : el.getChildren("reference")) {
81 String factoryId = StringUtils.safeString(child.getAttribute("id"));
82 SelectionProcessorBindingExtension ext = createReferenceBinding(browseContext, factoryId);
84 // Start tracking the new extension object, its removal will be notified of
85 // with removeExtension(extension, Object[]).
86 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
88 newExtensions.add(ext);
92 for (IConfigurationElement child : el.getChildren("implementation")) {
96 SelectionProcessor<?, ?> processor = (SelectionProcessor<?, ?>) child.createExecutableExtension("class");
97 SelectionProcessorBindingExtension ext = new SelectionProcessorImplementationBinding(browseContext, processor);
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);
105 } catch (CoreException e) {
107 System.out.println(" == Could not load ViewpointContributionFactory '" + child.getAttribute("class") + "' due to the following error: " + e.getMessage() );
116 this.extensions = newExtensions.toArray(new SelectionProcessorBindingExtension[newExtensions.size()]);
120 public void addExtension(IExtensionTracker tracker, IExtension extension) {
121 loadExtensions(extension.getConfigurationElements());
125 public void removeExtension(IExtension extension, Object[] objects) {
126 Set<SelectionProcessorBindingExtension> newExtensions = new HashSet<SelectionProcessorBindingExtension>(Arrays.asList(extensions));
128 for (Object o : objects) {
129 tracker.unregisterObject(extension, o);
130 newExtensions.remove(o);
134 this.extensions = newExtensions.toArray(new SelectionProcessorBindingExtension[newExtensions.size()]);
137 public Set<SelectionProcessorBinding> getBoundContributions(Set<String> browseContexts) {
138 HashSet<SelectionProcessorBinding> result = new HashSet<SelectionProcessorBinding>();
140 for (SelectionProcessorBindingExtension binding : getExtensions()) {
141 if (browseContexts.contains(binding.getBrowseContext())) {
142 SelectionProcessor<?, ?> processor = binding.getProcessor();
143 if (processor != null) {
144 // System.out.println("----------- Plugin contribution " + binding.getFactory());
145 result.add(new SelectionProcessorBindingImpl(processor));
147 // System.out.println("FAILED: ----------- No factory for " + binding);
155 private static SelectionProcessorBindingExtensionManager INSTANCE;
157 public static synchronized SelectionProcessorBindingExtensionManager getInstance() {
158 if (INSTANCE == null)
159 INSTANCE = new SelectionProcessorBindingExtensionManager();
163 public static synchronized void dispose() {
164 if (INSTANCE != null) {