use of org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl 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;
}
use of org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl in project kie-wb-common by kiegroup.
the class SVGGeneratorImpl method parseSVGView.
private ViewDefinitionImpl parseSVGView(final String viewId, final String svgPath, final InputStream svgStream, final StyleSheetDefinition styleSheetDefinition) throws Exception {
ViewDefinitionImpl svgShapeViewSource = null;
try {
Document root = parse(svgStream);
svgShapeViewSource = translate(viewId, svgPath, root, styleSheetDefinition);
} catch (final Exception e) {
throw new GeneratorException(e);
}
return svgShapeViewSource;
}
use of org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl 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;
}
use of org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl in project kie-wb-common by kiegroup.
the class SVGViewDefinitionGeneratorTest method testGenerate.
@Test
public void testGenerate() throws Exception {
final ViewFactory viewFactory = new ViewFactoryImpl("view1", "org.kie.test", "MyType.¢lass");
final RectDefinition mainDef = new RectDefinition("rect1", 1, 2, 0);
final CircleDefinition circleDefinition = new CircleDefinition("circle1", 25);
final ViewDefinitionImpl viewDefinition = new ViewDefinitionImpl(ID, X, Y, WIDTH, HEIGHT, styleSheetDefinition, new ViewBoxDefinitionImpl(VBOX_MIN_X, VBOX_MIN_Y, VBOX_WIDTH, VBOX_HEIGHT), mainDef, circleDefinition);
viewDefinition.setFactoryMethodName("svgViewTest");
viewDefinition.setPath("svg-view-test.svg");
final String generated = tested.generate(viewFactory, viewDefinition).toString();
assertTrue(generated.contains("public SVGShapeViewResource svgViewTest()"));
assertTrue(generated.contains("private SVGShapeView svgViewTestView(final boolean resizable)"));
assertTrue(generated.contains("return this.svgViewTestView(25.50d, 225.45d, resizable);"));
assertTrue(generated.contains("private SVGShapeView svgViewTestView(final double width, final double height, final boolean resizable) {"));
assertTrue(generated.contains("final SVGShapeViewImpl view = new SVGShapeViewImpl(\"viewDef1\", mainShape, width, height, resizable)"));
assertTrue(generated.contains("private SVGBasicShapeView svgViewTestBasicView() {"));
assertTrue(generated.contains("return this.svgViewTestBasicView(25.50d, 225.45d);"));
assertTrue(generated.contains("private SVGBasicShapeView svgViewTestBasicView(final double width, final double height) {"));
assertTrue(generated.contains("final SVGBasicShapeViewImpl view = new SVGBasicShapeViewImpl(\"viewDef1\", mainShape, width, height)"));
}
use of org.kie.workbench.common.stunner.svg.gen.model.impl.ViewDefinitionImpl 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;
}
Aggregations