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