/* * Created on Mar 6, 2006 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package implementation.source.java.nodes; import java.awt.geom.Point2D; /** * @author Timothy Ober * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public abstract class DFPort extends DFEndpoint { final int ARROW_LENGTH = 10; final double ARROW_WIDTH = Math.PI / 3; DFNode node; Point2D.Double position; DFLabel label; DFEdge edge; boolean externalPort; /** * */ public DFPort(DFNode nd) { super(); node = nd; node.addChild(this); position = new Point2D.Double(node.getX() + node.getWidth(), node.getY() + (node.getHeight() / 2)); drawPort(); label = new DFLabel("A port"); label.setX(getX()); label.setY(getY() + getHeight()); externalPort = false; } public void showLabel(boolean show) { if (show) { if (!getChildrenReference().contains(label)) { addChild(label); } } else { if (getChildrenReference().contains(label)) { removeChild(label); } } } public void drawPort() { drawArrow((float)(node.getX() + (node.getWidth() / 2)), (float)(node.getY() + (node.getHeight() / 2)), (float)position.x, (float)position.y); } protected abstract void drawArrow(float x0, float y0, float x1, float y1); public DFNode getNode() {return node;} public void updatePosition(float x, float y) { float x0 = (float)node.getX() + (float)node.getWidth() / 2; float y0 = (float)node.getY() + (float)node.getHeight() / 2; float o = y - y0; float a = x - x0; double h = Math.sqrt(Math.pow(a, 2) + Math.pow(o, 2)); if (h == 0) { return; } double xPos = ((node.getWidth() / 2) * (a / h)); double yPos = ((node.getHeight() / 2) * (o / h)); position.x = node.getX() + (node.getWidth() / 2) + xPos; position.y = node.getY() + (node.getHeight() / 2) + yPos; setX(position.x); setY(position.y); drawPort(); } public void setEdge(DFEdge e) {edge = e;} public DFEdge getEdge() {return edge;} public void setLabel(String s) {label.setText(s);} public String getLabel() {return label.getText();} public void setOutputPort(boolean s) {externalPort = s;} public boolean isOutputPort() {return externalPort;} }