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.swt;
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.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;
34 * @author Antti Villberg
36 public class DefaultIsCheckedProcessor2 implements NodeQueryProcessor<CheckedState> {
38 private final EvaluatorData data;
40 public DefaultIsCheckedProcessor2(EvaluatorData data) {
45 public QueryKey<CheckedState> getIdentifier() {
46 return BuiltinKeys.IS_CHECKED;
50 public CheckedState query(final NodeQueryManager manager, final NodeContext context) {
51 assert context != null;
53 Object input = context.getConstant(BuiltinKeys.INPUT);
56 Collection<Evaluator> evals = data.get(input);
60 ArrayList<Preference<CheckedStateFactory>> factories = new ArrayList<Preference<CheckedStateFactory>>(4);
61 for (Evaluator eval : evals) {
62 evaluateTree(manager, context, eval.getCheckStateTree(), factories);
65 if (factories.isEmpty())
68 if (factories.size() >= 1) {
69 Collections.sort(factories);
70 return manager.query(context, new CheckedStateKey(factories.get(0).object));
77 protected void evaluateTree(NodeQueryManager manager, NodeContext context, EvaluatorTree<CheckedStateFactory> tree, Collection<Preference<CheckedStateFactory>> result) {
78 Tester test = tree.getTester();
80 if (test.test(manager, context)) {
81 CollectionUtils.checkedAdd(tree.getAcceptedFactories(), result);
83 Collection<EvaluatorTree<CheckedStateFactory>> children = tree.getChildren();
86 for (EvaluatorTree<CheckedStateFactory> e : children) {
87 evaluateTree(manager, context, e, result);
93 public String toString() {
94 return "IsCheckedProcessor";