Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
de.knightsoftnet.gwtp.spring.annotation.processor.BackofficeViewCreator Maven / Gradle / Ivy
package de.knightsoftnet.gwtp.spring.annotation.processor;
import de.knightsoftnet.gwtp.spring.client.annotation.BackofficeClientGenerator;
import de.knightsoftnet.validators.annotation.processor.TypeUtils;
import de.knightsoftnet.validators.shared.data.FieldTypeEnum;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
/**
* Create gwtp view class.
*/
public class BackofficeViewCreator extends AbstractBackofficeCreator {
protected static final String CLASS_SUFFIX = CLASS_SUFFIX_VIEW;
public BackofficeViewCreator() {
super(CLASS_SUFFIX);
}
@Override
protected void addAdditionalImports(final String serverPackage, final Element element,
final BackofficeClientGenerator annotationInterface,
final ProcessingEnvironment processingEnv) {
addImports(//
"de.knightsoftnet.mtwidgets.client.ui.page.admin.AbstractAdminView", //
"de.knightsoftnet.mtwidgets.client.ui.widget.AdminNavigationWidget", //
"de.knightsoftnet.validators.client.editor.BeanValidationEditorDriver", //
"de.knightsoftnet.validators.client.editor.annotation.IsValidationDriver", //
"com.google.gwt.uibinder.client.UiBinder", //
"com.google.gwt.uibinder.client.UiField", //
"com.google.gwt.user.client.ui.Widget", //
"javax.inject.Inject", //
"javax.inject.Provider");
final String entityName = getEntityNameOfElement(element);
addImport(element.asType());
addImport(serverPackage + "." + entityName + BackofficeLocalInterfaceCreator.CLASS_SUFFIX);
addImport(
serverPackage + "." + entityName + BackofficePresenterCreator.CLASS_SUFFIX + ".MyProxy");
addImport(
serverPackage + "." + entityName + BackofficePresenterCreator.CLASS_SUFFIX + ".MyView");
detectBackofficeWidgetsOfElement("", null, element, processingEnv, true).forEach(
widget -> widget.getImports().stream().forEach(importEntry -> addImport(importEntry)));
}
@Override
protected void writeBody(final PrintWriter out, final String serverPackage, final Element element,
final BackofficeClientGenerator annotationInterface,
final ProcessingEnvironment processingEnv) {
final String entityName = getEntityNameOfElement(element);
final List widgets =
detectBackofficeWidgetsOfElement("", null, element, processingEnv, true);
out.print("public class ");
out.print(entityName);
out.print(suffix);
out.print(" extends AbstractAdminView<");
out.print(entityName);
out.println(", MyProxy, MyView>");
out.println(" implements MyView {");
out.println();
out.print(" public static final String TOKEN = \"");
out.print(annotationInterface.baseToken());
out.println("\";");
out.println(" public static final String TOKEN_WITH_ID = TOKEN + \"{id}\";");
out.println();
out.print(" interface Binder extends UiBinder {");
out.println(" }");
out.println();
out.println(" @IsValidationDriver");
out.print(" interface Driver extends BeanValidationEditorDriver<");
out.print(entityName);
out.print(", ");
out.print(entityName);
out.print(suffix);
out.println("> {");
out.println(" }");
out.println();
writeUiFieldDefinitions(out, widgets, element, annotationInterface, processingEnv);
out.println();
out.println(" @Inject");
out.print(" public ");
out.print(entityName);
out.print(suffix);
out.println("(final Driver driver, final Binder uiBinder,");
out.print(" final Provider> adminNavigationProvider,");
out.print(" final ");
out.print(entityName);
out.print(BackofficeLocalInterfaceCreator.CLASS_SUFFIX);
out.println(" messages) {");
out.println(" super(driver, adminNavigationProvider);");
writeInitializationProvidedUiFields(out, widgets);
out.println(" initWidget(uiBinder.createAndBindUi(this));");
writeUiFieldDrivers(out, widgets);
out.println(" driver.initialize(this);");
out.println(" driver.setSubmitButton(adminNavigation.getSaveEntry());");
out.println(" driver.addFormSubmitHandler(this);");
writeUiFieldValueSetting(out, widgets);
out.println(" }");
writeUiHandlers(out, widgets);
out.println("}");
}
private void writeUiFieldDefinitions(final PrintWriter out, final List widgets,
final Element element, final BackofficeClientGenerator annotationInterface,
final ProcessingEnvironment processingEnv) {
widgets.stream().filter(widget -> !widget.isIgnore()).forEach(widget -> {
switch (widget.getFieldType()) {
case EMBEDED:
writeEmbeddedView(widget, element, annotationInterface, processingEnv);
break;
case ONE_TO_MANY:
writeOneToManyEntryView(widget, element, annotationInterface, processingEnv);
writeOneToManyListEditor(widget, element, processingEnv);
break;
default:
break;
}
if (widget.isProvided()) {
out.println(" @UiField(provided = true)");
} else {
out.println(" @UiField");
}
out.print(" ");
out.print(widget.getWidgetName());
out.print(" ");
out.print(widget.getName());
out.println(";");
});
}
private void writeInitializationProvidedUiFields(final PrintWriter out,
final List widgets) {
widgets.stream().filter(widget -> widget.isProvided()).forEach(widget -> {
out.print(" ");
out.print(widget.getName());
out.print(" = ");
out.println(widget.getProvidedConstruction());
});
}
private void writeUiFieldValueSetting(final PrintWriter out,
final List widgets) {
widgets.stream().filter(widget -> !widget.isIgnore()).forEach(widget -> widget.getParameters()
.stream().filter(param -> !param.isUiBinder()).forEach(param -> {
out.print(" ");
out.print(widget.getName());
out.print(".");
out.print("set" + StringUtils.capitalize(param.getName()));
out.print("(");
out.print(param.getValue());
out.println(");");
}));
}
private void writeUiFieldDrivers(final PrintWriter out, final List widgets) {
widgets.stream()
.filter(widget -> !widget.isIgnore() && widget.getFieldType() == FieldTypeEnum.ONE_TO_MANY)
.forEach(widget -> {
out.print(" ");
out.print(widget.getName());
out.println(".setParentDriver(this.driver);");
});
}
private void writeUiHandlers(final PrintWriter out, final List widgets) {
widgets.stream()
.filter(widget -> !widget.isIgnore() && widget.getFieldType() == FieldTypeEnum.ONE_TO_MANY)
.forEach(widget -> {
out.println();
out.print(" @UiHandler(\"");
out.print(fieldNameToCamelCase("add." + widget.getName()));
out.println("\")");
out.print(" public void ");
out.print(fieldNameToCamelCase("add." + widget.getName()));
out.println("(final ClickEvent event) {");
out.print(" ");
out.print(widget.getName());
out.println(".addNewEntry();");
out.println(" }");
});
}
/**
* write class of a embedded view.
*
* @param widget for which we have to generate the view
* @param element the element which represents the entity
* @param annotationInterface annotation interface
* @param processingEnv processing environment
*/
private void writeEmbeddedView(final BackofficeWidget widget, final Element element,
final BackofficeClientGenerator annotationInterface,
final ProcessingEnvironment processingEnv) {
final String serverPackage = detectPackage(element, processingEnv);
final String entityName = getEntityNameOfElement(widget.getField());
final String entityType = widget.getWidgetName();
try {
final JavaFileObject builderFile =
processingEnv.getFiler().createSourceFile(serverPackage + "." + entityType);
try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
writePackage(serverPackage, out);
addAdditionalImports(serverPackage, element, annotationInterface, processingEnv);
writeImports(embeddedImports(widget), out, serverPackage);
out.println();
out.print("public class ");
out.print(entityType);
out.print(" extends Composite implements HasEditorDelegate<");
out.print(entityName);
out.println("> {");
out.println();
writeUiBinderInterface(entityType, out);
writeUiFieldDefinitions(out, widget.getChildWidgets(), element, annotationInterface,
processingEnv);
writeConstructor(widget, entityType, out);
out.println();
out.println(" @Override");
out.print(" public void setDelegate(final EditorDelegate<");
out.print(entityName);
out.println("> delegate) {");
out.println(" delegate.subscribe();");
out.println(" }");
out.println("}");
}
} catch (final IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
e.printStackTrace();
}
}
private void writeUiBinderInterface(final String entityType, final PrintWriter out) {
out.print(" interface Binder extends UiBinder {");
out.println(" }");
out.println();
}
private void writeConstructor(final BackofficeWidget widget, final String entityType,
final PrintWriter out) {
out.println();
out.print(" public ");
out.print(entityType);
out.println("() {");
out.println(" super();");
out.println(" final Binder uiBinder = GWT.create(Binder.class);");
if (widget.getChildWidgets().stream().anyMatch(childWidget -> childWidget.isProvided())) {
out.print(" final ");
out.print(TypeUtils.getClassNameWithoutPath(widget.getContaining()));
out.print(BackofficeLocalInterfaceCreator.CLASS_SUFFIX);
out.print(" messages = GWT.create(");
out.print(TypeUtils.getClassNameWithoutPath(widget.getContaining()));
out.print(BackofficeLocalInterfaceCreator.CLASS_SUFFIX);
out.println(".class);");
}
writeInitializationProvidedUiFields(out, widget.getChildWidgets());
out.println(" initWidget(uiBinder.createAndBindUi(this));");
writeUiFieldValueSetting(out, widget.getChildWidgets());
out.println(" }");
}
private List embeddedImports(final BackofficeWidget widget) {
final List imports = new ArrayList<>();
imports.add(widget.getField().asType().toString());
imports.add("com.google.gwt.core.shared.GWT");
imports.add("com.google.gwt.uibinder.client.UiBinder");
imports.add("com.google.gwt.uibinder.client.UiField");
imports.add("com.google.gwt.user.client.ui.Composite");
imports.add("com.google.gwt.user.client.ui.Widget");
imports.add("org.gwtproject.editor.client.EditorDelegate");
imports.add("org.gwtproject.editor.client.HasEditorDelegate");
widget.getImports().stream().forEach(importEntry -> imports.add(importEntry));
widget.getChildWidgets().forEach(fieldWidget -> fieldWidget.getImports().stream()
.forEach(importEntry -> imports.add(importEntry)));
return imports;
}
/**
* write class of a one to many entry view.
*
* @param widget for which we have to generate the view
* @param element the element which represents the entity
* @param annotationInterface annotation interface
* @param processingEnv processing environment
*/
private void writeOneToManyEntryView(final BackofficeWidget widget, final Element element,
final BackofficeClientGenerator annotationInterface,
final ProcessingEnvironment processingEnv) {
final String serverPackage = detectPackage(element, processingEnv);
final String entityType = widget.getWidgetName() + "Entry";
final String entityName = getEntityNameOfFirstTypeElement(widget.getField());
try {
final JavaFileObject builderFile =
processingEnv.getFiler().createSourceFile(serverPackage + "." + entityType);
try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
writePackage(serverPackage, out);
writeImports(oneToManyEntryImports(widget), out, serverPackage);
out.println();
out.print("public class ");
out.print(entityType);
out.print(" extends AbstractListItemView<");
out.print(entityName);
out.println("> {");
out.println();
writeUiBinderInterface(entityType, out);
writeUiFieldDefinitions(out, widget.getChildWidgets(), element, annotationInterface,
processingEnv);
writeConstructor(widget, entityType, out);
out.println();
out.println(" @UiHandler(\"deleteRow\")");
out.println(" public void onDeleteRow(final ClickEvent event) {");
out.println(" removeThisEntry();");
out.println(" }");
out.println("}");
}
} catch (final IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
e.printStackTrace();
}
}
private List oneToManyEntryImports(final BackofficeWidget widget) {
final List imports = new ArrayList<>();
imports.add("de.knightsoftnet.mtwidgets.client.ui.widget.helper.AbstractListItemView");
imports.add("com.google.gwt.core.shared.GWT");
imports.add("com.google.gwt.event.dom.client.ClickEvent");
imports.add("com.google.gwt.uibinder.client.UiBinder");
imports.add("com.google.gwt.uibinder.client.UiField");
imports.add("com.google.gwt.user.client.ui.Widget");
widget.getImports().stream().forEach(importEntry -> imports.add(importEntry));
widget.getChildWidgets().stream().filter(childWidget -> !childWidget.isIgnore())
.forEach(fieldWidget -> fieldWidget.getImports().stream()
.forEach(importEntry -> imports.add(importEntry)));
return imports;
}
/**
* write class of a one to many list editor.
*
* @param widget for which we have to generate the view
* @param element the element which represents the entity
* @param processingEnv processing environment
*/
private void writeOneToManyListEditor(final BackofficeWidget widget, final Element element,
final ProcessingEnvironment processingEnv) {
final String serverPackage = detectPackage(element, processingEnv);
final String entityType = widget.getWidgetName();
final String entityName = getEntityNameOfFirstTypeElement(widget.getField());
try {
final JavaFileObject builderFile =
processingEnv.getFiler().createSourceFile(serverPackage + "." + entityType);
try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
writePackage(serverPackage, out);
writeImports(oneToManyListEditorImports(widget), out, serverPackage);
out.println();
out.print("public class ");
out.print(entityType);
out.print(" extends AbstractListEditor<");
out.print(entityName);
out.print(", ");
out.print(entityType);
out.println("Entry> {");
out.println();
out.println(" @IsDriver");
out.print(" interface Driver extends SimpleBeanEditorDriver,");
out.print(" ListValidationEditor<");
out.print(entityName);
out.print(", ");
out.print(entityType);
out.println("Entry>> {");
out.println(" }");
out.println();
out.print(" private static Driver driver = new ");
out.print(entityType);
out.println("_Driver_Impl();");
out.println();
out.print(" private class ");
out.print(entityName);
out.println("ItemEditorSource");
out.print(" extends AbstractItemEditorSource<");
out.print(entityName);
out.print(", ");
out.print(entityType);
out.println("Entry> {");
out.println();
out.print(" public ");
out.print(entityName);
out.println("ItemEditorSource() {");
out.print(" super(");
out.print(entityType);
out.println(".this);");
out.println(" }");
out.println();
out.println(" @Override");
out.print(" protected ");
out.print(entityType);
out.println("Entry createItemView(final int index) {");
out.print(" return new ");
out.print(entityType);
out.println("Entry();");
out.println(" }");
out.println(" }");
out.println();
out.print(" private final ListValidationEditor<");
out.print(entityName);
out.print(", ");
out.print(entityType);
out.println("Entry> editor =");
out.print(" ListValidationEditor.of(new ");
out.print(entityName);
out.println("ItemEditorSource());");
out.println();
out.print(" public ");
out.print(entityType);
out.println("() {");
out.println(" super();");
out.println(" driver.initialize(editor);");
out.println(" }");
out.println();
out.println(" @Override");
out.print(" public ListValidationEditor<");
out.print(entityName);
out.print(", ");
out.print(entityType);
out.println("Entry> asEditor() {");
out.println(" return editor;");
out.println(" }");
out.println();
out.println(" @Override");
out.print(" protected ");
out.print(entityName);
out.println(" createData() {");
out.print(" return new ");
out.print(entityName);
out.println("();");
out.println(" }");
out.println("}");
}
} catch (final IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
e.printStackTrace();
}
}
private void writePackage(final String serverPackage, final PrintWriter out) {
out.print("package ");
out.print(serverPackage);
out.println(";");
out.println();
}
private List oneToManyListEditorImports(final BackofficeWidget widget) {
final List imports = new ArrayList<>();
imports.add("de.knightsoftnet.mtwidgets.client.ui.widget.helper.AbstractItemEditorSource");
imports.add("de.knightsoftnet.mtwidgets.client.ui.widget.helper.AbstractListEditor");
imports.add("de.knightsoftnet.validators.client.editor.impl.ListValidationEditor");
imports.add("org.gwtproject.editor.client.SimpleBeanEditorDriver");
imports.add("org.gwtproject.editor.client.annotation.IsDriver");
imports.add("java.util.List");
widget.getImports().stream().forEach(importEntry -> imports.add(importEntry));
return imports;
}
}