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.common.processors;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.NodeContext.QueryKey;
21 import org.simantics.browsing.ui.NodeQueryManager;
22 import org.simantics.browsing.ui.Tester;
23 import org.simantics.browsing.ui.common.EvaluatorData;
24 import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;
25 import org.simantics.browsing.ui.common.EvaluatorData.EvaluatorTree;
26 import org.simantics.browsing.ui.common.Preference;
27 import org.simantics.utils.datastructures.collections.CollectionUtils;
30 * A base class for query processors that resolve a set of suitable factory
31 * classes of a kind for a given INodeContext.
34 * The purpose of this class is to generically implement a basic logic for
35 * resolving available factories (viewpoint/labeler/labeldecorator/comparator)
36 * any given input. The implementation works based on an
37 * <code>EvaluatorData</code> instance that is specified externally.The logic is
41 * <li>Get all possible <code>Evaluator</code>s from the externally specified
42 * EvaluatorData (see {@link EvaluatorData#get(Object)}).</li>
43 * <li>For each <code>Evaluator</code>:
45 * <li>Walk the <code>EvaluatorTree</code> structure returned by
46 * {@link #getEvaluatorTree(Evaluator)} while checking at each node whether the
47 * {@link Tester} returned by {@link EvaluatorTree#getTester()} returns
48 * <code>true</code>. If <code>true</code> is returned, the factories at this
49 * node ({@link EvaluatorTree#getAcceptedFactories()}) are added to the result
50 * and all children ({@link EvaluatorTree#getChildren()}) are recursively
51 * walked. Otherwise if <code>false</code> is returned, the walking of the
52 * subtree will end.</li>
55 * <li>Sort the results in descending preference order, larger number equals
56 * higher preference</li>
60 * This class intended for sub-classing, please implement these methods:
62 * <li><code>getEvaluatorTree(Evaluator)</code> - return the appropriate
63 * <code>EvaluatorTree</code> of the specified <code>Evaluator</code></li>
66 * @author Tuukka Lehtonen
68 * @param <Factory> the factory type to use
70 * @see ComparableFactoryResolver
71 * @see LabelDecoratorFactoryResolver
72 * @see ImageDecoratorFactoryResolver
73 * @see LabelerFactoryResolver
74 * @see ImagerFactoryResolver
75 * @see ViewpointFactoryResolver
77 public abstract class AbstractFactoryResolverQueryProcessor<Factory> extends AbstractNodeQueryProcessor<Collection<Factory>> {
79 private final EvaluatorData data;
80 private final QueryKey<Collection<Factory>> identifier;
82 public AbstractFactoryResolverQueryProcessor(EvaluatorData data, QueryKey<Collection<Factory>> identifier) {
84 this.identifier = identifier;
88 public QueryKey<Collection<Factory>> getIdentifier() {
93 public Collection<Factory> query(final NodeQueryManager manager, final NodeContext context) {
94 assert context != null;
96 Object input = context.getConstant(BuiltinKeys.INPUT);
99 Collection<Evaluator> evals = data.get(input);
101 return Collections.emptyList();
103 ArrayList<Preference<Factory>> factories = new ArrayList<Preference<Factory>>(4);
104 for (Evaluator eval : evals) {
105 evaluateTree(manager, context, getEvaluatorTree(eval), factories);
108 if (factories.isEmpty())
109 return Collections.emptyList();
111 if (factories.size() > 1)
112 Collections.sort(factories);
114 ArrayList<Factory> result = new ArrayList<Factory>(factories.size());
115 for (Preference<Factory> p : factories) {
116 result.add(p.object);
122 protected void evaluateTree(NodeQueryManager manager, NodeContext context, EvaluatorTree<Factory> tree, Collection<Preference<Factory>> result) {
123 Tester test = tree.getTester();
125 if (test.test(manager, context)) {
126 CollectionUtils.checkedAdd(tree.getAcceptedFactories(), result);
128 Collection<EvaluatorTree<Factory>> children = tree.getChildren();
129 if (children == null)
131 for (EvaluatorTree<Factory> e : children) {
132 evaluateTree(manager, context, e, result);
141 protected abstract EvaluatorTree<Factory> getEvaluatorTree(Evaluator evaluator);