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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.element.handler.impl;
\r
14 import java.awt.geom.AffineTransform;
\r
15 import java.awt.geom.Point2D;
\r
16 import java.util.Collection;
\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
25 * Handler for an element that can be freely scaled.
\r
27 * @author Toni Kalajainen
\r
29 public class Scaleable implements Scale, Validator {
\r
31 private static final long serialVersionUID = -4033716332747863703L;
\r
35 public Scaleable(Double aspectRatio)
\r
37 this.aspectRatio = aspectRatio;
\r
41 public Double getFixedAspectRatio(IElement e) {
\r
46 public Point2D getScale(IElement e) {
\r
47 AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
\r
48 return _getScale(at);
\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
59 e.setHint(ElementHints.KEY_TRANSFORM, at);
\r
63 public Point2D getMaximumScale(IElement e) {
\r
68 public Point2D getMinimumScale(IElement e) {
\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
84 public void validate(final IElement e, final ICanvasContext ctx, Collection<Issue> lst) {
\r
86 if (aspectRatio!=null)
\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
93 lst.add(new Issue() {
\r
95 public String getMessage() {
\r
96 return "Aspect ratio is wrong";
\r
99 public void addSuggestions(Collection<Suggestion> suggestionList) {
\r
100 suggestionList.add(new Suggestion() {
\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
111 public String getMessage() {
\r
112 return "Scale height, keep width";
\r
116 // TODO min scale validator
\r
117 // TODO max scale validator
\r