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.scenegraph.utils;
14 import java.awt.AWTEvent;
15 import java.awt.Container;
16 import java.awt.event.MouseEvent;
17 import java.awt.event.MouseWheelEvent;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.NoninvertibleTransformException;
20 import java.awt.geom.Point2D;
21 import java.awt.geom.Rectangle2D;
22 import java.io.PrintStream;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.List;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.locks.Condition;
33 import java.util.concurrent.locks.Lock;
34 import java.util.concurrent.locks.ReentrantLock;
36 import org.simantics.scenegraph.IDynamicSelectionPainterNode;
37 import org.simantics.scenegraph.ILookupService;
38 import org.simantics.scenegraph.INode;
39 import org.simantics.scenegraph.INode.PropertySetter;
40 import org.simantics.scenegraph.ISelectionPainterNode;
41 import org.simantics.scenegraph.ParentNode;
42 import org.simantics.scenegraph.g2d.G2DParentNode;
43 import org.simantics.scenegraph.g2d.G2DSceneGraph;
44 import org.simantics.scenegraph.g2d.IG2DNode;
45 import org.simantics.scenegraph.g2d.events.EventDelegator;
46 import org.simantics.scenegraph.g2d.events.NodeEventHandler;
47 import org.simantics.scenegraph.g2d.events.SGMouseEvent;
48 import org.simantics.scenegraph.g2d.events.SGMouseWheelEvent;
49 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
50 import org.simantics.scenegraph.g2d.nodes.FlagNode;
51 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
52 import org.simantics.scl.runtime.function.Function1;
53 import org.simantics.scl.runtime.function.FunctionImpl1;
54 import org.simantics.utils.datastructures.Pair;
55 import org.simantics.utils.threads.IThreadWorkQueue;
58 * Utilities for debugging/printing the contents of a scenegraph.
60 * @author Tuukka Lehtonen
62 public final class NodeUtil {
67 public static interface Filter<T> {
68 public boolean accept(T t);
71 public static class PrefixFilter implements Filter<String> {
72 private final String prefix;
73 public PrefixFilter(String prefix) {
77 public boolean accept(String t) {
78 return t.startsWith(prefix);
83 * The name of the sibling-node that is used to represent that a node is selected.
85 public static final String SELECTION_NODE_NAME = "selection";
87 public static INode getNearestParentOfType(INode node, Class<?> clazz) {
88 ParentNode<?> parent = null;
90 parent = node.getParent();
94 if (clazz.isInstance(node))
99 public static INode getRootNode(INode node) {
100 ParentNode<?> parent = null;
102 parent = node.getParent();
109 public static G2DSceneGraph getRootNode(IG2DNode node) {
110 INode root = getRootNode((INode) node);
111 return (G2DSceneGraph) root;
114 public static G2DSceneGraph getPossibleRootNode(IG2DNode node) {
115 INode root = getRootNode((INode) node);
116 return (root instanceof G2DSceneGraph) ? (G2DSceneGraph) root : null;
120 * Method for seeking node from scenegraph by class
127 public static <T> T getNearestChildByClass(G2DParentNode parent, Class<T> clazz) {
128 return getNearestChildByClass(parent.getNodes(), clazz);
132 * Breadth-first-search implementation to be used by getNearestChildByClass method
139 @SuppressWarnings("unchecked")
140 public static <T> T getNearestChildByClass(Collection<IG2DNode> nodes, Class<T> clazz) {
141 Collection<IG2DNode> list = null;
142 for (IG2DNode n : nodes) {
143 if (clazz.isInstance(n)) {
145 } else if (n instanceof G2DParentNode) {
147 list = new ArrayList<IG2DNode>();
148 list.addAll(((G2DParentNode)n).getNodes());
151 if (list == null || list.isEmpty()) return null;
152 return getNearestChildByClass(list, clazz);
156 * Tries to look for a child node from the specified node with the specified
157 * ID. Returns <code>null</code> if the specified node is a not a
158 * {@link ParentNode}.
164 public static INode getChildById(INode node, String id) {
165 if (node instanceof ParentNode<?>) {
166 return ((ParentNode<?>) node).getNode(id);
172 * Looks for the first child node of the specified node based on 2D Z-order.
173 * The specified node must be a {@link G2DParentNode} for the method to
176 * @param node the node to get for the first child from
177 * @return <code>null</code> if the specified node is not a
178 * {@link G2DParentNode} or has no children.
180 public static INode getFirstChild(INode node) {
181 if (node instanceof G2DParentNode) {
182 G2DParentNode pn = (G2DParentNode) node;
183 IG2DNode[] sorted = pn.getSortedNodes();
184 if (sorted.length > 0)
191 * Returns a single child node of the specified node or <code>null</code> if
192 * there are more than one or zero children.
194 * @param node the node to get a possible single child from
195 * @return single child node or <code>null</code> if specified node has more
196 * than one or zero children
198 public static INode getPossibleChild(INode node) {
199 if (node instanceof ParentNode<?>) {
200 ParentNode<?> pn = (ParentNode<?>) node;
201 if (pn.getNodeCount() == 1)
202 return pn.getNodes().iterator().next();
208 * Counts the depth of the specified node in its scene graph node tree.
209 * Depth 1 equals root level.
211 * @param node the node for which to count a depth
212 * @return the depth of the node
214 public static int getDepth(INode node) {
216 ParentNode<?> parent = null;
218 parent = node.getParent();
226 private static final void printSceneGraph(PrintStream stream, int indentLevel, INode node, String id) {
227 for (int i = 0; i < indentLevel; ++i)
229 stream.print(node.getSimpleClassName());
231 String lookupId = tryLookupId(node);
232 if (lookupId != null) {
233 stream.print(" {" + id + ", lookupId = "+lookupId+"}");
235 stream.print(" {" + id + "}");
238 stream.println(node);
239 if (node instanceof G2DParentNode) {
240 G2DParentNode parentNode = (G2DParentNode) node;
241 for (String cid : parentNode.getSortedNodesById()) {
242 Object child = parentNode.getNode(cid);
243 if (child instanceof INode)
244 printSceneGraph(stream, indentLevel + 1, (INode) child, cid);
246 } else if (node instanceof ParentNode<?>) {
247 ParentNode<? extends INode> parentNode = (ParentNode<?>) node;
248 for (String cid : parentNode.getNodeIds()) {
249 INode child = parentNode.getNode(cid);
250 printSceneGraph(stream, indentLevel + 1, (INode) child);
255 public static final void printSceneGraph(PrintStream stream, int indentLevel, INode node) {
257 ParentNode<?> parent = node.getParent();
258 if (parent != null) {
259 Collection<String> ids = parent.getNodeIds();
260 for (String i : ids) {
261 INode n = parent.getNode(i);
268 printSceneGraph(stream, indentLevel, node, id);
271 public static final void printSceneGraph(int indentLevel, INode node) {
272 printSceneGraph(System.out, indentLevel, node);
275 public static final void printSceneGraph(INode node) {
276 printSceneGraph(System.out, 0, node);
279 public static interface NodeProcedure<T> {
280 T execute(INode node, String id);
284 * @param node the node to iterate possible children for
285 * @param procedure invoked for each child node, if returns a
286 * <code>non-null</code> value, the return value is collected into the result
288 * @return the list of collected children
290 public static final <T> List<T> forChildren(INode node, NodeProcedure<T> procedure) {
291 return forChildren(node, procedure, new ArrayList<T>());
295 * @param node the node to iterate possible children for
296 * @param procedure invoked for each child node, if returns a
297 * <code>non-null</code> value, the node is collected into the result
299 * @param result the result list into which selected children are collected
300 * or <code>null</code> to not collect
301 * @return the list of collected children or null if provided result list
302 * was <code>null</code>
304 public static final <T> List<T> forChildren(INode node, NodeProcedure<T> procedure, List<T> result) {
305 if (node instanceof ParentNode<?>) {
306 ParentNode<?> pn = (ParentNode<?>) node;
307 if (node instanceof G2DParentNode) {
308 G2DParentNode g2dpn = (G2DParentNode) node;
309 for (String id : g2dpn.getSortedNodesById()) {
310 INode n = pn.getNode(id);
311 T t = procedure.execute(n, id);
312 if (t != null && result != null)
316 for (String id : pn.getNodeIds()) {
317 INode n = pn.getNode(id);
318 T t = procedure.execute(n, id);
319 if (t != null && result != null)
327 public static final int countTreeNodes(INode node) {
329 if (node instanceof ParentNode<?>) {
330 ParentNode<? extends INode> pn = (ParentNode<?>) node;
331 Collection<? extends INode> ns = pn.getNodes();
333 result += countTreeNodes(n);
339 public static final StringBuilder printTreeNodes(INode node, StringBuilder builder) {
340 printTreeNodes(node, 0, builder);
344 public static final StringBuilder printTreeNodes(INode node, int indent, StringBuilder builder) {
345 for (int i = 0; i < indent; i++)
347 builder.append(node.toString() + "\n");
348 if (node instanceof ParentNode<?>) {
349 ParentNode<? extends INode> pn = (ParentNode<?>) node;
350 Collection<? extends INode> ns = pn.getNodes();
352 printTreeNodes(n, indent+2, builder);
358 public static final <T extends INode> Set<T> collectNodes(INode node, Class<T> clazz) {
359 Set<T> result = new HashSet<T>();
360 collectNodes(node, clazz, result);
364 @SuppressWarnings("unchecked")
365 public static final <T extends INode> void collectNodes(INode node, Class<T> clazz, Set<T> result) {
366 if (clazz.isInstance(node))
367 result.add((T) node);
368 if (node instanceof ParentNode<?>) {
369 ParentNode<? extends INode> pn = (ParentNode<?>) node;
370 Collection<? extends INode> ns = pn.getNodes();
372 collectNodes(n, clazz, result);
377 public static <T extends INode> T getSingleNode(INode node, Class<T> clazz) {
378 Set<T> all = collectNodes(node, clazz);
379 if(all.size() != 1) throw new RuntimeException("Expected exactly 1 instance of class " + clazz.getCanonicalName() + ", got " + all.size());
380 return (T)all.iterator().next();
383 public static final boolean hasChildren(INode node) {
384 if (node instanceof ParentNode<?>) {
385 ParentNode<?> pn = (ParentNode<?>) node;
386 return !pn.getNodes().isEmpty();
392 * Look for a single scene graph node by its ID in a path under a specified
395 * @param parent the parent node under which to start looking
396 * @param idPath the node ID path
397 * @return <code>null</code> if node was not found
398 * @throws ClassCastException if the found node was not of the expected type
401 @SuppressWarnings("unchecked")
402 public static <T extends INode> T findNodeById(INode parent, String... idPath) {
404 for (int i = 0;; ++i) {
405 if (i >= idPath.length)
407 if (n instanceof ParentNode<?>) {
408 n = ((ParentNode<?>) n).getNode(idPath[i]);
416 * Tries to find out whether a node is selected or not.
418 * DISCLAIMER: this is a hack for the current
419 * org.simantics.g2d.diagram.participant.ElementPainter implementation that
420 * will stop working if the implementation changes.
423 * @param ascendLimit the max. amount of steps towards parent nodes to take
424 * while looking for selection information
425 * @return <code>true</code> if evidence of selection is found
427 public static boolean isSelected(INode node, int ascendLimit) {
429 ParentNode<?> pn = null;
430 if (node instanceof ParentNode<?>) {
431 pn = (ParentNode<?>) node;
433 pn = node.getParent();
436 for (; pn != null && steps <= ascendLimit; pn = pn.getParent(), ++steps) {
437 INode child = pn.getNode(SELECTION_NODE_NAME);
444 public static Container findRootPane(INode node) {
445 G2DSceneGraph parent = findNearestParentNode(node, G2DSceneGraph.class);
448 return ((G2DSceneGraph) parent).getRootPane();
451 private static boolean isSelectionPainter(INode node) {
452 if (node instanceof ISelectionPainterNode) {
453 if (node instanceof IDynamicSelectionPainterNode)
454 return ((IDynamicSelectionPainterNode)node).showsSelection();
464 public static boolean needSelectionPaint(INode elementNode) {
465 // FIXME: there should be a cleaner way to implement this.
467 if (isSelectionPainter(elementNode)) {
468 // System.out.println("skipped selection painting for connection node child ISelectionPainterNode");
472 if (elementNode instanceof ConnectionNode) {
473 // System.out.println("connectionNode");
474 for (IG2DNode child : ((ConnectionNode) elementNode).getNodes()) {
475 // System.out.println(" child " + child);
476 if (isSelectionPainter(child)) {
477 // System.out.println("skipped selection painting for connection node child ISelectionPainterNode");
480 if (child instanceof SingleElementNode) {
481 for(IG2DNode child2 : ((SingleElementNode) child).getNodes()) {
482 // System.out.println(" child2 " + child2);
483 if (isSelectionPainter(child2)) {
484 // System.out.println("skipped selection painting for edge ISelectionPainterNode");
490 } else if (elementNode instanceof SingleElementNode) {
491 for (INode child : ((SingleElementNode) elementNode).getNodes()) {
492 if (isSelectionPainter(child))
500 private static final String SET_THREAD_CALLBACKS_NAME = "CGLIB$SET_THREAD_CALLBACKS"; // If this method is found, the class is enhanced by cglib, thus does not contain annotations
502 public static Method getSetterForProperty(String property, INode node) {
503 assert(node != null);
504 Class<?> cl = node.getClass();
507 boolean isEnhanced = false;
508 for(Method method : cl.getMethods()) {
509 if(method.isAnnotationPresent(PropertySetter.class)) {
510 PropertySetter ann = method.getAnnotation(PropertySetter.class);
511 if(ann.value().equals(property)) {
514 } else if(method.getName().equals(SET_THREAD_CALLBACKS_NAME) && method.getGenericParameterTypes().length == 1) {
515 // The class seems to be enhanced by cglib, hence annotations are not present. Check superclass for annotations..
517 cl = cl.getSuperclass();
518 break; // We are not going to find any annotations, stop loop and try with the parent class
521 if(!isEnhanced || cl == null) break;
527 * TODO: log exceptions for debug purposes
529 * @param property name of the property
530 * @param value can be null..
534 public static boolean setPropertyIfSupported(String property, Object value, INode node) {
535 Method setter = getSetterForProperty(property, node);
537 Class<?> pc[] = setter.getParameterTypes();
538 if(pc.length == 1 && (value == null || pc[0].isAssignableFrom(value.getClass()))) {
540 setter.invoke(node, value);
542 } catch (IllegalArgumentException e) {
543 // TODO Auto-generated catch block
545 } catch (IllegalAccessException e) {
546 // TODO Auto-generated catch block
548 } catch (InvocationTargetException e) {
549 // TODO Auto-generated catch block
550 e.getCause().printStackTrace();
555 System.err.println("Method " + setter.getName() + " expects " + pc[0].getCanonicalName() + " (got " + value.getClass().getCanonicalName() + ").");
564 public static INode findChildById(ParentNode<?> parent, String key) {
565 INode result = parent.getNode(key);
569 for (String entry : parent.getNodeIds()) {
570 if (entry.startsWith(key))
571 return parent.getNode(key);
574 for (INode node : parent.getNodes()) {
575 if (node instanceof ParentNode) {
576 result = findChildById((ParentNode<?>) node, key);
586 private static int getSegmentEnd(String suffix) {
588 for(pos=1;pos<suffix.length();++pos) {
589 char c = suffix.charAt(pos);
590 if(c == '/' || c == '#')
596 public static String decodeString(String string) {
600 public static INode browsePossible(INode node, String suffix) {
603 switch(suffix.charAt(0)) {
605 INode parent = node.getParent();
608 return browsePossible(parent, suffix.substring(1));
611 /*int segmentEnd = getSegmentEnd(suffix);
612 Variable property = getPossibleProperty(graph,
613 decodeString(suffix.substring(1, segmentEnd)));
616 return property.browsePossible(graph, suffix.substring(segmentEnd));*/
620 int segmentEnd = getSegmentEnd(suffix);
621 INode child = findChildById((ParentNode<?>)node, decodeString(suffix.substring(1, segmentEnd)));
624 return browsePossible(child, suffix.substring(segmentEnd));
631 public static Pair<INode, String> browsePossibleReference(INode node, String suffix) {
633 throw new RuntimeException("Did not find a reference.");
634 switch(suffix.charAt(0)) {
636 INode parent = node.getParent();
639 return browsePossibleReference(parent, suffix.substring(1));
642 return Pair.make(node, suffix.substring(1));
645 int segmentEnd = getSegmentEnd(suffix);
646 INode child = findChildById((ParentNode<?>)node, decodeString(suffix.substring(1, segmentEnd)));
649 return browsePossibleReference(child, suffix.substring(segmentEnd));
656 public static INode findChildByPrefix(G2DParentNode parent, String prefix) {
657 INode result = parent.getNode(prefix);
661 for (String entry : parent.getNodeIds()) {
662 if (entry.startsWith(prefix))
663 return parent.getNode(entry);
666 for (IG2DNode node : parent.getNodes()) {
667 if (node instanceof G2DParentNode) {
668 result = findChildByPrefix((G2DParentNode) node, prefix);
682 public static Collection<String> filterDirectChildIds(ParentNode<?> parent, String prefix) {
683 return filterDirectChildIds(parent, new PrefixFilter(prefix));
691 public static Collection<String> filterDirectChildIds(ParentNode<?> parent, Filter<String> childFilter) {
692 Collection<String> childIds = parent.getNodeIds();
693 ArrayList<String> result = new ArrayList<String>(childIds.size());
695 for (String id : childIds)
696 if (childFilter.accept(id))
707 public static Collection<INode> filterDirectChildren(ParentNode<?> parent, Filter<String> childFilter) {
708 Collection<String> childIds = parent.getNodeIds();
709 ArrayList<INode> result = new ArrayList<INode>(childIds.size());
711 for (String id : childIds)
712 if (childFilter.accept(id))
713 result.add( parent.getNode(id) );
720 * @return the lookup service for the specified node
721 * @throws UnsupportedOperationException if ILookupService is not available
723 public static ILookupService getLookupService(INode node) {
724 ParentNode<?> root = node.getRootNode();
725 if (!(root instanceof ILookupService))
726 throw new UnsupportedOperationException("ILookupService not supported by root node " + root + " attained from " + node);
727 return (ILookupService) root;
732 * @return <code>null</code> if lookup service is not available
734 public static ILookupService tryGetLookupService(INode node) {
735 ParentNode<?> root = node.getRootNode();
736 return root instanceof ILookupService ? (ILookupService) root : null;
742 * @return <code>null</code> if lookup failed, i.e. mapping does not exist
743 * @throws UnsupportedOperationException if lookup is not supported
744 * @see #getLookupService(INode)
745 * @see ILookupService
747 public static INode lookup(INode node, String id) {
748 ILookupService lookup = getLookupService(node);
749 return lookup.lookupNode(id);
755 * @return <code>null</code> if lookup not supported or lookup failed
756 * @see #tryGetLookupService(INode)
757 * @see ILookupService
759 public static INode tryLookup(INode node, String id) {
760 ILookupService lookup = tryGetLookupService(node);
761 return lookup != null ? lookup.lookupNode(id) : null;
768 * @return <code>null</code> if lookup failed, i.e. mapping does not exist
769 * @throws UnsupportedOperationException if lookup is not supported
770 * @throws ClassCastException if the found node cannot be cast to the
772 * @see #getLookupService(INode)
773 * @see ILookupService
775 public static <T> T lookup(INode node, String id, Class<T> clazz) {
776 ILookupService lookup = getLookupService(node);
777 INode found = lookup.lookupNode(id);
778 return found != null ? clazz.cast(found) : null;
784 * @return <code>null</code> if lookup not supported or lookup failed
785 * @see #tryGetLookupService(INode)
786 * @see ILookupService
788 public static <T> T tryLookup(INode node, String id, Class<T> clazz) {
789 ILookupService lookup = tryGetLookupService(node);
792 INode found = lookup.lookupNode(id);
793 return found != null ? clazz.cast(found) : null;
799 * @return <code>null</code> if lookup failed, i.e. mapping does not exist
800 * @throws UnsupportedOperationException if lookup is not supported
801 * @see #getLookupService(INode)
802 * @see ILookupService
804 public static String lookupId(INode node) {
805 ILookupService lookup = getLookupService(node);
806 return lookup.lookupId(node);
812 * @return <code>null</code> if lookup not supported or lookup failed
813 * @see #tryGetLookupService(INode)
814 * @see ILookupService
816 public static String tryLookupId(INode node) {
817 ILookupService lookup = tryGetLookupService(node);
818 return lookup != null ? lookup.lookupId(node) : null;
822 * Map the specified node to the specified ID in the {@link ILookupService}
823 * provided by the root node of the specified node.
827 * @throws UnsupportedOperationException if {@link ILookupService} is not
828 * available for the specified node
829 * @see #getLookupService(INode)
830 * @see ILookupService
832 public static void map(INode node, String id) {
833 getLookupService(node).map(id, node);
837 * Remove possible ILookupService mapping for the specified node.
839 * @param node the node to try to remove mappings for
840 * @return mapped ID or <code>null</code> if no mapping existed
841 * @throws UnsupportedOperationException if {@link ILookupService} is not
842 * available for the specified node
843 * @see ILookupService
844 * @see #getLookupService(INode)
846 public static String unmap(INode node) {
847 return getLookupService(node).unmap(node);
851 * Try to remove possible ILookupService mapping for the specified node.
853 * @param node the node to try to remove mappings for
854 * @return mapped ID or <code>null</code> if {@link ILookupService} is not
855 * supported or no mapping existed
856 * @see ILookupService
857 * @see #tryGetLookupService(INode)
859 public static String tryUnmap(INode node) {
860 ILookupService lookup = tryGetLookupService(node);
861 return lookup != null ? lookup.unmap(node) : null;
864 public static EventDelegator getEventDelegator(INode node) {
865 ParentNode<?> n = node.getRootNode();
866 if (n instanceof G2DSceneGraph) {
867 return ((G2DSceneGraph) n).getEventDelegator();
872 public static NodeEventHandler getNodeEventHandler(INode node) {
873 ParentNode<?> n = node.getRootNode();
874 return (n instanceof G2DSceneGraph) ? ((G2DSceneGraph) n).getEventHandler() : null;
875 // INodeEventHandlerProvider provider = findNearestParentNode(node, INodeEventHandlerProvider.class);
876 // return provider != null ? provider.getEventHandler() : null;
879 public static AWTEvent transformEvent(AWTEvent event, IG2DNode node) {
880 if (event instanceof MouseEvent) {
881 // Find node transform..
882 AffineTransform transform = getGlobalToLocalTransform(node, null);
883 if (transform == null) {
884 System.err.println("WARNING: Non-invertible transform for node: " + node);
887 MouseEvent me = (MouseEvent)event;
888 // Use double coordinates if available
889 Point2D p = new Point2D.Double((double)me.getX(), (double)me.getY());
890 transform.transform(p, p);
893 // Giving event.getSource() as a parameter for the new events will cause major delay for the event instantiation, hence dummy component is used
894 if (event instanceof MouseWheelEvent) {
895 e = new SGMouseWheelEvent(new DummyComponent(), me.getID(), me.getWhen(), me.getModifiers(), p.getX(), p.getY(), me.getClickCount(), me.isPopupTrigger(), ((MouseWheelEvent)me).getScrollType(), ((MouseWheelEvent)me).getScrollAmount(), ((MouseWheelEvent)me).getWheelRotation(), me);
897 e = new SGMouseEvent(new DummyComponent(), me.getID(), me.getWhen(), me.getModifiers(), p.getX(), p.getY(), me.getClickCount(), me.isPopupTrigger(), me.getButton(), me);
904 private static final boolean DEBUG_BOUNDS = false;
906 private static Rectangle2D getLocalBoundsImpl(INode node, Function1<INode, Boolean> filter, int indent) {
907 if (node instanceof IG2DNode) {
908 if (node instanceof G2DParentNode) {
909 G2DParentNode pNode = (G2DParentNode)node;
910 Iterator<IG2DNode> it = pNode.getNodes().iterator();
913 Rectangle2D bounds = null;
914 while (it.hasNext()) {
915 IG2DNode next = it.next();
916 if (filter != null && !filter.apply(next))
919 Rectangle2D bl = getLocalBoundsImpl(next, filter, indent+2);
922 for(int i=0;i<indent;i++) System.err.print(" ");
923 System.err.println("+getLocalBoundsImpl " + next + " => " + bl);
928 bounds = next.localToParent(bl.getFrame());
930 bounds.add(next.localToParent(bl));
936 for(int i=0;i<indent;i++) System.err.print(" ");
937 System.err.println("=getLocalBoundsImpl " + node + " => " + bounds);
942 Rectangle2D result = ((IG2DNode)node).getBoundsInLocal(true);
945 for(int i=0;i<indent;i++) System.err.print(" ");
946 System.err.println("=getLocalBoundsImpl " + node + " => " + result);
955 public static Rectangle2D getLocalBounds(INode node) {
956 return getLocalBoundsImpl(node, null, 0);
959 public static Rectangle2D getLocalBounds(INode node, final Set<INode> excluding) {
960 return getLocalBoundsImpl(node, new FunctionImpl1<INode, Boolean>() {
963 public Boolean apply(INode node) {
964 return !excluding.contains(node);
969 public static Rectangle2D getLocalBounds(INode node, final Class<?> excluding) {
970 return getLocalBoundsImpl(node, new FunctionImpl1<INode, Boolean>() {
973 public Boolean apply(INode node) {
974 return !excluding.isInstance(node);
979 public static Rectangle2D getLocalElementBounds(INode node) {
980 if(node instanceof ConnectionNode) {
981 return getLocalBounds(node);
982 } else if(node instanceof SingleElementNode) {
983 // For normal symbols
984 INode image = NodeUtil.findChildByPrefix((SingleElementNode)node, "composite_image");
986 // For generic text nodes
987 image = NodeUtil.findChildByPrefix((SingleElementNode) node, "text");
989 // For I/O table diagram flags (value of org.simantics.diagram.flag.FlagSceneGraph.VISUAL_ROOT)
990 image = NodeUtil.findChildByPrefix((SingleElementNode) node, "visual");
992 image = NodeUtil.getNearestChildByClass((SingleElementNode) node, FlagNode.class);
994 return getLocalElementBounds(image);
996 return getLocalBounds(node);
998 return getLocalBounds(node);
1002 public static <T> T findNearestParentNode(INode node, Class<T> ofClass) {
1003 ParentNode<?> parent = null;
1005 parent = node.getParent();
1008 if (ofClass.isInstance(parent))
1009 return ofClass.cast(parent);
1014 private static class PendingTester implements Runnable {
1016 private boolean pending = true;
1017 private final G2DSceneGraph sg;
1019 private final Lock pendingLock = new ReentrantLock();
1020 private final Condition pendingSet = pendingLock.newCondition();
1022 public PendingTester(G2DSceneGraph sg) {
1030 pending = sg.isPending();
1031 pendingSet.signalAll();
1033 pendingLock.unlock();
1037 public boolean isPending() {
1041 public void await() {
1045 pendingSet.await(10, TimeUnit.MILLISECONDS);
1046 } catch (InterruptedException e) {
1049 pendingLock.unlock();
1055 public static void waitPending(IThreadWorkQueue thread, G2DSceneGraph sg) {
1056 // Wait for 30s by default
1057 waitPending(thread, sg, 30000);
1061 * Synchronously waits until the the specified scene graph is no longer in
1064 * @param thread the thread to schedule pending checks into
1065 * @param sg the scene graph to wait upon
1067 public static void waitPending(IThreadWorkQueue thread, G2DSceneGraph sg, int timeoutMs) {
1068 PendingTester tester = new PendingTester(sg);
1069 long start = System.currentTimeMillis();
1070 while (tester.isPending()) {
1071 thread.asyncExec(tester);
1072 if (tester.isPending())
1074 long duration = System.currentTimeMillis() - start;
1075 if(duration > timeoutMs)
1076 throw new IllegalStateException("Timeout in resolving pending nodes.");
1080 public static void increasePending(INode node) {
1081 G2DSceneGraph sg = (G2DSceneGraph) node.getRootNode();
1083 sg.increasePending(node);
1086 public static void decreasePending(INode node) {
1087 G2DSceneGraph sg = (G2DSceneGraph) node.getRootNode();
1089 sg.decreasePending(node);
1094 public static AffineTransform getLocalToGlobalTransform(IG2DNode node, AffineTransform result) {
1095 result.setToIdentity();
1096 ParentNode<?> parent = node.getParent();
1097 while (parent != null) {
1098 result.preConcatenate(((IG2DNode) parent).getTransform());
1099 parent = parent.getParent();
1104 public static AffineTransform getLocalToGlobalTransform(IG2DNode node) {
1105 return getLocalToGlobalTransform(node, new AffineTransform());
1108 public static AffineTransform getGlobalToLocalTransform(IG2DNode node) throws NoninvertibleTransformException {
1109 AffineTransform transform = getLocalToGlobalTransform(node);
1114 public static AffineTransform getGlobalToLocalTransform(IG2DNode node, AffineTransform returnIfNonInvertible) {
1115 AffineTransform transform = getLocalToGlobalTransform(node);
1119 } catch (NoninvertibleTransformException e) {
1120 return returnIfNonInvertible;
1124 public static Point2D worldToLocal(IG2DNode local, Point2D pt, Point2D pt2) {
1125 AffineTransform at = getGlobalToLocalTransform(local, null);
1127 pt2.setLocation(pt);
1130 return at.transform(pt, pt2);
1133 public static Point2D localToWorld(IG2DNode local, Point2D pt, Point2D pt2) {
1134 AffineTransform at = getLocalToGlobalTransform(local);
1135 return at.transform(pt, pt2);
1138 public static String getNodeName(INode nn) {
1139 INode node = nn.getParent();
1140 ParentNode<?> pn = (ParentNode<?>) node;
1141 if (node instanceof G2DParentNode) {
1142 G2DParentNode g2dpn = (G2DParentNode) node;
1143 for (String id : g2dpn.getSortedNodesById())
1145 INode n = pn.getNode(id);
1155 * For asking whether specified parent node really is a parent of the
1156 * specified child node.
1161 * alleged child of parent
1162 * @return <code>true</code> if parent really is a parent of child
1164 public static boolean isParentOf(INode parent, INode child) {
1166 if (parent == child)
1168 child = child.getParent();