de.knightsoftnet.gwtp.spring.annotation.processor.BackofficeUiBinderCreator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gwtp-spring-integration-client Show documentation
Show all versions of gwtp-spring-integration-client Show documentation
Basics to bring GWTP and Spring together.
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("
");
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.print(" ");
out.println(" ");
out.println("
");
out.println("
");
out.println(" ");
out.println(" ");
out.println("
");
out.println(" ");
out.println(" ");
out.println("
");
}
private void writeXmlTokenImports(final PrintWriter out, final String serverPackage,
final String entityName) {
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(" ");
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("