]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/PerspectiveBarsManager.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / PerspectiveBarsManager.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.workbench;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.Collections;\r
16 import java.util.List;\r
17 \r
18 import org.eclipse.core.runtime.IConfigurationElement;\r
19 import org.eclipse.core.runtime.IExtension;\r
20 import org.eclipse.core.runtime.IExtensionPoint;\r
21 import org.eclipse.core.runtime.Platform;\r
22 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;\r
23 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;\r
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;\r
25 import org.eclipse.core.runtime.dynamichelpers.IFilter;\r
26 import org.simantics.ui.internal.Activator;\r
27 import org.simantics.utils.datastructures.MapList;\r
28 \r
29 public class PerspectiveBarsManager implements IExtensionChangeHandler {\r
30 \r
31     private final static String                           NAMESPACE  = Activator.PLUGIN_ID;\r
32 \r
33     private final static String                           EP_NAME    = "perspectiveBars";\r
34 \r
35     private ExtensionTracker                              tracker;\r
36 \r
37     private MapList<String, IPerspectiveBarsExtension> extensions = new MapList<String, IPerspectiveBarsExtension>();\r
38 \r
39     private static PerspectiveBarsManager       INSTANCE;\r
40 \r
41     public static synchronized PerspectiveBarsManager getInstance() {\r
42         if (INSTANCE == null)\r
43             INSTANCE = new PerspectiveBarsManager();\r
44         return INSTANCE;\r
45     }\r
46 \r
47     public static synchronized void dispose() {\r
48         if (INSTANCE != null) {\r
49             INSTANCE.close();\r
50             INSTANCE = null;\r
51         }\r
52     }\r
53 \r
54     private PerspectiveBarsManager() {\r
55         tracker = new ExtensionTracker();\r
56 \r
57         // Cache defined actions\r
58         IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);\r
59         loadExtensions(expt.getConfigurationElements());\r
60 \r
61         // Start tracking for new and removed extensions\r
62         IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);\r
63         tracker.registerHandler(this, filter);\r
64     }\r
65 \r
66     private void close() {\r
67         tracker.close();\r
68         tracker = null;\r
69         extensions = new MapList<String, IPerspectiveBarsExtension>();\r
70     }\r
71 \r
72     public synchronized List<IPerspectiveBarsExtension> getExtensions(String perspectiveId) {\r
73         List<IPerspectiveBarsExtension> exts = extensions.getValues(perspectiveId);\r
74         if (exts == null)\r
75             return Arrays.asList();\r
76         return Collections.unmodifiableList(exts);\r
77     }\r
78 \r
79     private synchronized void loadExtensions(IConfigurationElement[] elements) {\r
80         for (IConfigurationElement el : elements) {\r
81             String perspectiveId = el.getAttribute("perspectiveId");\r
82             Boolean menuBar = toBoolean(el.getAttribute("menuBar"));\r
83             Boolean coolBar = toBoolean(el.getAttribute("coolBar"));\r
84             Boolean statusLine = toBoolean(el.getAttribute("statusLine"));\r
85             Boolean perspectiveBar = toBoolean(el.getAttribute("perspectiveBar"));\r
86             Boolean fastViewBar = toBoolean(el.getAttribute("fastViewBar"));\r
87             Boolean progressIndicator = toBoolean(el.getAttribute("progressIndicator"));\r
88 \r
89             IPerspectiveBarsExtension ext = new IPerspectiveBarsExtension.Stub(perspectiveId,menuBar,coolBar,statusLine,perspectiveBar,fastViewBar,progressIndicator);\r
90 \r
91             // Start tracking the new extension object, its removal will be notified of\r
92             // with removeExtension(extension, Object[]).\r
93             tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);\r
94 \r
95             extensions.add(perspectiveId, ext);\r
96         }\r
97     }\r
98     \r
99     private Boolean toBoolean(String s) {\r
100         if (s == null)\r
101                 return null;\r
102         return Boolean.parseBoolean(s);\r
103     }\r
104 \r
105     @Override\r
106     public void addExtension(IExtensionTracker tracker, IExtension extension) {\r
107         loadExtensions(extension.getConfigurationElements());\r
108     }\r
109 \r
110     @Override\r
111     public synchronized void removeExtension(IExtension extension, Object[] objects) {\r
112         \r
113         for (Object o : objects) {\r
114             IPerspectiveBarsExtension ext = (IPerspectiveBarsExtension) o;\r
115             tracker.unregisterObject(extension, ext);\r
116             extensions.remove(ext.getPerspectiveId(), ext);\r
117         }\r
118 \r
119     }\r
120 \r
121 }\r