]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementatio\r
11  *******************************************************************************/\r
12 package org.simantics.structural.ui.menuContributions;\r
13 \r
14 import java.util.List;\r
15 \r
16 import org.eclipse.jface.action.Action;\r
17 import org.eclipse.jface.action.ActionContributionItem;\r
18 import org.eclipse.jface.action.IContributionItem;\r
19 import org.eclipse.jface.viewers.ISelection;\r
20 import org.eclipse.jface.viewers.ISelectionProvider;\r
21 import org.eclipse.ui.IWorkbenchPart;\r
22 import org.eclipse.ui.actions.CompoundContributionItem;\r
23 import org.simantics.browsing.ui.BuiltinKeys;\r
24 import org.simantics.browsing.ui.GraphExplorer;\r
25 import org.simantics.browsing.ui.NodeContext;\r
26 import org.simantics.browsing.ui.common.processors.ShowMaxChildrenProcessor;\r
27 import org.simantics.browsing.ui.content.PrunedChildrenResult;\r
28 import org.simantics.db.layer0.SelectionHints;\r
29 import org.simantics.structural.ui.Activator;\r
30 import org.simantics.utils.ui.ISelectionUtils;\r
31 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
32 \r
33 /**\r
34  * @author Tuukka Lehtonen\r
35  */\r
36 public class ShowAllChildrenContribution extends CompoundContributionItem {\r
37 \r
38     static IContributionItem[] NONE = {};\r
39 \r
40     @Override\r
41     protected IContributionItem[] getContributionItems() {\r
42         IWorkbenchPart part = WorkbenchUtils.getActiveWorkbenchPart();\r
43         if (part == null)\r
44             return NONE;\r
45 \r
46         GraphExplorer explorer = (GraphExplorer) part.getAdapter(GraphExplorer.class);\r
47         if (explorer == null)\r
48             return NONE;\r
49 \r
50         ISelectionProvider sp = (ISelectionProvider) explorer.getAdapter(ISelectionProvider.class);\r
51         if (sp == null)\r
52             return NONE;\r
53 \r
54         ISelection s = sp.getSelection();\r
55         List<NodeContext> nodes = ISelectionUtils.getPossibleKeys(s, SelectionHints.KEY_MAIN, NodeContext.class);\r
56         if (nodes.isEmpty())\r
57             return NONE;\r
58 \r
59         boolean allTruncated = true;\r
60         for (NodeContext node : nodes) {\r
61             PrunedChildrenResult prunedChildren = explorer.query(node, BuiltinKeys.PRUNED_CHILDREN);\r
62             NodeContext[] finalChildren = explorer.query(node, BuiltinKeys.FINAL_CHILDREN);\r
63             if (prunedChildren == null || finalChildren == null\r
64                     || prunedChildren.getPrunedChildren().length == finalChildren.length) {\r
65                 allTruncated = false;\r
66                 break;\r
67             }\r
68         }\r
69         if (!allTruncated)\r
70             return NONE;\r
71 \r
72         ShowMaxChildrenProcessor processor = (ShowMaxChildrenProcessor) explorer.getPrimitiveProcessor(BuiltinKeys.SHOW_MAX_CHILDREN);\r
73         if (processor == null)\r
74             return NONE;\r
75 \r
76         // Show all children\r
77         return new IContributionItem[] {\r
78                 new ActionContributionItem(new ShowAllChildrenAction(processor, nodes))\r
79         };\r
80     }\r
81 \r
82     static class ShowAllChildrenAction extends Action {\r
83         private ShowMaxChildrenProcessor processor;\r
84         private List<NodeContext>        nodes;\r
85 \r
86         public ShowAllChildrenAction(ShowMaxChildrenProcessor processor, List<NodeContext> nodes) {\r
87             super("Show All Children");\r
88             setImageDescriptor(Activator.SHOW_ALL_CHILDREN_ICON);\r
89             this.processor = processor;\r
90             this.nodes = nodes;\r
91         }\r
92 \r
93         @Override\r
94         public void run() {\r
95             for (NodeContext node : nodes)\r
96                 processor.replaceShowMaxChildren(node, Integer.MAX_VALUE);\r
97         }\r
98     }\r
99 \r
100 }\r