]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/Resizeable.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / Resizeable.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.element.handler.impl;
13
14 import java.awt.geom.Rectangle2D;
15 import java.util.Collection;
16
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;
24
25 /**
26  * This handler is used by elements whose internal size can be modified
27  * (eg. free graphics). 
28  * 
29  * @author Toni Kalajainen
30  */
31 public class Resizeable implements Resize, LifeCycle, Validator {
32
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); 
35
36     public static Resizeable initialSize(double width, double height) {
37         return new Resizeable(new Rectangle2D.Double(0, 0, width, height), null, null, null);
38     }
39
40     Rectangle2D minSize, maxSize, initialSize;
41     Double aspectRatio;
42
43     public Resizeable() 
44     {
45         this(null, null, null, null);
46     }
47
48     public Resizeable(Rectangle2D initialSize, Rectangle2D minSize, Rectangle2D maxSize, Double fixedAspectRatio)
49     {
50         this.minSize = minSize;
51         this.maxSize = maxSize;
52         this.initialSize = initialSize;
53
54         this.aspectRatio = fixedAspectRatio;
55         if (aspectRatio!=null) {
56             if (minSize!=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");
60             }
61             if (maxSize!=null) {
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");
65             }
66         }
67     }
68
69     @Override
70     public Double getFixedAspectRatio(IElement e) {
71         return aspectRatio;
72     }
73
74     @Override
75     public Rectangle2D getMaximumSize(IElement e) {
76         return maxSize;
77     }
78
79     @Override
80     public Rectangle2D getMinimumSize(IElement e) {
81         return minSize;
82     }
83
84     @Override
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);
89         return s;
90     }
91
92     @Override
93     public void resize(IElement e, Rectangle2D newSize) {
94 //              System.out.println(this+": bounds set to "+newSize);
95         e.setHint(ElementHints.KEY_BOUNDS, newSize);
96         /*
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");
100          */
101     }
102
103     @Override
104     public void validate(IElement e, ICanvasContext ctx, Collection<Issue> lst) {
105         // TODO Validate aspect ratio
106         if (aspectRatio!=null) {
107
108         }
109     }
110
111     @Override
112     public void onElementActivated(IDiagram d, IElement e) {
113     }
114
115     @Override
116     public void onElementCreated(IElement e) {
117         if (initialSize!=null)
118             e.setHint(ElementHints.KEY_BOUNDS, initialSize);
119         else
120             e.setHint(ElementHints.KEY_BOUNDS, new Rectangle2D.Double(0,0,1,1));
121
122     }
123
124     @Override
125     public void onElementDestroyed(IElement e) {
126     }
127
128     @Override
129     public void onElementDeactivated(IDiagram d, IElement e) {
130     }
131
132 }