1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.common.processors;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
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;
28 public class DefaultPrunedChildrenProcessor extends AbstractNodeQueryProcessor<PrunedChildrenResult> {
31 public QueryKey<PrunedChildrenResult> getIdentifier() {
32 return BuiltinKeys.PRUNED_CHILDREN;
36 public PrunedChildrenResult query(NodeQueryManager manager, NodeContext context) {
38 Collection<ViewpointContribution> contributions = manager.query(context, BuiltinKeys.VIEWPOINT_CONTRIBUTIONS);
39 if (contributions == null)
40 return PrunedChildrenResult.EMPTY;
42 Set<NodeContext> childSet = new HashSet<NodeContext>();
43 ArrayList<NodeContext> children = new ArrayList<NodeContext>();
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)
62 // Viewpoint viewpoint = manager.query(context, BuiltinKeys.SELECTED_VIEWPOINT);
63 // if (viewpoint == null)
64 // return PrunedChildrenResult.EMPTY;
66 // INodeContext[] children = viewpoint.getChildren();
68 Collection<SelectionRequest> selectionRequests = manager.query(context, BuiltinKeys.SELECTION_REQUESTS);
69 if (selectionRequests == null)
70 return new PrunedChildrenResult(children.size(), children.toArray(NodeContext.NONE));
72 SelectionRequest[] reqs = selectionRequests.toArray(new SelectionRequest[selectionRequests.size()]);
74 ArrayList<NodeContext> pruned = new ArrayList<NodeContext>();
75 for (NodeContext ctx : children) {
78 if (selectionRequests != null) {
79 for(SelectionRequest req : reqs) {
80 if (req.getRequest() == SelectionRequest.Request.FILTER) {
81 if (req.isIncluded(manager, ctx)) {
92 return new PrunedChildrenResult(children.size(), pruned.toArray(NodeContext.NONE));
96 public String toString() {
97 return "PrunedChildrenProcessor";