]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/requests/Nodes.java
Merge "Default property editing restores assertions"
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / requests / Nodes.java
index 01c89aaf19cee51f5f0d45c6e5de3e448310a15b..5d1da87970420ab81ca22de772d679025201ea53 100644 (file)
@@ -7,6 +7,9 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.Deque;
 import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.function.Predicate;
 
 import org.eclipse.jface.viewers.IFilter;
 import org.simantics.scl.runtime.function.Function1;
@@ -16,14 +19,17 @@ import org.simantics.scl.runtime.function.Function1;
  */
 public class Nodes {
 
+    public static final Predicate<Node> DIAGRAM_RESOURCE_PREDICATE = n -> n.getDiagramResource() != null;
+    public static final Predicate<Node> DIAGRAM_RESOURCE_AND_RVI_PREDICATE = n -> n.getDiagramResource() != null && n.getRVI() != null;
+
     public static Collection<Node> breadthFirstFlatten(IFilter filter, Collection<Node> roots) {
-        Collection<Node> result = new ArrayList<Node>();
-        List<Node> sortedRoots = new ArrayList<Node>(roots);
+        Collection<Node> result = new ArrayList<>();
+        List<Node> sortedRoots = new ArrayList<>(roots);
         Collections.sort(sortedRoots);
-        Deque<Node> todo = new ArrayDeque<Node>(sortedRoots);
+        Deque<Node> todo = new ArrayDeque<>(sortedRoots);
         while (!todo.isEmpty()) {
             Node n = todo.removeFirst();
-            List<Node> sorted = new ArrayList<Node>(n.getChildren());
+            List<Node> sorted = new ArrayList<>(n.getChildren());
             Collections.sort(sorted);
             todo.addAll(sorted);
             if (filter == null || filter.select(n))
@@ -33,8 +39,8 @@ public class Nodes {
     }
 
     public static Collection<Node> depthFirstFlatten(IFilter filter, Collection<Node> roots, Comparator<? super Node> comparator) {
-        Collection<Node> result = new ArrayList<Node>();
-        List<Node> sortedRoots = new ArrayList<Node>(roots);
+        Collection<Node> result = new ArrayList<>();
+        List<Node> sortedRoots = new ArrayList<>(roots);
         Collections.sort(sortedRoots, comparator);
         for (Node n : sortedRoots) {
             depthFirstFlattenRec(filter, comparator, n, result);
@@ -50,7 +56,7 @@ public class Nodes {
         if (children.isEmpty())
             return result;
 
-        List<Node> sorted = new ArrayList<Node>(children);
+        List<Node> sorted = new ArrayList<>(children);
         Collections.sort(sorted, comparator);
         for (Node child : sorted)
             depthFirstFlattenRec(filter, comparator, child, result);
@@ -67,7 +73,7 @@ public class Nodes {
      *         if the walk was cancelled
      */
     public static boolean walkTree(Function1<Node, Boolean> filter, Collection<Node> roots) {
-        List<Node> sortedRoots = new ArrayList<Node>(roots);
+        List<Node> sortedRoots = new ArrayList<>(roots);
         Collections.sort(sortedRoots);
         for (Node n : sortedRoots)
             if (!walkTreeRec(filter, n))
@@ -81,7 +87,7 @@ public class Nodes {
 
         Collection<Node> children = n.getChildren();
         if (!children.isEmpty()) {
-            List<Node> sorted = new ArrayList<Node>(children);
+            List<Node> sorted = new ArrayList<>(children);
             Collections.sort(sorted);
             for (Node child : sorted)
                 if (!walkTreeRec(filter, child))
@@ -90,4 +96,41 @@ public class Nodes {
         return true;
     }
 
+    public static boolean parentIsInSet(Set<Node> set, Node node) {
+        for (Node n = node.getParent(); n != null; n = n.getParent())
+            if (set.contains(n))
+                return true;
+        return false;
+    }
+
+    public static Set<Node> depthFirstFilter(Predicate<Node> filter, Collection<Node> nodes) {
+        Set<Node> result = new TreeSet<>(Node.CASE_INSENSITIVE_COMPARATOR);
+        for (Node n : nodes) {
+            Node newNode = depthFirstFilterRec(filter, n, null);
+            if (newNode != null)
+                result.add(newNode);
+        }
+        return result;
+    }
+
+    public static Node depthFirstFilter(Predicate<Node> filter, Node n) {
+        return depthFirstFilterRec(filter, n, null);
+    }
+
+    private static Node depthFirstFilterRec(Predicate<Node> filter, Node n, Node newParent) {
+        Collection<Node> children = n.getChildren();
+        if (children.isEmpty())
+            return filter.test(n) ? n.cloneWithoutChildren(newParent) : null;
+
+        Node newNode = n.cloneWithoutChildren(newParent);
+        int childCount = 0;
+        for (Node child : children) {
+            Node newChild = depthFirstFilterRec(filter, child, newNode);
+            if (newChild != null)
+                ++childCount;
+        }
+
+        return childCount > 0 ? newNode : null;
+    }
+
 }