]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural.ui/src/org/simantics/structural/ui/menuContributions/ShowAllChildrenContribution.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural.ui / src / org / simantics / structural / ui / menuContributions / ShowAllChildrenContribution.java
diff --git a/bundles/org.simantics.structural.ui/src/org/simantics/structural/ui/menuContributions/ShowAllChildrenContribution.java b/bundles/org.simantics.structural.ui/src/org/simantics/structural/ui/menuContributions/ShowAllChildrenContribution.java
new file mode 100644 (file)
index 0000000..ee6bb9b
--- /dev/null
@@ -0,0 +1,100 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * 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 implementatio\r
+ *******************************************************************************/\r
+package org.simantics.structural.ui.menuContributions;\r
+\r
+import java.util.List;\r
+\r
+import org.eclipse.jface.action.Action;\r
+import org.eclipse.jface.action.ActionContributionItem;\r
+import org.eclipse.jface.action.IContributionItem;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.ISelectionProvider;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.actions.CompoundContributionItem;\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.common.processors.ShowMaxChildrenProcessor;\r
+import org.simantics.browsing.ui.content.PrunedChildrenResult;\r
+import org.simantics.db.layer0.SelectionHints;\r
+import org.simantics.structural.ui.Activator;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ShowAllChildrenContribution extends CompoundContributionItem {\r
+\r
+    static IContributionItem[] NONE = {};\r
+\r
+    @Override\r
+    protected IContributionItem[] getContributionItems() {\r
+        IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();\r
+        if (part == null)\r
+            return NONE;\r
+\r
+        GraphExplorer explorer = (GraphExplorer) part.getAdapter(GraphExplorer.class);\r
+        if (explorer == null)\r
+            return NONE;\r
+\r
+        ISelectionProvider sp = (ISelectionProvider) explorer.getAdapter(ISelectionProvider.class);\r
+        if (sp == null)\r
+            return NONE;\r
+\r
+        ISelection s = sp.getSelection();\r
+        List<NodeContext> nodes = ISelectionUtils.getPossibleKeys(s, SelectionHints.KEY_MAIN, NodeContext.class);\r
+        if (nodes.isEmpty())\r
+            return NONE;\r
+\r
+        boolean allTruncated = true;\r
+        for (NodeContext node : nodes) {\r
+            PrunedChildrenResult prunedChildren = explorer.query(node, BuiltinKeys.PRUNED_CHILDREN);\r
+            NodeContext[] finalChildren = explorer.query(node, BuiltinKeys.FINAL_CHILDREN);\r
+            if (prunedChildren == null || finalChildren == null\r
+                    || prunedChildren.getPrunedChildren().length == finalChildren.length) {\r
+                allTruncated = false;\r
+                break;\r
+            }\r
+        }\r
+        if (!allTruncated)\r
+            return NONE;\r
+\r
+        ShowMaxChildrenProcessor processor = (ShowMaxChildrenProcessor) explorer.getPrimitiveProcessor(BuiltinKeys.SHOW_MAX_CHILDREN);\r
+        if (processor == null)\r
+            return NONE;\r
+\r
+        // Show all children\r
+        return new IContributionItem[] {\r
+                new ActionContributionItem(new ShowAllChildrenAction(processor, nodes))\r
+        };\r
+    }\r
+\r
+    static class ShowAllChildrenAction extends Action {\r
+        private ShowMaxChildrenProcessor processor;\r
+        private List<NodeContext>        nodes;\r
+\r
+        public ShowAllChildrenAction(ShowMaxChildrenProcessor processor, List<NodeContext> nodes) {\r
+            super("Show All Children");\r
+            setImageDescriptor(Activator.SHOW_ALL_CHILDREN_ICON);\r
+            this.processor = processor;\r
+            this.nodes = nodes;\r
+        }\r
+\r
+        @Override\r
+        public void run() {\r
+            for (NodeContext node : nodes)\r
+                processor.replaceShowMaxChildren(node, Integer.MAX_VALUE);\r
+        }\r
+    }\r
+\r
+}\r