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.Rectangle2D;
\r
15 import java.util.Collection;
\r
17 import org.simantics.g2d.canvas.ICanvasContext;
\r
18 import org.simantics.g2d.diagram.IDiagram;
\r
19 import org.simantics.g2d.element.ElementHints;
\r
20 import org.simantics.g2d.element.IElement;
\r
21 import org.simantics.g2d.element.handler.LifeCycle;
\r
22 import org.simantics.g2d.element.handler.Resize;
\r
23 import org.simantics.g2d.element.handler.Validator;
\r
26 * This handler is used by elements whose internal size can be modified
\r
27 * (eg. free graphics).
\r
29 * @author Toni Kalajainen
\r
31 public class Resizeable implements Resize, LifeCycle, Validator {
\r
33 private static final long serialVersionUID = -2892730866940581731L;
\r
34 public final static Resizeable UNCONSTRICTED = new Resizeable(new Rectangle2D.Double(0,0,100, 100), null, null, null);
\r
36 public static Resizeable initialSize(double width, double height) {
\r
37 return new Resizeable(new Rectangle2D.Double(0, 0, width, height), null, null, null);
\r
40 Rectangle2D minSize, maxSize, initialSize;
\r
43 public Resizeable()
\r
45 this(null, null, null, null);
\r
48 public Resizeable(Rectangle2D initialSize, Rectangle2D minSize, Rectangle2D maxSize, Double fixedAspectRatio)
\r
50 this.minSize = minSize;
\r
51 this.maxSize = maxSize;
\r
52 this.initialSize = initialSize;
\r
54 this.aspectRatio = fixedAspectRatio;
\r
55 if (aspectRatio!=null) {
\r
56 if (minSize!=null) {
\r
57 double msar = minSize.getWidth()/minSize.getHeight();
\r
58 if (Math.abs(msar-aspectRatio)>0.000001)
\r
59 throw new RuntimeException("The aspect ratio of MinSize does not match the given fixed aspect ratio");
\r
61 if (maxSize!=null) {
\r
62 double msar = maxSize.getWidth()/maxSize.getHeight();
\r
63 if (Math.abs(msar-aspectRatio)>0.000001)
\r
64 throw new RuntimeException("The aspect ratio of MinSize does not match the given fixed aspect ratio");
\r
70 public Double getFixedAspectRatio(IElement e) {
\r
75 public Rectangle2D getMaximumSize(IElement e) {
\r
80 public Rectangle2D getMinimumSize(IElement e) {
\r
85 public Rectangle2D getBounds(IElement e, Rectangle2D s) {
\r
86 if (s==null) s = new Rectangle2D.Double();
\r
87 s.setFrame((Rectangle2D)e.getHint(ElementHints.KEY_BOUNDS));
\r
88 // System.out.println(this+": returning bounds "+s);
\r
93 public void resize(IElement e, Rectangle2D newSize) {
\r
94 // System.out.println(this+": bounds set to "+newSize);
\r
95 e.setHint(ElementHints.KEY_BOUNDS, newSize);
\r
97 double ar = maxSize.getWidthe()/maxSize.getHeight();
\r
98 if (Math.abs(ar-aspectRatio)>0.000001)
\r
99 throw new RuntimeException("The aspect ratio of MinSize does not match the given fixed aspect ratio");
\r
104 public void validate(IElement e, ICanvasContext ctx, Collection<Issue> lst) {
\r
105 // TODO Validate aspect ratio
\r
106 if (aspectRatio!=null) {
\r
112 public void onElementActivated(IDiagram d, IElement e) {
\r
116 public void onElementCreated(IElement e) {
\r
117 if (initialSize!=null)
\r
118 e.setHint(ElementHints.KEY_BOUNDS, initialSize);
\r
120 e.setHint(ElementHints.KEY_BOUNDS, new Rectangle2D.Double(0,0,1,1));
\r
125 public void onElementDestroyed(IElement e) {
\r
129 public void onElementDeactivated(IDiagram d, IElement e) {
\r