1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
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;
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.
74 * @author Antti Villberg
76 public class Evaluators {
78 public static final boolean DEBUG = false;
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) {
89 Map<Class<?>, Evaluator> evaluators = new HashMap<Class<?>, Evaluator>();
91 HashMap<String, ContributionViewpointFactory> viewpointFactories = new HashMap<String, ContributionViewpointFactory>();
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));
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);
111 System.out.println("add root viewpoint " + factory + " to class " + clazz.getSimpleName());
112 evaluator.addViewpoint(factory, binding.getPreference());
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());
122 for(final ContributorBinding<ComparableContextFactory> binding : comparableContextContributions) {
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);
134 System.out.println("add root comparable context " + factory + " to class " + clazz.getSimpleName());
135 evaluator.addComparableContext(factory, binding.getPreference());
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());
144 for(final ContributorBinding<LabelerFactory> binding : labelerContributions) {
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);
156 System.out.println("add root labeler " + factory + " to class " + clazz.getSimpleName());
157 evaluator.addLabeler(factory, binding.getPreference());
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());
166 for(final ContributorBinding<CheckedStateFactory> binding : checkStateContributions) {
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);
178 System.out.println("add root check state " + factory + " to class " + clazz.getSimpleName());
179 evaluator.addCheckState(factory, binding.getPreference());
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());
188 for(final ContributorBinding<LabelDecoratorFactory> binding : labelDecoratorContributions) {
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);
200 System.out.println("add root label decorator " + factory + " to class " + clazz.getSimpleName());
201 evaluator.addLabelDecorator(factory, binding.getPreference());
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());
210 for(final ContributorBinding<ImagerFactory> binding : imagerContributions) {
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);
222 System.out.println("add root imager " + factory + " to class " + clazz.getSimpleName());
223 evaluator.addImager(factory, binding.getPreference());
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());
232 for(final ContributorBinding<ImageDecoratorFactory> binding : imageDecoratorContributions) {
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);
244 System.out.println("add root image decorator " + factory + " to class " + clazz.getSimpleName());
245 evaluator.addImageDecorator(factory, binding.getPreference());
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());
254 for(Map.Entry<Class<?>, Evaluator> entry : evaluators.entrySet()) {
255 data.addEvaluator(entry.getKey(), entry.getValue());
262 * @param browseContexts
263 * @param resourceManager
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.
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) {
277 final Set<String> allBrowseContexts = new HashSet<String>(browseContexts);
280 final EvaluatorImpl eval = new EvaluatorImpl();
281 data.addEvaluator(Object.class, eval);
285 // Load data for evaluator
286 processor.syncRequest(new ReadRequest() {
289 public void run(ReadGraph graph) throws DatabaseException {
291 // Map browse context names to resources
292 Collection<Resource> browseContextResources = new ArrayList<Resource>(browseContexts.size());
293 for(String browseContext : browseContexts) {
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.");
305 // Load browse context
307 final BrowseContext browseContext =
308 BrowseContext.create(graph, browseContextResources);
310 data.setBrowseContext(browseContext);
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);
316 allBrowseContexts.add(uri);
320 eval.addLabeler(new LabelerFactory() {
322 public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context,
324 return new EvaluatorLabeler(updater, context, key, browseContext, useNodeBrowseContexts);
328 eval.addImager(new ImagerFactory() {
330 public Imager create(PrimitiveQueryUpdater updater, NodeContext context,
332 return new EvaluatorImager(updater, context, key, browseContext, useNodeBrowseContexts);
336 eval.addCheckState(new CheckedStateFactory() {
339 public CheckedState create(PrimitiveQueryUpdater updater,
340 NodeContext context, CheckedStateKey key) {
341 return new EvaluatorCheckedState(updater, context, key, browseContext, useNodeBrowseContexts).getState();
345 eval.addLabelDecorator(new LabelDecoratorFactory() {
347 public LabelDecorator create(PrimitiveQueryUpdater updater,
348 NodeContext context, LabelDecoratorKey key) {
349 return new EvaluatorLabelDecorator(updater, context, key, browseContext, useNodeBrowseContexts);
353 eval.addImageDecorator(new ImageDecoratorFactory() {
355 public ImageDecorator create(PrimitiveQueryUpdater updater,
356 NodeContext context, ImageDecoratorKey key) {
357 return new EvaluatorImageDecorator(updater, context, key, browseContext, useNodeBrowseContexts);
361 eval.addViewpoint(new ViewpointFactory() {
363 public Viewpoint create(PrimitiveQueryUpdater updater,
364 final NodeContext context,
366 return new EvaluatorViewpoint(updater, context, key, browseContext, useNodeBrowseContexts, useNodeActionContexts);
369 } catch (InvalidContribution e) {
378 } catch (DatabaseException e) {
384 return allBrowseContexts;
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) {
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);
403 viewpoints.add(binding);
406 for(final ContributorBinding<ComparableContextFactory> binding : ComparableContextContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
407 comparableContextContributions.add(binding);
410 for(final ContributorBinding<LabelerFactory> binding : LabelerContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
411 labelerContributions.add(binding);
414 for(final ContributorBinding<CheckedStateFactory> binding : CheckedStateContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
415 checkStateContributions.add(binding);
418 for(final ContributorBinding<LabelDecoratorFactory> binding : LabelDecoratorContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
419 labelDecoratorContributions.add(binding);
422 for(final ContributorBinding<ImagerFactory> binding : ImagerContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
423 imagerContributions.add(binding);
426 for(final ContributorBinding<ImageDecoratorFactory> binding : ImageDecoratorContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
427 imageDecoratorContributions.add(binding);
432 public static EvaluatorData load(RequestProcessor processor, Set<String> browseContexts, ResourceManager resourceManager) throws InvalidBrowserIdException {
433 return load(processor, browseContexts, resourceManager, false, false);
436 public static EvaluatorData load(RequestProcessor processor, Set<String> browseContexts, ResourceManager resourceManager, boolean useNodeBrowseContexts, boolean useNodeActionContexts) throws InvalidBrowserIdException {
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>>();
446 EvaluatorDataImpl data = new EvaluatorDataImpl();
448 // Load modelled contributions
449 Set<String> allBrowseContexts = loadModelled(processor, browseContexts, resourceManager, data, useNodeBrowseContexts, useNodeActionContexts);
451 // Load extension point contributions
452 loadExtensions(processor, allBrowseContexts, resourceManager, viewpointContributions, comparableContextContributions, labelerContributions, checkStateContributions, labelDecoratorContributions, imagerContributions, imageDecoratorContributions);
454 // Create contributed evaluators
455 create(data, resourceManager, viewpointContributions, comparableContextContributions, labelerContributions, checkStateContributions, labelDecoratorContributions, imagerContributions, imageDecoratorContributions);
457 // Create plugin evaluators for the contexts
458 for(EvaluatorFactory factory : EvaluatorBindingExtensionManager.getInstance().getBoundFactories(allBrowseContexts)) {
459 data.addEvaluator(factory.getClazz(), factory.create(allBrowseContexts));