]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/requests/CollectionResult.java
Revert "Default property editing restores assertions"
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / requests / CollectionResult.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.modeling.requests;
13
14 import java.util.Collection;
15 import java.util.Comparator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.Vector;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentSkipListSet;
22
23 import org.eclipse.jface.viewers.IFilter;
24 import org.simantics.db.Resource;
25 import org.simantics.scl.runtime.function.Function1;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class CollectionResult {
31
32     public class DiagramFilter implements IFilter {
33
34         private final IFilter proxy;
35
36         public DiagramFilter(IFilter proxy) {
37             this.proxy = proxy;
38         }
39
40         @Override
41         public boolean select(Object node) {
42             return diagramSet.contains(node) && (proxy == null || proxy.select(node));
43         }
44
45     }
46
47     final public Set<Node>           roots       = new ConcurrentSkipListSet<Node>();
48     final private Set<Node>          diagramSet  = new ConcurrentSkipListSet<Node>();
49     final public List<Node>          diagramList = new Vector<Node>();
50     final public Map<Resource, Node> diagrams    = new ConcurrentHashMap<Resource, Node>();
51
52     public void addDiagram(Resource r, Node n) {
53         diagramList.add(n);
54         diagrams.put(r, n);
55         diagramSet.add(n);
56     }
57
58     public Collection<Node> breadthFirstFlatten() {
59         return breadthFirstFlatten(null);
60     }
61
62     public Collection<Node> breadthFirstFlatten(IFilter filter) {
63         return Nodes.breadthFirstFlatten(new DiagramFilter(filter), roots);
64     }
65
66     public Collection<Node> depthFirstFlatten() {
67         return depthFirstFlatten(null, null);
68     }
69
70     public Collection<Node> depthFirstFlatten(IFilter filter, Comparator<? super Node> comparator) {
71         return Nodes.depthFirstFlatten(new DiagramFilter(filter), roots, comparator);
72     }
73
74     /**
75      * @param f
76      *            function that takes the walked Node as argument and returns a
77      *            boolean to describe whether to continue the walk or cancel the
78      *            walk. The returned value cannot be <code>null</code>.
79      * @return <code>true</code> if the walk was completed or <code>false</code>
80      *         if the walk was cancelled
81      */
82     public boolean walkTree(Function1<Node, Boolean> f) {
83         return Nodes.walkTree(f, roots);
84    }
85
86 }