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.g2d.element;
14 import java.awt.Color;
16 import java.awt.Shape;
17 import java.awt.Stroke;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Area;
20 import java.awt.geom.NoninvertibleTransformException;
21 import java.awt.geom.Point2D;
22 import java.awt.geom.Rectangle2D;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.function.Consumer;
30 import org.simantics.g2d.canvas.ICanvasContext;
31 import org.simantics.g2d.diagram.IDiagram;
32 import org.simantics.g2d.diagram.handler.DataElementMap;
33 import org.simantics.g2d.diagram.handler.PickRequest;
34 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
35 import org.simantics.g2d.diagram.handler.Topology.Terminal;
36 import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil;
37 import org.simantics.g2d.element.handler.Adapter;
38 import org.simantics.g2d.element.handler.AdditionalColor;
39 import org.simantics.g2d.element.handler.BendsHandler;
40 import org.simantics.g2d.element.handler.BendsHandler.Bend;
41 import org.simantics.g2d.element.handler.BorderColor;
42 import org.simantics.g2d.element.handler.Clickable;
43 import org.simantics.g2d.element.handler.Clickable.ClickListener;
44 import org.simantics.g2d.element.handler.Clickable.PressStatus;
45 import org.simantics.g2d.element.handler.EdgeVisuals;
46 import org.simantics.g2d.element.handler.ElementAdapter;
47 import org.simantics.g2d.element.handler.FillColor;
48 import org.simantics.g2d.element.handler.Hover;
49 import org.simantics.g2d.element.handler.InternalSize;
50 import org.simantics.g2d.element.handler.Move;
51 import org.simantics.g2d.element.handler.Outline;
52 import org.simantics.g2d.element.handler.Parent;
53 import org.simantics.g2d.element.handler.Pick;
54 import org.simantics.g2d.element.handler.Resize;
55 import org.simantics.g2d.element.handler.Scale;
56 import org.simantics.g2d.element.handler.Stateful;
57 import org.simantics.g2d.element.handler.TerminalLayout;
58 import org.simantics.g2d.element.handler.TerminalTopology;
59 import org.simantics.g2d.element.handler.Text;
60 import org.simantics.g2d.element.handler.TextColor;
61 import org.simantics.g2d.element.handler.TextEditor;
62 import org.simantics.g2d.element.handler.TextFont;
63 import org.simantics.g2d.element.handler.Transform;
64 import org.simantics.g2d.participant.TransformUtil;
65 import org.simantics.g2d.utils.GeometryUtils;
66 import org.simantics.g2d.utils.geom.DirectionSet;
67 import org.simantics.scenegraph.INode;
68 import org.simantics.scenegraph.ParentNode;
69 import org.simantics.utils.ObjectUtils;
70 import org.simantics.utils.datastructures.hints.IHintContext;
71 import org.simantics.utils.datastructures.hints.IHintContext.Key;
74 * Utils for element users.
76 * @See {@link TerminalUtil}
77 * @See {@link ElementHandlerUtils} Utils for element handler (coders)
78 * @author Toni Kalajainen
80 public class ElementUtils {
82 @SuppressWarnings("unchecked")
83 public static <T> T getObject(IElement e) {
84 return (T) e.getHint(ElementHints.KEY_OBJECT);
87 public static void disable(IElement e)
89 Stateful enabled = e.getElementClass().getSingleItem(Stateful.class);
90 enabled.setEnabled(e, false);
93 public static void enable(IElement e)
95 Stateful enabled = e.getElementClass().getSingleItem(Stateful.class);
96 enabled.setEnabled(e, true);
103 public static boolean isHidden(IElement e) {
104 return e.containsHint(ElementHints.KEY_HIDDEN);
109 * @param state <code>null</code> to remove hidden state
112 public static void setHidden(IElement e, boolean hidden) {
114 e.setHint(ElementHints.KEY_HIDDEN, HideState.COMPLETELY_HIDDEN);
116 e.removeHint(ElementHints.KEY_HIDDEN);
119 public static void setText(IElement e, String text)
121 Text t = e.getElementClass().getSingleItem(Text.class);
125 public static String getText(IElement e)
127 Text t = e.getElementClass().getSingleItem(Text.class);
132 * Resizes or scales an element to fit it into a rectangle.
135 * @param rect rectangle on diagram
137 public static void fitToRectangle(IElement e, Rectangle2D rect)
139 ElementClass ec = e.getElementClass();
140 Move m = ec.getSingleItem(Move.class);
141 InternalSize b = ec.getSingleItem(InternalSize.class);
142 Rectangle2D internalSize = b.getBounds(e, null);
143 if (internalSize == null)
146 Resize rs = ec.getAtMostOneItemOfClass(Resize.class);
148 Scale s = ec.getAtMostOneItemOfClass(Scale.class);
149 Point2D scale = s==null?new Point2D.Double(1.0,1.0):s.getScale(e);
150 double width = rect.getWidth();
151 double height = rect.getHeight();
152 double aspectRatio = width/height;
154 Double requiredAspectRatio = rs==null?null:rs.getFixedAspectRatio(e);
155 if (requiredAspectRatio!=null)
157 if (aspectRatio>requiredAspectRatio)
158 width = height*requiredAspectRatio;
160 height = width / requiredAspectRatio;
165 m.moveTo(e, rect.getX(), rect.getY());
167 width /= scale.getX();
168 height /= scale.getY();
170 Rectangle2D r = new Rectangle2D.Double(0, 0, width, height);
175 double sx = rect.getWidth() / internalSize.getWidth();
176 double sy = rect.getHeight() / internalSize.getHeight();
177 double px = rect.getX() - internalSize.getX()*sx;
178 double py = rect.getY() - internalSize.getY()*sy;
180 scale.setLocation(sx, sy);
181 s.setScale(e, scale);
185 public static void addClickListener(IElement e, ICanvasContext ctx, ClickListener listener)
187 Clickable clickable = e.getElementClass().getAtMostOneItemOfClass(Clickable.class);
188 clickable.addListener(e, ctx, ctx.getThreadAccess(), listener);
191 public static Point2D getPos(IElement e)
193 Move m = e.getElementClass().getSingleItem(Move.class);
194 return m.getPosition(e);
197 public static Point2D getPos(IElement e, Point2D result)
199 Move m = e.getElementClass().getSingleItem(Move.class);
201 result = new Point2D.Double();
202 Point2D p = m.getPosition(e);
203 result.setLocation(p);
207 public static Point2D getAbsolutePos(IElement e)
209 Transform tr = e.getElementClass().getSingleItem(Transform.class);
210 AffineTransform at = tr.getTransform(e);
211 return new Point2D.Double(at.getTranslateX(), at.getTranslateY());
214 public static Point2D getAbsolutePos(IElement e, Point2D result)
216 Transform tr = e.getElementClass().getSingleItem(Transform.class);
217 AffineTransform at = tr.getTransform(e);
219 result = new Point2D.Double();
220 result.setLocation(at.getTranslateX(), at.getTranslateY());
224 public static void setPos(IElement e, Point2D newPosition)
226 Move m = e.getElementClass().getSingleItem(Move.class);
227 m.moveTo(e, newPosition.getX(), newPosition.getY());
230 public static void setPos(IElement e, double x, double y)
232 Move m = e.getElementClass().getSingleItem(Move.class);
236 public static IElement getByData(IDiagram d, Object data)
238 DataElementMap map = d.getDiagramClass().getSingleItem(DataElementMap.class);
239 return map.getElement(d, data);
242 public static Object getData(IDiagram d, IElement element)
244 DataElementMap map = d.getDiagramClass().getSingleItem(DataElementMap.class);
245 return map.getData(d, element);
249 * Get all terminals of an element.
252 * @param result a store for the terminals
253 * @param clearResult <code>true</code> to clear the result collection
255 * @return the specified result collection
257 public static Collection<Terminal> getTerminals(IElement e, Collection<Terminal> result, boolean clearResult) {
260 TerminalTopology tt = e.getElementClass().getSingleItem(TerminalTopology.class);
261 tt.getTerminals(e, result);
266 * Get a terminal of an element assuming there is only a single terminal.
270 * @return the only terminal of element e
271 * @throws IllegalArgumentException if there are zero or multiple terminals
273 public static Terminal getSingleTerminal(IElement e) {
274 ArrayList<Terminal> ts = new ArrayList<Terminal>(4);
275 TerminalTopology tt = e.getElementClass().getSingleItem(TerminalTopology.class);
276 tt.getTerminals(e, ts);
278 throw new IllegalArgumentException("expected 1 terminal, element e has " + ts.size() + " terminals: " + ts);
283 * Get a terminal of an element assuming there is only a single terminal.
284 * If there are no or multiple terminals, <code>null</code> is returned.
290 public static Terminal peekSingleTerminal(IElement e) {
291 ArrayList<Terminal> ts = new ArrayList<Terminal>(4);
292 TerminalTopology tt = e.getElementClass().getSingleItem(TerminalTopology.class);
293 tt.getTerminals(e, ts);
300 * Get allowed outward directions of a terminal
305 public static DirectionSet getTerminalDirection(IElement e, Terminal t)
307 List<TerminalLayout> tls = e.getElementClass().getItemsByClass(TerminalLayout.class);
308 DirectionSet result = new DirectionSet();
309 for (TerminalLayout tl : tls) {
310 tl.getTerminalDirection(e, t, result);
315 public static AffineTransform getTransform(IElement e)
317 return e.getElementClass().getSingleItem(Transform.class).getTransform(e);
320 public static AffineTransform getTransform(IElement e, AffineTransform result)
324 AffineTransform tr = e.getElementClass().getSingleItem(Transform.class).getTransform(e);
325 result.setTransform(tr);
330 * @param e the element to get the local transform from
331 * @param result the transform to set to the local transform value or
332 * <code>null</code> to allocate a new transform if the element
333 * doesn't provide one. By providing a result transform one can make
334 * sure that no internal state of the element is returned.
335 * @return the provided result transform or a new transform instance
336 * depending on the arguments
338 public static AffineTransform getLocalTransform(IElement e, AffineTransform result)
340 AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
342 result = new AffineTransform();
344 result.setTransform(at);
348 public static void setTransform(IElement e, AffineTransform at)
350 e.getElementClass().getSingleItem(Transform.class).setTransform(e, at);
353 public static AffineTransform getInvTransform(IElement e)
356 return e.getElementClass().getSingleItem(Transform.class).getTransform(e).createInverse();
357 } catch (NoninvertibleTransformException e1) {
358 throw new RuntimeException(e1);
364 * Element to canvas coordinates
366 * @param elementPoint
370 public static Point2D elementToCanvasCoordinate(IElement e, Point2D elementPoint, Point2D canvasPoint)
372 Transform t = e.getElementClass().getSingleItem(Transform.class);
373 AffineTransform at = t.getTransform(e);
374 return at.transform(elementPoint, canvasPoint);
378 * Element to control coordinates
381 * @param elementPoint
382 * @param controlPoint
385 public static Point2D elementToControlCoordinate(IElement e, ICanvasContext ctx, Point2D elementPoint, Point2D controlPoint)
387 Transform t = e.getElementClass().getSingleItem(Transform.class);
388 TransformUtil util = ctx.getSingleItem(TransformUtil.class);
389 Point2D canvasPoint = t.getTransform(e).transform(elementPoint, null);
390 return util.getTransform().transform(elementPoint, canvasPoint);
393 public static Point2D controlToElementCoordinate(IElement e, ICanvasContext ctx, Point2D controlPoint, Point2D elementPoint)
395 Transform t = e.getElementClass().getSingleItem(Transform.class);
396 AffineTransform at = t.getTransform(e);
397 TransformUtil util = ctx.getSingleItem(TransformUtil.class);
398 Point2D canvasPoint = util.controlToCanvas(controlPoint, new Point2D.Double());
399 if (elementPoint==null) elementPoint = new Point2D.Double();
401 at.inverseTransform(canvasPoint, elementPoint);
403 } catch (NoninvertibleTransformException e1) {
404 throw new RuntimeException(e1);
408 public static Point2D controlToCanvasCoordinate(ICanvasContext ctx, Point2D controlPoint, Point2D canvasPoint)
410 TransformUtil tu = ctx.getSingleItem(TransformUtil.class);
411 return tu.controlToCanvas(controlPoint, canvasPoint);
415 public static PressStatus getPressStatus(IElement e, ICanvasContext ctx)
417 Clickable c = e.getElementClass().getAtMostOneItemOfClass(Clickable.class);
418 if (c==null) return null;
419 return c.getPressStatus(e, ctx);
422 public static Color getBorderColor(IElement e)
424 return getBorderColor(e, null);
427 public static Color getFillColor(IElement e)
429 return getFillColor(e, null);
432 public static Color getAdditionalColor(IElement e)
434 return getAdditionalColor(e, null);
437 public static Color getTextColor(IElement e)
439 return getTextColor(e, null);
443 * Get border color of element of return defaultValue if border color is not
447 * @param defaultValue
450 public static Color getBorderColor(IElement e, Color defaultValue)
452 BorderColor bc = e.getElementClass().getAtMostOneItemOfClass(BorderColor.class);
453 if (bc==null) return defaultValue;
454 Color c = bc.getBorderColor(e);
455 return c != null ? c : defaultValue;
459 * Get fill color of element of return defaultValue if fill color is not
463 * @param defaultValue
466 public static Color getFillColor(IElement e, Color defaultValue)
468 FillColor fc = e.getElementClass().getAtMostOneItemOfClass(FillColor.class);
469 if (fc==null) return defaultValue;
470 Color c = fc.getFillColor(e);
471 return c != null ? c : defaultValue;
475 * Get additional color of element of return defaultValue if additional
476 * color is not available.
479 * @param defaultValue
482 public static Color getAdditionalColor(IElement e, Color defaultValue)
484 AdditionalColor ac = e.getElementClass().getAtMostOneItemOfClass(AdditionalColor.class);
485 if (ac==null) return null;
486 Color c = ac.getAdditionalColor(e);
487 return c != null ? c : defaultValue;
491 * Get text color of element of return defaultValue if text color is not
495 * @param defaultValue
498 public static Color getTextColor(IElement e, Color defaultValue)
500 TextColor tc = e.getElementClass().getAtMostOneItemOfClass(TextColor.class);
501 if (tc==null) return defaultValue;
502 Color c = tc.getTextColor(e);
503 return c != null ? c : defaultValue;
506 public static TextEditor getTextEditor(IElement e)
508 TextEditor ed = e.getElementClass().getAtMostOneItemOfClass(TextEditor.class);
512 public static void setBorderColor(IElement e, Color color)
514 BorderColor bc = e.getElementClass().getAtMostOneItemOfClass(BorderColor.class);
515 if (bc==null) return;
516 bc.setBorderColor(e, color);
519 public static void setFillColor(IElement e, Color color)
521 FillColor bc = e.getElementClass().getAtMostOneItemOfClass(FillColor.class);
522 if (bc==null) return;
523 bc.setFillColor(e, color);
526 public static void setAdditionalColor(IElement e, Color color)
528 AdditionalColor bc = e.getElementClass().getAtMostOneItemOfClass(AdditionalColor.class);
529 if (bc==null) return;
530 bc.setAdditionalColor(e, color);
533 public static void setTextColor(IElement e, Color color)
535 TextColor bc = e.getElementClass().getAtMostOneItemOfClass(TextColor.class);
536 if (bc==null) return;
537 bc.setTextColor(e, color);
540 public static void setEdgeStroke(IElement e, Stroke s)
542 EdgeVisuals ev = e.getElementClass().getSingleItem(EdgeVisuals.class);
547 * Fill given map with element bounds (the bounds on diagram)
550 * @param rects structure to be filled or null (instantates new)
551 * @return rects or newly instantiated structure
553 public static Map<IElement, Rectangle2D> getElementBoundsOnDiagram(Collection<IElement> elements, Map<IElement, Rectangle2D> rects)
555 if (rects == null) rects = new HashMap<IElement, Rectangle2D>();
556 for (IElement e : elements) {
557 Shape shape = getElementBoundsOnDiagram(e);
558 rects.put(e, shape.getBounds2D());
566 * @return element bounds in element coordinates
568 public static Rectangle2D getElementBounds(IElement e)
570 InternalSize b = e.getElementClass().getSingleItem(InternalSize.class);
571 return b.getBounds(e, new Rectangle2D.Double());
577 * @param result a rectangle for storing the result
578 * @return the specified result rectangle
580 public static Rectangle2D getElementBounds(IElement e, Rectangle2D result)
582 InternalSize b = e.getElementClass().getSingleItem(InternalSize.class);
583 return b.getBounds(e, result);
587 * Get rough estimation of outer bounds of an element
589 * @return bounds on a diagram
591 public static Shape getElementBoundsOnDiagram(IElement e)
593 Rectangle2D elementBounds = getElementBounds(e);
594 Transform t = e.getElementClass().getSingleItem(Transform.class);
595 AffineTransform canvasToElement = t.getTransform(e);
596 return GeometryUtils.transformShape(elementBounds, canvasToElement);
600 * Get rough estimation of outer bounds of an element
602 * @param result a rectangle for storing the result
603 * @return bounds on a diagram
605 public static Rectangle2D getElementBoundsOnDiagram(IElement e, Rectangle2D result)
607 result = getElementBounds(e, result);
608 Transform t = e.getElementClass().getSingleItem(Transform.class);
609 AffineTransform canvasToElement = t.getTransform(e);
610 Shape shp = GeometryUtils.transformShape(result, canvasToElement);
611 result.setFrame(shp.getBounds2D());
616 * Get union of outer bounds of a set of elements
618 * @return Union of element bounds (on diagram) or null
620 public static Shape getElementBoundsOnDiagram(Collection<IElement> elements)
622 if (elements.size()==0) return null;
623 if (elements.size()==1) return getElementBoundsOnDiagram(elements.iterator().next());
625 for (IElement e : elements) {
626 Shape bounds = getElementBoundsOnDiagram(e);
627 Area ae = bounds instanceof Area ? (Area) bounds : new Area(bounds);
634 * Get union of outer bounds of a set of elements
636 * @return Union of element bounds (on diagram) or null
638 public static Rectangle2D getSurroundingElementBoundsOnDiagram(Collection<IElement> elements)
640 if (elements.size()==0) return null;
641 if (elements.size()==1) return getElementBoundsOnDiagram(elements.iterator().next()).getBounds2D();
642 double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE, maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
643 for (IElement e : elements) {
644 Rectangle2D bounds = getElementBoundsOnDiagram(e).getBounds2D();
645 if (bounds.getMinX() < minX) minX = bounds.getMinX();
646 if (bounds.getMinY() < minY) minY = bounds.getMinY();
647 if (bounds.getMaxX() > maxX) maxX = bounds.getMaxX();
648 if (bounds.getMaxY() > maxY) maxY = bounds.getMaxY();
650 return new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);
654 * Get as accurate shape if available
657 * @return accurate shape of an element or <code>null</code> if shape is not available
659 public static Shape getElementShape(IElement e)
661 List<Outline> shapeProviders = e.getElementClass().getItemsByClass(Outline.class);
662 if (shapeProviders.isEmpty()) return null;
663 if (shapeProviders.size()==1) return shapeProviders.iterator().next().getElementShape(e);
665 for (Outline es : shapeProviders)
667 Shape shape = es.getElementShape(e);
668 Area ae = shape instanceof Area ? (Area) shape : new Area(shape);
674 public static Shape getElementShapeOnDiagram(IElement e)
676 Shape shape = getElementShape(e);
679 Transform t = e.getElementClass().getSingleItem(Transform.class);
680 AffineTransform canvasToElement = t.getTransform(e);
681 return GeometryUtils.transformShape(shape, canvasToElement);
685 * Get element shape is one exists otherwise its bounds
687 * @return shape or bounds
689 public static Shape getElementShapeOrBounds(IElement e)
691 Shape shape = getElementShape(e);
692 if (shape!=null) return shape;
693 return getElementBounds(e);
697 * Get element shape is one exists otherwise its bounds
699 * @return shape or bounds
701 public static Shape getElementShapeOrBoundsOnDiagram(IElement e)
703 Shape shape = getElementShapeOnDiagram(e);
704 if (shape!=null) return shape;
705 return getElementBoundsOnDiagram(e);
708 public static Shape getElementShapesOnDiagram(Collection<IElement> elements)
710 if (elements.isEmpty()) return null;
711 if (elements.size()==1) {
712 //ITask task = ThreadLogger.getInstance().begin("single element shape: " + elements.iterator().next() + ")");
713 Shape shp = getElementShapeOrBoundsOnDiagram(elements.iterator().next());
718 //ITask task = ThreadLogger.getInstance().begin("union of " + elements.size() + " element shapes");
719 for (IElement e : elements) {
720 //ITask task2 = ThreadLogger.getInstance().begin("calculate area of " + e);
721 Shape shape = getElementShapeOrBoundsOnDiagram(e);
723 //task2 = ThreadLogger.getInstance().begin("construct area from " + shape);
725 if (shape instanceof Area)
728 aa = new Area(shape);
730 //task2 = ThreadLogger.getInstance().begin("union area " + aa);
738 public static Shape mergeShapes(Collection<Shape> shapes)
740 if (shapes.isEmpty()) return null;
741 if (shapes.size()==1) return shapes.iterator().next();
743 for (Shape s : shapes)
748 public static boolean pickInElement(IElement e, ICanvasContext ctx, PickRequest req)
750 Rectangle2D elementBounds = getElementBounds(e);
752 // Pick with pick handler(s)
753 List<Pick> pickHandlers = e.getElementClass().getItemsByClass(Pick.class);
754 if (!pickHandlers.isEmpty())
756 // Rough filtering with bounds
757 if (!GeometryUtils.intersects(req.pickArea, elementBounds)) return false;
759 // Convert pick shape to element coordinates
760 for (Pick p : pickHandlers)
762 if (p.pickTest(e, req.pickArea, req.pickPolicy))
768 // Pick with shape handler(s)
769 List<Outline> shapeHandlers = e.getElementClass().getItemsByClass(Outline.class);
770 if (!shapeHandlers.isEmpty())
772 // Rough filtering with bounds
773 if (!GeometryUtils.intersects(req.pickArea, elementBounds)) return false;
775 // Intersection with one shape is enough
776 if (req.pickPolicy == PickPolicy.PICK_INTERSECTING_OBJECTS)
778 for (Outline es : shapeHandlers)
780 Shape elementShape = es.getElementShape(e);
781 if (GeometryUtils.intersects(req.pickArea, elementShape))
787 // Contains of all shapes is required
788 if (req.pickPolicy == PickPolicy.PICK_CONTAINED_OBJECTS)
790 for (Outline es : shapeHandlers)
792 Shape elementShape = es.getElementShape(e);
793 if (!GeometryUtils.contains(req.pickArea, elementShape))
802 if (req.pickPolicy == PickPolicy.PICK_INTERSECTING_OBJECTS)
804 if (GeometryUtils.intersects(req.pickArea, elementBounds))
810 if (req.pickPolicy == PickPolicy.PICK_CONTAINED_OBJECTS)
812 if (GeometryUtils.contains(req.pickArea, elementBounds))
819 * Get bends of an edge
822 * @param bends the handles of each bend point
823 * @param points collection to be filled with the bends points in the same
824 * order as the bend handle objects
826 public static void getBends(IElement e, List<Bend> bends, List<Point2D> points)
828 BendsHandler bh = e.getElementClass().getSingleItem(BendsHandler.class);
829 bh.getBends(e, bends);
832 Point2D pos = new Point2D.Double();
833 bh.getBendPosition(e, b, pos);
839 * Get bends of an edge
841 * @param points collection to be filled with the bends points
843 public static void getBends(IElement e, List<Point2D> points)
845 BendsHandler bh = e.getElementClass().getSingleItem(BendsHandler.class);
846 int bendCount = bh.getBendsCount(e);
847 ArrayList<Bend> bends = new ArrayList<Bend>(bendCount);
848 getBends(e, bends, points);
852 public static void resizeElement(IElement e, double x, double y, double w, double h) {
853 Move m = e.getElementClass().getSingleItem(Move.class);
855 Resize s = e.getElementClass().getSingleItem(Resize.class);
856 s.resize(e, new Rectangle2D.Double(0,0,w,h));
859 public static <T> T getHintOrDefault(IHintContext e, Key key, T defaultValue) {
860 T t = e.getHint(key);
861 assert key.isValueAccepted(defaultValue);
862 return t == null ? defaultValue : t;
865 public static void setOrRemoveHint(IHintContext e, Key key, Object value) {
869 assert key.isValueAccepted(value);
870 e.setHint(key, value);
874 public static boolean elementEquals(IElement e1, IElement e2) {
875 Object o1 = getObject(e1);
876 Object o2 = getObject(e2);
877 if (o1 == null && o2 == null)
878 return ObjectUtils.objectEquals(e1, e2);
879 return ObjectUtils.objectEquals(o1, o2);
882 public static IElement getDiagramMappedElement(IElement e) {
883 IDiagram d = e.peekDiagram();
886 DataElementMap map = d.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
889 Object o = map.getData(d, e);
892 IElement mapped = map.getElement(d, o);
893 return mapped != null ? mapped : e;
897 * Calculates the center of the bounding box containing all the specified
900 * @param the elements for which to calculate the center of a containing
902 * @param pivotPoint a Point2D for writing the result of the calculation or
903 * <code>null</code> to allocate a new Point2D if necessary
904 * @return the center of the containing bounding box or <code>null</code> if
905 * there are no elements
907 public static Point2D getElementBoundsCenter(Collection<IElement> elements, Point2D result) {
908 Shape b = getElementBoundsOnDiagram(elements);
911 Rectangle2D bounds = b.getBounds2D();
913 result = new Point2D.Double(bounds.getCenterX(), bounds.getCenterY());
915 result.setLocation(bounds.getCenterX(), bounds.getCenterY());
920 * A utility for retrieving the containg diagram of an element. The element
921 * does not have to be directly in a diagram, the utility will also look for
922 * through the element parents for a diagram too.
927 public static IDiagram getDiagram(IElement e) {
929 throw new IllegalArgumentException("null element");
930 IDiagram d = peekDiagram(e);
932 throw new IllegalStateException("element " + e + " is not part of a diagram");
937 * A utility for retrieving the containg diagram of an element. The element
938 * does not have to be directly in a diagram, the utility will also look for
939 * through the element parents for a diagram too.
942 * @return <code>null</code> if the element is not on a diagram nor is the
943 * element transitively a child of any element that is on a diagram
945 public static IDiagram peekDiagram(IElement e) {
947 IDiagram d = e.peekDiagram();
956 * Retrieves a possible parent element of an element.
958 * @param e the element to get a parent for
959 * @return the parent element or <code>null</code> if the element does not
960 * have a parent element
962 public static IElement getParent(IElement e) {
963 Parent p = e.getElementClass().getAtMostOneItemOfClass(Parent.class);
966 return p.getParent(e);
970 * Retrieves a possible parent element of an element.
972 * @param e the element to get a parent for
973 * @return the parent element or <code>null</code> if the element does not
974 * have a parent element
976 public static Collection<IElement> getParents(IElement e) {
977 List<IElement> result = new ArrayList<IElement>(3);
978 return getParents(e, result);
982 * Retrieves a possible parent element of an element.
984 * @param e the element to get a parent for
985 * @param result a collection wherein to store the possible parent elements
986 * @return the specified result collection
988 public static Collection<IElement> getParents(IElement e, Collection<IElement> result) {
991 Parent ph = p.getElementClass().getAtMostOneItemOfClass(Parent.class);
1005 public static String generateNodeId(IElement e) {
1008 String sgName = e.getHint(ElementHints.KEY_SG_NAME);
1009 if (sgName != null) prefix = sgName + " ";
1011 Object object = e.getHint(ElementHints.KEY_OBJECT);
1012 if (object != null) {
1013 return prefix + object.toString();
1015 // Warning: this can be hazardous for scene graph consistency!
1016 // Duplicate nodes may be introduced when elements are updated through
1017 // the Image interface.
1018 return String.valueOf(e.hashCode());
1023 * @param <T> the adaption target class
1024 * @param e the element to adapt
1025 * @param toClass the object class to adapt the element to
1026 * @return adapter result or <code>null</code> if adaptation failed
1028 public static <T> T adaptElement(IElement e, Class<T> toClass) {
1029 for (ElementAdapter adapter : e.getElementClass().getItemsByClass(ElementAdapter.class)){
1030 T t = adapter.adapt(e, toClass);
1038 * Tries to adapt an {@link ElementClass} into a requested class through
1039 * {@link Adapter} handlers. Implements the IElement adaptation logic
1040 * described in {@link Adapter}.
1043 * the adaption target class
1045 * the element to adapt
1047 * the object class to adapt the element to
1048 * @return adapter result or <code>null</code> if adaptation failed
1050 public static <T> T adapt(ElementClass ec, Class<T> toClass) {
1052 throw new IllegalArgumentException("null element class");
1053 if (toClass == null)
1054 throw new IllegalArgumentException("null target class");
1056 for (Adapter adapter : ec.getItemsByClass(Adapter.class)){
1057 T t = adapter.adapt(toClass);
1065 * Otherwise the same as {@link #adapt(ElementClass, Class)} but will throw
1066 * {@link UnsupportedOperationException} if the adaption fails.
1069 * the adaption target class
1071 * the element to adapt
1073 * the object class to adapt the element to
1074 * @return adapter result or <code>null</code> if adaptation failed
1075 * @throws UnsupportedOperationException
1077 public static <T> T checkedAdapt(ElementClass ec, Class<T> toClass) {
1078 T t = adapt(ec, toClass);
1081 throw new UnsupportedOperationException("cannot adapt " + ec + " to " + toClass);
1085 * Looks for a scene graph node from the specified element with the
1086 * specified node key. If the node does not exist, a new node is created
1087 * using the specified node class.
1089 * If a previous node exists, its class is verified to match the requested
1090 * node class and returned as such upon success. If the classes do not
1091 * match, an exception is raised since this is most likely a bug that needs
1092 * to be fixed elsewhere.
1096 * @param withParentNode
1097 * @param withNodeKey
1101 public static <T extends INode> T getOrCreateNode(IElement forElement, ParentNode<?> withParentNode, Key withNodeKey, Class<T> nodeClass) {
1102 return getOrCreateNode(forElement, withParentNode, withNodeKey, null, nodeClass);
1106 * Looks for a scene graph node from the specified element with the
1107 * specified node key. If the node does not exist, a new node is created
1108 * using the specified node class.
1110 * If a previous node exists, its class is verified to match the requested
1111 * node class and returned as such upon success. If the classes do not
1112 * match, an exception is raised since this is most likely a bug that needs
1113 * to be fixed elsewhere.
1117 * @param withParentNode
1118 * @param withNodeKey
1123 public static <T extends INode> T getOrCreateNode(IElement forElement, ParentNode<?> withParentNode, Key withNodeKey, String nodeId, Class<T> nodeClass) {
1124 return getOrCreateNode(forElement, withParentNode, withNodeKey, nodeId, nodeClass, null);
1128 * Looks for a scene graph node from the specified element with the
1129 * specified node key. If the node does not exist, a new node is created
1130 * using the specified node class.
1132 * If a previous node exists, its class is verified to match the requested
1133 * node class and returned as such upon success. If the classes do not
1134 * match, an exception is raised since this is most likely a bug that needs
1135 * to be fixed elsewhere.
1139 * @param withParentNode
1140 * @param withNodeKey
1143 * @param nodeCreationCallback a callback that is invoked with the node
1144 * instance if a new node was created by this method
1147 public static <T extends INode> T getOrCreateNode(IElement forElement, ParentNode<?> withParentNode, Key withNodeKey, String nodeId, Class<T> nodeClass, Consumer<T> nodeCreationCallback) {
1148 if (!(withNodeKey instanceof SceneGraphNodeKey))
1149 System.out.println("ElementUtils.getOrCreateNode: WARNING: removing scene graph node with that does not extend SceneGraphNodeKey: " + withNodeKey);
1151 @SuppressWarnings("unchecked")
1152 T node = (T) forElement.getHint(withNodeKey);
1154 node = nodeId != null ? withParentNode.getOrCreateNode(nodeId, nodeClass) : withParentNode.addNode(nodeClass);
1155 forElement.setHint(withNodeKey, node);
1156 if (nodeCreationCallback != null)
1157 nodeCreationCallback.accept(node);
1159 if (!nodeClass.isAssignableFrom(node.getClass())) {
1160 throw new ElementSceneGraphException("ElementUtils.getOrCreateNode: WARNING: existing node class (" + node.getClass() + ") does not match requested node class (" + nodeClass + ") for element " + forElement + " with parent node " + withParentNode + " and node key " + withNodeKey);
1162 // If the previously available node is not a parent of the specified
1163 // node create a new node under the specified parent and set that
1164 // as the node of the specified element.
1165 if (!withParentNode.equals(node.getParent())) {
1166 node = nodeId != null ? withParentNode.getOrCreateNode(nodeId, nodeClass) : withParentNode.addNode(nodeClass);
1167 forElement.setHint(withNodeKey, node);
1168 if (nodeCreationCallback != null)
1169 nodeCreationCallback.accept(node);
1180 public static INode removePossibleNode(IElement element, Key nodeKey) {
1181 if (!(nodeKey instanceof SceneGraphNodeKey))
1182 System.out.println("ElementUtils.removePossibleNode: WARNING: removing scene graph node with that does not extend SceneGraphNodeKey: " + nodeKey);
1184 INode node = element.getHint(nodeKey);
1195 public static Font getTextFont(IElement element) {
1196 TextFont tf = element.getElementClass().getSingleItem(TextFont.class);
1197 return tf.getFont(element);
1200 public static void setTextFont(IElement element, Font font) {
1201 TextFont tf = element.getElementClass().getSingleItem(TextFont.class);
1202 tf.setFont(element, font);
1205 public static <T> void addToCollectionHint(IElement element, Key key, T item) {
1206 Collection<T> collection = element.getHint(key);
1207 if (collection == null) {
1208 collection = new ArrayList<T>();
1209 element.setHint(key, collection);
1211 collection.add(item);
1214 public static <T> void removeFromCollectionHint(IElement element, Key key, T item) {
1215 Collection<T> collection = element.getHint(key);
1216 if (collection != null) {
1217 collection = new ArrayList<T>();
1218 collection.remove(item);
1219 if (collection.isEmpty())
1220 element.removeHint(key);
1224 public static void setHover(IElement e, boolean hover)
1226 Hover h = e.getElementClass().getSingleItem(Hover.class);
1227 h.setHover(e, hover);
1230 public static boolean isHovering(IElement e)
1232 Hover h = e.getElementClass().getSingleItem(Hover.class);
1233 return h.isHovering(e);