]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/PerspectiveContextBindingManager.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / PerspectiveContextBindingManager.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.workbench;
13
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
17
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;
28
29 public class PerspectiveContextBindingManager implements IExtensionChangeHandler {
30
31     private final static String                           NAMESPACE  = Activator.PLUGIN_ID;
32
33     private final static String                           EP_NAME    = "perspectiveContextBinding";
34
35     private ExtensionTracker                              tracker;
36
37     private MapList<String, IPerspectiveContextExtension> extensions = new MapList<String, IPerspectiveContextExtension>();
38
39     private static PerspectiveContextBindingManager       INSTANCE;
40
41     public static synchronized PerspectiveContextBindingManager getInstance() {
42         if (INSTANCE == null)
43             INSTANCE = new PerspectiveContextBindingManager();
44         return INSTANCE;
45     }
46
47     public static synchronized void dispose() {
48         if (INSTANCE != null) {
49             INSTANCE.close();
50             INSTANCE = null;
51         }
52     }
53
54     private PerspectiveContextBindingManager() {
55         tracker = new ExtensionTracker();
56
57         // Cache defined actions
58         IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
59         loadExtensions(expt.getConfigurationElements());
60
61         // Start tracking for new and removed extensions
62         IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
63         tracker.registerHandler(this, filter);
64     }
65
66     private void close() {
67         tracker.close();
68         tracker = null;
69         extensions = new MapList<String, IPerspectiveContextExtension>();
70     }
71
72     public synchronized List<IPerspectiveContextExtension> getExtensions(String perspectiveId) {
73         List<IPerspectiveContextExtension> exts = extensions.getValues(perspectiveId);
74         if (exts == null)
75             return Arrays.asList();
76         return Collections.unmodifiableList(exts);
77     }
78
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(",");
84
85             IPerspectiveContextExtension ext = new IPerspectiveContextExtension.Stub(perspectiveId, contextIds);
86
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);
90
91             extensions.add(perspectiveId, ext);
92         }
93     }
94
95     @Override
96     public void addExtension(IExtensionTracker tracker, IExtension extension) {
97         loadExtensions(extension.getConfigurationElements());
98     }
99
100     @Override
101     public synchronized void removeExtension(IExtension extension, Object[] objects) {
102        
103         for (Object o : objects) {
104             IPerspectiveContextExtension ext = (IPerspectiveContextExtension) o;
105             tracker.unregisterObject(extension, ext);
106             extensions.remove(ext.getPerspectiveId(), ext);
107         }
108
109     }
110
111 }