f@0: /*
f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0:
f@0: Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0:
f@0: This program is free software: you can redistribute it and/or modify
f@0: it under the terms of the GNU General Public License as published by
f@0: the Free Software Foundation, either version 3 of the License, or
f@0: (at your option) any later version.
f@0:
f@0: This program is distributed in the hope that it will be useful,
f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0: GNU General Public License for more details.
f@0:
f@0: You should have received a copy of the GNU General Public License
f@0: along with this program. If not, see .
f@0: */
f@0:
f@0: package uk.ac.qmul.eecs.ccmi.simpletemplate;
f@0:
f@0: import java.awt.Shape;
f@0: import java.awt.geom.Point2D;
f@0: import java.awt.geom.Rectangle2D;
f@0: import java.io.IOException;
f@0: import java.io.InputStream;
f@0: import java.util.List;
f@0:
f@0: import org.w3c.dom.Document;
f@0: import org.w3c.dom.Element;
f@0:
f@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
f@0: import uk.ac.qmul.eecs.ccmi.gui.Direction;
f@0: import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;
f@0:
f@0: /**
f@0: A rectangular shaped diagram node.
f@0: */
f@0: @SuppressWarnings("serial")
f@0: public class RectangularNode extends SimpleShapeNode{
f@0:
f@0: public RectangularNode(String nodeType, NodeProperties properties){
f@0: super(nodeType, properties);
f@0: bounds = dataDisplayBounds;
f@0: }
f@0:
f@0: @Override
f@0: public ShapeType getShapeType() {
f@0: return ShapeType.Rectangle;
f@0: }
f@0:
f@0: @Override
f@0: protected void translateImplementation(Point2D p, double dx, double dy){
f@0: /* if we clicked on a property node, just move that one */
f@0: for(List pnList : propertyNodesMap.values())
f@0: for(PropertyNode pn : pnList)
f@0: if(pn.contains(p)){
f@0: pn.translate(dx, dy);
f@0: return;
f@0: }
f@0:
f@0: bounds.setFrame(bounds.getX() + dx,
f@0: bounds.getY() + dy,
f@0: bounds.getWidth(),
f@0: bounds.getHeight());
f@0: super.translateImplementation(p, dx, dy);
f@0: }
f@0:
f@0: @Override
f@0: protected void reshapeInnerProperties(List insidePropertyTypes){
f@0: super.reshapeInnerProperties(insidePropertyTypes);
f@0: bounds.setFrame(dataDisplayBounds);
f@0: }
f@0:
f@0: @Override
f@0: public void decode(Document doc, Element nodeTag) throws IOException{
f@0: super.decode(doc, nodeTag);
f@0: bounds.setFrame(dataDisplayBounds);
f@0: }
f@0:
f@0: @Override
f@0: public Rectangle2D getBounds(){
f@0: return (Rectangle2D)bounds.clone();
f@0: }
f@0:
f@0: @Override
f@0: public Point2D getConnectionPoint(Direction d){
f@0: return calculateConnectionPoint(d, getBounds());
f@0: }
f@0:
f@0: public static Point2D calculateConnectionPoint(Direction d, Rectangle2D bounds){
f@0: double slope = bounds.getHeight() / bounds.getWidth();
f@0: double ex = d.getX();
f@0: double ey = d.getY();
f@0: double x = bounds.getCenterX();
f@0: double y = bounds.getCenterY();
f@0:
f@0: if (ex != 0 && -slope <= ey / ex && ey / ex <= slope){
f@0: // intersects at left or right boundary
f@0: if (ex > 0){
f@0: x = bounds.getMaxX();
f@0: y += (bounds.getWidth() / 2) * ey / ex;
f@0: }else{
f@0: x = bounds.getX();
f@0: y -= (bounds.getWidth() / 2) * ey / ex;
f@0: }
f@0: }else if (ey != 0){
f@0: // intersects at top or bottom
f@0: if (ey > 0){
f@0: x += (bounds.getHeight() / 2) * ex / ey;
f@0: y = bounds.getMaxY();
f@0: }else{
f@0: x -= (bounds.getHeight() / 2) * ex / ey;
f@0: y = bounds.getY();
f@0: }
f@0: }
f@0: return new Point2D.Double(x, y);
f@0: }
f@0:
f@0: public Shape getShape(){
f@0: return bounds;
f@0: }
f@0:
f@0: @Override
f@0: public InputStream getSound(){
f@0: return sound;
f@0: }
f@0:
f@0: public Object clone(){
f@0: RectangularNode cloned = (RectangularNode)super.clone();
f@0: cloned.bounds = (Rectangle2D.Double)bounds.clone();
f@0: return cloned;
f@0: }
f@0:
f@0: private Rectangle2D.Double bounds;
f@0: private static InputStream sound;
f@0:
f@0: static{
f@0: sound = RectangularNode.class.getResourceAsStream("audio/Rectangle.mp3");
f@0: SoundFactory.getInstance().loadSound(sound);
f@0: }
f@0: }