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