Search in sources :

Example 1 with GroupDefinition

use of org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition in project kie-wb-common by kiegroup.

the class SVGPrimitiveGeneratorUtils method generateSvgPrimitive.

public static String generateSvgPrimitive(final String instanceId, final Function<PrimitiveDefinition, PrimitiveDefinitionGenerator<PrimitiveDefinition<?>>> generatorProvider, final PrimitiveDefinition child, final Predicate<PrimitiveDefinition> generationFilter) {
    String childRaw = null;
    try {
        final StringBuffer childBuffer = generatorProvider.apply(child).generate(child);
        final String scalableRaw = String.valueOf(child.isScalable());
        final LayoutDefinition layoutDefinition = child.getLayoutDefinition();
        final String childLayoutRaw = formatLayout(layoutDefinition);
        if (generationFilter.test(child)) {
            if (child instanceof ShapeDefinition) {
                childRaw = AbstractGenerator.formatString(NEW_SVG_SHAPE_TEMPLATE, instanceId, childBuffer.toString(), scalableRaw, childLayoutRaw);
            } else if (child instanceof GroupDefinition) {
                final GroupDefinition groupDefinition = (GroupDefinition) child;
                final List<PrimitiveDefinition> children = groupDefinition.getChildren();
                if (children.stream().anyMatch(generationFilter)) {
                    // Generate the group primitive.
                    childRaw = AbstractGenerator.formatString(NEW_SVG_CONTAINER_TEMPLATE, instanceId, groupDefinition.getId(), childBuffer.toString(), scalableRaw, childLayoutRaw);
                    // Generate the group children ones.
                    for (final PrimitiveDefinition childDef : children) {
                        final String childDefInstanceId = SVGGeneratorFormatUtils.getValidInstanceId(childDef);
                        final String childDefRaw = generateSvgPrimitive(childDefInstanceId, generatorProvider, childDef, generationFilter);
                        if (null != childDefRaw) {
                            childRaw += childDefRaw;
                            childRaw += AbstractGenerator.formatString(GROUP_ADD_CHILD_TEMPLATE, instanceId, childDefInstanceId);
                        }
                    }
                }
            }
        }
    } catch (GeneratorException e) {
        throw new RuntimeException(e);
    }
    return childRaw;
}
Also used : ShapeDefinition(org.kie.workbench.common.stunner.svg.gen.model.ShapeDefinition) GroupDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition) List(java.util.List) PrimitiveDefinition(org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition) LayoutDefinition(org.kie.workbench.common.stunner.svg.gen.model.LayoutDefinition) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)

Example 2 with GroupDefinition

use of org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition in project kie-wb-common by kiegroup.

the class SVGDocumentTranslatorImpl method translate.

@Override
@SuppressWarnings("unchecked")
public ViewDefinition<SVGShapeView> translate(final SVGTranslatorContext context) throws TranslatorException {
    final Document root = context.getRoot();
    final StyleSheetDefinition styleSheetDefinition = context.getGlobalStyleSheet().orElse(null);
    // Initialze the context with the available translators.
    context.setElementTranslators(elementTranslators);
    // Process the SVG node stuff.
    final double[] svgCoord = new double[] { 0d, 0d };
    final double[] svgSize = new double[] { 0d, 0d };
    final ViewDefinition.ViewBoxDefinition[] viewBox = new ViewDefinition.ViewBoxDefinition[1];
    final NodeList svgNodes = root.getElementsByTagName(SVG_TAG);
    if (null != svgNodes && svgNodes.getLength() > 1) {
        throw new TranslatorException("Only a single SVG node supported.!");
    } else if (null == svgNodes || svgNodes.getLength() == 0) {
        throw new TranslatorException("No SVG root node found!");
    }
    final Element svgNode = (Element) svgNodes.item(0);
    // SVG id.
    String svgId = svgNode.getAttribute(ID);
    if (isEmpty(svgId)) {
        throw new TranslatorException("The SVG node must contain a valid ID attribute.");
    }
    context.setSVGId(svgId);
    if (null == context.getViewId()) {
        svgId = svgNode.getAttribute(ID);
        context.setViewId(svgId);
    }
    // View box.
    final String x = svgNode.getAttribute(X);
    final String y = svgNode.getAttribute(Y);
    final String width = svgNode.getAttribute(WIDTH);
    if (isEmpty(width)) {
        throw new TranslatorException("The SVG node [" + svgId + "] must contain a valid WIDTH attribute.");
    }
    final String height = svgNode.getAttribute(HEIGHT);
    if (isEmpty(height)) {
        throw new TranslatorException("The SVG node [" + svgId + "] must contain a valid HEIGHT attribute.");
    }
    svgCoord[0] = SVGAttributeParser.toPixelValue(x, X_DEFAULT);
    svgCoord[1] = SVGAttributeParser.toPixelValue(y, Y_DEFAULT);
    svgSize[0] = SVGAttributeParser.toPixelValue(width);
    svgSize[1] = SVGAttributeParser.toPixelValue(height);
    final String vbox = svgNode.getAttribute(VIEW_BOX);
    if (isEmpty(vbox)) {
        throw new TranslatorException("The SVG node [" + svgId + "] must contain a valid VIEWBOX attribute.");
    }
    viewBox[0] = SVGViewBoxTranslator.translate(vbox);
    // Parser innver SVG View elements.
    ShapeDefinition<?> main = null;
    final List<PrimitiveDefinition<?>> result = new LinkedList<>();
    final NodeList nodes = svgNode.getChildNodes();
    if (null != nodes) {
        for (int i = 0; i < nodes.getLength(); i++) {
            final Node node = nodes.item(i);
            if (node instanceof Element) {
                final Element element = (Element) node;
                final SVGElementTranslator<Element, Object> translator = context.getElementTranslator(element.getTagName());
                if (null != translator) {
                    final Object definition = translator.translate(element, context);
                    if (null != definition) {
                        if (definition instanceof PrimitiveDefinition) {
                            final PrimitiveDefinition primitiveDefinition = (PrimitiveDefinition) definition;
                            if (null == main) {
                                main = (ShapeDefinition<?>) primitiveDefinition;
                            } else {
                                result.add(primitiveDefinition);
                            }
                        } else if (definition instanceof ViewRefDefinition) {
                            context.addSVGViewRef((ViewRefDefinition) definition);
                        }
                    }
                }
            }
        }
    }
    if (null == main) {
        throw new TranslatorException("No SVG main node found.");
    }
    if (main instanceof GroupDefinition) {
        throw new TranslatorException("Main node cannot be a group.");
    }
    // Main view shape should listen for events.
    if (main instanceof AbstractShapeDefinition) {
        ((AbstractShapeDefinition) main).setMainShape(true);
        ((AbstractShapeDefinition) main).setListening(true);
    }
    // Generate the view definition instance.
    final ViewDefinition viewDefinition = new ViewDefinitionImpl(svgId, svgCoord[0], svgCoord[1], svgSize[0], svgSize[1], styleSheetDefinition, viewBox[0], main, result.toArray(new PrimitiveDefinition<?>[result.size()]));
    viewDefinition.getSVGViewRefs().addAll(context.getViewRefDefinitions());
    return viewDefinition;
}
Also used : ViewDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) AbstractShapeDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.AbstractShapeDefinition) ViewDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition) PrimitiveDefinition(org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) GroupDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition) ViewRefDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewRefDefinition) StyleSheetDefinition(org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition) TranslatorException(org.kie.workbench.common.stunner.svg.gen.exception.TranslatorException)

Example 3 with GroupDefinition

use of org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition in project kie-wb-common by kiegroup.

the class SVGGroupTranslator method doTranslate.

@Override
protected GroupDefinition doTranslate(final Element element, final SVGTranslatorContext context) throws TranslatorException {
    final String id = getId(element);
    final GroupDefinition groupDefinition = new GroupDefinition(id);
    final NodeList childNodes = element.getChildNodes();
    if (null != childNodes && childNodes.getLength() > 0) {
        for (int i = 0; i < childNodes.getLength(); i++) {
            final Node child = childNodes.item(i);
            if (child instanceof Element) {
                final Element childElement = (Element) child;
                final SVGElementTranslator<Element, Object> translator = context.getElementTranslator(childElement.getTagName());
                if (null != translator) {
                    final Object childDefinition = translator.translate(childElement, context);
                    if (childDefinition instanceof ViewRefDefinition) {
                        context.addSVGViewRef((ViewRefDefinition) childDefinition);
                    } else if (childDefinition instanceof ShapeDefinition) {
                        groupDefinition.getChildren().add((PrimitiveDefinition) childDefinition);
                    } else if (childDefinition instanceof GroupDefinition) {
                        throw new UnsupportedOperationException("Nested SVG groups are not allowed! [svgId=" + context.getSVGId() + "]");
                    }
                }
            }
        }
    }
    return groupDefinition;
}
Also used : ShapeDefinition(org.kie.workbench.common.stunner.svg.gen.model.ShapeDefinition) GroupDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ViewRefDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewRefDefinition) PrimitiveDefinition(org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition)

Example 4 with GroupDefinition

use of org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition in project kie-wb-common by kiegroup.

the class GroupDefinitionGeneratorTest method testGenerate.

@Test
public void testGenerate() throws Exception {
    final GroupDefinition definition = new GroupDefinition(ID);
    definition.setX(X);
    definition.setY(Y);
    definition.setAlpha(ALPHA);
    definition.setTransformDefinition(new TransformDefinitionImpl(VBOX_WIDTH, VBOX_HEIGHT, VBOX_MIN_X, VBOX_MIN_Y));
    final String generated = tested.generate(definition).toString();
    assertTrue(generated.contains("new " + Group.class.getName() + "()"));
    GeneratorAssertions.assertDraggable(generated);
    GeneratorAssertions.assertID(generated, ID);
    GeneratorAssertions.assertX(generated, X);
    GeneratorAssertions.assertY(generated, Y);
    GeneratorAssertions.assertAlpha(generated, ALPHA);
    GeneratorAssertions.assertScale(generated, VBOX_WIDTH, VBOX_HEIGHT);
    GeneratorAssertions.assertListening(generated, LISTENING);
}
Also used : GroupDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition) TransformDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.TransformDefinitionImpl) Test(org.junit.Test)

Example 5 with GroupDefinition

use of org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition in project kie-wb-common by kiegroup.

the class SVGDocumentTranslatorTest method testTranslate.

@Test
public void testTranslate() throws Exception {
    final ViewDefinition<SVGShapeView> viewDefinition = translator.translate(new SVGTranslatorContext(svgTest, "", styleSheetDefinition));
    assertNotNull(viewDefinition);
    assertEquals("svg-test-file", viewDefinition.getId());
    // View definition's viewBox.
    final ViewDefinition.ViewBoxDefinition viewBox = viewDefinition.getViewBox();
    assertNotNull(viewBox);
    final double minX = viewBox.getMinX();
    final double minY = viewBox.getMinY();
    final double width = viewBox.getWidth();
    final double height = viewBox.getHeight();
    assertEquals(minX, 0d, 0d);
    assertEquals(minY, 0d, 0d);
    assertEquals(width, 448d, 0d);
    assertEquals(height, 448d, 0d);
    // View definition's main shape.
    final PrimitiveDefinition mainShapeDef = viewDefinition.getMain();
    assertNotNull(mainShapeDef);
    assertTrue(mainShapeDef instanceof MultiPathDefinition);
    final MultiPathDefinition mainPathDef = (MultiPathDefinition) mainShapeDef;
    SVGTranslationTestAssertions.assertPath(mainPathDef);
    assertTrue(mainPathDef.isListening());
    // View definition's child shapes.
    final List<PrimitiveDefinition> childrenDefs = viewDefinition.getChildren();
    assertNotNull(childrenDefs);
    assertTrue(childrenDefs.size() == 3);
    final RectDefinition rectDefinition = (RectDefinition) childrenDefs.get(0);
    SVGTranslationTestAssertions.assertRectangle(rectDefinition);
    assertFalse(rectDefinition.isListening());
    final CircleDefinition circleDefinition = (CircleDefinition) childrenDefs.get(1);
    SVGTranslationTestAssertions.assertCircle(circleDefinition);
    assertFalse(circleDefinition.isListening());
    // Assert other svg reference elements.
    final GroupDefinition groupDefinition = (GroupDefinition) childrenDefs.get(2);
    assertFalse(groupDefinition.isListening());
    SVGTranslationTestAssertions.assertGroupRef(groupDefinition);
    final List<ViewRefDefinition> svgViewRefs = viewDefinition.getSVGViewRefs();
    assertNotNull(svgViewRefs);
    assertTrue(svgViewRefs.size() == 1);
    final ViewRefDefinition viewRefDef = svgViewRefs.get(0);
    SVGTranslationTestAssertions.assertViewRef(viewRefDef);
}
Also used : CircleDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.CircleDefinition) ViewDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition) SVGShapeView(org.kie.workbench.common.stunner.svg.client.shape.view.SVGShapeView) PrimitiveDefinition(org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition) MultiPathDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.MultiPathDefinition) GroupDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition) ViewRefDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewRefDefinition) SVGTranslatorContext(org.kie.workbench.common.stunner.svg.gen.translator.SVGTranslatorContext) RectDefinition(org.kie.workbench.common.stunner.svg.gen.model.impl.RectDefinition) Test(org.junit.Test)

Aggregations

GroupDefinition (org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition)5 PrimitiveDefinition (org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition)4 ViewRefDefinition (org.kie.workbench.common.stunner.svg.gen.model.ViewRefDefinition)3 Test (org.junit.Test)2 ShapeDefinition (org.kie.workbench.common.stunner.svg.gen.model.ShapeDefinition)2 ViewDefinition (org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 SVGShapeView (org.kie.workbench.common.stunner.svg.client.shape.view.SVGShapeView)1 GeneratorException (org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)1 TranslatorException (org.kie.workbench.common.stunner.svg.gen.exception.TranslatorException)1 LayoutDefinition (org.kie.workbench.common.stunner.svg.gen.model.LayoutDefinition)1 StyleSheetDefinition (org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition)1 AbstractShapeDefinition (org.kie.workbench.common.stunner.svg.gen.model.impl.AbstractShapeDefinition)1 CircleDefinition (org.kie.workbench.common.stunner.svg.gen.model.impl.CircleDefinition)1 MultiPathDefinition (org.kie.workbench.common.stunner.svg.gen.model.impl.MultiPathDefinition)1 RectDefinition (org.kie.workbench.common.stunner.svg.gen.model.impl.RectDefinition)1