]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Use node bounds for defined elements 78/378/2
authorAntti Villberg <antti.villberg@semantum.fi>
Thu, 23 Mar 2017 10:05:20 +0000 (12:05 +0200)
committerAntti Villberg <antti.villberg@semantum.fi>
Thu, 23 Mar 2017 10:29:29 +0000 (12:29 +0200)
refs #7109

Change-Id: I887c20e105c1f4b0f852de0d3e0d4beba3e30071

bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementFactory.java
bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefinedElementHandler.java [new file with mode: 0644]

index 03250d726c403ee9a864c3407584c7cb717e979b..6d5b30237870d2ad9c556197c6c8b88e5d45103b 100644 (file)
@@ -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 (file)
index 0000000..b5cc293
--- /dev/null
@@ -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