]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/Scaleable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / Scaleable.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/Scaleable.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/Scaleable.java
new file mode 100644 (file)
index 0000000..d7c071e
--- /dev/null
@@ -0,0 +1,123 @@
+/*******************************************************************************\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.AffineTransform;\r
+import java.awt.geom.Point2D;\r
+import java.util.Collection;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.Scale;\r
+import org.simantics.g2d.element.handler.Validator;\r
+\r
+/**\r
+ * Handler for an element that can be freely scaled.\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class Scaleable implements Scale, Validator {\r
+\r
+    private static final long serialVersionUID = -4033716332747863703L;\r
+\r
+    Double aspectRatio;\r
+\r
+    public Scaleable(Double aspectRatio)\r
+    {\r
+        this.aspectRatio = aspectRatio;\r
+    }\r
+\r
+    @Override\r
+    public Double getFixedAspectRatio(IElement e) {\r
+        return aspectRatio;\r
+    }\r
+\r
+    @Override\r
+    public Point2D getScale(IElement e) {\r
+        AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);\r
+        return _getScale(at);\r
+    }  \r
+\r
+    @Override\r
+    public void setScale(IElement e, Point2D newScale) {\r
+        Point2D oldScale = getScale(e);\r
+        double sx = newScale.getX() / oldScale.getX();\r
+        double sy = newScale.getY() / oldScale.getY();\r
+        AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);\r
+        at = new AffineTransform(at);\r
+        at.scale(sx, sy);\r
+        e.setHint(ElementHints.KEY_TRANSFORM, at);\r
+    }\r
+\r
+    @Override\r
+    public Point2D getMaximumScale(IElement e) {\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public Point2D getMinimumScale(IElement e) {\r
+        return null;\r
+    }\r
+\r
+    private static Point2D _getScale(AffineTransform at) {\r
+        double m00 = at.getScaleX();\r
+        double m11 = at.getScaleY();\r
+        double m10 = at.getShearY();\r
+        double m01 = at.getShearX();\r
+        // Project unit vector to canvas\r
+        double sx = Math.sqrt( m00*m00+m10*m10 );\r
+        double sy = Math.sqrt( m01*m01+m11*m11 );\r
+        return new Point2D.Double(sx, sy);\r
+    }\r
+\r
+    @Override\r
+    public void validate(final IElement e, final ICanvasContext ctx, Collection<Issue> lst) {\r
+        /*\r
+               if (aspectRatio!=null)\r
+               {\r
+                       // Validate aspect ratio\r
+                       final Point2D scale = getScale(e);\r
+                       double currentRatio = scale.getX() / scale.getY();\r
+                       if (Math.abs(currentRatio - aspectRatio)>0.000001)\r
+                       {\r
+                               lst.add(new Issue() {\r
+                                       @Override\r
+                                       public String getMessage() {\r
+                                               return "Aspect ratio is wrong";\r
+                                       }\r
+                                       @Override\r
+                                       public void addSuggestions(Collection<Suggestion> suggestionList) {\r
+                                               suggestionList.add(new Suggestion() {\r
+                                                       @Override\r
+                                                       public boolean fix() {\r
+                                                               double newSx = scale.getX();\r
+                                                               double newSy = newSx * aspectRatio;\r
+                                                               Point2D newScale = new Point2D.Double(newSx, newSy);\r
+                                                               setScale(e, newScale);\r
+                                                               ctx.setDirty();\r
+                                                               return true;\r
+                                                       }\r
+                                                       @Override\r
+                                                       public String getMessage() {\r
+                                                               return "Scale height, keep width";\r
+                                                       }});\r
+                                       }});\r
+                       }\r
+                       // TODO min scale validator\r
+                       // TODO max scale validator\r
+               }\r
+         */\r
+    }\r
+\r
+\r
+}\r