]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/SelectionProcessorBindingExtensionManager.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / SelectionProcessorBindingExtensionManager.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.selectionview;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.HashSet;\r
16 import java.util.Set;\r
17 \r
18 import org.eclipse.core.runtime.CoreException;\r
19 import org.eclipse.core.runtime.IConfigurationElement;\r
20 import org.eclipse.core.runtime.IExtension;\r
21 import org.eclipse.core.runtime.IExtensionPoint;\r
22 import org.eclipse.core.runtime.Platform;\r
23 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;\r
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;\r
25 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;\r
26 import org.eclipse.core.runtime.dynamichelpers.IFilter;\r
27 import org.simantics.browsing.ui.common.Activator;\r
28 import org.simantics.scl.reflection.OntologyVersions;\r
29 import org.simantics.utils.strings.StringUtils;\r
30 \r
31 /**\r
32  * @author Tuukka Lehtonen\r
33  */\r
34 public class SelectionProcessorBindingExtensionManager implements IExtensionChangeHandler {\r
35 \r
36     private final static String                  NAMESPACE  = Activator.PLUGIN_ID;\r
37 \r
38     private final static String                  EP_NAME    = "selectionProcessorBinding";\r
39 \r
40     private ExtensionTracker                     tracker;\r
41 \r
42     private SelectionProcessorBindingExtension[] extensions = new SelectionProcessorBindingExtension[0];\r
43 \r
44     SelectionProcessorBindingExtensionManager() {\r
45         tracker = new ExtensionTracker();\r
46 \r
47         // Cache defined actions\r
48         IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);\r
49         loadExtensions(expt.getConfigurationElements());\r
50 \r
51         // Start tracking for new and removed extensions\r
52         IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);\r
53         tracker.registerHandler(this, filter);\r
54     }\r
55 \r
56     void close() {\r
57         tracker.close();\r
58         tracker = null;\r
59         extensions = new SelectionProcessorBindingExtension[0];\r
60     }\r
61 \r
62     public SelectionProcessorBindingExtension[] getExtensions() {\r
63         return extensions;\r
64     }\r
65 \r
66     SelectionProcessorReferenceBinding createReferenceBinding(String browseContext, String factoryId) {\r
67         return new SelectionProcessorReferenceBinding(browseContext, factoryId);\r
68     }\r
69 \r
70 \r
71     private void loadExtensions(IConfigurationElement[] elements) {\r
72 \r
73         Set<SelectionProcessorBindingExtension> newExtensions = new HashSet<SelectionProcessorBindingExtension>(Arrays.asList(extensions));\r
74 \r
75         for (IConfigurationElement el : elements) {\r
76 \r
77             String browseContext = OntologyVersions.getInstance().currentVersion(StringUtils.safeString(el.getAttribute("browseContext")));\r
78 \r
79             for (IConfigurationElement child : el.getChildren("reference")) {\r
80 \r
81                 String factoryId = StringUtils.safeString(child.getAttribute("id"));\r
82                 SelectionProcessorBindingExtension ext = createReferenceBinding(browseContext, factoryId);\r
83 \r
84                 // Start tracking the new extension object, its removal will be notified of\r
85                 // with removeExtension(extension, Object[]).\r
86                 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);\r
87 \r
88                 newExtensions.add(ext);\r
89 \r
90             }\r
91 \r
92             for (IConfigurationElement child : el.getChildren("implementation")) {\r
93 \r
94                 try {\r
95 \r
96                     SelectionProcessor<?, ?> processor = (SelectionProcessor<?, ?>) child.createExecutableExtension("class");\r
97                     SelectionProcessorBindingExtension ext = new SelectionProcessorImplementationBinding(browseContext, processor);\r
98 \r
99                     // Start tracking the new extension object, its removal will be notified of\r
100                     // with removeExtension(extension, Object[]).\r
101                     tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);\r
102 \r
103                     newExtensions.add(ext);\r
104 \r
105                 } catch (CoreException e) {\r
106 \r
107                     System.out.println(" == Could not load ViewpointContributionFactory '" + child.getAttribute("class") + "' due to the following error: " + e.getMessage()  );\r
108 \r
109                 }\r
110 \r
111             }\r
112 \r
113         }\r
114 \r
115         // Atomic assignment\r
116         this.extensions = newExtensions.toArray(new SelectionProcessorBindingExtension[newExtensions.size()]);\r
117     }\r
118 \r
119     @Override\r
120     public void addExtension(IExtensionTracker tracker, IExtension extension) {\r
121         loadExtensions(extension.getConfigurationElements());\r
122     }\r
123 \r
124     @Override\r
125     public void removeExtension(IExtension extension, Object[] objects) {\r
126         Set<SelectionProcessorBindingExtension> newExtensions = new HashSet<SelectionProcessorBindingExtension>(Arrays.asList(extensions));\r
127 \r
128         for (Object o : objects) {\r
129             tracker.unregisterObject(extension, o);\r
130             newExtensions.remove(o);\r
131         }\r
132 \r
133         // Atomic assignment\r
134         this.extensions = newExtensions.toArray(new SelectionProcessorBindingExtension[newExtensions.size()]);\r
135     }\r
136 \r
137     public Set<SelectionProcessorBinding> getBoundContributions(Set<String> browseContexts) {\r
138         HashSet<SelectionProcessorBinding> result = new HashSet<SelectionProcessorBinding>();\r
139 \r
140         for (SelectionProcessorBindingExtension binding : getExtensions()) {\r
141             if (browseContexts.contains(binding.getBrowseContext())) {\r
142                 SelectionProcessor<?, ?> processor = binding.getProcessor();\r
143                 if (processor != null) {\r
144 //                    System.out.println("----------- Plugin contribution " + binding.getFactory());\r
145                     result.add(new SelectionProcessorBindingImpl(processor));\r
146                 } else {\r
147 //                    System.out.println("FAILED: ----------- No factory for " + binding);\r
148                 }\r
149             }\r
150         }\r
151 \r
152         return result;\r
153     }\r
154 \r
155     private static SelectionProcessorBindingExtensionManager INSTANCE;\r
156 \r
157     public static synchronized SelectionProcessorBindingExtensionManager getInstance() {\r
158         if (INSTANCE == null)\r
159             INSTANCE = new SelectionProcessorBindingExtensionManager();\r
160         return INSTANCE;\r
161     }\r
162 \r
163     public static synchronized void dispose() {\r
164         if (INSTANCE != null) {\r
165             INSTANCE.close();\r
166             INSTANCE = null;\r
167         }\r
168     }\r
169 \r
170 }\r