]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/AbstractFactoryResolverQueryProcessor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / processors / AbstractFactoryResolverQueryProcessor.java
diff --git a/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/AbstractFactoryResolverQueryProcessor.java b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/AbstractFactoryResolverQueryProcessor.java
new file mode 100644 (file)
index 0000000..99d7583
--- /dev/null
@@ -0,0 +1,143 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.common.processors;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.NodeContext.QueryKey;\r
+import org.simantics.browsing.ui.NodeQueryManager;\r
+import org.simantics.browsing.ui.Tester;\r
+import org.simantics.browsing.ui.common.EvaluatorData;\r
+import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;\r
+import org.simantics.browsing.ui.common.EvaluatorData.EvaluatorTree;\r
+import org.simantics.browsing.ui.common.Preference;\r
+import org.simantics.utils.datastructures.collections.CollectionUtils;\r
+\r
+/**\r
+ * A base class for query processors that resolve a set of suitable factory\r
+ * classes of a kind for a given INodeContext.\r
+ * \r
+ * <p>\r
+ * The purpose of this class is to generically implement a basic logic for\r
+ * resolving available factories (viewpoint/labeler/labeldecorator/comparator)\r
+ * any given input. The implementation works based on an\r
+ * <code>EvaluatorData</code> instance that is specified externally.The logic is\r
+ * as follows:\r
+ * </p>\r
+ * <ol>\r
+ * <li>Get all possible <code>Evaluator</code>s from the externally specified\r
+ * EvaluatorData (see {@link EvaluatorData#get(Object)}).</li>\r
+ * <li>For each <code>Evaluator</code>:\r
+ * <ul>\r
+ * <li>Walk the <code>EvaluatorTree</code> structure returned by\r
+ * {@link #getEvaluatorTree(Evaluator)} while checking at each node whether the\r
+ * {@link Tester} returned by {@link EvaluatorTree#getTester()} returns\r
+ * <code>true</code>. If <code>true</code> is returned, the factories at this\r
+ * node ({@link EvaluatorTree#getAcceptedFactories()}) are added to the result\r
+ * and all children ({@link EvaluatorTree#getChildren()}) are recursively\r
+ * walked. Otherwise if <code>false</code> is returned, the walking of the\r
+ * subtree will end.</li>\r
+ * </ul>\r
+ * </li>\r
+ * <li>Sort the results in descending preference order, larger number equals\r
+ * higher preference</li>\r
+ * </ol>\r
+ * \r
+ * <p>\r
+ * This class intended for sub-classing, please implement these methods:\r
+ * <ul>\r
+ * <li><code>getEvaluatorTree(Evaluator)</code> - return the appropriate\r
+ * <code>EvaluatorTree</code> of the specified <code>Evaluator</code></li>\r
+ * </ul>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @param <Factory> the factory type to use\r
+ * \r
+ * @see ComparableFactoryResolver\r
+ * @see LabelDecoratorFactoryResolver\r
+ * @see ImageDecoratorFactoryResolver\r
+ * @see LabelerFactoryResolver\r
+ * @see ImagerFactoryResolver\r
+ * @see ViewpointFactoryResolver\r
+ */\r
+public abstract class AbstractFactoryResolverQueryProcessor<Factory> extends AbstractNodeQueryProcessor<Collection<Factory>> {\r
+\r
+    private final EvaluatorData data;\r
+    private final QueryKey<Collection<Factory>> identifier;\r
+\r
+    public AbstractFactoryResolverQueryProcessor(EvaluatorData data, QueryKey<Collection<Factory>> identifier) {\r
+        this.data = data;\r
+        this.identifier = identifier;\r
+    }\r
+\r
+    @Override\r
+    public QueryKey<Collection<Factory>> getIdentifier() {\r
+        return identifier;\r
+    }\r
+\r
+    @Override\r
+    public Collection<Factory> query(final NodeQueryManager manager, final NodeContext context) {\r
+        assert context != null;\r
+\r
+        Object input = context.getConstant(BuiltinKeys.INPUT);\r
+        assert input != null;\r
+\r
+        Collection<Evaluator> evals = data.get(input);\r
+        if (evals.isEmpty())\r
+            return Collections.emptyList();\r
+\r
+        ArrayList<Preference<Factory>> factories = new ArrayList<Preference<Factory>>(4);\r
+        for (Evaluator eval : evals) {\r
+            evaluateTree(manager, context, getEvaluatorTree(eval), factories);\r
+        }\r
+\r
+        if (factories.isEmpty())\r
+            return Collections.emptyList();\r
+\r
+        if (factories.size() > 1)\r
+            Collections.sort(factories);\r
+\r
+        ArrayList<Factory> result = new ArrayList<Factory>(factories.size());\r
+        for (Preference<Factory> p : factories) {\r
+            result.add(p.object);\r
+        }\r
+\r
+        return result;\r
+    }\r
+\r
+    protected void evaluateTree(NodeQueryManager manager, NodeContext context, EvaluatorTree<Factory> tree, Collection<Preference<Factory>> result) {\r
+        Tester test = tree.getTester();\r
+\r
+        if (test.test(manager, context)) {\r
+            CollectionUtils.checkedAdd(tree.getAcceptedFactories(), result);\r
+\r
+            Collection<EvaluatorTree<Factory>> children = tree.getChildren();\r
+            if (children == null)\r
+                return;\r
+            for (EvaluatorTree<Factory> e : children) {\r
+                evaluateTree(manager, context, e, result);\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * @param evaluator\r
+     * @return\r
+     */\r
+    protected abstract EvaluatorTree<Factory> getEvaluatorTree(Evaluator evaluator);\r
+\r
+}\r