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