]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.element.handler.impl;\r
13 \r
14 import java.awt.geom.AffineTransform;\r
15 import java.awt.geom.Point2D;\r
16 import java.util.Collection;\r
17 \r
18 import org.simantics.g2d.canvas.ICanvasContext;\r
19 import org.simantics.g2d.element.ElementHints;\r
20 import org.simantics.g2d.element.IElement;\r
21 import org.simantics.g2d.element.handler.Scale;\r
22 import org.simantics.g2d.element.handler.Validator;\r
23 \r
24 /**\r
25  * Handler for an element that can be freely scaled.\r
26  * \r
27  * @author Toni Kalajainen\r
28  */\r
29 public class Scaleable implements Scale, Validator {\r
30 \r
31     private static final long serialVersionUID = -4033716332747863703L;\r
32 \r
33     Double aspectRatio;\r
34 \r
35     public Scaleable(Double aspectRatio)\r
36     {\r
37         this.aspectRatio = aspectRatio;\r
38     }\r
39 \r
40     @Override\r
41     public Double getFixedAspectRatio(IElement e) {\r
42         return aspectRatio;\r
43     }\r
44 \r
45     @Override\r
46     public Point2D getScale(IElement e) {\r
47         AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);\r
48         return _getScale(at);\r
49     }   \r
50 \r
51     @Override\r
52     public void setScale(IElement e, Point2D newScale) {\r
53         Point2D oldScale = getScale(e);\r
54         double sx = newScale.getX() / oldScale.getX();\r
55         double sy = newScale.getY() / oldScale.getY();\r
56         AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);\r
57         at = new AffineTransform(at);\r
58         at.scale(sx, sy);\r
59         e.setHint(ElementHints.KEY_TRANSFORM, at);\r
60     }\r
61 \r
62     @Override\r
63     public Point2D getMaximumScale(IElement e) {\r
64         return null;\r
65     }\r
66 \r
67     @Override\r
68     public Point2D getMinimumScale(IElement e) {\r
69         return null;\r
70     }\r
71 \r
72     private static Point2D _getScale(AffineTransform at) {\r
73         double m00 = at.getScaleX();\r
74         double m11 = at.getScaleY();\r
75         double m10 = at.getShearY();\r
76         double m01 = at.getShearX();\r
77         // Project unit vector to canvas\r
78         double sx = Math.sqrt( m00*m00+m10*m10 );\r
79         double sy = Math.sqrt( m01*m01+m11*m11 );\r
80         return new Point2D.Double(sx, sy);\r
81     }\r
82 \r
83     @Override\r
84     public void validate(final IElement e, final ICanvasContext ctx, Collection<Issue> lst) {\r
85         /*\r
86                 if (aspectRatio!=null)\r
87                 {\r
88                         // Validate aspect ratio\r
89                         final Point2D scale = getScale(e);\r
90                         double currentRatio = scale.getX() / scale.getY();\r
91                         if (Math.abs(currentRatio - aspectRatio)>0.000001)\r
92                         {\r
93                                 lst.add(new Issue() {\r
94                                         @Override\r
95                                         public String getMessage() {\r
96                                                 return "Aspect ratio is wrong";\r
97                                         }\r
98                                         @Override\r
99                                         public void addSuggestions(Collection<Suggestion> suggestionList) {\r
100                                                 suggestionList.add(new Suggestion() {\r
101                                                         @Override\r
102                                                         public boolean fix() {\r
103                                                                 double newSx = scale.getX();\r
104                                                                 double newSy = newSx * aspectRatio;\r
105                                                                 Point2D newScale = new Point2D.Double(newSx, newSy);\r
106                                                                 setScale(e, newScale);\r
107                                                                 ctx.setDirty();\r
108                                                                 return true;\r
109                                                         }\r
110                                                         @Override\r
111                                                         public String getMessage() {\r
112                                                                 return "Scale height, keep width";\r
113                                                         }});\r
114                                         }});\r
115                         }\r
116                         // TODO min scale validator\r
117                         // TODO max scale validator\r
118                 }\r
119          */\r
120     }\r
121 \r
122 \r
123 }\r