for (Item item : previewModel.getItems(Item.EDGE_LABEL)) {
Edge edge = (Edge) item.getSource();
Item edgeItem = previewModel.getItem(Item.EDGE, edge);
EdgeColor edgeColor = (EdgeColor) properties.getValue(PreviewProperty.EDGE_COLOR);
NodeItem sourceItem = (NodeItem) edgeItem.getData(EdgeRenderer.SOURCE);
NodeItem targetItem = (NodeItem) edgeItem.getData(EdgeRenderer.TARGET);
Color color = edgeColor.getColor((Color) item.getData(EdgeItem.COLOR),
(Color) sourceItem.getData(NodeItem.COLOR),
(Color) targetItem.getData(NodeItem.COLOR));
item.setData(EDGE_COLOR, color);
if (edge.isSelfLoop()) {
//Middle
Float x = sourceItem.getData(NodeItem.X);
Float y = sourceItem.getData(NodeItem.Y);
Float size = sourceItem.getData(NodeItem.SIZE);
Vector v1 = new Vector(x, y);
v1.add(size, -size);
Vector v2 = new Vector(x, y);
v2.add(size, size);
Vector middle = bezierPoint(x, y, v1.x, v1.y, v2.x, v2.y, x, y, 0.5f);
item.setData(LABEL_X, middle.x);
item.setData(LABEL_Y, middle.y);
} else if (properties.getBooleanValue(PreviewProperty.EDGE_CURVED)) {
//Middle of the curve
Float x1 = sourceItem.getData(NodeItem.X);
Float x2 = targetItem.getData(NodeItem.X);
Float y1 = sourceItem.getData(NodeItem.Y);
Float y2 = targetItem.getData(NodeItem.Y);
//Curved edgs
Vector direction = new Vector(x2, y2);
direction.sub(new Vector(x1, y1));
float length = direction.mag();
direction.normalize();
float factor = properties.getFloatValue(EdgeRenderer.BEZIER_CURVENESS) * length;
// normal vector to the edge
Vector n = new Vector(direction.y, -direction.x);
n.mult(factor);
// first control point
Vector v1 = new Vector(direction.x, direction.y);
v1.mult(factor);
v1.add(new Vector(x1, y1));
v1.add(n);
// second control point
Vector v2 = new Vector(direction.x, direction.y);
v2.mult(-factor);
v2.add(new Vector(x2, y2));
v2.add(n);
Vector middle = bezierPoint(x1, y1, v1.x, v1.y, v2.x, v2.y, x2, y2, 0.5f);
item.setData(LABEL_X, middle.x);
item.setData(LABEL_Y, middle.y);
} else {
Float x = ((Float) sourceItem.getData(NodeItem.X) + (Float) targetItem.getData(NodeItem.X)) / 2f;
Float y = ((Float) sourceItem.getData(NodeItem.Y) + (Float) targetItem.getData(NodeItem.Y)) / 2f;
item.setData(LABEL_X, x);
item.setData(LABEL_Y, y);
}
}