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.AffineTransform;
|
fiore@0
|
24 import java.awt.geom.GeneralPath;
|
fiore@0
|
25 import java.awt.geom.Path2D;
|
fiore@0
|
26 import java.awt.geom.Point2D;
|
fiore@0
|
27 import java.awt.geom.Rectangle2D;
|
fiore@0
|
28 import java.awt.geom.Rectangle2D.Double;
|
fiore@0
|
29 import java.io.InputStream;
|
fiore@0
|
30 import java.util.List;
|
fiore@0
|
31
|
fiore@0
|
32 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
|
fiore@0
|
33 import uk.ac.qmul.eecs.ccmi.gui.Direction;
|
fiore@0
|
34 import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
|
fiore@0
|
35
|
fiore@0
|
36 /**
|
fiore@0
|
37 *
|
fiore@0
|
38 * A triangular shaped diagram node.
|
fiore@0
|
39 *
|
fiore@0
|
40 */
|
fiore@0
|
41 @SuppressWarnings("serial")
|
fiore@0
|
42 public class TriangularNode extends SimpleShapeNode {
|
fiore@0
|
43
|
fiore@0
|
44
|
fiore@0
|
45 public TriangularNode(String typeName, NodeProperties properties) {
|
fiore@0
|
46 super(typeName, properties);
|
fiore@0
|
47 Rectangle2D dataBounds = getMinBounds();
|
fiore@0
|
48 dataDisplayBounds.setFrame(dataBounds);
|
fiore@0
|
49 tShape = getOutShape(dataBounds);
|
fiore@0
|
50 /* by building the shape around dataBounds which was at (0,0) the new bounds */
|
fiore@0
|
51 /* are now negative, so we need to bring the new bounds back at (0,0) */
|
fiore@0
|
52 Rectangle2D bounds = getBounds();
|
fiore@0
|
53 translateImplementation(new Point2D.Double(),0-bounds.getX(),0-bounds.getY());
|
fiore@0
|
54 }
|
fiore@0
|
55
|
fiore@0
|
56 @Override
|
fiore@0
|
57 protected Rectangle2D getMinBounds(){
|
fiore@0
|
58 Rectangle2D minBounds = super.getMinBounds();
|
fiore@0
|
59 return new Rectangle2D.Double(minBounds.getX(),minBounds.getY(),minBounds.getWidth()/2,minBounds.getHeight()/2);
|
fiore@0
|
60 }
|
fiore@0
|
61
|
fiore@0
|
62 @Override
|
fiore@0
|
63 public ShapeType getShapeType() {
|
fiore@0
|
64 return ShapeType.Triangle;
|
fiore@0
|
65 }
|
fiore@0
|
66
|
fiore@0
|
67 @Override
|
fiore@0
|
68 protected void translateImplementation(Point2D p, double dx, double dy){
|
fiore@0
|
69 /* if we clicked on a property node, just move that one */
|
fiore@0
|
70 for(List<PropertyNode> pnList : propertyNodesMap.values())
|
fiore@0
|
71 for(PropertyNode pn : pnList)
|
fiore@0
|
72 if(pn.contains(p)){
|
fiore@0
|
73 pn.translate(dx, dy);
|
fiore@0
|
74 return;
|
fiore@0
|
75 }
|
fiore@0
|
76 super.translateImplementation(p,dx, dy);
|
fiore@0
|
77 tShape.transform(AffineTransform.getTranslateInstance(dx, dy));
|
fiore@0
|
78 }
|
fiore@0
|
79
|
fiore@0
|
80 public static Path2D.Double getOutShape(Rectangle2D r){
|
fiore@0
|
81 Path2D.Double triangle = new Path2D.Double(GeneralPath.WIND_EVEN_ODD,3);
|
fiore@0
|
82 double minEdge = Math.min(r.getWidth(), r.getHeight());
|
fiore@0
|
83 triangle.moveTo(r.getCenterX(), r.getY()-minEdge);
|
fiore@0
|
84
|
fiore@0
|
85 double angle = Math.atan(minEdge/(r.getWidth()/2));
|
fiore@0
|
86 double w = r.getHeight()/ Math.tan(angle);
|
fiore@0
|
87 triangle.lineTo(r.getX()-w, r.getMaxY());
|
fiore@0
|
88 triangle.lineTo(r.getMaxX()+w, r.getMaxY());
|
fiore@0
|
89 triangle.closePath();
|
fiore@0
|
90 return triangle;
|
fiore@0
|
91 }
|
fiore@0
|
92
|
fiore@0
|
93 @Override
|
fiore@0
|
94 protected void reshapeInnerProperties(List<String> insidePropertyTypes){
|
fiore@0
|
95 nameLabel = new MultiLineString();
|
fiore@0
|
96 nameLabel.setText(getName().isEmpty() ? " " : getName());
|
fiore@0
|
97 nameLabel.setBold(true);
|
fiore@0
|
98
|
fiore@0
|
99 if(!super.anyInsideProperties()){
|
fiore@0
|
100 dataDisplayBounds.setFrame(dataDisplayBounds.getX(),
|
fiore@0
|
101 dataDisplayBounds.getY(),
|
fiore@0
|
102 nameLabel.getBounds().getWidth(),
|
fiore@0
|
103 nameLabel.getBounds().getHeight());
|
fiore@0
|
104 Rectangle2D minBounds = getMinBounds();
|
fiore@0
|
105 dataDisplayBounds.add(new Rectangle2D.Double(dataDisplayBounds.getX(), dataDisplayBounds.getY(), minBounds.getWidth(),minBounds.getHeight()));
|
fiore@0
|
106 tShape = getOutShape(dataDisplayBounds);
|
fiore@0
|
107 }else {
|
fiore@0
|
108 Rectangle2D r = nameLabel.getBounds();
|
fiore@0
|
109
|
fiore@0
|
110 for(int i=0; i<insidePropertyTypes.size();i++){
|
fiore@0
|
111 propertyLabels[i] = new MultiLineString();
|
fiore@0
|
112 if(getProperties().getValues(insidePropertyTypes.get(i)).size() == 0){
|
fiore@0
|
113 propertyLabels[i].setText(" ");
|
fiore@0
|
114 }else{
|
fiore@0
|
115 propertyLabels[i].setJustification(MultiLineString.LEFT);
|
fiore@0
|
116 String[] a = new String[getProperties().getValues(insidePropertyTypes.get(i)).size()];
|
fiore@0
|
117 propertyLabels[i].setText(getProperties().getValues(insidePropertyTypes.get(i)).toArray(a), getProperties().getModifiers(insidePropertyTypes.get(i)));
|
fiore@0
|
118 }
|
fiore@0
|
119 r.add(new Rectangle2D.Double(r.getX(),r.getMaxY(),propertyLabels[i].getBounds().getWidth(),propertyLabels[i].getBounds().getHeight()));
|
fiore@0
|
120 }
|
fiore@0
|
121 /* set a gap to uniformly distribute the extra space among property rectangles to reach the minimum bound's height */
|
fiore@0
|
122 boundsGap = 0;
|
fiore@0
|
123 Rectangle2D.Double minBounds = (Rectangle2D.Double)getMinBounds();
|
fiore@0
|
124 if(r.getHeight() < minBounds.height){
|
fiore@0
|
125 boundsGap = minBounds.height - r.getHeight();
|
fiore@0
|
126 boundsGap /= insidePropertyTypes.size();
|
fiore@0
|
127 }
|
fiore@0
|
128 r.add(minBounds);
|
fiore@0
|
129 dataDisplayBounds.setFrame(new Rectangle2D.Double(dataDisplayBounds.x,dataDisplayBounds.y,r.getWidth(),r.getHeight()));
|
fiore@0
|
130
|
fiore@0
|
131 tShape = getOutShape(dataDisplayBounds);
|
fiore@0
|
132 }
|
fiore@0
|
133
|
fiore@0
|
134 }
|
fiore@0
|
135
|
fiore@0
|
136 @Override
|
fiore@0
|
137 public Double getBounds() {
|
fiore@0
|
138 return (Double)tShape.getBounds2D();
|
fiore@0
|
139 }
|
fiore@0
|
140
|
fiore@0
|
141 @Override
|
fiore@0
|
142 public InputStream getSound(){
|
fiore@0
|
143 return sound;
|
fiore@0
|
144 }
|
fiore@0
|
145
|
fiore@0
|
146 @Override
|
fiore@0
|
147 public Point2D getConnectionPoint(Direction d) {
|
fiore@0
|
148 return calculateConnectionPoint(d,getBounds());
|
fiore@0
|
149 }
|
fiore@0
|
150
|
fiore@0
|
151 public static Point2D calculateConnectionPoint(Direction d, Rectangle2D bounds) {
|
fiore@0
|
152 if(d.getX() == 0){
|
fiore@0
|
153 return new Point2D.Double(bounds.getCenterX(),
|
fiore@0
|
154 d.getY() > 0 ? bounds.getY() : bounds.getMaxY());
|
fiore@0
|
155 }
|
fiore@0
|
156
|
fiore@0
|
157 boolean left = false;
|
fiore@0
|
158 boolean right = false;
|
fiore@0
|
159 double dirTan = d.getY()/d.getX();
|
fiore@0
|
160 double boundsTan = bounds.getHeight()/bounds.getWidth();
|
fiore@0
|
161 double alfa = Math.atan(dirTan);
|
fiore@0
|
162 double alfaDegrees = Math.toDegrees(alfa);
|
fiore@0
|
163
|
fiore@0
|
164 if(d.getY() < 0){ //from the top
|
fiore@0
|
165 if(alfaDegrees < 0)
|
fiore@0
|
166 right = true;
|
fiore@0
|
167 else
|
fiore@0
|
168 left = true;
|
fiore@0
|
169 }else{ //from the bottom
|
fiore@0
|
170 if(dirTan < boundsTan && d.getX() > 0)
|
fiore@0
|
171 right = true;
|
fiore@0
|
172 else if(dirTan > -boundsTan && d.getX() < 0)
|
fiore@0
|
173 left = true;
|
fiore@0
|
174 }
|
fiore@0
|
175
|
fiore@0
|
176 if(right){
|
fiore@0
|
177 double beta = Math.atan(bounds.getHeight()/(bounds.getWidth()/2));
|
fiore@0
|
178 double py = bounds.getHeight()/2;
|
fiore@0
|
179 double x = py/ (Math.tan(alfa)-Math.tan(beta));
|
fiore@0
|
180 double y = x * Math.tan(alfa);
|
fiore@0
|
181 return new Point2D.Double(bounds.getCenterX()-x, bounds.getCenterY()-y);
|
fiore@0
|
182 }
|
fiore@0
|
183 else if(left){
|
fiore@0
|
184 double beta = - Math.atan(bounds.getHeight()/(bounds.getWidth()/2));
|
fiore@0
|
185 double py = bounds.getHeight()/2;
|
fiore@0
|
186 double x = py/ (Math.tan(alfa)-Math.tan(beta));
|
fiore@0
|
187 double y = x * Math.tan(alfa);
|
fiore@0
|
188 return new Point2D.Double(bounds.getCenterX()-x, bounds.getCenterY()-y);
|
fiore@0
|
189 }
|
fiore@0
|
190 else{
|
fiore@0
|
191 return new Point2D.Double(
|
fiore@0
|
192 bounds.getCenterX() + ((bounds.getHeight()/2) * (d.getX()/d.getY()) ),
|
fiore@0
|
193 bounds.getMaxY());
|
fiore@0
|
194 }
|
fiore@0
|
195 }
|
fiore@0
|
196
|
fiore@0
|
197 @Override
|
fiore@0
|
198 public Shape getShape() {
|
fiore@0
|
199 return tShape;
|
fiore@0
|
200 }
|
fiore@0
|
201
|
fiore@0
|
202 public Object clone(){
|
fiore@0
|
203 return new TriangularNode(getType(),(NodeProperties)getProperties().clone());
|
fiore@0
|
204 }
|
fiore@0
|
205
|
fiore@0
|
206 private Path2D.Double tShape;
|
fiore@0
|
207 private static InputStream sound;
|
fiore@0
|
208
|
fiore@0
|
209 static {
|
fiore@0
|
210 sound = TriangularNode.class.getResourceAsStream("audio/Triangle.mp3");
|
fiore@0
|
211 SoundFactory.getInstance().loadSound(sound);
|
fiore@0
|
212 }
|
fiore@0
|
213 }
|