/*
* @(#)RectangleFigure.java
*
* Project: JHotdraw - a GUI framework for technical drawings
* http://www.jhotdraw.org
* http://jhotdraw.sourceforge.net
* Copyright: ? by the original author(s) and all contributors
* License: Lesser GNU Public License (LGPL)
* http://www.opensource.org/licenses/lgpl-license.html
*/
package research.figure;
import java.awt.*;
import java.io.IOException;
import java.util.Vector;
import research.*;
import research.connector.BoxConnector;
import research.connector.ChopBoxConnector;
import research.connector.Connector;
import research.store.StorableOutput;
import research.store.StorableInput;
/**
* A rectangle figure.
*
* @version <$CURRENT_VERSION$>
*/
public class RectangleFigure 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;
private static final int NORTH_WEST = 5;
private static final int NORTH_EAST = 6;
private static final int SOUTH_WEST = 7;
private static final int SOUTH_EAST = 8;
private Rectangle fDisplayBox;
/*
* Serialization support.
*/
private static final long serialVersionUID = 184722075881789163L;
private int rectangleFigureSerializedDataVersion = 1;
public RectangleFigure() {
this(new Point(0, 0), new Point(4, 4));
}
public RectangleFigure(Point origin, Point corner) {
basicDisplayBox(origin, corner);
initConnectors();
}
protected void initConnectors() {
connector = new Connector[9];
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[NORTH_WEST] = new BoxConnector(this, BoxConnector.NORTH_WEST);
connector[NORTH_EAST] = new BoxConnector(this, BoxConnector.NORTH_EAST);
connector[SOUTH_WEST] = new BoxConnector(this, BoxConnector.SOUTH_WEST);
connector[SOUTH_EAST] = new BoxConnector(this, BoxConnector.SOUTH_EAST);
connector[CENTER] = new ChopBoxConnector(this);
}
public void basicDisplayBox(Point origin, Point corner) {
if (fDisplayBox == null)
fDisplayBox = new Rectangle();
fDisplayBox.setSize(0,0);
fDisplayBox.setLocation(origin);
fDisplayBox.add(corner);
}
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();
r.setBounds(fDisplayBox);
return r;
}
protected void basicMoveBy(int x, int y) {
fDisplayBox.translate(x, y);
}
public void drawBackground(Graphics g) {
Rectangle r = getDisplayBox();
g.fillRect(r.x, r.y, r.width, r.height);
}
public void drawFrame(Graphics g) {
Rectangle r = getDisplayBox();
g.drawRect(r.x, r.y, r.width, r.height);
}
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 NORTH_WEST;
} else if (y < rect.y + rect.height - rect.height / 3) {
return WEST;
} else {
return SOUTH_WEST;
}
} 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 NORTH_EAST;
} else if (y < rect.y + rect.height - rect.height / 3) {
return EAST;
} else {
return SOUTH_EAST;
}
}
}
//-- store / load ----------------------------------------------
public void write(StorableOutput dw) {
super.write(dw);
dw.writeInt(fDisplayBox.x);
dw.writeInt(fDisplayBox.y);
dw.writeInt(fDisplayBox.width);
dw.writeInt(fDisplayBox.height);
}
public void read(StorableInput dr) throws IOException {
super.read(dr);
fDisplayBox = new Rectangle(dr.readInt(),
dr.readInt(),
dr.readInt(),
dr.readInt());
}
}