f@0: /* f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool f@0: 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.Ellipse2D; 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: * f@0: * An elliptical shaped diagram node. f@0: * f@0: */ f@0: @SuppressWarnings("serial") f@0: public class EllipticalNode extends SimpleShapeNode { f@0: f@0: public EllipticalNode(String typeName, NodeProperties properties){ f@0: super(typeName, properties); f@0: Rectangle2D r = getMinBounds(); f@0: eShape = new Ellipse2D.Double(0,0,r.getWidth(),r.getHeight()); f@0: } f@0: f@0: @Override f@0: public ShapeType getShapeType() { f@0: return ShapeType.Ellipse; f@0: } f@0: f@0: public static Rectangle2D getOutBounds(Rectangle2D r){ f@0: double h = r.getHeight()/2; f@0: double w = r.getWidth()/2; f@0: f@0: double anglew = Math.atan(h/w); f@0: double angleh = Math.atan(w/h); f@0: f@0: double a = w + h * Math.tan(angleh)/2; f@0: double b = h + w * Math.tan(anglew)/2; f@0: f@0: return new Rectangle2D.Double( f@0: r.getCenterX() - a, f@0: r.getCenterY() - b, f@0: 2*a, f@0: 2*b f@0: ); f@0: } f@0: f@0: @Override f@0: protected void reshapeInnerProperties(List insidePropertyTypes){ f@0: super.reshapeInnerProperties(insidePropertyTypes); f@0: eShape.setFrame(super.anyInsideProperties() ? getOutBounds(dataDisplayBounds) : 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: eShape.setFrame(super.anyInsideProperties() ? getOutBounds(dataDisplayBounds) : dataDisplayBounds ); 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: eShape.setFrame(eShape.getX() + dx, f@0: eShape.getY() + dy, f@0: eShape.getWidth(), f@0: eShape.getHeight()); f@0: super.translateImplementation(p,dx, dy); f@0: } f@0: f@0: @Override f@0: public Rectangle2D getBounds() { f@0: return eShape.getBounds2D(); 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 a = bounds.getWidth() / 2; f@0: double b = bounds.getHeight() / 2; f@0: double x = d.getX(); f@0: double y = d.getY(); f@0: double cx = bounds.getCenterX(); f@0: double cy = bounds.getCenterY(); f@0: f@0: if (a != 0 && b != 0 && !(x == 0 && y == 0)){ f@0: double t = Math.sqrt((x * x) / (a * a) + (y * y) / (b * b)); f@0: return new Point2D.Double(cx + x / t, cy + y / t); f@0: } f@0: else{ f@0: return new Point2D.Double(cx, cy); f@0: } f@0: } f@0: f@0: @Override f@0: public Shape getShape() { f@0: return eShape; f@0: } f@0: f@0: @Override f@0: public InputStream getSound(){ f@0: return sound; f@0: } f@0: f@0: @Override f@0: public Object clone(){ f@0: EllipticalNode n = (EllipticalNode)super.clone(); f@0: n.eShape = (Ellipse2D.Double)eShape.clone(); f@0: return n; f@0: } f@0: f@0: private Ellipse2D.Double eShape; f@0: private static InputStream sound; f@0: static{ f@0: sound = EllipticalNode.class.getResourceAsStream("audio/Ellipse.mp3"); f@0: SoundFactory.getInstance().loadSound(sound); f@0: } f@0: } f@0: