package de.FeatureModellingTool.GraphicalEditor;
import research.AbstractFigure;
import research.BoxHandleKit;
import research.RelativeLocator;
import research.NullHandle;
import research.connector.Connector;
import research.connector.BoxConnector;
import research.connector.ChopBoxConnector;
import java.awt.*;
import java.util.Vector;
/**
* Created by IntelliJ IDEA.
* User: saturn
* Date: 2003-9-24
* Time: 22:12:01
* To change this template use Options | File Templates.
*/
public class PLFigure extends AbstractFigure implements ConstraintFigure {
protected static BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
private static final int LEFT = 0;
private static final int RIGHT = 1;
//protected Rectangle box = null;
public final static Integer L2R_REQUIRE = new Integer(0);
public final static Integer R2L_REQUIRE = new Integer(1);
public final static Integer MUTEX = new Integer(2);
public final static Integer EQUIVALENCE = new Integer(3);
public final static String Multi = "Multi";
public final static String Single = "Single";
public final static String All = "All";
protected static String PLType = "PLType";
protected static String SourceType = "SourceType";
protected static String SinkType = "SinkType";
protected Vector handles = null;
public PLFigure() {
this(new Point(0, 0));
}
public PLFigure(Point position) {
Rectangle box = new Rectangle(position.x, position.y, 60, 30);
setAttribute("bounds", box);
setAttribute("frameColor", new Color(102, 102, 255));
setAttribute(PLType, L2R_REQUIRE);
setAttribute(SourceType, Single);
setAttribute(SinkType, Single);
initConnectors();
}
protected void initConnectors() {
connector = new Connector[2];
connector[LEFT] = new BoxConnector(this, BoxConnector.WEST);
connector[RIGHT] = new BoxConnector(this, BoxConnector.EAST);
}
public Connector connectorAt(int x, int y) {
Rectangle box = (Rectangle) getAttribute("bounds");
int centerX = box.x + box.width / 2;
if (x <= centerX)
return connector[LEFT];
else
return connector[RIGHT];
}
public Rectangle getDisplayBox() {
Rectangle box = (Rectangle) getAttribute("bounds");
return (Rectangle) box.clone();
}
public Rectangle getDisplayBox(Rectangle r) {
if (r == null)
r = new Rectangle();
Rectangle box = (Rectangle) getAttribute("bounds");
r.setBounds(box);
return r;
}
protected void basicMoveBy(int x, int y) {
Rectangle box = (Rectangle) getAttribute("bounds");
box.translate(x, y);
setAttribute("bounds", box);
}
public Vector handles() {
if (handles == null) {
handles = new Vector();
handles.addElement(new NullHandle(this, RelativeLocator.northWest()));
handles.addElement(new NullHandle(this, RelativeLocator.northEast()));
handles.addElement(new NullHandle(this, RelativeLocator.southEast()));
handles.addElement(new NullHandle(this, RelativeLocator.southWest()));
}
return (Vector) handles.clone();
}
public void basicDisplayBox(Point origin, Point corner) {
Rectangle box = (Rectangle) getAttribute("bounds");
box = new Rectangle(origin);
box.add(corner);
setAttribute("bounds", box);
}
public void drawFrame(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Stroke old = g2d.getStroke();
Rectangle displayBox = getDisplayBox();
Rectangle r = new Rectangle(displayBox.x + displayBox.width / 4, displayBox.y, displayBox.width / 2, displayBox.height);
g2d.setStroke(stroke);
g.drawOval(r.x, r.y, r.width, r.height);
Integer plType = (Integer) getAttribute(PLType);
String sourceType = (String) getAttribute(SourceType);
String sinkType = (String) getAttribute(SinkType);
if (plType == null)
plType = L2R_REQUIRE;
g.drawLine(r.x + r.width / 4, r.y + r.height / 2, r.x + r.width - r.width / 4, r.y + r.height / 2);
int x0 = r.x + r.width / 4;
int x1 = r.x + r.width * 3 / 8;
int x2 = r.x + r.width - r.width * 3 / 8;
int x3 = r.x + r.width - r.width / 4;
int y0 = r.y + r.height / 2;
int y1 = r.y + r.height / 2 - r.height / 8;
int y2 = r.y + r.height / 2 + r.height / 8;
if (plType.intValue() == L2R_REQUIRE.intValue()) {
g.drawLine(x2, y1, x3, y0);
g.drawLine(x2, y2, x3, y0);
} else if (plType.intValue() == R2L_REQUIRE.intValue()) {
g.drawLine(x1, y1, x0, y0);
g.drawLine(x1, y2, x0, y0);
} else if (plType.intValue() == MUTEX.intValue()) {
g.drawLine(x1, y1, x2, y2);
g.drawLine(x2, y1, x1, y2);
} else if (plType.intValue() == EQUIVALENCE.intValue()) {
g.drawLine(x2, y1, x3, y0);
g.drawLine(x2, y2, x3, y0);
g.drawLine(x1, y1, x0, y0);
g.drawLine(x1, y2, x0, y0);
}
g.drawLine(displayBox.x, displayBox.y + displayBox.height / 2,
displayBox.x + displayBox.width / 4, displayBox.y + displayBox.height / 2);
g.drawLine(displayBox.x + displayBox.width, displayBox.y + displayBox.height / 2,
displayBox.x + displayBox.width - displayBox.width / 4, displayBox.y + displayBox.height / 2);
Point left = new Point(displayBox.x, displayBox.y + displayBox.height / 2);
Point right = new Point(displayBox.x + displayBox.width, displayBox.y + displayBox.height / 2);
g.drawRect(left.x - 1, left.y - 1, 2, 2);
g.drawRect(right.x - 1, right.y - 1, 2, 2);
//
Rectangle lR = new Rectangle(displayBox.x, displayBox.y, displayBox.width / 4, displayBox.height / 2);
Rectangle rR = new Rectangle(displayBox.x + displayBox.width - displayBox.width / 4, displayBox.y, displayBox.width / 4, displayBox.height / 2);
lR.width /= 2;
lR.height /= 2;
rR.width /= 2;
rR.height /= 2;
rR.x += displayBox.x + displayBox.width - (rR.x + rR.width);
lR.translate(lR.width * 3 / 4, lR.height * 3 / 4);
rR.translate(-rR.width * 3 / 4, rR.height * 3 / 4);
Color textColor = (Color) getAttribute("textColor");
Color oldC = g.getColor();
g.setColor(textColor);
if (sourceType.equals(Single)) {
draw1(g, lR);
} else {
lR.width -= lR.width / 3;
lR.x += lR.width / 3;
if (sourceType.equals(Multi))
drawV(g, lR);
else
drawA(g, lR);
}
if (sinkType.equals(Single)) {
draw1(g, rR);
} else {
rR.width -= rR.width / 3;
rR.x += rR.width / 3;
if (sinkType.equals(Multi))
drawV(g, rR);
else
drawA(g, rR);
}
g.setColor(oldC);
//
g2d.setStroke(old);
}
protected static void draw1(Graphics g, Rectangle r) {
g.drawLine(r.x + r.width - r.width / 2, r.y, r.x + r.width / 2, r.y + r.height);
g.drawLine(r.x + r.width - r.width / 2, r.y, r.x + r.width - r.width / 2 - r.width / 3, r.y + r.height / 3);
g.drawLine(r.x + r.width / 2 - r.width / 3, r.y + r.height, r.x + r.width / 2 + r.width / 3, r.y + r.height);
}
protected static void drawN(Graphics g, Rectangle r) {
g.drawLine(r.x + r.width - r.width / 2 * 2, r.y, r.x, r.y + r.height);
g.drawLine(r.x + r.width, r.y, r.x + r.width / 2 * 2, r.y + r.height);
g.drawLine(r.x + r.width - r.width / 2 * 2, r.y, r.x + r.width / 2 * 2, r.y + r.height);
}
protected static void drawV(Graphics g, Rectangle r) {
g.drawLine(r.x, r.y, r.x + r.width/2, r.y + r.height);
g.drawLine(r.x + r.width, r.y, r.x + r.width/2, r.y + r.height);
}
protected static void drawA(Graphics g, Rectangle r) {
g.drawLine(r.x + r.width - r.width / 2, r.y, r.x, r.y + r.height);
g.drawLine(r.x + r.width - r.width / 2, r.y, r.x + r.width, r.y + r.height);
}
}