]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/PerspectiveBarsManager.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / PerspectiveBarsManager.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 PerspectiveBarsManager implements IExtensionChangeHandler {
30
31     private final static String                           NAMESPACE  = Activator.PLUGIN_ID;
32
33     private final static String                           EP_NAME    = "perspectiveBars";
34
35     private ExtensionTracker                              tracker;
36
37     private MapList<String, IPerspectiveBarsExtension> extensions = new MapList<String, IPerspectiveBarsExtension>();
38
39     private static PerspectiveBarsManager       INSTANCE;
40
41     public static synchronized PerspectiveBarsManager getInstance() {
42         if (INSTANCE == null)
43             INSTANCE = new PerspectiveBarsManager();
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 PerspectiveBarsManager() {
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, IPerspectiveBarsExtension>();
70     }
71
72     public synchronized List<IPerspectiveBarsExtension> getExtensions(String perspectiveId) {
73         List<IPerspectiveBarsExtension> 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             Boolean menuBar = toBoolean(el.getAttribute("menuBar"));
83             Boolean coolBar = toBoolean(el.getAttribute("coolBar"));
84             Boolean statusLine = toBoolean(el.getAttribute("statusLine"));
85             Boolean perspectiveBar = toBoolean(el.getAttribute("perspectiveBar"));
86             Boolean fastViewBar = toBoolean(el.getAttribute("fastViewBar"));
87             Boolean progressIndicator = toBoolean(el.getAttribute("progressIndicator"));
88
89             IPerspectiveBarsExtension ext = new IPerspectiveBarsExtension.Stub(perspectiveId,menuBar,coolBar,statusLine,perspectiveBar,fastViewBar,progressIndicator);
90
91             // Start tracking the new extension object, its removal will be notified of
92             // with removeExtension(extension, Object[]).
93             tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
94
95             extensions.add(perspectiveId, ext);
96         }
97     }
98     
99     private Boolean toBoolean(String s) {
100         if (s == null)
101                 return null;
102         return Boolean.parseBoolean(s);
103     }
104
105     @Override
106     public void addExtension(IExtensionTracker tracker, IExtension extension) {
107         loadExtensions(extension.getConfigurationElements());
108     }
109
110     @Override
111     public synchronized void removeExtension(IExtension extension, Object[] objects) {
112         
113         for (Object o : objects) {
114             IPerspectiveBarsExtension ext = (IPerspectiveBarsExtension) o;
115             tracker.unregisterObject(extension, ext);
116             extensions.remove(ext.getPerspectiveId(), ext);
117         }
118
119     }
120
121 }