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