fiore@0
|
1 /*
|
fiore@0
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
fiore@0
|
3
|
fiore@0
|
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
fiore@0
|
5
|
fiore@0
|
6 This program is free software: you can redistribute it and/or modify
|
fiore@0
|
7 it under the terms of the GNU General Public License as published by
|
fiore@0
|
8 the Free Software Foundation, either version 3 of the License, or
|
fiore@0
|
9 (at your option) any later version.
|
fiore@0
|
10
|
fiore@0
|
11 This program is distributed in the hope that it will be useful,
|
fiore@0
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
fiore@0
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
fiore@0
|
14 GNU General Public License for more details.
|
fiore@0
|
15
|
fiore@0
|
16 You should have received a copy of the GNU General Public License
|
fiore@0
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
fiore@0
|
18 */
|
fiore@0
|
19
|
fiore@0
|
20 package uk.ac.qmul.eecs.ccmi.simpletemplate;
|
fiore@0
|
21
|
fiore@0
|
22 import java.awt.Shape;
|
fiore@0
|
23 import java.awt.geom.Ellipse2D;
|
fiore@0
|
24 import java.awt.geom.Point2D;
|
fiore@0
|
25 import java.awt.geom.Rectangle2D;
|
fiore@0
|
26 import java.io.IOException;
|
fiore@0
|
27 import java.io.InputStream;
|
fiore@0
|
28 import java.util.List;
|
fiore@0
|
29
|
fiore@0
|
30 import org.w3c.dom.Document;
|
fiore@0
|
31 import org.w3c.dom.Element;
|
fiore@0
|
32
|
fiore@0
|
33 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
|
fiore@0
|
34 import uk.ac.qmul.eecs.ccmi.gui.Direction;
|
fiore@0
|
35 import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
|
fiore@0
|
36
|
fiore@0
|
37 /**
|
fiore@0
|
38 *
|
fiore@0
|
39 * An elliptical shaped diagram node.
|
fiore@0
|
40 *
|
fiore@0
|
41 */
|
fiore@0
|
42 @SuppressWarnings("serial")
|
fiore@0
|
43 public class EllipticalNode extends SimpleShapeNode {
|
fiore@0
|
44
|
fiore@0
|
45 public EllipticalNode(String typeName, NodeProperties properties){
|
fiore@0
|
46 super(typeName, properties);
|
fiore@0
|
47 Rectangle2D r = getMinBounds();
|
fiore@0
|
48 eShape = new Ellipse2D.Double(0,0,r.getWidth(),r.getHeight());
|
fiore@0
|
49 }
|
fiore@0
|
50
|
fiore@0
|
51 @Override
|
fiore@0
|
52 public ShapeType getShapeType() {
|
fiore@0
|
53 return ShapeType.Ellipse;
|
fiore@0
|
54 }
|
fiore@0
|
55
|
fiore@0
|
56 public static Rectangle2D getOutBounds(Rectangle2D r){
|
fiore@0
|
57 double h = r.getHeight()/2;
|
fiore@0
|
58 double w = r.getWidth()/2;
|
fiore@0
|
59
|
fiore@0
|
60 double anglew = Math.atan(h/w);
|
fiore@0
|
61 double angleh = Math.atan(w/h);
|
fiore@0
|
62
|
fiore@0
|
63 double a = w + h * Math.tan(angleh)/2;
|
fiore@0
|
64 double b = h + w * Math.tan(anglew)/2;
|
fiore@0
|
65
|
fiore@0
|
66 return new Rectangle2D.Double(
|
fiore@0
|
67 r.getCenterX() - a,
|
fiore@0
|
68 r.getCenterY() - b,
|
fiore@0
|
69 2*a,
|
fiore@0
|
70 2*b
|
fiore@0
|
71 );
|
fiore@0
|
72 }
|
fiore@0
|
73
|
fiore@0
|
74 @Override
|
fiore@0
|
75 protected void reshapeInnerProperties(List<String> insidePropertyTypes){
|
fiore@0
|
76 super.reshapeInnerProperties(insidePropertyTypes);
|
fiore@0
|
77 eShape.setFrame(super.anyInsideProperties() ? getOutBounds(dataDisplayBounds) : dataDisplayBounds );
|
fiore@0
|
78 }
|
fiore@0
|
79
|
fiore@0
|
80 @Override
|
fiore@0
|
81 public void decode(Document doc, Element nodeTag) throws IOException{
|
fiore@0
|
82 super.decode(doc, nodeTag);
|
fiore@0
|
83 eShape.setFrame(super.anyInsideProperties() ? getOutBounds(dataDisplayBounds) : dataDisplayBounds );
|
fiore@0
|
84 }
|
fiore@0
|
85
|
fiore@0
|
86 @Override
|
fiore@0
|
87 protected void translateImplementation(Point2D p,double dx, double dy){
|
fiore@0
|
88 /* if we clicked on a property node, just move that one */
|
fiore@0
|
89 for(List<PropertyNode> pnList : propertyNodesMap.values())
|
fiore@0
|
90 for(PropertyNode pn : pnList)
|
fiore@0
|
91 if(pn.contains(p)){
|
fiore@0
|
92 pn.translate(dx, dy);
|
fiore@0
|
93 return;
|
fiore@0
|
94 }
|
fiore@0
|
95 eShape.setFrame(eShape.getX() + dx,
|
fiore@0
|
96 eShape.getY() + dy,
|
fiore@0
|
97 eShape.getWidth(),
|
fiore@0
|
98 eShape.getHeight());
|
fiore@0
|
99 super.translateImplementation(p,dx, dy);
|
fiore@0
|
100 }
|
fiore@0
|
101
|
fiore@0
|
102 @Override
|
fiore@0
|
103 public Rectangle2D getBounds() {
|
fiore@0
|
104 return eShape.getBounds2D();
|
fiore@0
|
105 }
|
fiore@0
|
106
|
fiore@0
|
107 @Override
|
fiore@0
|
108 public Point2D getConnectionPoint(Direction d) {
|
fiore@0
|
109 return calculateConnectionPoint(d, getBounds());
|
fiore@0
|
110 }
|
fiore@0
|
111
|
fiore@0
|
112 public static Point2D calculateConnectionPoint(Direction d, Rectangle2D bounds){
|
fiore@0
|
113 double a = bounds.getWidth() / 2;
|
fiore@0
|
114 double b = bounds.getHeight() / 2;
|
fiore@0
|
115 double x = d.getX();
|
fiore@0
|
116 double y = d.getY();
|
fiore@0
|
117 double cx = bounds.getCenterX();
|
fiore@0
|
118 double cy = bounds.getCenterY();
|
fiore@0
|
119
|
fiore@0
|
120 if (a != 0 && b != 0 && !(x == 0 && y == 0)){
|
fiore@0
|
121 double t = Math.sqrt((x * x) / (a * a) + (y * y) / (b * b));
|
fiore@0
|
122 return new Point2D.Double(cx + x / t, cy + y / t);
|
fiore@0
|
123 }
|
fiore@0
|
124 else{
|
fiore@0
|
125 return new Point2D.Double(cx, cy);
|
fiore@0
|
126 }
|
fiore@0
|
127 }
|
fiore@0
|
128
|
fiore@0
|
129 @Override
|
fiore@0
|
130 public Shape getShape() {
|
fiore@0
|
131 return eShape;
|
fiore@0
|
132 }
|
fiore@0
|
133
|
fiore@0
|
134 @Override
|
fiore@0
|
135 public InputStream getSound(){
|
fiore@0
|
136 return sound;
|
fiore@0
|
137 }
|
fiore@0
|
138
|
fiore@0
|
139 @Override
|
fiore@0
|
140 public Object clone(){
|
fiore@0
|
141 EllipticalNode n = (EllipticalNode)super.clone();
|
fiore@0
|
142 n.eShape = (Ellipse2D.Double)eShape.clone();
|
fiore@0
|
143 return n;
|
fiore@0
|
144 }
|
fiore@0
|
145
|
fiore@0
|
146 private Ellipse2D.Double eShape;
|
fiore@0
|
147 private static InputStream sound;
|
fiore@0
|
148 static{
|
fiore@0
|
149 sound = EllipticalNode.class.getResourceAsStream("audio/Ellipse.mp3");
|
fiore@0
|
150 SoundFactory.getInstance().loadSound(sound);
|
fiore@0
|
151 }
|
fiore@0
|
152 }
|
fiore@0
|
153
|