]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/AbstractFactoryResolverQueryProcessor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / processors / AbstractFactoryResolverQueryProcessor.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.processors;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17
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;
28
29 /**
30  * A base class for query processors that resolve a set of suitable factory
31  * classes of a kind for a given INodeContext.
32  * 
33  * <p>
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
38  * as follows:
39  * </p>
40  * <ol>
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>:
44  * <ul>
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>
53  * </ul>
54  * </li>
55  * <li>Sort the results in descending preference order, larger number equals
56  * higher preference</li>
57  * </ol>
58  * 
59  * <p>
60  * This class intended for sub-classing, please implement these methods:
61  * <ul>
62  * <li><code>getEvaluatorTree(Evaluator)</code> - return the appropriate
63  * <code>EvaluatorTree</code> of the specified <code>Evaluator</code></li>
64  * </ul>
65  * 
66  * @author Tuukka Lehtonen
67  * 
68  * @param <Factory> the factory type to use
69  * 
70  * @see ComparableFactoryResolver
71  * @see LabelDecoratorFactoryResolver
72  * @see ImageDecoratorFactoryResolver
73  * @see LabelerFactoryResolver
74  * @see ImagerFactoryResolver
75  * @see ViewpointFactoryResolver
76  */
77 public abstract class AbstractFactoryResolverQueryProcessor<Factory> extends AbstractNodeQueryProcessor<Collection<Factory>> {
78
79     private final EvaluatorData data;
80     private final QueryKey<Collection<Factory>> identifier;
81
82     public AbstractFactoryResolverQueryProcessor(EvaluatorData data, QueryKey<Collection<Factory>> identifier) {
83         this.data = data;
84         this.identifier = identifier;
85     }
86
87     @Override
88     public QueryKey<Collection<Factory>> getIdentifier() {
89         return identifier;
90     }
91
92     @Override
93     public Collection<Factory> query(final NodeQueryManager manager, final NodeContext context) {
94         assert context != null;
95
96         Object input = context.getConstant(BuiltinKeys.INPUT);
97         assert input != null;
98
99         Collection<Evaluator> evals = data.get(input);
100         if (evals.isEmpty())
101             return Collections.emptyList();
102
103         ArrayList<Preference<Factory>> factories = new ArrayList<Preference<Factory>>(4);
104         for (Evaluator eval : evals) {
105             evaluateTree(manager, context, getEvaluatorTree(eval), factories);
106         }
107
108         if (factories.isEmpty())
109             return Collections.emptyList();
110
111         if (factories.size() > 1)
112             Collections.sort(factories);
113
114         ArrayList<Factory> result = new ArrayList<Factory>(factories.size());
115         for (Preference<Factory> p : factories) {
116             result.add(p.object);
117         }
118
119         return result;
120     }
121
122     protected void evaluateTree(NodeQueryManager manager, NodeContext context, EvaluatorTree<Factory> tree, Collection<Preference<Factory>> result) {
123         Tester test = tree.getTester();
124
125         if (test.test(manager, context)) {
126             CollectionUtils.checkedAdd(tree.getAcceptedFactories(), result);
127
128             Collection<EvaluatorTree<Factory>> children = tree.getChildren();
129             if (children == null)
130                 return;
131             for (EvaluatorTree<Factory> e : children) {
132                 evaluateTree(manager, context, e, result);
133             }
134         }
135     }
136
137     /**
138      * @param evaluator
139      * @return
140      */
141     protected abstract EvaluatorTree<Factory> getEvaluatorTree(Evaluator evaluator);
142
143 }