]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultShowMaxChildrenProcessor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / DefaultShowMaxChildrenProcessor.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultShowMaxChildrenProcessor.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultShowMaxChildrenProcessor.java
new file mode 100644 (file)
index 0000000..3314685
--- /dev/null
@@ -0,0 +1,112 @@
+/*******************************************************************************\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.swt;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+import gnu.trove.map.hash.TObjectIntHashMap;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.common.processors.AbstractPrimitiveQueryProcessor;\r
+import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;\r
+import org.simantics.browsing.ui.common.processors.ShowMaxChildrenProcessor;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DefaultShowMaxChildrenProcessor extends AbstractPrimitiveQueryProcessor<Integer> implements\r
+        ShowMaxChildrenProcessor, ProcessorLifecycle {\r
+\r
+    private final Integer MAX_INT = Integer.MAX_VALUE;\r
+\r
+    /**\r
+     * The set of currently expanded node contexts.\r
+     */\r
+    private final TObjectIntHashMap<NodeContext>               showMaxChildren        = new TObjectIntHashMap<NodeContext>();\r
+    private final THashMap<NodeContext, PrimitiveQueryUpdater> showMaxChildrenQueries = new THashMap<NodeContext, PrimitiveQueryUpdater>();\r
+\r
+    public DefaultShowMaxChildrenProcessor() {\r
+    }\r
+\r
+    @Override\r
+    public Object getIdentifier() {\r
+        return BuiltinKeys.SHOW_MAX_CHILDREN;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return "ShowMaxChildrenProcessor";\r
+    }\r
+\r
+    @Override\r
+    public Integer query(PrimitiveQueryUpdater updater, NodeContext context, PrimitiveQueryKey<Integer> key) {\r
+        int maxChildren = showMaxChildren.get(context);\r
+        //System.out.println("maxChildren(" + updater + ", " + context + "): " + maxChildren);\r
+        showMaxChildrenQueries.put(context, updater);\r
+        if (maxChildren == 0)\r
+            return null;\r
+        return Integer.valueOf(maxChildren);\r
+    }\r
+\r
+    @Override\r
+    public boolean setShowMaxChildren(NodeContext context, int maxChildren) {\r
+        return _setShowMaxChildren(context, maxChildren);\r
+    }\r
+\r
+    @Override\r
+    public boolean replaceShowMaxChildren(NodeContext context, int maxChildren) {\r
+        return nodeStatusChanged(context, maxChildren);\r
+    }\r
+\r
+    private boolean _setShowMaxChildren(NodeContext context, int maxChildren) {\r
+        if (maxChildren > 0) {\r
+            return this.showMaxChildren.put(context, maxChildren) != maxChildren;\r
+        } else {\r
+            return this.showMaxChildren.remove(context) != 0;\r
+        }\r
+    }\r
+\r
+    protected boolean nodeStatusChanged(NodeContext context, int maxChildren) {\r
+        boolean result = _setShowMaxChildren(context, maxChildren);\r
+        PrimitiveQueryUpdater updater = showMaxChildrenQueries.get(context);\r
+        if (updater != null) {\r
+            Integer newValue = null;\r
+            if (maxChildren > 0) {\r
+                if (maxChildren != Integer.MAX_VALUE)\r
+                    newValue = Integer.valueOf(maxChildren);\r
+                else\r
+                    newValue = MAX_INT;\r
+            }\r
+            updater.scheduleReplace(context, BuiltinKeys.SHOW_MAX_CHILDREN, newValue);\r
+        }\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public void attached(GraphExplorer explorer) {\r
+    }\r
+\r
+    @Override\r
+    public void detached(GraphExplorer explorer) {\r
+        clear();\r
+    }\r
+\r
+    @Override\r
+    public void clear() {\r
+        showMaxChildren.clear();\r
+        showMaxChildrenQueries.clear();\r
+    }\r
+\r
+}
\ No newline at end of file