for (String nodeType : nodeTypes) {
NodeList nodes = documentElement.getElementsByTagName(nodeType);
for (int i = 0; i < nodes.getLength(); i++) {
Element node = (Element) nodes.item(i);
try {
StateNode sn = new StateNode();
String gval = node.getAttribute("g");
String[] vals = gval.split(",", 4);
int x = Integer.parseInt(vals[0]);
int y = Integer.parseInt(vals[1]);
int w = Integer.parseInt(vals[2]);
int h = Integer.parseInt(vals[3]);
sn.setX(x);
sn.setY(y);
sn.setWidth(w);
sn.setHeight(h);
sn.setNodeType(nodeType);
String name = node.getAttribute("name");
sn.setLabel(name);
res.put(name, sn);
if ("start".equals(nodeType)) {
res.put("__AWF__start_node", sn);
}
loger.fine("Found node" + name + ": " + x + "," + y + "," + w + "," + h);
} catch (Exception e) {
loger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
//once again - for transitions
for (String nodeType : nodeTypes) {
NodeList nodes = documentElement.getElementsByTagName(nodeType);
for (int i = 0; i < nodes.getLength(); i++) {
Element node = (Element) nodes.item(i);
try {
String startNodeName = node.getAttribute("name");
StateNode startNode = (StateNode) res.get(startNodeName);
if (startNode == null) {
loger.severe("Start node " + startNodeName +
" has not been localized, skipping transition drawing too.");
continue;
}
NodeList transitions = node.getElementsByTagName("transition");
for (int j=0; j < transitions.getLength(); j++) {
Element transitionEl = (Element) transitions.item(j);
String name = transitionEl.getAttribute("name");
String to = transitionEl.getAttribute("to") ;
StateNode endNode = (StateNode) res.get(to);
if (endNode == null) {
loger.severe("End node " + to + " has not been localized for transition " + name +
" of node " + startNodeName + ", skipping transition drawing.");
continue;
}
String g = transitionEl.getAttribute("g");
if (g != null) {
String[] dockersAndDistances = g.split(":");
String[] dockers = new String[0];
if (dockersAndDistances.length == 2) {
dockers = dockersAndDistances[0].split(";");//what the other numbers mean - I have no idea...
}
//calculate line start node which is a center of the start node
int startX = startNode.getX() + startNode.getWidth()/2;
int startY = startNode.getY() + startNode.getHeight()/2;
//and the same for end node
int endX = endNode.getX() + endNode.getWidth()/2;
int endY = endNode.getY() + endNode.getHeight()/2;
TransitionArc arc = new TransitionArc();
arc.setName(name);
arc.addPoint(startX, startY);
for (String docker : dockers) {
String[] split = docker.split(",",2);
arc.addPoint(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
}
arc.addPoint(endX, endY);
double a;//remember about vertical line
double b;
endX = arc.getPath().get(1).getX();
endY = arc.getPath().get(1).getY();
if (startX - endX == 0) { //whoa - vertical line - simple case, but requires special approach
if (endY > startNode.getY()+startNode.getHeight()) { //below
startY = startNode.getY()+startNode.getHeight();
} else {
startY = startNode.getY();
}
} else {
a = ((double)(startY-endY))/((double)(startX - endX));
b = (double)startY - (double)startX*a;
for (int x = startX; x <= endX; x++) {
int y = (int) Math.round(a*x+b);
boolean inside = false;
if (x >= startNode.getX() && x <= startNode.getX() + startNode.getWidth()) {
if (y >= startNode.getY() && y <= startNode.getY() + startNode.getHeight()) {
inside = true;
}
}
if (!inside) {
startX = x;
startY = y;
break;
}
}
for (int x = startX; x > endX; x--) {
int y = (int) Math.round(a*x+b);
boolean inside = false;
if (x >= startNode.getX() && x <= startNode.getX() + startNode.getWidth()) {
if (y >= startNode.getY() && y <= startNode.getY() + startNode.getHeight()) {
inside = true;
}
}
if (!inside) {
startX = x;
startY = y;
break;
}
}
}
arc.getPath().get(0).setX(startX);
arc.getPath().get(0).setY(startY);
endX = arc.getPath().get(arc.getPath().size()-1).getX();
endY = arc.getPath().get(arc.getPath().size()-1).getY();
startX = arc.getPath().get(arc.getPath().size()-2).getX();
startY = arc.getPath().get(arc.getPath().size()-2).getY();
if (startX - endX == 0) { //whoa - vertical line - simple case, but requires special approach
if (startY > endNode.getY()+endNode.getHeight()) { //below
endY = endNode.getY()+endNode.getHeight();
} else {
endY = endNode.getY();
}
} else {
a = ((double)(startY-endY))/((double)(startX - endX));//remember about vertical line
//startY = startX*a+b
b = (double)startY - (double)startX*a;
for (int x = endX; x <= startX; x++) {
int y = (int) Math.round(a*x+b);
boolean inside = false;
if (x >= endNode.getX() && x <= endNode.getX() + endNode.getWidth()) {
if (y >= endNode.getY() && y <= endNode.getY() + endNode.getHeight()) {
inside = true;
}
}
if (!inside) {
endX = x;
endY = y;
break;
}
}
for (int x = endX; x > startX; x--) {
int y = (int) Math.round(a*x+b);
boolean inside = false;
if (x >= endNode.getX() && x <= endNode.getX() + endNode.getWidth()) {
if (y >= endNode.getY() && y <= endNode.getY() + endNode.getHeight()) {
inside = true;
}
}
if (!inside) {
endX = x;