--- /dev/null
+/*******************************************************************************\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.ui.workbench;\r
+\r
+import java.util.Arrays;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\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.ui.internal.Activator;\r
+import org.simantics.utils.datastructures.MapList;\r
+\r
+public class PerspectiveContextBindingManager implements IExtensionChangeHandler {\r
+\r
+ private final static String NAMESPACE = Activator.PLUGIN_ID;\r
+\r
+ private final static String EP_NAME = "perspectiveContextBinding";\r
+\r
+ private ExtensionTracker tracker;\r
+\r
+ private MapList<String, IPerspectiveContextExtension> extensions = new MapList<String, IPerspectiveContextExtension>();\r
+\r
+ private static PerspectiveContextBindingManager INSTANCE;\r
+\r
+ public static synchronized PerspectiveContextBindingManager getInstance() {\r
+ if (INSTANCE == null)\r
+ INSTANCE = new PerspectiveContextBindingManager();\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
+ private PerspectiveContextBindingManager() {\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
+ private void close() {\r
+ tracker.close();\r
+ tracker = null;\r
+ extensions = new MapList<String, IPerspectiveContextExtension>();\r
+ }\r
+\r
+ public synchronized List<IPerspectiveContextExtension> getExtensions(String perspectiveId) {\r
+ List<IPerspectiveContextExtension> exts = extensions.getValues(perspectiveId);\r
+ if (exts == null)\r
+ return Arrays.asList();\r
+ return Collections.unmodifiableList(exts);\r
+ }\r
+\r
+ private synchronized void loadExtensions(IConfigurationElement[] elements) {\r
+ for (IConfigurationElement el : elements) {\r
+ String perspectiveId = el.getAttribute("perspectiveId");\r
+ String contextId = el.getAttribute("contextIds");\r
+ String[] contextIds = contextId.split(",");\r
+\r
+ IPerspectiveContextExtension ext = new IPerspectiveContextExtension.Stub(perspectiveId, contextIds);\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
+ extensions.add(perspectiveId, ext);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void addExtension(IExtensionTracker tracker, IExtension extension) {\r
+ loadExtensions(extension.getConfigurationElements());\r
+ }\r
+\r
+ @Override\r
+ public synchronized void removeExtension(IExtension extension, Object[] objects) {\r
+ \r
+ for (Object o : objects) {\r
+ IPerspectiveContextExtension ext = (IPerspectiveContextExtension) o;\r
+ tracker.unregisterObject(extension, ext);\r
+ extensions.remove(ext.getPerspectiveId(), ext);\r
+ }\r
+\r
+ }\r
+\r
+}\r