package research.figure;
import research.AbstractFigure;
import research.BoxHandleKit;
import research.connector.Connector;
import research.connector.BoxConnector;
import research.connector.ChopBoxConnector;
import java.awt.*;
import java.util.Vector;
/**
* Created by IntelliJ IDEA.
* User: zhangwei
* Date: 2004-7-3
* Time: 16:10:33
* To change this template use File | Settings | File Templates.
*/
public class DiamondFigure extends AbstractFigure {
private static final int CENTER = 0;
private static final int NORTH = 1;
private static final int SOUTH = 2;
private static final int WEST = 3;
private static final int EAST = 4;
public static final String DISPLAY_BOX = "displayBox";
protected transient Polygon polygon = null;
public static Polygon generateDiamond(Polygon polygon, Rectangle rect) {
if (polygon == null)
polygon = new Polygon();
else
polygon.reset();
polygon.addPoint(rect.x + rect.width / 2, rect.y);
polygon.addPoint(rect.x + rect.width, rect.y + rect.height / 2);
polygon.addPoint(rect.x + rect.width / 2, rect.y + rect.height);
polygon.addPoint(rect.x, rect.y + rect.height / 2);
return polygon;
}
public DiamondFigure() {
this(new Point(0, 0), new Point(4, 4));
}
public DiamondFigure(Point origin, Point corner) {
Rectangle displayBox = new Rectangle(origin);
displayBox.add(corner);
this.setAttribute(DISPLAY_BOX, displayBox);
polygon = generateDiamond(polygon, displayBox);
initConnectors();
}
protected void initConnectors() {
connector = new Connector[5];
connector[NORTH] = new BoxConnector(this, BoxConnector.NORTH);
connector[SOUTH] = new BoxConnector(this, BoxConnector.SOUTH);
connector[WEST] = new BoxConnector(this, BoxConnector.WEST);
connector[EAST] = new BoxConnector(this, BoxConnector.EAST);
connector[CENTER] = new ChopBoxConnector(this);
}
public void basicDisplayBox(Point origin, Point corner) {
Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
displayBox.setSize(0, 0);
displayBox.setLocation(origin);
displayBox.add(corner);
polygon = generateDiamond(polygon, displayBox);
this._setAttribute(DISPLAY_BOX, displayBox);
}
public Vector handles() {
Vector handles = new Vector();
BoxHandleKit.addHandles(this, handles);
return handles;
}
public Rectangle getDisplayBox() {
return getDisplayBox(null);
}
public Rectangle getDisplayBox(Rectangle r) {
if (r == null)
r = new Rectangle();
Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
r.setBounds(displayBox);
return r;
}
protected void basicMoveBy(int x, int y) {
Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
displayBox.translate(x, y);
polygon = generateDiamond(polygon, displayBox);
this._setAttribute(DISPLAY_BOX, displayBox);
}
public void drawBackground(Graphics g) {
Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
polygon = generateDiamond(polygon, displayBox);
g.fillPolygon(polygon);
}
public void drawFrame(Graphics g) {
Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
polygon = generateDiamond(polygon, displayBox);
g.drawPolygon(polygon);
}
public Connector connectorAt(int x, int y) {
Rectangle rect = getDisplayBox();
if (!rect.contains(x, y)) return null;
int index = findLocation(x, y);
if (index < 0) return null;
Connector[] array = getConnectors();
return array[index];
}
private int findLocation(int x, int y) {
Rectangle rect = getDisplayBox();
if (x < rect.x + rect.width / 3) {
if (y < rect.y + rect.height / 3) {
return -1;
} else if (y < rect.y + rect.height - rect.height / 3) {
return WEST;
} else {
return -1;
}
} else if (x < rect.x + rect.width - rect.width / 3) {
if (y < rect.y + rect.height / 3) {
return NORTH;
} else if (y < rect.y + rect.height - rect.height / 3) {
return CENTER;
} else {
return SOUTH;
}
} else {
if (y < rect.y + rect.height / 3) {
return -1;
} else if (y < rect.y + rect.height - rect.height / 3) {
return EAST;
} else {
return -1;
}
}
}
}