]> gerrit.simantics Code Review - simantics/platform.git/blob
b0e09381fb351108321409da8e3c7e6286d2716b
[simantics/platform.git] /
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.graph.impl;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jface.resource.ResourceManager;
22 import org.simantics.browsing.ui.BuiltinKeys.CheckedStateKey;
23 import org.simantics.browsing.ui.BuiltinKeys.ImageDecoratorKey;
24 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
25 import org.simantics.browsing.ui.BuiltinKeys.LabelDecoratorKey;
26 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
27 import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;
28 import org.simantics.browsing.ui.CheckedState;
29 import org.simantics.browsing.ui.NodeContext;
30 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
31 import org.simantics.browsing.ui.Tester;
32 import org.simantics.browsing.ui.common.EvaluatorData;
33 import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;
34 import org.simantics.browsing.ui.common.EvaluatorData.EvaluatorTree;
35 import org.simantics.browsing.ui.common.EvaluatorDataImpl;
36 import org.simantics.browsing.ui.common.EvaluatorImpl;
37 import org.simantics.browsing.ui.common.exception.InvalidBrowserIdException;
38 import org.simantics.browsing.ui.common.extension.CheckedStateContributorBindingExtensionManager;
39 import org.simantics.browsing.ui.common.extension.ComparableContextContributorBindingExtensionManager;
40 import org.simantics.browsing.ui.common.extension.EvaluatorBindingExtensionManager;
41 import org.simantics.browsing.ui.common.extension.EvaluatorFactory;
42 import org.simantics.browsing.ui.common.extension.ImageDecoratorContributorBindingExtensionManager;
43 import org.simantics.browsing.ui.common.extension.ImagerContributorBindingExtensionManager;
44 import org.simantics.browsing.ui.common.extension.LabelDecoratorContributorBindingExtensionManager;
45 import org.simantics.browsing.ui.common.extension.LabelerContributorBindingExtensionManager;
46 import org.simantics.browsing.ui.common.extension.ViewpointContributionContributorBindingExtensionManager;
47 import org.simantics.browsing.ui.content.CheckedStateFactory;
48 import org.simantics.browsing.ui.content.ComparableContextFactory;
49 import org.simantics.browsing.ui.content.ContributorBinding;
50 import org.simantics.browsing.ui.content.ImageDecorator;
51 import org.simantics.browsing.ui.content.ImageDecoratorFactory;
52 import org.simantics.browsing.ui.content.Imager;
53 import org.simantics.browsing.ui.content.ImagerFactory;
54 import org.simantics.browsing.ui.content.LabelDecorator;
55 import org.simantics.browsing.ui.content.LabelDecoratorFactory;
56 import org.simantics.browsing.ui.content.Labeler;
57 import org.simantics.browsing.ui.content.LabelerFactory;
58 import org.simantics.browsing.ui.content.Viewpoint;
59 import org.simantics.browsing.ui.content.ViewpointContributionFactory;
60 import org.simantics.browsing.ui.content.ViewpointFactory;
61 import org.simantics.browsing.ui.model.InvalidContribution;
62 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
63 import org.simantics.db.ReadGraph;
64 import org.simantics.db.RequestProcessor;
65 import org.simantics.db.Resource;
66 import org.simantics.db.common.request.ReadRequest;
67 import org.simantics.db.exception.DatabaseException;
68 import org.simantics.db.exception.ResourceNotFoundException;
69
70 /**
71  * An entry point for loading an EvaluatorData in a data-oriented fashion. Also
72  * adds contributions made by extensions of the related extension points.
73  * 
74  * @author Antti Villberg
75  */
76 public class Evaluators {
77
78     public static final boolean DEBUG = false;
79
80     public static void create(EvaluatorData data, final ResourceManager resourceManager,
81             final Map<String, Collection<ContributorBinding<ViewpointContributionFactory>>> viewpointContributions,
82             final Set<ContributorBinding<ComparableContextFactory>> comparableContextContributions,
83             final Set<ContributorBinding<LabelerFactory>> labelerContributions,
84             final Set<ContributorBinding<CheckedStateFactory>> checkStateContributions,
85             final Set<ContributorBinding<LabelDecoratorFactory>> labelDecoratorContributions,
86             final Set<ContributorBinding<ImagerFactory>> imagerContributions,
87             final Set<ContributorBinding<ImageDecoratorFactory>> imageDecoratorContributions) {
88
89         Map<Class<?>, Evaluator> evaluators = new HashMap<Class<?>, Evaluator>();
90
91         HashMap<String, ContributionViewpointFactory> viewpointFactories = new HashMap<String, ContributionViewpointFactory>();
92
93         for(final Map.Entry<String, Collection<ContributorBinding<ViewpointContributionFactory>>> entry : viewpointContributions.entrySet()) {
94             Collection<ViewpointContributionFactory> factories = new ArrayList<ViewpointContributionFactory>();
95             for(ContributorBinding<ViewpointContributionFactory> binding : entry.getValue()) factories.add(binding.getContributor().getFactory());
96             viewpointFactories.put(entry.getKey(), new ContributionViewpointFactory(entry.getKey(), factories));
97         }
98
99         for(final Map.Entry<String, Collection<ContributorBinding<ViewpointContributionFactory>>> entry : viewpointContributions.entrySet()) {
100             ContributionViewpointFactory factory = viewpointFactories.get(entry.getKey());
101             for(ContributorBinding<ViewpointContributionFactory> binding : entry.getValue()) {
102                 Tester test = binding.getContributor().getNodeContextTester();
103                 Class<?> clazz = binding.getContributor().getInputClass();
104                 Evaluator evaluator = evaluators.get(clazz);
105                 if(evaluator == null) {
106                     evaluator = data.newEvaluator();
107                     evaluators.put(clazz, evaluator);
108                 }
109                 if(test == null) {
110                     if (DEBUG)
111                         System.out.println("add root viewpoint " + factory + " to class " + clazz.getSimpleName());
112                     evaluator.addViewpoint(factory, binding.getPreference());
113                 } else {
114                     if (DEBUG)
115                         System.out.println("add viewpoint " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
116                     EvaluatorTree<ViewpointFactory> branch = ((EvaluatorImpl)evaluator).getViewpointTree().addBranch(test);
117                     branch.addFactory(factory, binding.getPreference());
118                 }
119             }
120         }
121
122         for(final ContributorBinding<ComparableContextFactory> binding : comparableContextContributions) {
123
124             ComparableContextFactory factory = binding.getContributor().getFactory();
125             Tester test = binding.getContributor().getNodeContextTester();
126             Class<?> clazz = binding.getContributor().getInputClass();
127             Evaluator evaluator = evaluators.get(clazz);
128             if(evaluator == null) {
129                 evaluator = data.newEvaluator();
130                 evaluators.put(clazz, evaluator);
131             }
132             if(test == null) {
133                 if (DEBUG)
134                     System.out.println("add root comparable context " + factory + " to class " + clazz.getSimpleName());
135                 evaluator.addComparableContext(factory, binding.getPreference());
136             } else {
137                 if (DEBUG)
138                     System.out.println("add comparable context " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
139                 EvaluatorTree<ComparableContextFactory> branch = ((EvaluatorImpl)evaluator).getComparableContextTree().addBranch(test);
140                 branch.addFactory(factory, binding.getPreference());
141             }
142         }
143
144         for(final ContributorBinding<LabelerFactory> binding : labelerContributions) {
145
146             LabelerFactory factory = binding.getContributor().getFactory();
147             Tester test = binding.getContributor().getNodeContextTester();
148             Class<?> clazz = binding.getContributor().getInputClass();
149             Evaluator evaluator = evaluators.get(clazz);
150             if(evaluator == null) {
151                 evaluator = data.newEvaluator();
152                 evaluators.put(clazz, evaluator);
153             }
154             if(test == null) {
155                 if (DEBUG)
156                     System.out.println("add root labeler " + factory + " to class " + clazz.getSimpleName());
157                 evaluator.addLabeler(factory, binding.getPreference());
158             } else {
159                 if (DEBUG)
160                     System.out.println("add labeler " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
161                 EvaluatorTree<LabelerFactory> branch = ((EvaluatorImpl)evaluator).getLabelerTree().addBranch(test);
162                 branch.addFactory(factory, binding.getPreference());
163             }
164         }
165
166         for(final ContributorBinding<CheckedStateFactory> binding : checkStateContributions) {
167
168             CheckedStateFactory factory = binding.getContributor().getFactory();
169             Tester test = binding.getContributor().getNodeContextTester();
170             Class<?> clazz = binding.getContributor().getInputClass();
171             Evaluator evaluator = evaluators.get(clazz);
172             if(evaluator == null) {
173                 evaluator = data.newEvaluator();
174                 evaluators.put(clazz, evaluator);
175             }
176             if(test == null) {
177                 if (DEBUG)
178                     System.out.println("add root check state " + factory + " to class " + clazz.getSimpleName());
179                 evaluator.addCheckState(factory, binding.getPreference());
180             } else {
181                 if (DEBUG)
182                     System.out.println("add check state " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
183                 EvaluatorTree<CheckedStateFactory> branch = ((EvaluatorImpl)evaluator).getCheckStateTree().addBranch(test);
184                 branch.addFactory(factory, binding.getPreference());
185             }
186         }
187
188         for(final ContributorBinding<LabelDecoratorFactory> binding : labelDecoratorContributions) {
189
190             LabelDecoratorFactory factory = binding.getContributor().getFactory();
191             Tester test = binding.getContributor().getNodeContextTester();
192             Class<?> clazz = binding.getContributor().getInputClass();
193             Evaluator evaluator = evaluators.get(clazz);
194             if(evaluator == null) {
195                 evaluator = data.newEvaluator();
196                 evaluators.put(clazz, evaluator);
197             }
198             if(test == null) {
199                 if (DEBUG)
200                     System.out.println("add root label decorator " + factory + " to class " + clazz.getSimpleName());
201                 evaluator.addLabelDecorator(factory, binding.getPreference());
202             } else {
203                 if (DEBUG)
204                     System.out.println("add label decorator " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
205                 EvaluatorTree<LabelDecoratorFactory> branch = ((EvaluatorImpl)evaluator).getLabelDecoratorTree().addBranch(test);
206                 branch.addFactory(factory, binding.getPreference());
207             }
208         }
209
210         for(final ContributorBinding<ImagerFactory> binding : imagerContributions) {
211
212             ImagerFactory factory = binding.getContributor().getFactory();
213             Tester test = binding.getContributor().getNodeContextTester();
214             Class<?> clazz = binding.getContributor().getInputClass();
215             Evaluator evaluator = evaluators.get(clazz);
216             if(evaluator == null) {
217                 evaluator = data.newEvaluator();
218                 evaluators.put(clazz, evaluator);
219             }
220             if(test == null) {
221                 if (DEBUG)
222                     System.out.println("add root imager " + factory + " to class " + clazz.getSimpleName());
223                 evaluator.addImager(factory, binding.getPreference());
224             } else {
225                 if (DEBUG)
226                     System.out.println("add imager " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
227                 EvaluatorTree<ImagerFactory> branch = ((EvaluatorImpl)evaluator).getImagerTree().addBranch(test);
228                 branch.addFactory(factory, binding.getPreference());
229             }
230         }
231
232         for(final ContributorBinding<ImageDecoratorFactory> binding : imageDecoratorContributions) {
233
234             ImageDecoratorFactory factory = binding.getContributor().getFactory();
235             Tester test = binding.getContributor().getNodeContextTester();
236             Class<?> clazz = binding.getContributor().getInputClass();
237             Evaluator evaluator = evaluators.get(clazz);
238             if(evaluator == null) {
239                 evaluator = data.newEvaluator();
240                 evaluators.put(clazz, evaluator);
241             }
242             if(test == null) {
243                 if (DEBUG)
244                     System.out.println("add root image decorator " + factory + " to class " + clazz.getSimpleName());
245                 evaluator.addImageDecorator(factory, binding.getPreference());
246             } else {
247                 if (DEBUG)
248                     System.out.println("add image decorator " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
249                 EvaluatorTree<ImageDecoratorFactory> branch = ((EvaluatorImpl)evaluator).getImageDecoratorTree().addBranch(test);
250                 branch.addFactory(factory, binding.getPreference());
251             }
252         }
253
254         for(Map.Entry<Class<?>, Evaluator> entry : evaluators.entrySet()) {
255             data.addEvaluator(entry.getKey(), entry.getValue());
256         }
257
258     }
259
260     /**
261      * @param processor
262      * @param browseContexts
263      * @param resourceManager
264      * @param data
265      * @return the URIs of the actual set of browse contexts loaded by this
266      *         method. If an included browse context resource has no URI, it
267      *         will not be included in this set.
268      */
269     public static Set<String> loadModelled(
270             RequestProcessor processor, 
271             final Set<String> browseContexts, 
272             final ResourceManager resourceManager,
273             final EvaluatorDataImpl data,
274             final boolean useNodeBrowseContexts,
275             final boolean useNodeActionContexts) {
276
277         final Set<String> allBrowseContexts = new HashSet<String>(browseContexts);
278
279         // Create evaluator
280         final EvaluatorImpl eval = new EvaluatorImpl();
281         data.addEvaluator(Object.class, eval);  
282         
283         try {
284
285             // Load data for evaluator
286             processor.syncRequest(new ReadRequest() {
287                 
288                 @Override
289                 public void run(ReadGraph graph) throws DatabaseException {
290
291                     // Map browse context names to resources
292                     Collection<Resource> browseContextResources = new ArrayList<Resource>(browseContexts.size());
293                     for(String browseContext : browseContexts) {
294                         try {
295                             browseContextResources.add(graph.getResource(browseContext));
296                         } catch(ResourceNotFoundException e) {
297                             // Expected result, if no modelled contributions exist.
298                             //System.err.println("Didn't find " + browseContext + " while loading model browser.");
299                         } catch(DatabaseException e) {
300                             System.err.println("Didn't find " + browseContext + " while loading model browser.");
301                             e.printStackTrace();
302                         }
303                     }
304
305                     // Load browse context
306                     try {
307                         final BrowseContext browseContext = 
308                             BrowseContext.create(graph, browseContextResources);
309                         
310                         data.setBrowseContext(browseContext);
311
312                         // Get URI's for all found browse contexts to fill return value
313                         for (Resource context : BrowseContext.findSubcontexts(graph, browseContextResources)) {
314                             String uri = graph.getPossibleURI(context);
315                             if (uri != null)
316                                 allBrowseContexts.add(uri);
317                         }
318
319                         // Fill evaluator
320                         eval.addLabeler(new LabelerFactory() {
321                             @Override
322                             public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context,
323                                     LabelerKey key) {
324                                 return new EvaluatorLabeler(updater, context, key, browseContext, useNodeBrowseContexts);
325                             }
326                         }, 0.0);
327
328                         eval.addImager(new ImagerFactory() {
329                             @Override
330                             public Imager create(PrimitiveQueryUpdater updater, NodeContext context,
331                                     ImagerKey key) {
332                                 return new EvaluatorImager(updater, context, key, browseContext, useNodeBrowseContexts);
333                             }
334                         }, 0.0);
335
336                         eval.addCheckState(new CheckedStateFactory() {
337
338                             @Override
339                             public CheckedState create(PrimitiveQueryUpdater updater,
340                                     NodeContext context, CheckedStateKey key) {
341                                 return new EvaluatorCheckedState(updater, context, key, browseContext, useNodeBrowseContexts).getState();
342                             }
343                         }, 0.0);
344
345                         eval.addLabelDecorator(new LabelDecoratorFactory() {
346                             @Override
347                             public LabelDecorator create(PrimitiveQueryUpdater updater,
348                                     NodeContext context, LabelDecoratorKey key) {
349                                 return new EvaluatorLabelDecorator(updater, context, key, browseContext, useNodeBrowseContexts);
350                             }
351                         }, 0.0);
352
353                         eval.addImageDecorator(new ImageDecoratorFactory() {
354                             @Override
355                             public ImageDecorator create(PrimitiveQueryUpdater updater,
356                                     NodeContext context, ImageDecoratorKey key) {
357                                 return new EvaluatorImageDecorator(updater, context, key, browseContext, useNodeBrowseContexts);
358                             }
359                         }, 0.0);
360
361                         eval.addViewpoint(new ViewpointFactory() {
362                             @Override
363                             public Viewpoint create(PrimitiveQueryUpdater updater, 
364                                     final NodeContext context,
365                                     ViewpointKey key) {
366                                 return new EvaluatorViewpoint(updater, context, key, browseContext, useNodeBrowseContexts, useNodeActionContexts);
367                             }
368                         }, 0.0);
369                     } catch (InvalidContribution e) {
370                         e.printStackTrace();
371                         return;
372                     }
373
374                 }
375
376             });
377
378         } catch (DatabaseException e) {
379
380             e.printStackTrace();
381
382         }
383
384         return allBrowseContexts;
385     }
386
387     public static void loadExtensions(RequestProcessor processor, final Set<String> browseContexts, final ResourceManager resourceManager,
388             final Map<String, Collection<ContributorBinding<ViewpointContributionFactory>>> viewpointContributions,
389             final Set<ContributorBinding<ComparableContextFactory>> comparableContextContributions,
390             final Set<ContributorBinding<LabelerFactory>> labelerContributions,
391             final Set<ContributorBinding<CheckedStateFactory>> checkStateContributions,
392             final Set<ContributorBinding<LabelDecoratorFactory>> labelDecoratorContributions,
393             final Set<ContributorBinding<ImagerFactory>> imagerContributions,
394             final Set<ContributorBinding<ImageDecoratorFactory>> imageDecoratorContributions) {
395
396         for(ContributorBinding<ViewpointContributionFactory> binding : ViewpointContributionContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
397             String viewpointId = binding.getContributor().getFactory().getViewpointId();
398             Collection<ContributorBinding<ViewpointContributionFactory>> viewpoints = viewpointContributions.get(viewpointId);
399             if(viewpoints == null) {
400                 viewpoints = new ArrayList<ContributorBinding<ViewpointContributionFactory>>();
401                 viewpointContributions.put(viewpointId, viewpoints);
402             }
403             viewpoints.add(binding);
404         }
405
406         for(final ContributorBinding<ComparableContextFactory> binding : ComparableContextContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
407             comparableContextContributions.add(binding);
408         }
409
410         for(final ContributorBinding<LabelerFactory> binding : LabelerContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
411             labelerContributions.add(binding);
412         }
413
414         for(final ContributorBinding<CheckedStateFactory> binding : CheckedStateContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
415             checkStateContributions.add(binding);
416         }
417
418         for(final ContributorBinding<LabelDecoratorFactory> binding : LabelDecoratorContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
419             labelDecoratorContributions.add(binding);
420         }
421
422         for(final ContributorBinding<ImagerFactory> binding : ImagerContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
423             imagerContributions.add(binding);
424         }
425
426         for(final ContributorBinding<ImageDecoratorFactory> binding : ImageDecoratorContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
427             imageDecoratorContributions.add(binding);
428         }
429
430     }
431
432     public static EvaluatorData load(RequestProcessor processor, Set<String> browseContexts, ResourceManager resourceManager) throws InvalidBrowserIdException {
433         return load(processor, browseContexts, resourceManager, false, false);
434     }
435
436     public static EvaluatorData load(RequestProcessor processor, Set<String> browseContexts, ResourceManager resourceManager, boolean useNodeBrowseContexts, boolean useNodeActionContexts) throws InvalidBrowserIdException {
437
438         Map<String, Collection<ContributorBinding<ViewpointContributionFactory>>> viewpointContributions = new HashMap<String, Collection<ContributorBinding<ViewpointContributionFactory>>>();
439         Set<ContributorBinding<ComparableContextFactory>> comparableContextContributions = new HashSet<ContributorBinding<ComparableContextFactory>>();
440         Set<ContributorBinding<LabelerFactory>> labelerContributions = new HashSet<ContributorBinding<LabelerFactory>>();
441         Set<ContributorBinding<CheckedStateFactory>> checkStateContributions = new HashSet<ContributorBinding<CheckedStateFactory>>();
442         Set<ContributorBinding<LabelDecoratorFactory>> labelDecoratorContributions = new HashSet<ContributorBinding<LabelDecoratorFactory>>();
443         Set<ContributorBinding<ImagerFactory>> imagerContributions = new HashSet<ContributorBinding<ImagerFactory>>();
444         Set<ContributorBinding<ImageDecoratorFactory>> imageDecoratorContributions = new HashSet<ContributorBinding<ImageDecoratorFactory>>();
445
446         EvaluatorDataImpl data = new EvaluatorDataImpl();
447
448         // Load modelled contributions
449         Set<String> allBrowseContexts = loadModelled(processor, browseContexts, resourceManager, data, useNodeBrowseContexts, useNodeActionContexts);
450
451         // Load extension point contributions
452         loadExtensions(processor, allBrowseContexts, resourceManager, viewpointContributions, comparableContextContributions, labelerContributions, checkStateContributions, labelDecoratorContributions, imagerContributions, imageDecoratorContributions);
453
454         // Create contributed evaluators
455         create(data, resourceManager, viewpointContributions, comparableContextContributions, labelerContributions, checkStateContributions, labelDecoratorContributions, imagerContributions, imageDecoratorContributions);
456
457         // Create plugin evaluators for the contexts
458         for(EvaluatorFactory factory : EvaluatorBindingExtensionManager.getInstance().getBoundFactories(allBrowseContexts)) {
459             data.addEvaluator(factory.getClazz(), factory.create(allBrowseContexts));
460         }
461
462         return data;
463
464     }
465     
466 }