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.ui.workbench;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IExtension;
20 import org.eclipse.core.runtime.IExtensionPoint;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
23 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
25 import org.eclipse.core.runtime.dynamichelpers.IFilter;
26 import org.simantics.ui.internal.Activator;
27 import org.simantics.utils.datastructures.MapList;
29 public class PerspectiveContextBindingManager implements IExtensionChangeHandler {
31 private final static String NAMESPACE = Activator.PLUGIN_ID;
33 private final static String EP_NAME = "perspectiveContextBinding";
35 private ExtensionTracker tracker;
37 private MapList<String, IPerspectiveContextExtension> extensions = new MapList<String, IPerspectiveContextExtension>();
39 private static PerspectiveContextBindingManager INSTANCE;
41 public static synchronized PerspectiveContextBindingManager getInstance() {
43 INSTANCE = new PerspectiveContextBindingManager();
47 public static synchronized void dispose() {
48 if (INSTANCE != null) {
54 private PerspectiveContextBindingManager() {
55 tracker = new ExtensionTracker();
57 // Cache defined actions
58 IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
59 loadExtensions(expt.getConfigurationElements());
61 // Start tracking for new and removed extensions
62 IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
63 tracker.registerHandler(this, filter);
66 private void close() {
69 extensions = new MapList<String, IPerspectiveContextExtension>();
72 public synchronized List<IPerspectiveContextExtension> getExtensions(String perspectiveId) {
73 List<IPerspectiveContextExtension> exts = extensions.getValues(perspectiveId);
75 return Arrays.asList();
76 return Collections.unmodifiableList(exts);
79 private synchronized void loadExtensions(IConfigurationElement[] elements) {
80 for (IConfigurationElement el : elements) {
81 String perspectiveId = el.getAttribute("perspectiveId");
82 String contextId = el.getAttribute("contextIds");
83 String[] contextIds = contextId.split(",");
85 IPerspectiveContextExtension ext = new IPerspectiveContextExtension.Stub(perspectiveId, contextIds);
87 // Start tracking the new extension object, its removal will be notified of
88 // with removeExtension(extension, Object[]).
89 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
91 extensions.add(perspectiveId, ext);
96 public void addExtension(IExtensionTracker tracker, IExtension extension) {
97 loadExtensions(extension.getConfigurationElements());
101 public synchronized void removeExtension(IExtension extension, Object[] objects) {
103 for (Object o : objects) {
104 IPerspectiveContextExtension ext = (IPerspectiveContextExtension) o;
105 tracker.unregisterObject(extension, ext);
106 extensions.remove(ext.getPerspectiveId(), ext);