Search in sources :

Example 1 with GridLayoutAlgorithm

use of org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm in project opennms by OpenNMS.

the class ManualLayoutAlgorithmTest method verifyDefaultsToGridLayout.

/*
     * If no coordinates are defined for the vertex and there are no layout coordinates yet, verify that it falls
     * back to the GridLayout.
     */
@Test
public void verifyDefaultsToGridLayout() {
    final ManualTest test = new ManualTest(new SimpleGraphBuilder("dummy").vertex("1").vertex("2").get());
    new ManualLayoutAlgorithm(test.layoutManager).updateLayout(test.graph);
    Map<VertexRef, Point> manualLocations = test.layout.getLocations();
    new GridLayoutAlgorithm().updateLayout(test.graph);
    Assert.assertEquals(test.layout.getLocations(), manualLocations);
}
Also used : SimpleGraphBuilder(org.opennms.features.topology.api.support.SimpleGraphBuilder) Point(org.opennms.features.topology.api.Point) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) GridLayoutAlgorithm(org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm) Test(org.junit.Test)

Example 2 with GridLayoutAlgorithm

use of org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm 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);
            }
        }
    }
}
Also used : LayoutManager(org.opennms.features.topology.app.internal.support.LayoutManager) Point(org.opennms.features.topology.api.Point) GridLayoutAlgorithm(org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm) Layout(org.opennms.features.topology.api.Layout) Collection(java.util.Collection) Vertex(org.opennms.features.topology.api.topo.Vertex) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) Graph(org.opennms.features.topology.api.Graph) LayoutAlgorithm(org.opennms.features.topology.api.LayoutAlgorithm) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) Vertex(org.opennms.features.topology.api.topo.Vertex) Layout(org.opennms.features.topology.api.Layout) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) Point(org.opennms.features.topology.api.Point) GridLayoutAlgorithm(org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm)

Aggregations

Point (org.opennms.features.topology.api.Point)2 GridLayoutAlgorithm (org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm)2 Collection (java.util.Collection)1 Test (org.junit.Test)1 Graph (org.opennms.features.topology.api.Graph)1 Layout (org.opennms.features.topology.api.Layout)1 LayoutAlgorithm (org.opennms.features.topology.api.LayoutAlgorithm)1 SimpleGraphBuilder (org.opennms.features.topology.api.support.SimpleGraphBuilder)1 DefaultVertexRef (org.opennms.features.topology.api.topo.DefaultVertexRef)1 Vertex (org.opennms.features.topology.api.topo.Vertex)1 VertexRef (org.opennms.features.topology.api.topo.VertexRef)1 LayoutManager (org.opennms.features.topology.app.internal.support.LayoutManager)1 LayoutEntity (org.opennms.netmgt.topology.persistence.api.LayoutEntity)1 PointEntity (org.opennms.netmgt.topology.persistence.api.PointEntity)1