Search in sources :

Example 1 with ViewDefinition

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

the class SVGViewFactoryGenerator method generate.

@Override
public StringBuffer generate(final ViewFactory viewFactory) throws GeneratorException {
    final List<StringBuffer> viewBuffers = new LinkedList<>();
    final String name = viewFactory.getSimpleName();
    final String pkg = viewFactory.getPackage();
    final List<ViewDefinition<?>> viewDefinitions = viewFactory.getViewDefinitions();
    final Map<String, String> staticFields = new LinkedHashMap<>();
    viewDefinitions.stream().forEach((viewDefinition) -> {
        try {
            if (viewDefinition instanceof ViewDefinitionImpl) {
                staticFields.putAll(((ViewDefinitionImpl) viewDefinition).getStaticFields());
            }
            final StringBuffer viewBuffer = generateView(viewFactory, viewDefinition);
            viewBuffers.add(viewBuffer);
        } catch (GeneratorException e) {
            throw new RuntimeException(e);
        }
    });
    // Generate template context.
    final List<String> viewsContent = new LinkedList<>();
    viewBuffers.forEach(b -> viewsContent.add(b.toString()));
    Map<String, Object> root = new HashMap<>();
    root.put("genClassName", this.getClass().getName());
    root.put("name", name);
    root.put("pkg", pkg);
    root.put("implementedTypeName", viewFactory.getImplementedType());
    root.put("fmethods", viewsContent);
    root.put("fields", generateStaticFields(staticFields));
    // Generate the code using the given template.
    StringBuffer result;
    try {
        result = writeTemplate(root);
    } catch (final GenerationException e) {
        throw new GeneratorException(e);
    }
    return result;
}
Also used : ViewDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ViewDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) GenerationException(org.uberfire.annotations.processors.exceptions.GenerationException) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)

Example 2 with ViewDefinition

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

the class SVGGeneratorImpl method generate.

@Override
public StringBuffer generate(final SVGGeneratorRequest request) throws GeneratorException {
    final String name = request.getName();
    final String pkg = request.getPkg();
    final String typeOf = request.getImplementedType();
    final String cssPath = request.getCssPath();
    final Map<String, String> viewSources = request.getViewSources();
    final ViewFactoryImpl viewFactory = new ViewFactoryImpl(name, pkg, typeOf);
    // Process the global CSS declaration specified in the factory, if any.
    final StyleSheetDefinition[] styleSheetDefinition = new StyleSheetDefinition[1];
    if (null != cssPath && cssPath.trim().length() > 0) {
        final InputStream cssStream = loadResource(cssPath);
        if (null != cssStream) {
            try {
                styleSheetDefinition[0] = SVGStyleTranslator.parseStyleSheetDefinition(cssPath, cssStream);
                viewFactory.setStyleSheetDefinition(styleSheetDefinition[0]);
            } catch (Exception e) {
                throw new RuntimeException("Error while processing the glocal CSS file [" + cssPath + "] ", e);
            }
        }
    }
    // Process all SVG files specified in the factory.
    // TODO: Hmmm
    final Set<String> processedSvgIds = new LinkedHashSet<>();
    viewSources.forEach((fMethodName, svgPath) -> {
        parseSVGViewSource(fMethodName, svgPath, styleSheetDefinition[0], result -> {
            result.setId(fMethodName);
            result.setFactoryMethodName(fMethodName);
            viewFactory.getViewDefinitions().add(result);
        });
        processedSvgIds.add(fMethodName);
    });
    // Parse referenced svg files as well, if any.
    final List<ViewDefinition<?>> referencedViewDefinitions = new LinkedList<>();
    viewFactory.getViewDefinitions().stream().flatMap(v -> v.getSVGViewRefs().stream()).filter(vd -> !processedSvgIds.contains(vd.getViewRefId())).forEach(vd -> parseSVGViewSource(vd.getViewRefId(), vd.getFilePath(), styleSheetDefinition[0], result -> {
        final String id = SVGGeneratorFormatUtils.getValidInstanceId(result);
        result.setFactoryMethodName(id);
        referencedViewDefinitions.add(result);
        processedSvgIds.add(id);
    }));
    viewFactory.getViewDefinitions().addAll(referencedViewDefinitions);
    return viewFactoryGenerator.generate(viewFactory);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ViewFactoryImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl) SVGViewFactoryGenerator(org.kie.workbench.common.stunner.svg.gen.codegen.impl.SVGViewFactoryGenerator) StyleSheetDefinition(org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition) SVGGeneratorFormatUtils(org.kie.workbench.common.stunner.svg.gen.codegen.impl.SVGGeneratorFormatUtils) ViewDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition) ViewDefinitionImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl) SVGTranslatorContext(org.kie.workbench.common.stunner.svg.gen.translator.SVGTranslatorContext) Document(org.w3c.dom.Document) Map(java.util.Map) LinkedList(java.util.LinkedList) SVGGeneratorRequest(org.kie.workbench.common.stunner.svg.gen.SVGGeneratorRequest) Path(java.nio.file.Path) SVGDocumentTranslator(org.kie.workbench.common.stunner.svg.gen.translator.SVGDocumentTranslator) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Consumer(java.util.function.Consumer) SVGGenerator(org.kie.workbench.common.stunner.svg.gen.SVGGenerator) List(java.util.List) Paths(java.nio.file.Paths) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SVGStyleTranslator(org.kie.workbench.common.stunner.svg.gen.translator.css.SVGStyleTranslator) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) InputStream(java.io.InputStream) ViewDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException) LinkedList(java.util.LinkedList) ViewFactoryImpl(org.kie.workbench.common.stunner.svg.gen.model.impl.ViewFactoryImpl) StyleSheetDefinition(org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition)

Example 3 with ViewDefinition

use of org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition 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 4 with ViewDefinition

use of org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition 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

ViewDefinition (org.kie.workbench.common.stunner.svg.gen.model.ViewDefinition)4 LinkedList (java.util.LinkedList)3 ViewDefinitionImpl (org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl)3 GeneratorException (org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)2 PrimitiveDefinition (org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition)2 StyleSheetDefinition (org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition)2 ViewRefDefinition (org.kie.workbench.common.stunner.svg.gen.model.ViewRefDefinition)2 GroupDefinition (org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition)2 SVGTranslatorContext (org.kie.workbench.common.stunner.svg.gen.translator.SVGTranslatorContext)2 Document (org.w3c.dom.Document)2 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1