/** * Places the given end label of the given edge starting at the given node. * * @param node source node of the edge. * @param edge the edge whose end label to place. * @param label the end label to place. * @param labelSpacing space between objects and labels. */ private void placeEndLabel( final LNode node, final LEdge edge, final LLabel label, final double labelSpacing) { // Get the nearest port (source port for tail labels, target port for head labels) LPort port = null; if (label.getProperty(LayeredOptions.EDGE_LABELS_PLACEMENT) == EdgeLabelPlacement.TAIL) { port = edge.getSource(); } else if (label.getProperty(LayeredOptions.EDGE_LABELS_PLACEMENT) == EdgeLabelPlacement.HEAD) { port = edge.getTarget(); } // Initialize offset with zero if no offset was present if (!northOffset.containsKey(port.getNode())) { northOffset.put(port.getNode(), 0.0); } if (!southOffset.containsKey(port.getNode())) { southOffset.put(port.getNode(), 0.0); } if (!portLabelOffsetHint.containsKey(port)) { portLabelOffsetHint.put(port, 0.0); } // Calculate end label position based on side choice // Port side undefined can be left out, because there would be no reasonable // way of handling them if (label.getProperty(InternalProperties.LABEL_SIDE) == LabelSide.ABOVE) { placeEndLabelUpwards(node, label, port, labelSpacing); } else { placeEndLabelDownwards(node, label, port, labelSpacing); } }
/** * Places the given end label below the edge. * * @param node source node of the edge the label belongs to. * @param label the label to place. * @param port the end port of the edge the label is nearest to. * @param labelSpacing space between objects and labels. */ private void placeEndLabelUpwards( final LNode node, final LLabel label, final LPort port, final double labelSpacing) { // Remember some stuff KVector labelPosition = label.getPosition(); KVector absolutePortPosition = KVector.sum(port.getPosition(), port.getNode().getPosition()); KVector absolutePortAnchor = port.getAbsoluteAnchor(); LMargin portMargin = port.getMargin(); // Actually calculate the coordinates switch (port.getSide()) { case WEST: labelPosition.x = Math.min(absolutePortPosition.x, absolutePortAnchor.x) - portMargin.left - label.getSize().x - labelSpacing; labelPosition.y = port.getAbsoluteAnchor().y - label.getSize().y - labelSpacing; break; case EAST: labelPosition.x = Math.max(absolutePortPosition.x + port.getSize().x, absolutePortAnchor.x) + portMargin.right + labelSpacing; labelPosition.y = port.getAbsoluteAnchor().y - label.getSize().y - labelSpacing; break; case NORTH: labelPosition.x = port.getAbsoluteAnchor().x + labelSpacing; labelPosition.y = Math.min(absolutePortPosition.y, absolutePortAnchor.y) - portMargin.top - label.getSize().y - labelSpacing; break; case SOUTH: labelPosition.x = port.getAbsoluteAnchor().x + labelSpacing; labelPosition.y = Math.max(absolutePortPosition.y + port.getSize().y, absolutePortAnchor.y) + portMargin.bottom + labelSpacing; break; } }