From 5d2af6df53f3895287cecd5e09fa1e38f271b53e Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Thu, 23 Mar 2017 12:05:20 +0200 Subject: [PATCH] Use node bounds for defined elements refs #7109 Change-Id: I887c20e105c1f4b0f852de0d3e0d4beba3e30071 --- .../adapter/DefinedElementFactory.java | 2 +- .../adapter/DefinedElementHandler.java | 123 ++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementHandler.java diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementFactory.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementFactory.java index 03250d726..6d5b30237 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementFactory.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementFactory.java @@ -230,7 +230,7 @@ public class DefinedElementFactory extends ElementFactoryAdapter { DefaultTransform.INSTANCE, StaticSymbolImageInitializer.INSTANCE, new StaticSymbolImpl(img), - ImageClass.ImageElementHandler.INSTANCE, + DefinedElementHandler.INSTANCE, new Terminals(terminals), SimpleElementLayers.INSTANCE, PlainElementPropertySetter.INSTANCE diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementHandler.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementHandler.java new file mode 100644 index 000000000..b5cc293ac --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementHandler.java @@ -0,0 +1,123 @@ +package org.simantics.diagram.adapter; + +import java.awt.Shape; +import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; + +import org.simantics.g2d.connection.handler.ConnectionHandler; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementHints; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.InternalSize; +import org.simantics.g2d.element.handler.Outline; +import org.simantics.g2d.element.handler.Resize; +import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.image.Image; +import org.simantics.scenegraph.Node; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.IG2DNode; +import org.simantics.utils.datastructures.hints.IHintContext.Key; + +public class DefinedElementHandler implements SceneGraph, InternalSize, Resize, Outline { + + private static final long serialVersionUID = -2074850877791708846L; + + public static final DefinedElementHandler INSTANCE = new DefinedElementHandler(); + + public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "IMAGE_SG_NODE"); + + protected Image getImage(IElement e) { + Image i = e.getHint(ElementHints.KEY_IMAGE); + if (i == null) + throw new IllegalStateException("element " + e + " has no ElementHints.KEY_IMAGE hint"); + return i; + } + + protected Key getNodeKey() { + return KEY_SG_NODE; + } + + @Override + public void init(final IElement e, final G2DParentNode parent) { + Image i = getImage(e); + Node node = i.init(parent); + if (node != null) + e.setHint(getNodeKey(), node); + if(node instanceof IG2DNode) { + IG2DNode n = (IG2DNode)node; + AffineTransform at = ElementUtils.getTransform(e); + if(at != null) { + n.setTransform(at); // FIXME: not tested.. + } + } + } + + public void cleanup(final IElement e) { + Node node = e.removeHint(getNodeKey()); + if (node != null) + node.remove(); + } + + public Rectangle2D imageBounds(IElement e) { + Image i = getImage(e); + return i.getBounds(); + } + + @Override + public Rectangle2D getBounds(IElement e, Rectangle2D size) { + + if (size == null) + size = new Rectangle2D.Double(); + + ElementClass ec = e.getElementClass(); + if(ec.containsClass(ConnectionHandler.class)) { + size.setFrame(imageBounds(e)); + } else { + IG2DNode node = e.getHint(getNodeKey()); + if(node != null) { + size.setFrame(node.getBoundsInLocal()); + } else { + size.setFrame(imageBounds(e)); + } + } + + return size; + + } + + @Override + public Double getFixedAspectRatio(IElement e) { + Image i = getImage(e); + Rectangle2D r = i.getBounds(); + return r.getWidth() / r.getHeight(); + } + + @Override + public Rectangle2D getMaximumSize(IElement e) { + Image i = getImage(e); + return i.getBounds(); + } + + @Override + public Rectangle2D getMinimumSize(IElement e) { + Image i = getImage(e); + return i.getBounds(); + } + + @Override + public void resize(IElement e, Rectangle2D newSize) { + } + + @Override + public Shape getElementShape(IElement e) { + + IG2DNode node = e.getHint(getNodeKey()); + if (node == null) + throw new IllegalStateException(); + return node.getBoundsInLocal(); + + } + +} \ No newline at end of file -- 2.47.1