Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#185 - add the possibility to execute in action in case a ShowPopUpEv… #186

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions etc/wiki/14 Controller & Component.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyContext> {

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ private void handlePopUpFiltersAnnotation(RoundEnvironment roundEnv)
PopUpFiltersAnnotationValidator.builder()
.roundEnvironment(roundEnv)
.processingEnvironment(processingEnv)
.popUpFilterElement(popUpFiltersElement)
.build()
.validate(popUpFiltersElement);
// scan filter element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
public class PopUpControllerAnnotationValidator {

private ProcessorUtils processorUtils;

private ProcessingEnvironment processingEnvironment;

private Element popUpControllerElement;

@SuppressWarnings("unused")
Expand Down Expand Up @@ -81,9 +79,7 @@ public void validate()
public static final class Builder {

ProcessingEnvironment processingEnvironment;

RoundEnvironment roundEnvironment;

Element popUpControllerElement;

public Builder processingEnvironment(ProcessingEnvironment processingEnvironment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,35 @@
public abstract class AbstractPopUpFilter<C extends IsContext>
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.
* <p>
* 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
* <p>
Expand All @@ -56,15 +78,4 @@ public void setEventBus(SimpleEventBus eventBus) {
this.eventBus = eventBus;
}

/**
* Fires a NaluError event.
* <p>
* Use this method to communicate an error inside a filter.
*
* @param event the error event
*/
public void fireNaluErrorEvent(NaluErrorEvent event) {
this.eventBus.fireEvent(event);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ public interface IsPopUpFilter {

boolean filter(ShowPopUpEvent event);

IsPopUpFilter.CancelHandler getCancelHandler();

@FunctionalInterface
interface CancelHandler {

void onCancel();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,11 +33,11 @@ public class PopUpControllerFactory {
/* map of components (key: name of class, Value: ControllerCreator */
private final Map<String, IsPopUpControllerCreator> creatorStore;
/* map of components (key: name of class, Value: controller instance */
private final Map<String, PopUpControllerInstance> popUpControllerStore;
private final Map<String, PopUpControllerInstance> popUpControllerStore;
/* map of filters (key: name of class, Value: filter instance */
private final Map<String, IsPopUpFilter> popUpFilterStore;
private final Map<String, AbstractPopUpFilter<?>> popUpFilterStore;
/* Nalu event bus to catch the ShowPopUpEvents */
private EventBus eventBus;
private EventBus eventBus;

private PopUpControllerFactory() {
this.creatorStore = new HashMap<>();
Expand All @@ -60,7 +59,7 @@ public void registerPopUpController(String popUpName,
}

public void registerPopUpFilter(String popUpName,
IsPopUpFilter filter) {
AbstractPopUpFilter<?> filter) {
this.popUpFilterStore.put(popUpName,
filter);
}
Expand All @@ -73,32 +72,48 @@ public void register(EventBus eventBus) {
}
}

private void onShowPopUp(ShowPopUpEvent e) {
if (!filterEvent(e)) {
private void onShowPopUp(ShowPopUpEvent event) {
List<String> 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()
Expand All @@ -116,10 +131,4 @@ private void onShowPopUp(ShowPopUpEvent e) {
}
}

private boolean filterEvent(ShowPopUpEvent e) {
return this.popUpFilterStore.values()
.stream()
.allMatch(popUpFilter -> popUpFilter.filter(e));
}

}