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