Search in sources :

Example 1 with LayoutDefinition

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

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

the class SVGViewDefinitionGenerator method generate.

@Override
@SuppressWarnings("unchecked")
public StringBuffer generate(final ViewFactory viewFactory, final ViewDefinition<SVGShapeView> viewDefinition) throws GeneratorException {
    StringBuffer result = null;
    final String factoryName = viewFactory.getSimpleName();
    final String viewId = viewDefinition.getId();
    final String methodName = viewDefinition.getFactoryMethodName();
    final ShapeDefinition main = viewDefinition.getMain();
    if (null != main) {
        final Map<String, Object> root = new HashMap<>();
        // Generate the children primitives.
        final List<String> childrenRaw = new LinkedList<>();
        final List<PrimitiveDefinition> children = viewDefinition.getChildren();
        for (final PrimitiveDefinition child : children) {
            final String childId = SVGGeneratorFormatUtils.getValidInstanceId(child);
            String childRaw = SVGPrimitiveGeneratorUtils.generateSvgPrimitive(childId, SVGViewDefinitionGenerator::getGenerator, child);
            if (null != childRaw) {
                childrenRaw.add(childRaw);
                childrenRaw.add(AbstractGenerator.formatString(PRIM_CHILD_TEMPLATE, childId));
            }
        }
        // SVG View children.
        final List<String> svgChildrenRaw = new LinkedList<>();
        final List<ViewRefDefinition> svgViewRefs = viewDefinition.getSVGViewRefs();
        svgViewRefs.forEach(viewRef -> {
            final String parent = viewRef.getParent();
            final String svgName = viewRef.getFilePath();
            final String viewRefId = viewRef.getViewRefId();
            final boolean existReferencedView = viewFactory.getViewDefinitions().stream().anyMatch(def -> viewRefId.equals(def.getId()));
            if (existReferencedView) {
                final String childRaw = formatString(SVG_CHILD_TEMPLATE, parent, factoryName, viewRefId);
                svgChildrenRaw.add(childRaw);
            } else {
                throw new RuntimeException("The view [" + viewRefId + "] references " + "another the view [" + svgName + "], but no factory method " + "for it exists in [" + viewFactory.getImplementedType() + "]");
            }
        });
        // Look for the state shape view.
        final List<ShapeDefinition> stateViews = new LinkedList<>();
        SVGModelUtils.visit(viewDefinition, p -> {
            if (p instanceof ShapeDefinition && SVGPrimitiveGeneratorUtils.CAN_GENERATE_PRIMITIVE_CODE.test(p)) {
                final ShapeDefinition shapeDefinition = (ShapeDefinition) p;
                shapeDefinition.getStateDefinition().ifPresent(s -> stateViews.add((ShapeDefinition) p));
            }
        });
        final String stateViewIds = stateViews.isEmpty() ? "view" : stateViews.stream().map(d -> SVGGeneratorFormatUtils.getValidInstanceId(d.getId())).collect(Collectors.joining(","));
        final String stateViewPolicyType = stateViews.isEmpty() ? ShapeStateDefaultHandler.RenderType.STROKE.name() : ((ShapeDefinition.ShapeStateDefinition) stateViews.get(0).getStateDefinition().get()).name();
        final String stateViewPolicy = ShapeStateDefaultHandler.RenderType.class.getName().replace("$", ".") + "." + stateViewPolicyType.toUpperCase();
        // Generate the main shape.
        final PrimitiveDefinitionGenerator<PrimitiveDefinition<?>> mainGenerator = getGenerator(main);
        final StringBuffer mainBuffer = mainGenerator.generate(main);
        final LayoutDefinition mainLayoutDefinition = main.getLayoutDefinition();
        final String mainLayoutRaw = SVGPrimitiveGeneratorUtils.formatLayout(mainLayoutDefinition);
        // Generate the view's text styling stuff.
        final StyleSheetDefinition globalStyleSheetDefinition = ((ViewDefinitionImpl) viewDefinition).getGlobalStyleSheetDefinition();
        final String viewTextRaw = null != globalStyleSheetDefinition ? SVGShapeTextCodeBuilder.generate("view", viewId, globalStyleSheetDefinition) : "";
        // Populate the context and generate using the template.
        root.put("viewId", viewId);
        root.put("name", methodName);
        root.put("mainShape", mainBuffer.toString());
        root.put("layout", mainLayoutRaw);
        root.put("width", formatDouble(viewDefinition.getWidth()));
        root.put("height", formatDouble(viewDefinition.getHeight()));
        root.put("text", viewTextRaw);
        root.put("stateViewIds", stateViewIds);
        root.put("stateViewPolicy", stateViewPolicy);
        root.put("children", childrenRaw);
        root.put("svgChildren", svgChildrenRaw);
        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) ShapeDefinition(org.kie.workbench.common.stunner.svg.gen.model.ShapeDefinition) HashMap(java.util.HashMap) PrimitiveDefinition(org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition) LinkedList(java.util.LinkedList) ViewRefDefinition(org.kie.workbench.common.stunner.svg.gen.model.ViewRefDefinition) StyleSheetDefinition(org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition) GenerationException(org.uberfire.annotations.processors.exceptions.GenerationException) LayoutDefinition(org.kie.workbench.common.stunner.svg.gen.model.LayoutDefinition) GeneratorException(org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)

Example 3 with LayoutDefinition

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

the class AbstractSVGPrimitiveTranslator method translateLayout.

protected LayoutDefinition translateLayout(final E element, final O def) {
    final String layoutRaw = element.getAttributeNS(SVGDocumentTranslator.STUNNER_URI, SVGDocumentTranslator.STUNNER_ATTR_NS_LAYOUT);
    final LayoutDefinition l = isEmpty(layoutRaw) ? LayoutDefinition.NONE : LayoutDefinition.valueOf(layoutRaw);
    def.setLayoutDefinition(l);
    return l;
}
Also used : LayoutDefinition(org.kie.workbench.common.stunner.svg.gen.model.LayoutDefinition)

Aggregations

LayoutDefinition (org.kie.workbench.common.stunner.svg.gen.model.LayoutDefinition)3 GeneratorException (org.kie.workbench.common.stunner.svg.gen.exception.GeneratorException)2 PrimitiveDefinition (org.kie.workbench.common.stunner.svg.gen.model.PrimitiveDefinition)2 ShapeDefinition (org.kie.workbench.common.stunner.svg.gen.model.ShapeDefinition)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 StyleSheetDefinition (org.kie.workbench.common.stunner.svg.gen.model.StyleSheetDefinition)1 ViewRefDefinition (org.kie.workbench.common.stunner.svg.gen.model.ViewRefDefinition)1 GroupDefinition (org.kie.workbench.common.stunner.svg.gen.model.impl.GroupDefinition)1 ViewDefinitionImpl (org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl)1 GenerationException (org.uberfire.annotations.processors.exceptions.GenerationException)1