Search in sources :

Example 1 with HasBounds

use of org.kie.workbench.common.stunner.core.graph.content.HasBounds in project kie-wb-common by kiegroup.

the class SugiyamaLayoutServiceTest method testGetHeight.

@Test
public void testGetHeight() {
    final Node n = mock(Node.class);
    final HasBounds hasBounds = mock(HasBounds.class);
    final Bounds bounds = mock(Bounds.class);
    final double height = 15;
    final int expected = (int) height;
    when(bounds.getHeight()).thenReturn(height);
    when(hasBounds.getBounds()).thenReturn(bounds);
    when(n.getContent()).thenReturn(hasBounds);
    int actual = layoutService.getHeight(n);
    assertEquals(expected, actual);
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) HasBounds(org.kie.workbench.common.stunner.core.graph.content.HasBounds) Bounds(org.kie.workbench.common.stunner.core.graph.content.Bounds) HasBounds(org.kie.workbench.common.stunner.core.graph.content.HasBounds) Test(org.junit.Test)

Example 2 with HasBounds

use of org.kie.workbench.common.stunner.core.graph.content.HasBounds in project kie-wb-common by kiegroup.

the class AbstractLayoutService method hasLayoutInformation.

/**
 * Checks if the specified graph has layout information.
 * A graph with at 25% of its nodes or less at position (0,0) is considered a graph with no layout information.
 * @param graph The graph.
 * @return True if the graph has layout information, false otherwise.
 */
@Override
public boolean hasLayoutInformation(final Graph<?, ?> graph) {
    final double threshold = getLayoutInformationThreshold(graph);
    int nodesWithLayout = 0;
    for (final Node n : graph.nodes()) {
        final Object content = n.getContent();
        if (content instanceof HasBounds) {
            if (!isNullOrCloseToZero(((HasBounds) content).getBounds())) {
                nodesWithLayout++;
            }
        }
        if (nodesWithLayout >= threshold) {
            return true;
        }
    }
    return false;
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) HasBounds(org.kie.workbench.common.stunner.core.graph.content.HasBounds)

Example 3 with HasBounds

use of org.kie.workbench.common.stunner.core.graph.content.HasBounds in project kie-wb-common by kiegroup.

the class LayoutHelper method applyLayout.

public void applyLayout(final Diagram diagram, final LayoutExecutor layoutExecutor, final boolean overrideCurrentLayout) {
    final Graph<?, Node> graph = diagram.getGraph();
    if (graph != null && (overrideCurrentLayout || !this.layoutService.hasLayoutInformation(graph))) {
        final Layout layout = this.layoutService.createLayout(graph);
        layoutExecutor.applyLayout(layout, graph);
        for (final Node node : graph.nodes()) {
            if (CanvasLayoutUtils.isCanvasRoot(diagram, node)) {
                if (node.getContent() instanceof HasBounds) {
                    ((HasBounds) node.getContent()).setBounds(Bounds.create(0, 0, 0, 0));
                }
            }
        }
    }
}
Also used : Layout(org.kie.workbench.common.stunner.core.graph.processing.layout.Layout) Node(org.kie.workbench.common.stunner.core.graph.Node) HasBounds(org.kie.workbench.common.stunner.core.graph.content.HasBounds)

Example 4 with HasBounds

use of org.kie.workbench.common.stunner.core.graph.content.HasBounds in project kie-wb-common by kiegroup.

the class OpenDiagramLayoutExecutor method applyLayout.

@Override
public void applyLayout(final Layout layout, final Graph graph) {
    if (layout.getNodePositions().size() == 0) {
        return;
    }
    final HashMap<String, Node> indexByUuid = new HashMap<>();
    for (final Object n : graph.nodes()) {
        if (n instanceof Node) {
            final Node node = (Node) n;
            indexByUuid.put(node.getUUID(), node);
        }
    }
    for (final VertexPosition position : layout.getNodePositions()) {
        final Node indexed = indexByUuid.get(position.getId());
        if (indexed.getContent() instanceof HasBounds) {
            ((HasBounds) indexed.getContent()).setBounds(Bounds.create(position.getUpperLeft().getX(), position.getUpperLeft().getY(), position.getBottomRight().getX(), position.getBottomRight().getY()));
        }
    }
    notifyUser();
}
Also used : HashMap(java.util.HashMap) Node(org.kie.workbench.common.stunner.core.graph.Node) VertexPosition(org.kie.workbench.common.stunner.core.graph.processing.layout.VertexPosition) HasBounds(org.kie.workbench.common.stunner.core.graph.content.HasBounds)

Example 5 with HasBounds

use of org.kie.workbench.common.stunner.core.graph.content.HasBounds in project kie-wb-common by kiegroup.

the class SugiyamaLayoutServiceTest method testGetWidth.

@Test
public void testGetWidth() {
    final Node n = mock(Node.class);
    final HasBounds hasBounds = mock(HasBounds.class);
    final Bounds bounds = mock(Bounds.class);
    final double width = 17;
    final int expected = (int) width;
    when(bounds.getWidth()).thenReturn(width);
    when(hasBounds.getBounds()).thenReturn(bounds);
    when(n.getContent()).thenReturn(hasBounds);
    int actual = layoutService.getWidth(n);
    assertEquals(expected, actual);
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) HasBounds(org.kie.workbench.common.stunner.core.graph.content.HasBounds) Bounds(org.kie.workbench.common.stunner.core.graph.content.Bounds) HasBounds(org.kie.workbench.common.stunner.core.graph.content.HasBounds) Test(org.junit.Test)

Aggregations

Node (org.kie.workbench.common.stunner.core.graph.Node)8 HasBounds (org.kie.workbench.common.stunner.core.graph.content.HasBounds)8 Bounds (org.kie.workbench.common.stunner.core.graph.content.Bounds)4 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Bound (org.kie.workbench.common.stunner.core.graph.content.Bound)2 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)2 Layout (org.kie.workbench.common.stunner.core.graph.processing.layout.Layout)2 VertexPosition (org.kie.workbench.common.stunner.core.graph.processing.layout.VertexPosition)2 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 OptionalInt (java.util.OptionalInt)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1