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