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