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