From de990f625254620a9d3d8b77ee8b2a47f82f9015 Mon Sep 17 00:00:00 2001 From: Frank Hossfeld Date: Thu, 1 Apr 2021 18:13:32 +0200 Subject: [PATCH] #185 - add the possibility to execute in action in case a ShowPopUpEvent is canceled by a filter - done (#186) --- etc/wiki/14 Controller & Component.md | 25 +++++++++ .../nalukit/nalu/processor/NaluProcessor.java | 1 + .../PopUpControllerAnnotationValidator.java | 4 -- .../PopUpFiltersAnnotationValidator.java | 34 +++++++++++- .../client/component/AbstractPopUpFilter.java | 37 ++++++++----- .../nalu/client/filter/IsPopUpFilter.java | 9 ++++ .../application/PopUpControllerFactory.java | 53 +++++++++++-------- 7 files changed, 123 insertions(+), 40 deletions(-) diff --git a/etc/wiki/14 Controller & Component.md b/etc/wiki/14 Controller & Component.md index 85570077f..472cfae25 100644 --- a/etc/wiki/14 Controller & Component.md +++ b/etc/wiki/14 Controller & Component.md @@ -784,6 +784,31 @@ public class MyPopUpFilter } ``` +It is possible to add a (optinal) cancel handler, which gets executed in case the `ShowPopUpEvent` is canceled: + +```java +public class MyPopUpFilter + extends AbstractPopUpFilter { + + public IolaniEditPopUpFilter() { + } + + @Override + public boolean filter(ShowPopUpEvent event) { + if ("MyDoNotShowPopUp".equals(event.getName())) { + return false; // never show this popup + } + return true; // we are happy to continue + } + + @Override + public IsPopUpFilter.CancelHandler getCancelHandler() { + return () -> [Do something ... ]; + } + +} +``` + By using the `PopUpFilter`-annotation, you can register the popup filter: ```java @Application(context = MyContext.class, diff --git a/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/NaluProcessor.java b/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/NaluProcessor.java index d0dfa0630..72d743a4d 100644 --- a/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/NaluProcessor.java +++ b/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/NaluProcessor.java @@ -578,6 +578,7 @@ private void handlePopUpFiltersAnnotation(RoundEnvironment roundEnv) PopUpFiltersAnnotationValidator.builder() .roundEnvironment(roundEnv) .processingEnvironment(processingEnv) + .popUpFilterElement(popUpFiltersElement) .build() .validate(popUpFiltersElement); // scan filter element diff --git a/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/scanner/validation/PopUpControllerAnnotationValidator.java b/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/scanner/validation/PopUpControllerAnnotationValidator.java index fa50797e9..bb5f63f17 100644 --- a/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/scanner/validation/PopUpControllerAnnotationValidator.java +++ b/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/scanner/validation/PopUpControllerAnnotationValidator.java @@ -28,9 +28,7 @@ public class PopUpControllerAnnotationValidator { private ProcessorUtils processorUtils; - private ProcessingEnvironment processingEnvironment; - private Element popUpControllerElement; @SuppressWarnings("unused") @@ -81,9 +79,7 @@ public void validate() public static final class Builder { ProcessingEnvironment processingEnvironment; - RoundEnvironment roundEnvironment; - Element popUpControllerElement; public Builder processingEnvironment(ProcessingEnvironment processingEnvironment) { diff --git a/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/scanner/validation/PopUpFiltersAnnotationValidator.java b/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/scanner/validation/PopUpFiltersAnnotationValidator.java index cc3e559c0..a1e298b31 100644 --- a/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/scanner/validation/PopUpFiltersAnnotationValidator.java +++ b/nalu-processor/src/main/java/com/github/nalukit/nalu/processor/scanner/validation/PopUpFiltersAnnotationValidator.java @@ -15,20 +15,33 @@ */ package com.github.nalukit.nalu.processor.scanner.validation; +import com.github.nalukit.nalu.client.application.IsApplication; import com.github.nalukit.nalu.processor.ProcessorException; +import com.github.nalukit.nalu.processor.ProcessorUtils; import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.RoundEnvironment; import javax.lang.model.element.Element; +import javax.lang.model.element.TypeElement; public class PopUpFiltersAnnotationValidator { + private ProcessorUtils processorUtils; + private ProcessingEnvironment processingEnvironment; + private Element popUpFilterElement; + @SuppressWarnings("unused") private PopUpFiltersAnnotationValidator() { } @SuppressWarnings("unused") private PopUpFiltersAnnotationValidator(Builder builder) { + this.popUpFilterElement = builder.popUpFilterElement; + this.processingEnvironment = builder.processingEnvironment; + this.processorUtils = ProcessorUtils.builder() + .processingEnvironment(processingEnvironment) + .build(); + ; setUp(); } @@ -46,13 +59,32 @@ public void validate(Element popFilterElement) .isInterface()) { throw new ProcessorException("Nalu-Processor: @PopUpFilters can only be used on a type (interface)"); } + TypeElement typeElement = (TypeElement) this.popUpFilterElement; + // @PopUpController can only be used on a class + if (!typeElement.getKind() + .isInterface()) { + throw new ProcessorException("Nalu-Processor: @PopUpController can only be used with an interface"); + } + // @PopUpController can only be used on a interface that extends IsApplication + if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), + typeElement.asType(), + this.processingEnvironment.getElementUtils() + .getTypeElement(IsApplication.class.getCanonicalName()) + .asType())) { + throw new ProcessorException("Nalu-Processor: @PopUpController can only be used on a class that extends IsApplication"); + } } public static final class Builder { ProcessingEnvironment processingEnvironment; + RoundEnvironment roundEnvironment; + Element popUpFilterElement; - RoundEnvironment roundEnvironment; + public Builder popUpFilterElement(Element popUpFilterrElement) { + this.popUpFilterElement = popUpFilterrElement; + return this; + } public Builder processingEnvironment(ProcessingEnvironment processingEnvironment) { this.processingEnvironment = processingEnvironment; diff --git a/nalu/src/main/java/com/github/nalukit/nalu/client/component/AbstractPopUpFilter.java b/nalu/src/main/java/com/github/nalukit/nalu/client/component/AbstractPopUpFilter.java index bb46d65d2..9811536b3 100644 --- a/nalu/src/main/java/com/github/nalukit/nalu/client/component/AbstractPopUpFilter.java +++ b/nalu/src/main/java/com/github/nalukit/nalu/client/component/AbstractPopUpFilter.java @@ -25,13 +25,35 @@ public abstract class AbstractPopUpFilter implements IsPopUpFilter { - protected C context; - protected SimpleEventBus eventBus; + protected C context; + protected SimpleEventBus eventBus; public AbstractPopUpFilter() { super(); } + /** + * Default implementation for getting the CancelHandler. + * The eturn value is null. + * + * @return always null + */ + @Override + public IsPopUpFilter.CancelHandler getCancelHandler() { + return null; + } + + /** + * Fires a NaluError event. + *

+ * Use this method to communicate an error inside a filter. + * + * @param event the error event + */ + public void fireNaluErrorEvent(NaluErrorEvent event) { + this.eventBus.fireEvent(event); + } + /** * Sets the context instance *

@@ -56,15 +78,4 @@ public void setEventBus(SimpleEventBus eventBus) { this.eventBus = eventBus; } - /** - * Fires a NaluError event. - *

- * Use this method to communicate an error inside a filter. - * - * @param event the error event - */ - public void fireNaluErrorEvent(NaluErrorEvent event) { - this.eventBus.fireEvent(event); - } - } diff --git a/nalu/src/main/java/com/github/nalukit/nalu/client/filter/IsPopUpFilter.java b/nalu/src/main/java/com/github/nalukit/nalu/client/filter/IsPopUpFilter.java index 53732e535..7b9a86f99 100644 --- a/nalu/src/main/java/com/github/nalukit/nalu/client/filter/IsPopUpFilter.java +++ b/nalu/src/main/java/com/github/nalukit/nalu/client/filter/IsPopUpFilter.java @@ -22,4 +22,13 @@ public interface IsPopUpFilter { boolean filter(ShowPopUpEvent event); + IsPopUpFilter.CancelHandler getCancelHandler(); + + @FunctionalInterface + interface CancelHandler { + + void onCancel(); + + } + } diff --git a/nalu/src/main/java/com/github/nalukit/nalu/client/internal/application/PopUpControllerFactory.java b/nalu/src/main/java/com/github/nalukit/nalu/client/internal/application/PopUpControllerFactory.java index e560c0934..9de02bbee 100644 --- a/nalu/src/main/java/com/github/nalukit/nalu/client/internal/application/PopUpControllerFactory.java +++ b/nalu/src/main/java/com/github/nalukit/nalu/client/internal/application/PopUpControllerFactory.java @@ -17,14 +17,13 @@ package com.github.nalukit.nalu.client.internal.application; import com.github.nalukit.nalu.client.application.event.LogEvent; +import com.github.nalukit.nalu.client.component.AbstractPopUpFilter; import com.github.nalukit.nalu.client.component.event.ShowPopUpEvent; import com.github.nalukit.nalu.client.filter.IsPopUpFilter; import com.github.nalukit.nalu.client.internal.annotation.NaluInternalUse; import org.gwtproject.event.shared.EventBus; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; +import java.util.*; @NaluInternalUse public class PopUpControllerFactory { @@ -34,11 +33,11 @@ public class PopUpControllerFactory { /* map of components (key: name of class, Value: ControllerCreator */ private final Map creatorStore; /* map of components (key: name of class, Value: controller instance */ - private final Map popUpControllerStore; + private final Map popUpControllerStore; /* map of filters (key: name of class, Value: filter instance */ - private final Map popUpFilterStore; + private final Map> popUpFilterStore; /* Nalu event bus to catch the ShowPopUpEvents */ - private EventBus eventBus; + private EventBus eventBus; private PopUpControllerFactory() { this.creatorStore = new HashMap<>(); @@ -60,7 +59,7 @@ public void registerPopUpController(String popUpName, } public void registerPopUpFilter(String popUpName, - IsPopUpFilter filter) { + AbstractPopUpFilter filter) { this.popUpFilterStore.put(popUpName, filter); } @@ -73,32 +72,48 @@ public void register(EventBus eventBus) { } } - private void onShowPopUp(ShowPopUpEvent e) { - if (!filterEvent(e)) { + private void onShowPopUp(ShowPopUpEvent event) { + List cancelHandelerKeys = new ArrayList<>(); + boolean cancelEvent = false; + for (String popUpFilterKey : this.popUpFilterStore.keySet()) { + if (!this.popUpFilterStore.get(popUpFilterKey).filter(event)) { + cancelHandelerKeys.add(popUpFilterKey); + } + } + if (cancelHandelerKeys.size() > 0) { + for (String key : cancelHandelerKeys) { + IsPopUpFilter.CancelHandler handler = this.popUpFilterStore.get(key).getCancelHandler(); + if (handler != null) { + this.popUpFilterStore.get(key) + .getCancelHandler() + .onCancel(); + } + } return; } + IsPopUpControllerCreator creator = null; - PopUpControllerInstance popUpComponentController = this.popUpControllerStore.get(e.getName()); + PopUpControllerInstance popUpComponentController = this.popUpControllerStore.get(event.getName()); if (Objects.isNull(popUpComponentController)) { - PopUpControllerInstance instance = this.popUpControllerStore.get(e.getName()); + PopUpControllerInstance instance = this.popUpControllerStore.get(event.getName()); if (Objects.isNull(instance)) { - creator = this.creatorStore.get(e.getName()); + creator = this.creatorStore.get(event.getName()); if (Objects.isNull(creator)) { LogEvent.create() .sdmOnly(false) - .addMessage("PopUpControllerFactory: PopUpController for name >>" + e.getName() + "<< not found"); + .addMessage("PopUpControllerFactory: PopUpController for name >>" + event.getName() + "<< not found"); return; } instance = creator.create(); - this.popUpControllerStore.put(e.getName(), + this.popUpControllerStore.put(event.getName(), instance); popUpComponentController = instance; } } popUpComponentController.getController() - .setDataStore(e.getDataStore()); + .setDataStore(event.getDataStore()); popUpComponentController.getController() - .setCommandStore(e.getCommandStore()); + .setCommandStore(event.getCommandStore()); PopUpControllerInstance finalPopUpComponentController = popUpComponentController; if (creator == null) { finalPopUpComponentController.getController() @@ -116,10 +131,4 @@ private void onShowPopUp(ShowPopUpEvent e) { } } - private boolean filterEvent(ShowPopUpEvent e) { - return this.popUpFilterStore.values() - .stream() - .allMatch(popUpFilter -> popUpFilter.filter(e)); - } - }