]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/DefaultPrunedChildrenProcessor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / processors / DefaultPrunedChildrenProcessor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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 implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.common.processors;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.simantics.browsing.ui.BuiltinKeys;
20 import org.simantics.browsing.ui.NodeContext;
21 import org.simantics.browsing.ui.NodeQueryManager;
22 import org.simantics.browsing.ui.SelectionRequest;
23 import org.simantics.browsing.ui.Tester;
24 import org.simantics.browsing.ui.NodeContext.QueryKey;
25 import org.simantics.browsing.ui.content.PrunedChildrenResult;
26 import org.simantics.browsing.ui.content.ViewpointContribution;
27
28 public class DefaultPrunedChildrenProcessor extends AbstractNodeQueryProcessor<PrunedChildrenResult> {
29
30     @Override
31     public QueryKey<PrunedChildrenResult> getIdentifier() {
32         return BuiltinKeys.PRUNED_CHILDREN;
33     }
34
35     @Override
36     public PrunedChildrenResult query(NodeQueryManager manager, NodeContext context) {
37
38         Collection<ViewpointContribution> contributions = manager.query(context, BuiltinKeys.VIEWPOINT_CONTRIBUTIONS);
39         if (contributions == null)
40             return PrunedChildrenResult.EMPTY;
41
42         Set<NodeContext> childSet = new HashSet<NodeContext>();
43         ArrayList<NodeContext> children = new ArrayList<NodeContext>();
44
45         // Gather NodeContexts from all viewpoint contributions
46         // into the 'children' result list to preserve the order.
47         // 'childSet' is used to prevent duplicate contributions.
48         for (ViewpointContribution contribution : contributions) {
49             Class<?> clazz = contribution.getInputClass();
50             if (clazz.isInstance(context.getConstant(BuiltinKeys.INPUT))) {
51                 Tester test = contribution.getNodeContextTester();
52                 if (test == null || test.test(manager, context)) {
53                     Collection<NodeContext> contrib = contribution.getContribution();
54                     children.ensureCapacity(children.size() + contrib.size());
55                     for (NodeContext nc : contrib)
56                         if (childSet.add(nc))
57                             children.add(nc);
58                 }
59             }
60         }
61
62 //        Viewpoint viewpoint = manager.query(context, BuiltinKeys.SELECTED_VIEWPOINT);
63 //        if (viewpoint == null)
64 //            return PrunedChildrenResult.EMPTY;
65 //
66 //        INodeContext[] children = viewpoint.getChildren();
67
68         Collection<SelectionRequest> selectionRequests = manager.query(context, BuiltinKeys.SELECTION_REQUESTS);
69         if (selectionRequests == null)
70             return new PrunedChildrenResult(children.size(), children.toArray(NodeContext.NONE));
71
72         SelectionRequest[] reqs = selectionRequests.toArray(new SelectionRequest[selectionRequests.size()]);
73
74         ArrayList<NodeContext> pruned = new ArrayList<NodeContext>();
75         for (NodeContext ctx : children) {
76             boolean add = true;
77
78             if (selectionRequests != null) {
79                 for(SelectionRequest req : reqs) {
80                     if (req.getRequest() == SelectionRequest.Request.FILTER) {
81                         if (req.isIncluded(manager, ctx)) {
82                             add = false;
83                             break;
84                         }
85                     }
86                 }
87             }
88
89             if (add)
90                 pruned.add(ctx);
91         }
92         return new PrunedChildrenResult(children.size(), pruned.toArray(NodeContext.NONE));
93     }
94
95     @Override
96     public String toString() {
97         return "PrunedChildrenProcessor";
98     }
99
100 }