annotate java/src/uk/ac/qmul/eecs/ccmi/simpletemplate/EllipticalNode.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.Ellipse2D;
f@0 24 import java.awt.geom.Point2D;
f@0 25 import java.awt.geom.Rectangle2D;
f@0 26 import java.io.IOException;
f@0 27 import java.io.InputStream;
f@0 28 import java.util.List;
f@0 29
f@0 30 import org.w3c.dom.Document;
f@0 31 import org.w3c.dom.Element;
f@0 32
f@0 33 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
f@0 34 import uk.ac.qmul.eecs.ccmi.gui.Direction;
f@0 35 import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
f@0 36
f@0 37 /**
f@0 38 *
f@0 39 * An elliptical shaped diagram node.
f@0 40 *
f@0 41 */
f@0 42 @SuppressWarnings("serial")
f@0 43 public class EllipticalNode extends SimpleShapeNode {
f@0 44
f@0 45 public EllipticalNode(String typeName, NodeProperties properties){
f@0 46 super(typeName, properties);
f@0 47 Rectangle2D r = getMinBounds();
f@0 48 eShape = new Ellipse2D.Double(0,0,r.getWidth(),r.getHeight());
f@0 49 }
f@0 50
f@0 51 @Override
f@0 52 public ShapeType getShapeType() {
f@0 53 return ShapeType.Ellipse;
f@0 54 }
f@0 55
f@0 56 public static Rectangle2D getOutBounds(Rectangle2D r){
f@0 57 double h = r.getHeight()/2;
f@0 58 double w = r.getWidth()/2;
f@0 59
f@0 60 double anglew = Math.atan(h/w);
f@0 61 double angleh = Math.atan(w/h);
f@0 62
f@0 63 double a = w + h * Math.tan(angleh)/2;
f@0 64 double b = h + w * Math.tan(anglew)/2;
f@0 65
f@0 66 return new Rectangle2D.Double(
f@0 67 r.getCenterX() - a,
f@0 68 r.getCenterY() - b,
f@0 69 2*a,
f@0 70 2*b
f@0 71 );
f@0 72 }
f@0 73
f@0 74 @Override
f@0 75 protected void reshapeInnerProperties(List<String> insidePropertyTypes){
f@0 76 super.reshapeInnerProperties(insidePropertyTypes);
f@0 77 eShape.setFrame(super.anyInsideProperties() ? getOutBounds(dataDisplayBounds) : dataDisplayBounds );
f@0 78 }
f@0 79
f@0 80 @Override
f@0 81 public void decode(Document doc, Element nodeTag) throws IOException{
f@0 82 super.decode(doc, nodeTag);
f@0 83 eShape.setFrame(super.anyInsideProperties() ? getOutBounds(dataDisplayBounds) : dataDisplayBounds );
f@0 84 }
f@0 85
f@0 86 @Override
f@0 87 protected void translateImplementation(Point2D p,double dx, double dy){
f@0 88 /* if we clicked on a property node, just move that one */
f@0 89 for(List<PropertyNode> pnList : propertyNodesMap.values())
f@0 90 for(PropertyNode pn : pnList)
f@0 91 if(pn.contains(p)){
f@0 92 pn.translate(dx, dy);
f@0 93 return;
f@0 94 }
f@0 95 eShape.setFrame(eShape.getX() + dx,
f@0 96 eShape.getY() + dy,
f@0 97 eShape.getWidth(),
f@0 98 eShape.getHeight());
f@0 99 super.translateImplementation(p,dx, dy);
f@0 100 }
f@0 101
f@0 102 @Override
f@0 103 public Rectangle2D getBounds() {
f@0 104 return eShape.getBounds2D();
f@0 105 }
f@0 106
f@0 107 @Override
f@0 108 public Point2D getConnectionPoint(Direction d) {
f@0 109 return calculateConnectionPoint(d, getBounds());
f@0 110 }
f@0 111
f@0 112 public static Point2D calculateConnectionPoint(Direction d, Rectangle2D bounds){
f@0 113 double a = bounds.getWidth() / 2;
f@0 114 double b = bounds.getHeight() / 2;
f@0 115 double x = d.getX();
f@0 116 double y = d.getY();
f@0 117 double cx = bounds.getCenterX();
f@0 118 double cy = bounds.getCenterY();
f@0 119
f@0 120 if (a != 0 && b != 0 && !(x == 0 && y == 0)){
f@0 121 double t = Math.sqrt((x * x) / (a * a) + (y * y) / (b * b));
f@0 122 return new Point2D.Double(cx + x / t, cy + y / t);
f@0 123 }
f@0 124 else{
f@0 125 return new Point2D.Double(cx, cy);
f@0 126 }
f@0 127 }
f@0 128
f@0 129 @Override
f@0 130 public Shape getShape() {
f@0 131 return eShape;
f@0 132 }
f@0 133
f@0 134 @Override
f@0 135 public InputStream getSound(){
f@0 136 return sound;
f@0 137 }
f@0 138
f@0 139 @Override
f@0 140 public Object clone(){
f@0 141 EllipticalNode n = (EllipticalNode)super.clone();
f@0 142 n.eShape = (Ellipse2D.Double)eShape.clone();
f@0 143 return n;
f@0 144 }
f@0 145
f@0 146 private Ellipse2D.Double eShape;
f@0 147 private static InputStream sound;
f@0 148 static{
f@0 149 sound = EllipticalNode.class.getResourceAsStream("audio/Ellipse.mp3");
f@0 150 SoundFactory.getInstance().loadSound(sound);
f@0 151 }
f@0 152 }
f@0 153