]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultIsCheckedProcessor2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / DefaultIsCheckedProcessor2.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.swt;
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.CheckedState;
20 import org.simantics.browsing.ui.NodeContext;
21 import org.simantics.browsing.ui.NodeQueryManager;
22 import org.simantics.browsing.ui.NodeQueryProcessor;
23 import org.simantics.browsing.ui.Tester;
24 import org.simantics.browsing.ui.BuiltinKeys.CheckedStateKey;
25 import org.simantics.browsing.ui.NodeContext.QueryKey;
26 import org.simantics.browsing.ui.common.EvaluatorData;
27 import org.simantics.browsing.ui.common.Preference;
28 import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;
29 import org.simantics.browsing.ui.common.EvaluatorData.EvaluatorTree;
30 import org.simantics.browsing.ui.content.CheckedStateFactory;
31 import org.simantics.utils.datastructures.collections.CollectionUtils;
32
33 /**
34  * @author Antti Villberg
35  */
36 public class DefaultIsCheckedProcessor2 implements NodeQueryProcessor<CheckedState> {
37
38     private final EvaluatorData data;
39
40     public DefaultIsCheckedProcessor2(EvaluatorData data) {
41         this.data = data;
42     }
43
44     @Override
45     public QueryKey<CheckedState> getIdentifier() {
46         return BuiltinKeys.IS_CHECKED;
47     }
48
49     @Override
50     public CheckedState query(final NodeQueryManager manager, final NodeContext context) {
51         assert context != null;
52
53         Object input = context.getConstant(BuiltinKeys.INPUT);
54         assert input != null;
55
56         Collection<Evaluator> evals = data.get(input);
57         if (evals.isEmpty())
58             return null;
59
60         ArrayList<Preference<CheckedStateFactory>> factories = new ArrayList<Preference<CheckedStateFactory>>(4);
61         for (Evaluator eval : evals) {
62             evaluateTree(manager, context, eval.getCheckStateTree(), factories);
63         }
64
65         if (factories.isEmpty())
66             return null;
67
68         if (factories.size() >= 1) {
69             Collections.sort(factories);
70             return manager.query(context, new CheckedStateKey(factories.get(0).object));
71         } else {
72                 return null;
73         }
74
75     }
76
77     protected void evaluateTree(NodeQueryManager manager, NodeContext context, EvaluatorTree<CheckedStateFactory> tree, Collection<Preference<CheckedStateFactory>> result) {
78         Tester test = tree.getTester();
79
80         if (test.test(manager, context)) {
81             CollectionUtils.checkedAdd(tree.getAcceptedFactories(), result);
82
83             Collection<EvaluatorTree<CheckedStateFactory>> children = tree.getChildren();
84             if (children == null)
85                 return;
86             for (EvaluatorTree<CheckedStateFactory> e : children) {
87                 evaluateTree(manager, context, e, result);
88             }
89         }
90     }
91     
92     @Override
93     public String toString() {
94         return "IsCheckedProcessor";
95     }
96
97 }