use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class ElkGraphTransformer method importGraph.
// /////////////////////////////////////////////////////////////////////////////
// Implementation of interface methods
@Override
public DCGraph importGraph(final ElkNode graph) {
parent = graph;
// Split graph into components
List<List<ElkNode>> components = ElkGraphComponentsProcessor.split(graph);
// This list of lists is used to construct the DCGraph from its smallest units, so called DCElements.
List<List<DCElement>> result = Lists.newArrayList();
for (List<ElkNode> component : components) {
// Each of the subResult lists will represent a connected component in the DCGraph.
List<DCElement> subResult = Lists.newArrayList();
result.add(subResult);
Set<ElkEdge> edgeSet = Sets.newHashSet();
for (ElkNode node : component) {
// "true" indicates - we need to consider the position of ElkNodes when applying the new layout of the
// DCGraph back to the KGraph.
DCElement componentNode = importElkShape(node, true, 0.0f, 0.0f);
subResult.add(componentNode);
// Compute offset for labels (used for computing absolute position)
double nodeX = node.getX();
double nodeY = node.getY();
// For use in the Debugview only
componentNode.setParentCoords(new KVector(nodeX, nodeY));
// next look at all labels of this ElkNode ...
List<ElkLabel> labels = node.getLabels();
for (ElkLabel label : labels) {
// "false" indicates - ElkLabels belonging to nodes have coordinates relative to their ElkNode. So
// don't
// adjust their position when applying the layout of the DCGraph back to the KGraph.
DCElement componentLabel = importElkShape(label, false, nodeX, nodeY);
subResult.add(componentLabel);
}
// ... then the ports of the ElkNode
List<ElkPort> ports = node.getPorts();
for (ElkPort port : ports) {
// "false" - ElkPorts have coordinates relative to their ElkNode
DCElement componentPort = importElkShape(port, false, nodeX, nodeY);
subResult.add(componentPort);
// ElkPorts can have labels, too!
// Compute offset for labels of ElkPorts
double portX = port.getX() + nodeX;
double portY = port.getY() + nodeY;
labels = port.getLabels();
for (ElkLabel label : labels) {
// false - ElkLabels have coordinates relative to their ElkPort
DCElement componentLabel = importElkShape(label, false, portX, portY);
subResult.add(componentLabel);
}
}
edgeSet.addAll(Sets.newHashSet(ElkGraphUtil.allIncidentEdges(node)));
}
importElkEdges(edgeSet, subResult);
}
// Finally create the DCGraph
transformedGraph = new DCGraph(result, componentSpacing / 2);
// Don't forget to copy properties of parent ElkNode to the DCGraph
transformedGraph.copyProperties(graph);
return transformedGraph;
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class DisCoGraphRenderer method renderPort.
/**
* Paints a port for the given dirty area.
*
* @param port
* the port to paint
* @param graphics
* the graphics context used to paint
* @param area
* dirty area that needs painting
* @param offset
* offset to be added to relative coordinates
* @param labelAlpha
* alpha value for labels
*/
private void renderPort(final ElkPort port, final GC graphics, final Rectangle area, final KVector offset, final int labelAlpha) {
PaintRectangle rect = boundsMap.get(port);
if (rect == null) {
rect = new PaintRectangle(port, offset, getScale());
boundsMap.put(port, rect);
}
if (!rect.painted && rect.intersects(area)) {
// CHECKSTYLEOFF MagicNumber
graphics.setAlpha(255);
if (configurator.getPortFillColor() != null) {
graphics.setBackground(configurator.getPortFillColor());
graphics.fillRectangle(rect.x, rect.y, rect.width, rect.height);
}
if (configurator.getPortBorderColor() != null) {
graphics.setForeground(configurator.getPortBorderColor());
graphics.drawRectangle(rect.x, rect.y, rect.width, rect.height);
}
rect.painted = true;
}
// paint port labels
if (configurator.getPortLabelFont() != null) {
graphics.setFont(configurator.getPortLabelFont());
KVector portOffset = new KVector(rect.x, rect.y);
for (ElkLabel label : port.getLabels()) {
renderLabel(label, graphics, area, portOffset, labelAlpha);
}
}
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class DisCoGraphRenderer method renderEdge.
/**
* Paints an edge for the given dirty area.
*
* @param graph
* the top-level node of the graph
* @param edge
* the edge to paint
* @param graphics
* the graphics context used to paint
* @param area
* dirty area that needs painting
* @param labelAlpha
* alpha value for labels
*/
private void renderEdge(final ElkNode graph, final ElkEdge edge, final GC graphics, final Rectangle area, final KVector edgeBaseOffset, final int labelAlpha) {
if (configurator.getEdgeColor() == null) {
return;
}
if (!ElkGraphUtil.isDescendant(ElkGraphUtil.getSourceNode(edge), graph) || !ElkGraphUtil.isDescendant(ElkGraphUtil.getTargetNode(edge), graph)) {
// the edge points to some node outside of the rendered subgraph
return;
}
// calculate an offset for edge coordinates
ElkNode parent = ElkGraphUtil.getSourceNode(edge);
if (!ElkGraphUtil.isDescendant(ElkGraphUtil.getTargetNode(edge), parent)) {
parent = parent.getParent();
}
ElkNode node = parent;
KVector offset = new KVector().add(edgeBaseOffset);
while (node != graph) {
offset.add(node.getX() * getScale(), node.getY() * getScale());
node = node.getParent();
}
// ElkEdgeLayout edgeLayout = edge.getData(ElkEdgeLayout.class);
PaintRectangle rect = boundsMap.get(edge);
if (rect == null) {
rect = new PaintRectangle(edge, offset, getScale());
boundsMap.put(edge, rect);
}
if (!rect.painted && rect.intersects(area)) {
// CHECKSTYLEOFF MagicNumber
graphics.setAlpha(255);
// CHECKSTYLEON MagicNumber
// The background color is required to fill the arrow of directed
// edges
graphics.setForeground(configurator.getEdgeColor());
graphics.setBackground(configurator.getEdgeColor());
ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(edge, false, false);
KVectorChain bendPoints = ElkUtil.createVectorChain(edgeSection);
if (edge.getProperty(CoreOptions.EDGE_ROUTING) == EdgeRouting.SPLINES) {
bendPoints = ElkMath.approximateBezierSpline(bendPoints);
}
bendPoints.scale(getScale()).offset(offset);
KVector point1 = bendPoints.getFirst();
for (KVector point2 : bendPoints) {
graphics.drawLine((int) Math.round(point1.x), (int) Math.round(point1.y), (int) Math.round(point2.x), (int) Math.round(point2.y));
point1 = point2;
}
if (edge.getProperty(CoreOptions.EDGE_TYPE) != EdgeType.UNDIRECTED) {
// draw an arrow at the last segment of the connection
int[] arrowPoly = makeArrow(bendPoints.get(bendPoints.size() - 2), bendPoints.getLast());
if (arrowPoly != null) {
graphics.fillPolygon(arrowPoly);
}
}
rect.painted = true;
}
// paint junction points
KVectorChain vc = edge.getProperty(CoreOptions.JUNCTION_POINTS);
if (vc != null) {
for (KVector v : vc) {
KVector center = v.clone().scale(getScale()).add(offset).sub(2, 2);
// CHECKSTYLEOFF MagicNumber
graphics.fillOval((int) center.x, (int) center.y, 6, 6);
// CHECKSTYLEON MagicNumber
}
}
// paint the edge labels
if (configurator.getEdgeLabelFont() != null) {
graphics.setFont(configurator.getEdgeLabelFont());
for (ElkLabel label : edge.getLabels()) {
renderLabel(label, graphics, area, offset, labelAlpha);
}
}
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class ElkGraphImporter method applyLayout.
// /////////////////////////////////////////////////////////////////////////////
// Apply Layout Results
@Override
public void applyLayout(final FGraph fgraph) {
ElkNode kgraph = (ElkNode) fgraph.getProperty(InternalProperties.ORIGIN);
// calculate the offset from border spacing and node distribution
double minXPos = Integer.MAX_VALUE;
double minYPos = Integer.MAX_VALUE;
double maxXPos = Integer.MIN_VALUE;
double maxYPos = Integer.MIN_VALUE;
for (FNode node : fgraph.getNodes()) {
KVector pos = node.getPosition();
KVector size = node.getSize();
minXPos = Math.min(minXPos, pos.x - size.x / 2);
minYPos = Math.min(minYPos, pos.y - size.y / 2);
maxXPos = Math.max(maxXPos, pos.x + size.x / 2);
maxYPos = Math.max(maxYPos, pos.y + size.y / 2);
}
ElkPadding padding = kgraph.getProperty(ForceOptions.PADDING);
KVector offset = new KVector(padding.getLeft() - minXPos, padding.getTop() - minYPos);
// process the nodes
for (FNode fnode : fgraph.getNodes()) {
Object object = fnode.getProperty(InternalProperties.ORIGIN);
if (object instanceof ElkNode) {
// set the node position
ElkNode knode = (ElkNode) object;
KVector nodePos = fnode.getPosition().add(offset);
knode.setLocation(nodePos.x - knode.getWidth() / 2, nodePos.y - knode.getHeight() / 2);
}
}
// process the edges
for (FEdge fedge : fgraph.getEdges()) {
ElkEdge kedge = (ElkEdge) fedge.getProperty(InternalProperties.ORIGIN);
ElkEdgeSection kedgeSection = ElkGraphUtil.firstEdgeSection(kedge, true, true);
KVector startLocation = fedge.getSourcePoint();
kedgeSection.setStartLocation(startLocation.x, startLocation.y);
KVector endLocation = fedge.getTargetPoint();
kedgeSection.setEndLocation(endLocation.x, endLocation.y);
}
// process the labels
for (FLabel flabel : fgraph.getLabels()) {
ElkLabel klabel = (ElkLabel) flabel.getProperty(InternalProperties.ORIGIN);
KVector labelPos = flabel.getPosition().add(offset);
klabel.setLocation(labelPos.x, labelPos.y);
}
// set up the parent node
double width = (maxXPos - minXPos) + padding.getHorizontal();
double height = (maxYPos - minYPos) + padding.getVertical();
ElkUtil.resizeNode(kgraph, width, height, false, true);
}
use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.
the class ElkGraphImporter method transformEdges.
/**
* Transforms the edges defined by the given layout node.
*
* @param parentNode the layout node whose edges to transform.
* @param fgraph the force graph.
* @param elemMap the element map that maps the original {@code KGraph} elements to the
* transformed {@code FGraph} elements.
*/
private void transformEdges(final ElkNode parentNode, final FGraph fgraph, final Map<ElkNode, FNode> elemMap) {
for (ElkNode knode : parentNode.getChildren()) {
for (ElkEdge kedge : ElkGraphUtil.allOutgoingEdges(knode)) {
// We don't support hyperedges
if (kedge.isHyperedge()) {
throw new UnsupportedGraphException("Graph must not contain hyperedges.");
}
// exclude edges that pass hierarchy bounds as well as self-loops
if (!kedge.isHierarchical() && knode != ElkGraphUtil.connectableShapeToNode(kedge.getTargets().get(0))) {
// create a force edge
FEdge newEdge = new FEdge();
newEdge.copyProperties(kedge);
// TODO
// newEdge.checkProperties(Properties.LABEL_SPACING, Properties.REPULSIVE_POWER);
newEdge.setProperty(InternalProperties.ORIGIN, kedge);
newEdge.setSource(elemMap.get(knode));
newEdge.setTarget(elemMap.get(ElkGraphUtil.connectableShapeToNode(kedge.getTargets().get(0))));
fgraph.getEdges().add(newEdge);
// transform the edge's labels
for (ElkLabel klabel : kedge.getLabels()) {
FLabel newLabel = new FLabel(newEdge, klabel.getText());
newLabel.copyProperties(klabel);
newLabel.setProperty(InternalProperties.ORIGIN, klabel);
newLabel.getSize().x = Math.max(klabel.getWidth(), 1);
newLabel.getSize().y = Math.max(klabel.getHeight(), 1);
newLabel.refreshPosition();
fgraph.getLabels().add(newLabel);
}
}
}
}
}
Aggregations