use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class LayoutDotExporter method setEdgeLabels.
@Override
protected void setEdgeLabels(final ElkEdge kedge, final List<Attribute> attributes, final boolean isVertical) {
super.setEdgeLabels(kedge, attributes, isVertical);
// set label distance and angle
if (Iterables.any(kedge.getLabels(), (ElkLabel label) -> {
EdgeLabelPlacement elp = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
return elp == EdgeLabelPlacement.HEAD || elp == EdgeLabelPlacement.TAIL;
})) {
double distance = kedge.getProperty(GraphvizMetaDataProvider.LABEL_DISTANCE);
if (distance >= 0.0) {
attributes.add(createAttribute(Attributes.LABELDISTANCE, distance));
}
double angle = kedge.getProperty(GraphvizMetaDataProvider.LABEL_ANGLE);
attributes.add(createAttribute(Attributes.LABELANGLE, angle));
}
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class DotExporter method applyEdgeLabelPos.
/**
* Applies the edge label positions for the given edge.
*
* @param elkedge edge for which labels are processed
* @param posString string with label position
* @param placement label placement to choose
* @param offsetx x offset added to positions
* @param offsety y offset added to positions
*/
private void applyEdgeLabelPos(final ElkEdge elkedge, final String posString, final EdgeLabelPlacement placement, final KVector offset) {
double combinedWidth = 0.0;
double combinedHeight = 0.0;
for (ElkLabel label : elkedge.getLabels()) {
EdgeLabelPlacement elp = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
if (elp == placement) {
combinedWidth = Math.max(combinedWidth, label.getWidth());
combinedHeight += label.getHeight();
}
}
try {
KVector pos = new KVector();
pos.parse(posString);
double xpos = pos.x - combinedWidth / 2 + offset.x;
double ypos = pos.y - combinedHeight / 2 + offset.y;
for (ElkLabel label : elkedge.getLabels()) {
EdgeLabelPlacement elp = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
if (elp == placement) {
double xoffset = (combinedWidth - label.getWidth()) / 2;
label.setX(xpos + xoffset);
label.setY(ypos);
ypos += label.getHeight();
}
}
} catch (IllegalArgumentException exception) {
// ignore exception
}
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class DotExporter method setEdgeLabels.
/**
* Set edge labels for the given edge.
*
* @param elkedge edge whose labels shall be set
* @param attributes edge attribute list to which the labels are added
* @param isVertical indicates whether vertical layout direction is active
*/
protected void setEdgeLabels(final ElkEdge elkedge, final List<Attribute> attributes, final boolean isVertical) {
if (elkedge.getLabels().isEmpty()) {
return;
}
// as Graphviz only supports positioning of one label per label placement, all labels
// are stacked to one big label as workaround
StringBuilder midLabel = new StringBuilder(), headLabel = new StringBuilder(), tailLabel = new StringBuilder();
String fontName = null;
int fontSize = 0;
boolean isCenterFontName = false, isCenterFontSize = false;
for (ElkLabel label : elkedge.getLabels()) {
StringBuilder buffer = midLabel;
EdgeLabelPlacement placement = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
boolean takeFontName = false, takeFontSize = false;
switch(placement) {
case HEAD:
takeFontName = fontName == null;
takeFontSize = fontSize <= 0;
buffer = headLabel;
break;
case TAIL:
takeFontName = fontName == null;
takeFontSize = fontSize <= 0;
buffer = tailLabel;
break;
default:
// CENTER
takeFontName = fontName == null || !isCenterFontName;
isCenterFontName = true;
takeFontSize = fontSize <= 0 || !isCenterFontSize;
isCenterFontSize = true;
break;
}
if (buffer.length() > 0) {
buffer.append("\n");
}
buffer.append(label.getText());
if (takeFontName) {
fontName = label.getProperty(CoreOptions.FONT_NAME);
}
if (takeFontSize) {
fontSize = label.getProperty(CoreOptions.FONT_SIZE);
// increase the font size to let Graphviz prepare more room for the label
if (fontSize > 0) {
fontSize *= FONT_SIZE_MULT;
}
}
}
// edge overlapping
if (midLabel.length() > 0) {
double labelSpacing = elkedge.getProperty(CoreOptions.SPACING_EDGE_LABEL);
if (labelSpacing < 1) {
labelSpacing = 0;
}
int charsToAdd = (int) labelSpacing - 1;
for (int i = 0; i < charsToAdd; i++) {
midLabel.append(isVertical ? "O" : "\nO");
}
attributes.add(createAttribute(Attributes.LABEL, createString(midLabel.toString())));
}
// set head label
if (headLabel.length() > 0) {
attributes.add(createAttribute(Attributes.HEADLABEL, createString(headLabel.toString())));
}
// set tail label
if (tailLabel.length() > 0) {
attributes.add(createAttribute(Attributes.TAILLABEL, createString(tailLabel.toString())));
}
// set font name
if (fontName != null && fontName.length() > 0) {
attributes.add(createAttribute(Attributes.FONTNAME, "\"" + fontName + "\""));
}
// set font size
if (fontSize > 0) {
attributes.add(createAttribute(Attributes.FONTSIZE, fontSize));
}
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class DotImporter method transformEdge.
/**
* Transforms an edge.
*
* @param statement an edge statement
* @param parent the parent node
* @param transData the transformation data instance
* @param defaultProps default values for edge options
*/
private void transformEdge(final EdgeStatement statement, final ElkNode parent, final IDotTransformationData<GraphvizModel, ElkNode> transData, final IPropertyHolder defaultProps) {
String sourceName = statement.getSourceNode().getName();
ElkNode source = transformNode(sourceName, parent, transData);
ElkPort sourcePort = null;
if (statement.getSourceNode().getPort() != null) {
String portName = statement.getSourceNode().getPort().getCompass_pt();
if (portName == null) {
portName = statement.getSourceNode().getPort().getName();
}
sourcePort = transformPort(portName, source, transData);
}
ListIterator<EdgeTarget> targetIter = statement.getEdgeTargets().listIterator();
while (targetIter.hasNext()) {
EdgeTarget edgeTarget = targetIter.next();
ElkEdge elkedge = ElkGraphUtil.createEdge(null);
if (sourcePort != null) {
elkedge.getSources().add(sourcePort);
} else {
elkedge.getSources().add(source);
}
ElkNode target;
ElkPort targetPort = null;
Node edgeTargetNode = edgeTarget.getTargetnode();
String targetName;
if (edgeTargetNode == null) {
targetName = edgeTarget.getTargetSubgraph().getName();
target = transformNode(targetName, parent, transData);
} else {
targetName = edgeTargetNode.getName();
target = transformNode(targetName, parent, transData);
if (edgeTargetNode.getPort() != null) {
String portName = edgeTargetNode.getPort().getCompass_pt();
if (portName == null) {
portName = edgeTargetNode.getPort().getName();
}
targetPort = transformPort(portName, target, transData);
}
}
if (targetPort != null) {
elkedge.getTargets().add(targetPort);
} else {
elkedge.getTargets().add(target);
}
elkedge.copyProperties(defaultProps);
if (targetIter.previousIndex() == 0) {
// this is the first target - just store the edge statement
elkedge.setProperty(PROP_STATEMENT, statement);
} else {
// the edge statement has more that one target - create a copy
EdgeStatement newStatement = DotFactory.eINSTANCE.createEdgeStatement();
Node sourceNode = DotFactory.eINSTANCE.createNode();
sourceNode.setName(sourceName);
newStatement.setSourceNode(sourceNode);
targetIter.remove();
newStatement.getEdgeTargets().add(edgeTarget);
for (Attribute attr : statement.getAttributes()) {
newStatement.getAttributes().add(EcoreUtil.copy(attr));
}
elkedge.setProperty(PROP_STATEMENT, newStatement);
}
// evaluate attributes for the new edge
for (Attribute attr : statement.getAttributes()) {
String value = trimValue(attr);
if (Attributes.LABEL.equals(attr.getName())) {
ElkLabel label = ElkGraphUtil.createLabel(value, elkedge);
label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.CENTER);
} else if (Attributes.HEADLABEL.equals(attr.getName())) {
ElkLabel label = ElkGraphUtil.createLabel(value, elkedge);
label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.HEAD);
} else if (Attributes.TAILLABEL.equals(attr.getName())) {
ElkLabel label = ElkGraphUtil.createLabel(value, elkedge);
label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.TAIL);
} else {
transformAttribute(elkedge, attr, transData);
}
}
// the edge target is the source for the next edge target
source = target;
sourceName = targetName;
sourcePort = targetPort;
}
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class RandomGraphGenerator method addEdgeLabel.
/**
* Creates a {@link KLabel} with random text and adds it to the edge.
*
* @param edge
* the edge to add the label to.
*/
private void addEdgeLabel(final ElkEdge edge) {
ElkLabel label = ElkGraphUtil.createLabel(BlindTextGenerator.generate(), edge);
edge.getLabels().add(label);
}
Aggregations