annotate java/src/uk/ac/qmul/eecs/ccmi/simpletemplate/TriangularNode.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19
f@0 20 package uk.ac.qmul.eecs.ccmi.simpletemplate;
f@0 21
f@0 22 import java.awt.Shape;
f@0 23 import java.awt.geom.AffineTransform;
f@0 24 import java.awt.geom.GeneralPath;
f@0 25 import java.awt.geom.Path2D;
f@0 26 import java.awt.geom.Point2D;
f@0 27 import java.awt.geom.Rectangle2D;
f@0 28 import java.awt.geom.Rectangle2D.Double;
f@0 29 import java.io.InputStream;
f@0 30 import java.util.List;
f@0 31
f@0 32 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
f@0 33 import uk.ac.qmul.eecs.ccmi.gui.Direction;
f@0 34 import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
f@0 35
f@0 36 /**
f@0 37 *
f@0 38 * A triangular shaped diagram node.
f@0 39 *
f@0 40 */
f@0 41 @SuppressWarnings("serial")
f@0 42 public class TriangularNode extends SimpleShapeNode {
f@0 43
f@0 44
f@0 45 public TriangularNode(String typeName, NodeProperties properties) {
f@0 46 super(typeName, properties);
f@0 47 Rectangle2D dataBounds = getMinBounds();
f@0 48 dataDisplayBounds.setFrame(dataBounds);
f@0 49 tShape = getOutShape(dataBounds);
f@0 50 /* by building the shape around dataBounds which was at (0,0) the new bounds */
f@0 51 /* are now negative, so we need to bring the new bounds back at (0,0) */
f@0 52 Rectangle2D bounds = getBounds();
f@0 53 translateImplementation(new Point2D.Double(),0-bounds.getX(),0-bounds.getY());
f@0 54 }
f@0 55
f@0 56 @Override
f@0 57 protected Rectangle2D getMinBounds(){
f@0 58 Rectangle2D minBounds = super.getMinBounds();
f@0 59 return new Rectangle2D.Double(minBounds.getX(),minBounds.getY(),minBounds.getWidth()/2,minBounds.getHeight()/2);
f@0 60 }
f@0 61
f@0 62 @Override
f@0 63 public ShapeType getShapeType() {
f@0 64 return ShapeType.Triangle;
f@0 65 }
f@0 66
f@0 67 @Override
f@0 68 protected void translateImplementation(Point2D p, double dx, double dy){
f@0 69 /* if we clicked on a property node, just move that one */
f@0 70 for(List<PropertyNode> pnList : propertyNodesMap.values())
f@0 71 for(PropertyNode pn : pnList)
f@0 72 if(pn.contains(p)){
f@0 73 pn.translate(dx, dy);
f@0 74 return;
f@0 75 }
f@0 76 super.translateImplementation(p,dx, dy);
f@0 77 tShape.transform(AffineTransform.getTranslateInstance(dx, dy));
f@0 78 }
f@0 79
f@0 80 public static Path2D.Double getOutShape(Rectangle2D r){
f@0 81 Path2D.Double triangle = new Path2D.Double(GeneralPath.WIND_EVEN_ODD,3);
f@0 82 double minEdge = Math.min(r.getWidth(), r.getHeight());
f@0 83 triangle.moveTo(r.getCenterX(), r.getY()-minEdge);
f@0 84
f@0 85 double angle = Math.atan(minEdge/(r.getWidth()/2));
f@0 86 double w = r.getHeight()/ Math.tan(angle);
f@0 87 triangle.lineTo(r.getX()-w, r.getMaxY());
f@0 88 triangle.lineTo(r.getMaxX()+w, r.getMaxY());
f@0 89 triangle.closePath();
f@0 90 return triangle;
f@0 91 }
f@0 92
f@0 93 @Override
f@0 94 protected void reshapeInnerProperties(List<String> insidePropertyTypes){
f@0 95 nameLabel = new MultiLineString();
f@0 96 nameLabel.setText(getName().isEmpty() ? " " : getName());
f@0 97 nameLabel.setBold(true);
f@0 98
f@0 99 if(!super.anyInsideProperties()){
f@0 100 dataDisplayBounds.setFrame(dataDisplayBounds.getX(),
f@0 101 dataDisplayBounds.getY(),
f@0 102 nameLabel.getBounds().getWidth(),
f@0 103 nameLabel.getBounds().getHeight());
f@0 104 Rectangle2D minBounds = getMinBounds();
f@0 105 dataDisplayBounds.add(new Rectangle2D.Double(dataDisplayBounds.getX(), dataDisplayBounds.getY(), minBounds.getWidth(),minBounds.getHeight()));
f@0 106 tShape = getOutShape(dataDisplayBounds);
f@0 107 }else {
f@0 108 Rectangle2D r = nameLabel.getBounds();
f@0 109
f@0 110 for(int i=0; i<insidePropertyTypes.size();i++){
f@0 111 propertyLabels[i] = new MultiLineString();
f@0 112 if(getProperties().getValues(insidePropertyTypes.get(i)).size() == 0){
f@0 113 propertyLabels[i].setText(" ");
f@0 114 }else{
f@0 115 propertyLabels[i].setJustification(MultiLineString.LEFT);
f@0 116 String[] a = new String[getProperties().getValues(insidePropertyTypes.get(i)).size()];
f@0 117 propertyLabels[i].setText(getProperties().getValues(insidePropertyTypes.get(i)).toArray(a), getProperties().getModifiers(insidePropertyTypes.get(i)));
f@0 118 }
f@0 119 r.add(new Rectangle2D.Double(r.getX(),r.getMaxY(),propertyLabels[i].getBounds().getWidth(),propertyLabels[i].getBounds().getHeight()));
f@0 120 }
f@0 121 /* set a gap to uniformly distribute the extra space among property rectangles to reach the minimum bound's height */
f@0 122 boundsGap = 0;
f@0 123 Rectangle2D.Double minBounds = (Rectangle2D.Double)getMinBounds();
f@0 124 if(r.getHeight() < minBounds.height){
f@0 125 boundsGap = minBounds.height - r.getHeight();
f@0 126 boundsGap /= insidePropertyTypes.size();
f@0 127 }
f@0 128 r.add(minBounds);
f@0 129 dataDisplayBounds.setFrame(new Rectangle2D.Double(dataDisplayBounds.x,dataDisplayBounds.y,r.getWidth(),r.getHeight()));
f@0 130
f@0 131 tShape = getOutShape(dataDisplayBounds);
f@0 132 }
f@0 133
f@0 134 }
f@0 135
f@0 136 @Override
f@0 137 public Double getBounds() {
f@0 138 return (Double)tShape.getBounds2D();
f@0 139 }
f@0 140
f@0 141 @Override
f@0 142 public InputStream getSound(){
f@0 143 return sound;
f@0 144 }
f@0 145
f@0 146 @Override
f@0 147 public Point2D getConnectionPoint(Direction d) {
f@0 148 return calculateConnectionPoint(d,getBounds());
f@0 149 }
f@0 150
f@0 151 public static Point2D calculateConnectionPoint(Direction d, Rectangle2D bounds) {
f@0 152 if(d.getX() == 0){
f@0 153 return new Point2D.Double(bounds.getCenterX(),
f@0 154 d.getY() > 0 ? bounds.getY() : bounds.getMaxY());
f@0 155 }
f@0 156
f@0 157 boolean left = false;
f@0 158 boolean right = false;
f@0 159 double dirTan = d.getY()/d.getX();
f@0 160 double boundsTan = bounds.getHeight()/bounds.getWidth();
f@0 161 double alfa = Math.atan(dirTan);
f@0 162 double alfaDegrees = Math.toDegrees(alfa);
f@0 163
f@0 164 if(d.getY() < 0){ //from the top
f@0 165 if(alfaDegrees < 0)
f@0 166 right = true;
f@0 167 else
f@0 168 left = true;
f@0 169 }else{ //from the bottom
f@0 170 if(dirTan < boundsTan && d.getX() > 0)
f@0 171 right = true;
f@0 172 else if(dirTan > -boundsTan && d.getX() < 0)
f@0 173 left = true;
f@0 174 }
f@0 175
f@0 176 if(right){
f@0 177 double beta = Math.atan(bounds.getHeight()/(bounds.getWidth()/2));
f@0 178 double py = bounds.getHeight()/2;
f@0 179 double x = py/ (Math.tan(alfa)-Math.tan(beta));
f@0 180 double y = x * Math.tan(alfa);
f@0 181 return new Point2D.Double(bounds.getCenterX()-x, bounds.getCenterY()-y);
f@0 182 }
f@0 183 else if(left){
f@0 184 double beta = - Math.atan(bounds.getHeight()/(bounds.getWidth()/2));
f@0 185 double py = bounds.getHeight()/2;
f@0 186 double x = py/ (Math.tan(alfa)-Math.tan(beta));
f@0 187 double y = x * Math.tan(alfa);
f@0 188 return new Point2D.Double(bounds.getCenterX()-x, bounds.getCenterY()-y);
f@0 189 }
f@0 190 else{
f@0 191 return new Point2D.Double(
f@0 192 bounds.getCenterX() + ((bounds.getHeight()/2) * (d.getX()/d.getY()) ),
f@0 193 bounds.getMaxY());
f@0 194 }
f@0 195 }
f@0 196
f@0 197 @Override
f@0 198 public Shape getShape() {
f@0 199 return tShape;
f@0 200 }
f@0 201
f@0 202 public Object clone(){
f@0 203 return new TriangularNode(getType(),(NodeProperties)getProperties().clone());
f@0 204 }
f@0 205
f@0 206 private Path2D.Double tShape;
f@0 207 private static InputStream sound;
f@0 208
f@0 209 static {
f@0 210 sound = TriangularNode.class.getResourceAsStream("audio/Triangle.mp3");
f@0 211 SoundFactory.getInstance().loadSound(sound);
f@0 212 }
f@0 213 }