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.elementclass.connection;
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Path2D;
16 import java.awt.geom.Point2D;
17 import java.awt.geom.Rectangle2D;
19 import org.simantics.g2d.element.IElement;
20 import org.simantics.g2d.element.handler.InternalSize;
21 import org.simantics.g2d.element.handler.BendsHandler;
22 import org.simantics.g2d.element.handler.Move;
23 import org.simantics.g2d.element.handler.Resize;
24 import org.simantics.g2d.element.handler.Scale;
25 import org.simantics.g2d.element.handler.Transform;
28 * Implements Bounds, Scale, Move and Transform for edges.
30 * @See {@link InternalSize}
33 * @See {@link Transform}
34 * @author Toni Kalajainen
36 public class EdgeTransforms implements InternalSize, Resize, Scale, Move, Transform {
38 private static final long serialVersionUID = -1675978959634691347L;
39 public static final EdgeTransforms INSTANCE = new EdgeTransforms();
42 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
43 if (size==null) size = new Rectangle2D.Double();
44 BendsHandler bends = e.getElementClass().getSingleItem(BendsHandler.class);
45 Path2D path = bends.getPath(e);
46 size.setFrame(path.getBounds2D());
51 public void resize(IElement e, Rectangle2D newSize) {
52 BendsHandler bends = e.getElementClass().getSingleItem(BendsHandler.class);
53 Path2D path = bends.getPath(e);
54 Rectangle2D oldSize = path.getBounds2D();
55 double sx = newSize.getWidth() / oldSize.getWidth();
56 double sy = newSize.getHeight() / oldSize.getHeight();
57 double tx = newSize.getMinX() + oldSize.getMinX();
58 double ty = newSize.getMinY() + oldSize.getMinY();
59 AffineTransform at = new AffineTransform();
62 Path2D newPath = new Path2D.Double(path, at);
63 bends.setPath(e, newPath);
67 public Double getFixedAspectRatio(IElement e) {
72 public Rectangle2D getMaximumSize(IElement e) {
77 public Rectangle2D getMinimumSize(IElement e) {
82 public Point2D getMaximumScale(IElement e) {
87 public Point2D getMinimumScale(IElement e) {
91 private final static Point2D SCALE = new Point2D.Double(1.0, 1.0);
92 private final static Point2D POSITION = new Point2D.Double(0.0, 0.0);
93 private final static AffineTransform TRANSLATE = new AffineTransform();
95 public Point2D getScale(IElement e) {
100 public Point2D getPosition(IElement e) {
105 public AffineTransform getTransform(IElement e) {
110 public void moveTo(IElement e, double x, double y) {
111 BendsHandler bends = e.getElementClass().getSingleItem(BendsHandler.class);
112 Path2D path = bends.getPath(e);
113 AffineTransform at = new AffineTransform();
115 Path2D newPath = new Path2D.Double(path, at);
116 bends.setPath(e, newPath);
120 public void setScale(IElement e, Point2D newScale) {
121 BendsHandler bends = e.getElementClass().getSingleItem(BendsHandler.class);
122 Path2D path = bends.getPath(e);
123 AffineTransform at = new AffineTransform();
124 at.scale(newScale.getX(), newScale.getY());
125 Path2D newPath = new Path2D.Double(path, at);
126 bends.setPath(e, newPath);
131 public void setTransform(IElement e, AffineTransform at) {
132 BendsHandler bends = e.getElementClass().getSingleItem(BendsHandler.class);
133 Path2D path = bends.getPath(e);
134 Path2D newPath = new Path2D.Double(path, at);
135 bends.setPath(e, newPath);