+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.selectionview;\r
+\r
+import java.util.Arrays;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.eclipse.core.runtime.CoreException;\r
+import org.eclipse.core.runtime.IConfigurationElement;\r
+import org.eclipse.core.runtime.IExtension;\r
+import org.eclipse.core.runtime.IExtensionPoint;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;\r
+import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;\r
+import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;\r
+import org.eclipse.core.runtime.dynamichelpers.IFilter;\r
+import org.simantics.browsing.ui.common.Activator;\r
+import org.simantics.scl.reflection.OntologyVersions;\r
+import org.simantics.utils.strings.StringUtils;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SelectionProcessorBindingExtensionManager implements IExtensionChangeHandler {\r
+\r
+ private final static String NAMESPACE = Activator.PLUGIN_ID;\r
+\r
+ private final static String EP_NAME = "selectionProcessorBinding";\r
+\r
+ private ExtensionTracker tracker;\r
+\r
+ private SelectionProcessorBindingExtension[] extensions = new SelectionProcessorBindingExtension[0];\r
+\r
+ SelectionProcessorBindingExtensionManager() {\r
+ tracker = new ExtensionTracker();\r
+\r
+ // Cache defined actions\r
+ IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);\r
+ loadExtensions(expt.getConfigurationElements());\r
+\r
+ // Start tracking for new and removed extensions\r
+ IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);\r
+ tracker.registerHandler(this, filter);\r
+ }\r
+\r
+ void close() {\r
+ tracker.close();\r
+ tracker = null;\r
+ extensions = new SelectionProcessorBindingExtension[0];\r
+ }\r
+\r
+ public SelectionProcessorBindingExtension[] getExtensions() {\r
+ return extensions;\r
+ }\r
+\r
+ SelectionProcessorReferenceBinding createReferenceBinding(String browseContext, String factoryId) {\r
+ return new SelectionProcessorReferenceBinding(browseContext, factoryId);\r
+ }\r
+\r
+\r
+ private void loadExtensions(IConfigurationElement[] elements) {\r
+\r
+ Set<SelectionProcessorBindingExtension> newExtensions = new HashSet<SelectionProcessorBindingExtension>(Arrays.asList(extensions));\r
+\r
+ for (IConfigurationElement el : elements) {\r
+\r
+ String browseContext = OntologyVersions.getInstance().currentVersion(StringUtils.safeString(el.getAttribute("browseContext")));\r
+\r
+ for (IConfigurationElement child : el.getChildren("reference")) {\r
+\r
+ String factoryId = StringUtils.safeString(child.getAttribute("id"));\r
+ SelectionProcessorBindingExtension ext = createReferenceBinding(browseContext, factoryId);\r
+\r
+ // Start tracking the new extension object, its removal will be notified of\r
+ // with removeExtension(extension, Object[]).\r
+ tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);\r
+\r
+ newExtensions.add(ext);\r
+\r
+ }\r
+\r
+ for (IConfigurationElement child : el.getChildren("implementation")) {\r
+\r
+ try {\r
+\r
+ SelectionProcessor<?, ?> processor = (SelectionProcessor<?, ?>) child.createExecutableExtension("class");\r
+ SelectionProcessorBindingExtension ext = new SelectionProcessorImplementationBinding(browseContext, processor);\r
+\r
+ // Start tracking the new extension object, its removal will be notified of\r
+ // with removeExtension(extension, Object[]).\r
+ tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);\r
+\r
+ newExtensions.add(ext);\r
+\r
+ } catch (CoreException e) {\r
+\r
+ System.out.println(" == Could not load ViewpointContributionFactory '" + child.getAttribute("class") + "' due to the following error: " + e.getMessage() );\r
+\r
+ }\r
+\r
+ }\r
+\r
+ }\r
+\r
+ // Atomic assignment\r
+ this.extensions = newExtensions.toArray(new SelectionProcessorBindingExtension[newExtensions.size()]);\r
+ }\r
+\r
+ @Override\r
+ public void addExtension(IExtensionTracker tracker, IExtension extension) {\r
+ loadExtensions(extension.getConfigurationElements());\r
+ }\r
+\r
+ @Override\r
+ public void removeExtension(IExtension extension, Object[] objects) {\r
+ Set<SelectionProcessorBindingExtension> newExtensions = new HashSet<SelectionProcessorBindingExtension>(Arrays.asList(extensions));\r
+\r
+ for (Object o : objects) {\r
+ tracker.unregisterObject(extension, o);\r
+ newExtensions.remove(o);\r
+ }\r
+\r
+ // Atomic assignment\r
+ this.extensions = newExtensions.toArray(new SelectionProcessorBindingExtension[newExtensions.size()]);\r
+ }\r
+\r
+ public Set<SelectionProcessorBinding> getBoundContributions(Set<String> browseContexts) {\r
+ HashSet<SelectionProcessorBinding> result = new HashSet<SelectionProcessorBinding>();\r
+\r
+ for (SelectionProcessorBindingExtension binding : getExtensions()) {\r
+ if (browseContexts.contains(binding.getBrowseContext())) {\r
+ SelectionProcessor<?, ?> processor = binding.getProcessor();\r
+ if (processor != null) {\r
+// System.out.println("----------- Plugin contribution " + binding.getFactory());\r
+ result.add(new SelectionProcessorBindingImpl(processor));\r
+ } else {\r
+// System.out.println("FAILED: ----------- No factory for " + binding);\r
+ }\r
+ }\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ private static SelectionProcessorBindingExtensionManager INSTANCE;\r
+\r
+ public static synchronized SelectionProcessorBindingExtensionManager getInstance() {\r
+ if (INSTANCE == null)\r
+ INSTANCE = new SelectionProcessorBindingExtensionManager();\r
+ return INSTANCE;\r
+ }\r
+\r
+ public static synchronized void dispose() {\r
+ if (INSTANCE != null) {\r
+ INSTANCE.close();\r
+ INSTANCE = null;\r
+ }\r
+ }\r
+\r
+}\r