use of org.opennms.features.topology.api.Layout in project opennms by OpenNMS.
the class ManualLayoutAlgorithm method updateLayout.
@Override
public void updateLayout(Graph graph) {
final LayoutEntity layoutEntity = layoutManager != null ? layoutManager.loadLayout(graph) : null;
if (layoutEntity != null) {
// if we have a persisted layout, we apply it ...
final Layout layout = graph.getLayout();
final Collection<Vertex> vertices = graph.getDisplayVertices();
for (Vertex vertex : vertices) {
PointEntity pointEntity = layoutEntity.getPosition(vertex.getNamespace(), vertex.getId());
layout.setLocation(vertex, new Point(pointEntity.getX(), pointEntity.getY()));
}
} else {
// otherwise we apply the manual layout ...
final Collection<Vertex> vertices = graph.getDisplayVertices();
final Layout layout = graph.getLayout();
final long notLayedOutCount = vertices.stream().filter(v -> {
Point location = layout.getLocation(v);
return location.getX() == 0 && location.getY() == 0;
}).count();
final long noVertexLocationCount = vertices.stream().filter(v -> {
boolean hasNoX = v.getX() == null || v.getX().intValue() == 0;
boolean hasNoY = v.getY() == null || v.getY().intValue() == 0;
return hasNoX && hasNoY;
}).count();
// manually apply the Grid Layout
if (notLayedOutCount == vertices.size() && noVertexLocationCount == vertices.size()) {
new GridLayoutAlgorithm().updateLayout(graph);
} else if (noVertexLocationCount != vertices.size()) {
// If we have at least one vertex with coordinates != (0,0), we apply them to the layout
for (Vertex vertex : vertices) {
layout.setLocation(vertex, new Point(vertex.getX(), vertex.getY()));
}
} else {
// This is only done when the user explicitly saves the layout
for (Vertex vertex : vertices) {
Point p = layout.getLocation(vertex);
layout.setLocation(vertex, p);
}
}
}
}
use of org.opennms.features.topology.api.Layout in project opennms by OpenNMS.
the class SimpleLayoutAlgorithm method updateLayout.
@Override
public void updateLayout(Graph graph) {
Layout layout = graph.getLayout();
int r = 100;
int cx = 500;
int cy = 500;
int i = 0;
for (Vertex vertex : graph.getDisplayVertices()) {
s_log.debug("Laying out vertex id : {}", vertex);
if (i == 0) {
layout.setLocation(vertex, new Point(cx, cy));
} else {
int n = i - 1;
double a = (2 * Math.PI) / (graph.getDisplayVertices().size() - 1);
int x = (int) (r * Math.cos(n * a) + cx);
int y = (int) (r * Math.sin(n * a) + cy);
layout.setLocation(vertex, new Point(x, y));
}
i++;
}
}
use of org.opennms.features.topology.api.Layout in project opennms by OpenNMS.
the class LayoutManager method persistLayout.
public void persistLayout(GraphContainer graphContainer) {
final List<VertexRef> vertexRefs = toVertexRef(graphContainer.getGraph().getDisplayVertices());
final String id = calculateHash(vertexRefs);
LayoutEntity layoutEntity = layoutDao.get(id);
if (layoutEntity == null) {
layoutEntity = new LayoutEntity();
layoutEntity.setId(id);
layoutEntity.setCreated(new Date());
layoutEntity.setCreator(graphContainer.getApplicationContext().getUsername());
}
layoutEntity.setUpdated(new Date());
layoutEntity.setUpdator(graphContainer.getApplicationContext().getUsername());
final Layout layout = graphContainer.getGraph().getLayout();
final List<VertexPositionEntity> vertexPositionEntities = vertexRefs.stream().map(vertexRef -> {
final Point p = layout.getLocation(vertexRef);
PointEntity pointEntity = new PointEntity();
pointEntity.setX((int) p.getX());
pointEntity.setY((int) p.getY());
final VertexPositionEntity vertexEntity = new VertexPositionEntity();
vertexEntity.setVertexRef(toVertexRefEntity(vertexRef));
vertexEntity.setPosition(pointEntity);
return vertexEntity;
}).collect(Collectors.toList());
layoutEntity.getVertexPositions().clear();
for (VertexPositionEntity eachVertexPosition : vertexPositionEntities) {
layoutEntity.addVertexPosition(eachVertexPosition);
}
layoutDao.saveOrUpdate(layoutEntity);
}
use of org.opennms.features.topology.api.Layout in project opennms by OpenNMS.
the class FRLayoutAlgorithm method updateLayout.
@Override
public void updateLayout(final Graph graph) {
final Layout graphLayout = graph.getLayout();
SparseGraph<VertexRef, EdgeRef> jungGraph = new SparseGraph<VertexRef, EdgeRef>();
Collection<Vertex> vertices = graph.getDisplayVertices();
for (Vertex v : vertices) {
jungGraph.addVertex(v);
}
Collection<Edge> edges = graph.getDisplayEdges();
for (Edge e : edges) {
jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex());
}
FRLayout<VertexRef, EdgeRef> layout = new FRLayout<VertexRef, EdgeRef>(jungGraph);
// Initialize the vertex positions to the last known positions from the layout
Dimension size = selectLayoutSize(graph);
layout.setInitializer(initializer(graphLayout, (int) size.getWidth() / 2, (int) size.getHeight() / 2));
// Resize the graph to accommodate the number of vertices
layout.setSize(size);
while (!layout.done()) {
layout.step();
}
// Store the new positions in the layout
for (Vertex v : vertices) {
graphLayout.setLocation(v, new Point(layout.getX(v) - (size.getWidth() / 2), (int) layout.getY(v) - (size.getHeight() / 2)));
}
}
use of org.opennms.features.topology.api.Layout in project opennms by OpenNMS.
the class CircleLayoutAlgorithm method updateLayout.
@Override
public void updateLayout(final Graph graph) {
final Layout graphLayout = graph.getLayout();
SparseGraph<VertexRef, Edge> jungGraph = new SparseGraph<VertexRef, Edge>();
Collection<? extends Vertex> vertices = graph.getDisplayVertices();
for (VertexRef v : vertices) {
jungGraph.addVertex(v);
}
for (Edge e : graph.getDisplayEdges()) {
jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex());
}
CircleLayout<VertexRef, Edge> layout = new CircleLayout<VertexRef, Edge>(jungGraph);
layout.setInitializer(initializer(graphLayout));
layout.setSize(selectLayoutSize(graph));
for (VertexRef v : vertices) {
graphLayout.setLocation(v, new Point(layout.getX(v), layout.getY(v)));
}
}
Aggregations