]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/Resizeable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / Resizeable.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/Resizeable.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/Resizeable.java
new file mode 100644 (file)
index 0000000..e2acb84
--- /dev/null
@@ -0,0 +1,132 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.element.handler.impl;\r
+\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.Collection;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.LifeCycle;\r
+import org.simantics.g2d.element.handler.Resize;\r
+import org.simantics.g2d.element.handler.Validator;\r
+\r
+/**\r
+ * This handler is used by elements whose internal size can be modified\r
+ * (eg. free graphics). \r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class Resizeable implements Resize, LifeCycle, Validator {\r
+\r
+    private static final long serialVersionUID = -2892730866940581731L;\r
+    public final static Resizeable UNCONSTRICTED = new Resizeable(new Rectangle2D.Double(0,0,100, 100), null, null, null); \r
+\r
+    public static Resizeable initialSize(double width, double height) {\r
+        return new Resizeable(new Rectangle2D.Double(0, 0, width, height), null, null, null);\r
+    }\r
+\r
+    Rectangle2D minSize, maxSize, initialSize;\r
+    Double aspectRatio;\r
+\r
+    public Resizeable() \r
+    {\r
+        this(null, null, null, null);\r
+    }\r
+\r
+    public Resizeable(Rectangle2D initialSize, Rectangle2D minSize, Rectangle2D maxSize, Double fixedAspectRatio)\r
+    {\r
+        this.minSize = minSize;\r
+        this.maxSize = maxSize;\r
+        this.initialSize = initialSize;\r
+\r
+        this.aspectRatio = fixedAspectRatio;\r
+        if (aspectRatio!=null) {\r
+            if (minSize!=null) {\r
+                double msar = minSize.getWidth()/minSize.getHeight();\r
+                if (Math.abs(msar-aspectRatio)>0.000001)\r
+                    throw new RuntimeException("The aspect ratio of MinSize does not match the given fixed aspect ratio");\r
+            }\r
+            if (maxSize!=null) {\r
+                double msar = maxSize.getWidth()/maxSize.getHeight();\r
+                if (Math.abs(msar-aspectRatio)>0.000001)\r
+                    throw new RuntimeException("The aspect ratio of MinSize does not match the given fixed aspect ratio");\r
+            }\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Double getFixedAspectRatio(IElement e) {\r
+        return aspectRatio;\r
+    }\r
+\r
+    @Override\r
+    public Rectangle2D getMaximumSize(IElement e) {\r
+        return maxSize;\r
+    }\r
+\r
+    @Override\r
+    public Rectangle2D getMinimumSize(IElement e) {\r
+        return minSize;\r
+    }\r
+\r
+    @Override\r
+    public Rectangle2D getBounds(IElement e, Rectangle2D s) {\r
+        if (s==null) s = new Rectangle2D.Double();\r
+        s.setFrame((Rectangle2D)e.getHint(ElementHints.KEY_BOUNDS));\r
+//             System.out.println(this+": returning bounds "+s);\r
+        return s;\r
+    }\r
+\r
+    @Override\r
+    public void resize(IElement e, Rectangle2D newSize) {\r
+//             System.out.println(this+": bounds set to "+newSize);\r
+        e.setHint(ElementHints.KEY_BOUNDS, newSize);\r
+        /*\r
+               double ar = maxSize.getWidthe()/maxSize.getHeight();\r
+               if (Math.abs(ar-aspectRatio)>0.000001)\r
+                       throw new RuntimeException("The aspect ratio of MinSize does not match the given fixed aspect ratio");\r
+         */\r
+    }\r
+\r
+    @Override\r
+    public void validate(IElement e, ICanvasContext ctx, Collection<Issue> lst) {\r
+        // TODO Validate aspect ratio\r
+        if (aspectRatio!=null) {\r
+\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void onElementActivated(IDiagram d, IElement e) {\r
+    }\r
+\r
+    @Override\r
+    public void onElementCreated(IElement e) {\r
+        if (initialSize!=null)\r
+            e.setHint(ElementHints.KEY_BOUNDS, initialSize);\r
+        else\r
+            e.setHint(ElementHints.KEY_BOUNDS, new Rectangle2D.Double(0,0,1,1));\r
+\r
+    }\r
+\r
+    @Override\r
+    public void onElementDestroyed(IElement e) {\r
+    }\r
+\r
+    @Override\r
+    public void onElementDeactivated(IDiagram d, IElement e) {\r
+    }\r
+\r
+}\r