annotate java/src/uk/ac/qmul/eecs/ccmi/simpletemplate/RectangularNode.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) 2002 Cay S. Horstmann (http://horstmann.com)
f@0 5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 6
f@0 7 This program is free software: you can redistribute it and/or modify
f@0 8 it under the terms of the GNU General Public License as published by
f@0 9 the Free Software Foundation, either version 3 of the License, or
f@0 10 (at your option) any later version.
f@0 11
f@0 12 This program is distributed in the hope that it will be useful,
f@0 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 15 GNU General Public License for more details.
f@0 16
f@0 17 You should have received a copy of the GNU General Public License
f@0 18 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 19 */
f@0 20
f@0 21 package uk.ac.qmul.eecs.ccmi.simpletemplate;
f@0 22
f@0 23 import java.awt.Shape;
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 A rectangular shaped diagram node.
f@0 39 */
f@0 40 @SuppressWarnings("serial")
f@0 41 public class RectangularNode extends SimpleShapeNode{
f@0 42
f@0 43 public RectangularNode(String nodeType, NodeProperties properties){
f@0 44 super(nodeType, properties);
f@0 45 bounds = dataDisplayBounds;
f@0 46 }
f@0 47
f@0 48 @Override
f@0 49 public ShapeType getShapeType() {
f@0 50 return ShapeType.Rectangle;
f@0 51 }
f@0 52
f@0 53 @Override
f@0 54 protected void translateImplementation(Point2D p, double dx, double dy){
f@0 55 /* if we clicked on a property node, just move that one */
f@0 56 for(List<PropertyNode> pnList : propertyNodesMap.values())
f@0 57 for(PropertyNode pn : pnList)
f@0 58 if(pn.contains(p)){
f@0 59 pn.translate(dx, dy);
f@0 60 return;
f@0 61 }
f@0 62
f@0 63 bounds.setFrame(bounds.getX() + dx,
f@0 64 bounds.getY() + dy,
f@0 65 bounds.getWidth(),
f@0 66 bounds.getHeight());
f@0 67 super.translateImplementation(p, dx, dy);
f@0 68 }
f@0 69
f@0 70 @Override
f@0 71 protected void reshapeInnerProperties(List<String> insidePropertyTypes){
f@0 72 super.reshapeInnerProperties(insidePropertyTypes);
f@0 73 bounds.setFrame(dataDisplayBounds);
f@0 74 }
f@0 75
f@0 76 @Override
f@0 77 public void decode(Document doc, Element nodeTag) throws IOException{
f@0 78 super.decode(doc, nodeTag);
f@0 79 bounds.setFrame(dataDisplayBounds);
f@0 80 }
f@0 81
f@0 82 @Override
f@0 83 public Rectangle2D getBounds(){
f@0 84 return (Rectangle2D)bounds.clone();
f@0 85 }
f@0 86
f@0 87 @Override
f@0 88 public Point2D getConnectionPoint(Direction d){
f@0 89 return calculateConnectionPoint(d, getBounds());
f@0 90 }
f@0 91
f@0 92 public static Point2D calculateConnectionPoint(Direction d, Rectangle2D bounds){
f@0 93 double slope = bounds.getHeight() / bounds.getWidth();
f@0 94 double ex = d.getX();
f@0 95 double ey = d.getY();
f@0 96 double x = bounds.getCenterX();
f@0 97 double y = bounds.getCenterY();
f@0 98
f@0 99 if (ex != 0 && -slope <= ey / ex && ey / ex <= slope){
f@0 100 // intersects at left or right boundary
f@0 101 if (ex > 0){
f@0 102 x = bounds.getMaxX();
f@0 103 y += (bounds.getWidth() / 2) * ey / ex;
f@0 104 }else{
f@0 105 x = bounds.getX();
f@0 106 y -= (bounds.getWidth() / 2) * ey / ex;
f@0 107 }
f@0 108 }else if (ey != 0){
f@0 109 // intersects at top or bottom
f@0 110 if (ey > 0){
f@0 111 x += (bounds.getHeight() / 2) * ex / ey;
f@0 112 y = bounds.getMaxY();
f@0 113 }else{
f@0 114 x -= (bounds.getHeight() / 2) * ex / ey;
f@0 115 y = bounds.getY();
f@0 116 }
f@0 117 }
f@0 118 return new Point2D.Double(x, y);
f@0 119 }
f@0 120
f@0 121 public Shape getShape(){
f@0 122 return bounds;
f@0 123 }
f@0 124
f@0 125 @Override
f@0 126 public InputStream getSound(){
f@0 127 return sound;
f@0 128 }
f@0 129
f@0 130 public Object clone(){
f@0 131 RectangularNode cloned = (RectangularNode)super.clone();
f@0 132 cloned.bounds = (Rectangle2D.Double)bounds.clone();
f@0 133 return cloned;
f@0 134 }
f@0 135
f@0 136 private Rectangle2D.Double bounds;
f@0 137 private static InputStream sound;
f@0 138
f@0 139 static{
f@0 140 sound = RectangularNode.class.getResourceAsStream("audio/Rectangle.mp3");
f@0 141 SoundFactory.getInstance().loadSound(sound);
f@0 142 }
f@0 143 }