All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.knightsoftnet.gwtp.spring.annotation.processor.BackofficeUiBinderCreator Maven / Gradle / Ivy

There is a newer version: 2.3.2
Show newest version
package de.knightsoftnet.gwtp.spring.annotation.processor;

import de.knightsoftnet.gwtp.spring.client.annotation.BackofficeClientGenerator;
import de.knightsoftnet.validators.shared.data.FieldTypeEnum;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

/**
 * Create localization property file.
 */
public class BackofficeUiBinderCreator
    extends AbstractBackofficeCreator {

  protected static final String CLASS_EXTENSION = ".ui.xml";
  protected static final List> GRID_COLUMNS = List.of(

      List.of(11, 1), //
      List.of(6, 5, 1), //
      List.of(4, 4, 3, 1), //
      List.of(3, 3, 3, 2, 1), //
      List.of(4, 4, 4, 4, 7, 1), //
      List.of(4, 4, 4, 4, 4, 3, 1), //
      List.of(3, 3, 3, 3, 3, 3, 5, 1), //
      List.of(3, 3, 3, 3, 3, 3, 3, 2, 1), //
      List.of(3, 3, 3, 3, 3, 3, 3, 3, 11, 1), //
      List.of(3, 3, 3, 3, 3, 3, 3, 3, 3, 8, 1), //
      List.of(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 1), //
      List.of(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1)

  );

  public BackofficeUiBinderCreator() {
    super(CLASS_SUFFIX_VIEW + CLASS_EXTENSION);
  }

  /**
   * write ui binder file.
   */
  public void writeUiBinder(final Element element, final BackofficeClientGenerator generator,
      final ProcessingEnvironment processingEnv) {
    final String serverPackage = detectPackage(element, processingEnv);
    final String resourceName = getEntityNameOfElement(element) + suffix;

    try {
      final FileObject builderFile = processingEnv.getFiler()
          .createResource(StandardLocation.SOURCE_OUTPUT, serverPackage, resourceName);
      try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
        writeUiBinder(out, element, generator, serverPackage, processingEnv);
      }
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }


  private void writeUiBinder(final PrintWriter out, final Element element,
      final BackofficeClientGenerator generator, final String serverPackage,
      final ProcessingEnvironment processingEnv) throws IOException {
    final String entityName = getEntityNameOfElement(element);
    final List widgets =
        detectBackofficeWidgetsOfElement("", null, element, processingEnv, true);

    writeXmlHeader(out, generator, serverPackage, entityName, widgets);
    writeXmlTokenImports(out, serverPackage, entityName);

    out.print("  ");

    writeXmlAdminNavigation(out, generator);
    writeXmlFieldWidgets(out, generator, widgets, serverPackage, entityName, StringUtils.EMPTY,
        true, processingEnv);

    out.println("  ");
    out.println("");
  }

  private void writeEmbeddedUiBinder(final BackofficeWidget widget,
      final BackofficeClientGenerator generator, final String serverPackage,
      final String entityName, final String prefix, final ProcessingEnvironment processingEnv)
      throws IOException {
    final List widgets = widget.getChildWidgets();

    final FileObject builderFile = processingEnv.getFiler().createResource(
        StandardLocation.SOURCE_OUTPUT, serverPackage, widget.getWidgetName() + CLASS_EXTENSION);
    try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {

      writeXmlHeader(out, generator, serverPackage, entityName, widgets);

      out.println("  ");

      writeXmlFieldWidgets(out, generator, widgets, serverPackage, entityName, prefix, false,
          processingEnv);

      out.println("  ");
      out.println("");
    }
  }

  private void writeXmlFieldWidgets(final PrintWriter out,
      final BackofficeClientGenerator generator, final List widgets,
      final String serverPackage, final String entityName, final String prefix,
      final boolean autofocusHandling, final ProcessingEnvironment processingEnv)
      throws IOException {
    boolean divOpen = false;
    boolean autofocusSet = !autofocusHandling;
    boolean idExists = false;
    int widgetsInLine = 2;
    for (final BackofficeWidget widget : widgets) {
      if (!widget.isIgnore()) {
        if (widgetsInLine >= 2 || widget.getFieldType() == FieldTypeEnum.EMBEDED
            || widget.getFieldType() == FieldTypeEnum.ONE_TO_MANY) {
          if (divOpen) {
            out.println("    
"); } if (widget.getFieldType() == FieldTypeEnum.EMBEDED || widget.getFieldType() == FieldTypeEnum.ONE_TO_MANY) { divOpen = false; widgetsInLine = 2; } else { out.print("
"); divOpen = true; widgetsInLine = 0; } } if ("id".equals(widget.getName())) { idExists = true; } else if (!autofocusSet && idExists) { widget.getParameters().add(BackofficeWidgetParameter.of("autofocus", "true", true)); autofocusSet = true; } writeWidgetWithLabel(out, widget, generator, serverPackage, entityName, prefix, processingEnv); widgetsInLine++; } } if (divOpen) { out.println("
"); } } private void writeXmlAdminNavigation(final PrintWriter out, final BackofficeClientGenerator generator) { out.print("
"); out.print("
"); out.println("

"); out.println(" "); out.println("

"); out.println("
"); out.println("
"); out.print("
"); out.println(" "); out.println("
"); } private void writeXmlTokenImports(final PrintWriter out, final String serverPackage, final String entityName) { out.print(" "); out.print(" "); out.println(""); } private void writeXmlHeader(final PrintWriter out, final BackofficeClientGenerator generator, final String serverPackage, final String entityName, final List widgets) { out.println(""); out.println(" widget.getFieldType() == FieldTypeEnum.EMBEDED || widget.getFieldType() == FieldTypeEnum.ONE_TO_MANY)) { out.print(" xmlns:e=\"urn:import:"); out.print(serverPackage); out.println("\""); } out.println(" xmlns:g=\"urn:import:com.google.gwt.user.client.ui\""); out.println(" xmlns:w=\"urn:import:de.knightsoftnet.mtwidgets.client.ui.widget\">"); out.println(""); out.print(" "); for (final String header : generator.uiBinderHeader()) { out.print(" "); out.println(header); } out.println(""); } private void writeWidgetWithLabel(final PrintWriter out, final BackofficeWidget widget, final BackofficeClientGenerator generator, final String serverPackage, final String entityName, final String prefix, final ProcessingEnvironment processingEnv) throws IOException { if (widget.getFieldType() != FieldTypeEnum.EMBEDED && widget.getFieldType() != FieldTypeEnum.ONE_TO_MANY) { out.print("
"); out.print(" "); out.println("
"); out.print("
"); } writeWidget(out, widget, generator, serverPackage, entityName, prefix, processingEnv); if (widget.getFieldType() != FieldTypeEnum.EMBEDED && widget.getFieldType() != FieldTypeEnum.ONE_TO_MANY) { out.println("
"); } } private void writeWidget(final PrintWriter out, final BackofficeWidget widget, final BackofficeClientGenerator generator, final String serverPackage, final String entityName, final String prefix, final ProcessingEnvironment processingEnv) throws IOException { switch (widget.getFieldType()) { case EMBEDED: writeEmbededWidget(out, widget, generator, serverPackage, entityName, prefix, processingEnv); break; case ONE_TO_MANY: writeOneToManyWidget(out, widget, generator, serverPackage, entityName, prefix, processingEnv); break; default: out.print(" param.isUiBinder()).forEach(param -> { out.print(" "); out.print(param.getName()); out.print("=\""); out.print(param.getValue()); out.print("\""); }); out.print(" validationMessageElement=\"{"); out.print(widget.getName()); out.println("Validation}\" />"); out.print(" "); break; } } private void writeEmbededWidget(final PrintWriter out, final BackofficeWidget widget, final BackofficeClientGenerator generator, final String serverPackage, final String entityName, final String prefix, final ProcessingEnvironment processingEnv) throws IOException { out.print(" "); final String newPrefix = prefix + widget.getName() + "."; writeEmbeddedUiBinder(widget, generator, serverPackage, entityName, newPrefix, processingEnv); } private void writeOneToManyWidget(final PrintWriter out, final BackofficeWidget widget, final BackofficeClientGenerator generator, final String serverPackage, final String entityName, final String prefix, final ProcessingEnvironment processingEnv) throws IOException { final String newPrefix = prefix + widget.getName() + "."; out.print("
"); final List displayWidgets = extractBackofficeWidgets(widget); int gridPos = 0; for (final BackofficeWidget displayWidget : displayWidgets) { writeColumnDiv(out, GRID_COLUMNS.get(displayWidgets.size() - 1).get(gridPos++), generator); out.print(" "); out.println("
"); } writeColumnDiv(out, GRID_COLUMNS.get(displayWidgets.size() - 1).get(gridPos), generator); out.println("  "); out.println("
"); out.println(" "); out.print(" "); out.print("
"); out.print("
"); out.print(" "); out.println("
"); out.println("
"); out.print("
"); out.print("
"); out.print(" "); out.print(" ➕ "); out.println(" "); out.println("
"); out.println("
"); writeOneToManyUiBinder(widget, generator, serverPackage, entityName, newPrefix, processingEnv); } private void writeColumnDiv(final PrintWriter out, final int gridWidth, final BackofficeClientGenerator generator) { out.print("
"); } private List extractBackofficeWidgets(final BackofficeWidget widget) { final List displayWidgets = widget.getChildWidgets().stream() .filter(refwidget -> !(refwidget.isIgnore() || "id".equals(refwidget.getName()))) .collect(Collectors.toList()); return displayWidgets; } private void writeOneToManyUiBinder(final BackofficeWidget widget, final BackofficeClientGenerator generator, final String serverPackage, final String entityName, final String prefix, final ProcessingEnvironment processingEnv) throws IOException { final FileObject builderFile = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, serverPackage, widget.getWidgetName() + "Entry" + CLASS_EXTENSION); try (PrintWriter out = new PrintWriter(builderFile.openWriter())) { writeXmlHeader(out, generator, serverPackage, entityName, widget.getChildWidgets()); out.print(" "); final List displayWidgets = extractBackofficeWidgets(widget); int gridPos = 0; for (final BackofficeWidget displayWidget : displayWidgets) { writeColumnDiv(out, GRID_COLUMNS.get(displayWidgets.size() - 1).get(gridPos++), generator); writeWidget(out, displayWidget, generator, serverPackage, entityName, prefix, processingEnv); out.println("
"); } writeColumnDiv(out, GRID_COLUMNS.get(displayWidgets.size() - 1).get(gridPos++), generator); final Optional idWidget = widget.getChildWidgets().stream() .filter(refwidget -> "id".equals(refwidget.getName())).findFirst(); if (idWidget.isPresent()) { out.print(" param.isUiBinder()) .forEach(param -> { out.print(" "); out.print(param.getName()); out.print("=\""); out.print(param.getValue()); out.print("\""); }); out.println(" visible=\"false\"/>"); } out.println(" "); out.println(" "); out.println(" "); out.println(""); } } @Override protected void addAdditionalImports(final String serverPackage, final Element element, final BackofficeClientGenerator annotationInterface, final ProcessingEnvironment processingEnv) { // ignore } @Override protected void writeBody(final PrintWriter out, final String serverPackage, final Element element, final BackofficeClientGenerator annotationInterface, final ProcessingEnvironment processingEnv) { // ignore } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy