Professional Documents
Culture Documents
net.e2ser.petri.Color;
net.e2ser.petri.Editor;
net.e2ser.petri.RobPoint;
net.e2ser.petri.SimulatorMode;
net.e2ser.util.XMLWriter;
android.graphics.Canvas;
android.graphics.Rect;
android.graphics.drawable.ShapeDrawable;
/**
*
* <p>Title: Petri Net Editor/Simulator</p>
* <p>Description: A 2D node class</p>
* <p>Copyright: Copyright (c) 2009</p>
* <p>Company: </p>
* @author Rob Esser
* @version 1.0
*/
abstract public class AbstractComponent extends AbstractElement {
private static final ArrayList<AbstractComponent> EMPTYLIST = new ArrayList<Ab
stractComponent>();
private static final int DefaultComponentWidth = 20;
private static final int DefaultComponentHeight = 20;
private Rect componentBounds;
private float textSize = 6.0f;
protected ShapeDrawable shape;
protected String name = null; // the name of the element
protected boolean inverse = false;
public RobComponent parent = null;
protected int fillColor = Color.GRAY;
public AbstractComponent(RobComponent parent, int x, int y) {
this.parent = parent;
componentBounds = new Rect(x, y, x + DefaultComponentWidth, y + DefaultCompo
nentHeight);
}
/**
* make myself noticeable
* @param delay int
*/
public void blink(int delay) {
// force myself to blink
inverse = true;
parent.changed(this);
if (delay > 0) {
try {Thread.sleep(delay);
} catch (InterruptedException e) {}
} else {
//invalid delay setting
}
inverse = false;
parent.changed(this);
}
/**
* add a token - default do nothing
*/
public void addToken() {
}
/**
* remove a token - default do nothing
*/
public void removeToken() {
}
/**
* fire the node - by default do nothing
* @param mode SimulatorMode
* @param animation boolean
* @param delay int
*/
public void fire(SimulatorMode mode, boolean animation, int delay) {
}
/**
* draw myself on the Canvas
* @param canvas where to draw
*/
abstract public void draw(Canvas canvas);
/**
* Draw a marker to indicate that the node is selected
* @param canvas where to draw
*/
public void drawSelected(Canvas canvas) {
Rect r = new Rect(shape.getBounds());
shape.getPaint().setColor(Color.darker(Color.darker(fillColor)));
r.inset(r.width() / 3, r.height() / 3);
canvas.drawRect(r, shape.getPaint());
}
/**
* return a point on my circumference to which an arc is joined
* @param otherEnd RobPoint
* @return RobPoint
*/
abstract RobPoint connectionPoint(RobPoint otherEnd);
/**
* Return the top of this hierarchy
* @return RobComponent
*/
public RobComponent topNode() {
// return the root node
return parent.topNode();
}
/**
* Can a conection from node be connected to me?
* @param node AbstractComponent
* @return boolean
*/
public boolean canAddConnection(AbstractComponent node) {
// can always start a connection from me
return true;
}
/**
* Only can add a connection if nodes are of different classes!
* @param node1 AbstractComponent
* @param node2 AbstractComponent
* @return boolean
*/
public boolean canAddConnection(AbstractComponent node1, AbstractComponent nod
e2) {
return !node1.getClass().equals(node2.getClass());
}
/**
* Move the node origin
* @param x double
* @param y double
*/
public void move(double x, double y) {
Rect r = shape.getBounds();
int newX = Math.max((int) x, 0);
int newY = Math.max((int) y, 0);
r.offsetTo(newX, newY);
shape.setBounds(r);
componentBounds.offsetTo((int) (newX / Editor.DisplayScale), (int) (ne
wY / Editor.DisplayScale));
}
/**
* does my shape contain the point
* @param x int
* @param y int
* @return boolean
*/
public boolean contains(int x, int y) {
return shape.getBounds().contains(x, y);
}
/**
return EMPTYLIST;
}
/**
* return all inputs of myself
* @return Vector
*/
public ArrayList<AbstractComponent> allInputs() {
return parent.allInputs(this);
}
/**
* return all outputs of myself
* @return Vector
*/
public ArrayList<AbstractComponent> allOutputs() {
return parent.allOutputs(this);
}
/**
* return all outputs of myself that are not also inputs
* @return Vector
*/
public ArrayList<AbstractComponent> onlyOutputs() {
return parent.onlyOutputs(this);
}
public int componentWidth() {
return componentBounds.width();
}
public int componentHeight() {
return componentBounds.height();
}
/**
* Output an XML representation of myself
* @param writer object that represents writer state
* @throws IOException
*/
abstract public void toXML(XMLWriter writer) throws IOException;
}