]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/utils/NodeUtil.java
Replace System.err and System.out with SLF4J Logging
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / utils / NodeUtil.java
index 80b9a8a52c288207c5eb56f1c9ffc5989d0ddd5e..5dac6fd2a59b68bc380fddbdbdda71efc5fd74f5 100644 (file)
@@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Function;
 
 import org.simantics.scenegraph.IDynamicSelectionPainterNode;
 import org.simantics.scenegraph.ILookupService;
@@ -53,6 +54,8 @@ import org.simantics.scl.runtime.function.Function1;
 import org.simantics.scl.runtime.function.FunctionImpl1;
 import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.threads.IThreadWorkQueue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Utilities for debugging/printing the contents of a scenegraph.
@@ -61,6 +64,7 @@ import org.simantics.utils.threads.IThreadWorkQueue;
  */
 public final class NodeUtil {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(NodeUtil.class);
     /**
      * @param <T>
      */
@@ -96,6 +100,18 @@ public final class NodeUtil {
         }
     }
 
+    public static INode getPossibleNearestParentOfType(INode node, Class<?> clazz) {
+        ParentNode<?> parent = null;
+        while (true) {
+            parent = node.getParent();
+            if (parent == null)
+                return null;
+            node = parent;
+            if (clazz.isInstance(node))
+                return node;
+        }
+    }
+
     public static INode getRootNode(INode node) {
         ParentNode<?> parent = null;
         while (true) {
@@ -276,6 +292,7 @@ public final class NodeUtil {
         printSceneGraph(System.out, 0, node);
     }
 
+    @FunctionalInterface
     public static interface NodeProcedure<T> {
         T execute(INode node, String id);
     }
@@ -324,6 +341,47 @@ public final class NodeUtil {
         return result;
     }
 
+    /**
+     * Recursively iterates through all child nodes of the specified node and
+     * for those nodes that are of class <code>ofClass</code>, invokes
+     * <code>consumer</code>.
+     * 
+     * @param node
+     * @param ofClass
+     * @param consumer
+     */
+    @SuppressWarnings("unchecked")
+    public static <T extends INode> INode forChildrenDeep(INode node, Class<T> ofClass, Function<T, INode> func) {
+        return forChildrenDeep(node, n -> ofClass.isInstance(n) ? func.apply((T) n) : null);
+    }
+
+    public static <T extends INode> INode forChildrenDeep(INode node, Function<INode, INode> func) {
+        INode ret = func.apply(node);
+        if (ret != null)
+            return ret;
+
+        if (node instanceof ParentNode<?>) {
+            if (node instanceof G2DParentNode) {
+                G2DParentNode g2dpn = (G2DParentNode) node;
+                for (IG2DNode n : g2dpn.getSortedNodes()) {
+                    INode r = forChildrenDeep(n, func);
+                    if (r != null) {
+                        return r;
+                    }
+                }
+            } else {
+                for (INode n : ((ParentNode<?>) node).getNodes()) {
+                    INode r = forChildrenDeep(n, func);
+                    if (r != null) {
+                        return r;
+                    }
+                }
+            }
+        }
+
+        return null;
+    }
+
     public static final int countTreeNodes(INode node) {
         int result = 1;
         if (node instanceof ParentNode<?>) {
@@ -336,11 +394,12 @@ public final class NodeUtil {
         return result;
     }
 
-    public static final void printTreeNodes(INode node, StringBuilder builder) {
+    public static final StringBuilder printTreeNodes(INode node, StringBuilder builder) {
         printTreeNodes(node, 0, builder);
+        return builder;
     }
 
-    public static final void printTreeNodes(INode node, int indent, StringBuilder builder) {
+    public static final StringBuilder printTreeNodes(INode node, int indent, StringBuilder builder) {
         for (int i = 0; i < indent; i++)
             builder.append(" ");
         builder.append(node.toString() + "\n");
@@ -351,6 +410,7 @@ public final class NodeUtil {
                 printTreeNodes(n, indent+2, builder);
             }
         }
+        return builder;
     }
 
     public static final <T extends INode> Set<T> collectNodes(INode node, Class<T> clazz) {
@@ -550,7 +610,7 @@ public final class NodeUtil {
             } else {
 
                 if(pc.length > 0) {
-                    System.err.println("Method " + setter.getName() + " expects " + pc[0].getCanonicalName() + " (got " + value.getClass().getCanonicalName() + ").");
+                    LOGGER.warn("Method " + setter.getName() + " expects " + pc[0].getCanonicalName() + " (got " + value.getClass().getCanonicalName() + ").");
                 }
 
             }
@@ -879,7 +939,7 @@ public final class NodeUtil {
             // Find node transform..
             AffineTransform transform = getGlobalToLocalTransform(node, null);
             if (transform == null) {
-                System.err.println("WARNING: Non-invertible transform for node: " + node);
+                LOGGER.warn("WARNING: Non-invertible transform for node: " + node);
                 return event;
             }
             MouseEvent me = (MouseEvent)event;
@@ -918,7 +978,7 @@ public final class NodeUtil {
 
                     if(DEBUG_BOUNDS) {
                         for(int i=0;i<indent;i++) System.err.print(" ");
-                        System.err.println("+getLocalBoundsImpl " + next  + " => " + bl);
+                        LOGGER.warn("+getLocalBoundsImpl " + next  + " => " + bl);
                     }
 
                     if(bl != null) {
@@ -932,7 +992,7 @@ public final class NodeUtil {
 
                 if(DEBUG_BOUNDS) {
                     for(int i=0;i<indent;i++) System.err.print(" ");
-                    System.err.println("=getLocalBoundsImpl " + node  + " => " + bounds);
+                    LOGGER.warn("=getLocalBoundsImpl " + node  + " => " + bounds);
                 }
 
                 return bounds;
@@ -941,7 +1001,7 @@ public final class NodeUtil {
                 if(result != null) {
                     if(DEBUG_BOUNDS) {
                         for(int i=0;i<indent;i++) System.err.print(" ");
-                        System.err.println("=getLocalBoundsImpl " + node  + " => " + result);
+                        LOGGER.warn("=getLocalBoundsImpl " + node  + " => " + result);
                     }
                     return result;
                 }