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