Skip to content
Open
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ apply(plugin: 'cuba')
cuba {
artifact {
group = 'de.balvi.cuba.declarativecontrollers'
version = "0.10.0"
version = '0.11.0'
isSnapshot = false
}
tomcat {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.balvi.cuba.declarativecontrollers.web.annotationexecutor;

import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.gui.screen.Screen;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
Expand All @@ -9,23 +10,22 @@

abstract public class AbstractAnnotationDispatcherBean<T extends AnnotationExecutor, K extends AnnotationExecutor> {

protected Field[] getDeclaredFields(Frame frame) {
return frame.getClass().getDeclaredFields();
protected Field[] getDeclaredFields(Screen screen) {
return screen.getClass().getDeclaredFields();
}

protected com.haulmont.cuba.gui.components.Component getFieldValue(Frame frame, Field field) {
protected com.haulmont.cuba.gui.components.Component getFieldValue(Screen screen, Field field) {
try {
field.setAccessible(true);
return (com.haulmont.cuba.gui.components.Component) field.get(frame);
return (com.haulmont.cuba.gui.components.Component) field.get(screen);
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
}


protected Annotation[] getClassAnnotations(Frame frame) {
return frame.getClass().getAnnotations();
protected Annotation[] getClassAnnotations(Screen screen) {
return screen.getClass().getAnnotations();
}

protected Collection<? extends AnnotationExecutor> getSupportedAnnotationExecutors(Collection<? extends AnnotationExecutor> potentialAnnotationExecutors, Annotation annotation) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.balvi.cuba.declarativecontrollers.web.annotationexecutor.screen;

import com.haulmont.cuba.gui.components.Window;
import com.haulmont.cuba.gui.screen.Screen;
import com.haulmont.cuba.gui.screen.ScreenOptions;
import de.balvi.cuba.declarativecontrollers.web.annotationexecutor.AnnotationExecutor;

import java.lang.annotation.Annotation;
import java.util.Map;

public interface ScreenAnnotationExecutor<A extends Annotation> extends AnnotationExecutor {

void init(A annotation, Screen screen, ScreenOptions options);

void beforeShow(A annotation, Screen screen, ScreenOptions options);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.balvi.cuba.declarativecontrollers.web.annotationexecutor.screen;

import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.screen.Screen;
import com.haulmont.cuba.gui.screen.ScreenOptions;
import de.balvi.cuba.declarativecontrollers.web.annotationexecutor.AnnotationExecutor;

import java.lang.annotation.Annotation;

public interface ScreenFieldAnnotationExecutor<A extends Annotation, T extends Component> extends AnnotationExecutor {

void init(A annotation, Screen screen, T target, ScreenOptions options);

void beforeshow(A annotation, Screen screen, T target, ScreenOptions options);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.balvi.cuba.declarativecontrollers.web.screen;

import com.haulmont.cuba.gui.screen.Screen;
import com.haulmont.cuba.gui.screen.ScreenOptions;
import com.haulmont.cuba.gui.screen.Subscribe;

import javax.inject.Inject;

public class AnnotatableScreen extends Screen {

@Inject
protected ScreenAnnotationDispatcher screenAnnotationDispatcher;
private ScreenOptions screenOptions;

public AnnotatableScreen(){
addInitListener(this::onInitEvent);
addBeforeShowListener(this::onBeforeShowEvent);
}

public void onInitEvent(InitEvent event) {
this.screenOptions = event.getOptions();
screenAnnotationDispatcher.executeInit(this, screenOptions);
}

public void onBeforeShowEvent(BeforeShowEvent event) {
screenAnnotationDispatcher.executeBeforeShow(this, screenOptions);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.balvi.cuba.declarativecontrollers.web.screen;

import com.haulmont.cuba.gui.screen.Screen;
import com.haulmont.cuba.gui.screen.ScreenOptions;
import de.balvi.cuba.declarativecontrollers.web.standardbrowse.AnnotatableStandardLookup;

public interface ScreenAnnotationDispatcher {

String NAME = "dbcdc_ScreenAnnotationExecutorService";

void executeInit(Screen screen, ScreenOptions screenOptions);
void executeBeforeShow(Screen screen, ScreenOptions screenOptions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package de.balvi.cuba.declarativecontrollers.web.screen;

import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.gui.screen.Screen;
import com.haulmont.cuba.gui.screen.ScreenOptions;
import de.balvi.cuba.declarativecontrollers.web.annotationexecutor.AbstractAnnotationDispatcherBean;
import de.balvi.cuba.declarativecontrollers.web.annotationexecutor.screen.ScreenAnnotationExecutor;
import de.balvi.cuba.declarativecontrollers.web.annotationexecutor.screen.ScreenFieldAnnotationExecutor;
import groovy.transform.CompileStatic;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Collection;

@CompileStatic
@Component(ScreenAnnotationDispatcher.NAME)
public class ScreenAnnotationDispatcherBean extends AbstractAnnotationDispatcherBean<ScreenAnnotationExecutor, ScreenFieldAnnotationExecutor> implements ScreenAnnotationDispatcher {

@Override
public void executeInit(Screen screen, ScreenOptions screenOptions) {
executeInitUniversal(screen, screenOptions);
}

private void executeInitUniversal(Screen screen, ScreenOptions screenOptions) {
executeInitForClassAnnotations(screen, screenOptions);
executeInitForFieldAnnotations(screen, screenOptions);
}

private void executeInitForFieldAnnotations(Screen screen, ScreenOptions screenOptions) {
for (Field field : getDeclaredFields(screen)) {
Annotation[] fieldAnnotations = field.getAnnotations();

for (Annotation annotation : fieldAnnotations) {

Collection<ScreenFieldAnnotationExecutor> supportedAnnotationExecutors = getSupportedScreenFieldAnnotationExecutors(annotation);

if (supportedAnnotationExecutors != null && supportedAnnotationExecutors.size() > 0) {
com.haulmont.cuba.gui.components.Component fieldValue = getFieldValue(screen, field);
for (ScreenFieldAnnotationExecutor annotationExecutor : supportedAnnotationExecutors) {
annotationExecutor.init(annotation, screen, fieldValue, screenOptions);
}
}

}
}
}

private void executeInitForClassAnnotations(Screen screen, ScreenOptions screenOptions) {
for (Annotation annotation : getClassAnnotations(screen)) {
Collection<ScreenAnnotationExecutor> supportedAnnotationExecutors = getSupportedScreenAnnotationExecutors(annotation);

for (ScreenAnnotationExecutor annotationExecutor : supportedAnnotationExecutors) {
annotationExecutor.init(annotation, screen, screenOptions);
}
}
}

@Override
public void executeBeforeShow(Screen screen, ScreenOptions screenOptions) {
executeBeforeShowUniversal(screen, screenOptions);
}

private void executeBeforeShowUniversal(Screen screen, ScreenOptions screenOptions){
executeBeforeShowForClassAnnotations(screen, screenOptions);
executeBeforeShowForFieldAnnotations(screen, screenOptions);
}

private void executeBeforeShowForFieldAnnotations(Screen screen, ScreenOptions screenOptions) {
for (Field field : getDeclaredFields(screen)) {

Annotation[] fieldAnnotations = field.getAnnotations();
for (Annotation annotation : fieldAnnotations) {

Collection<ScreenFieldAnnotationExecutor> supportedAnnotationExecutors = getSupportedScreenFieldAnnotationExecutors(annotation);

if (supportedAnnotationExecutors != null && supportedAnnotationExecutors.size() > 0) {
com.haulmont.cuba.gui.components.Component fieldValue = getFieldValue(screen, field);
for (ScreenFieldAnnotationExecutor annotationExecutor : supportedAnnotationExecutors) {
annotationExecutor.beforeshow(annotation, screen, fieldValue, screenOptions);
}
}
}
}
}

private void executeBeforeShowForClassAnnotations(Screen screen, ScreenOptions screenOptions) {
for (Annotation annotation : getClassAnnotations(screen)) {

Collection<ScreenAnnotationExecutor> supportedAnnotations = getSupportedScreenAnnotationExecutors(annotation);

for (ScreenAnnotationExecutor annotationExecutor : supportedAnnotations) {
annotationExecutor.beforeShow(annotation, screen, screenOptions);
}
}
}


protected Collection<ScreenAnnotationExecutor> getSupportedScreenAnnotationExecutors(Annotation annotation) {
Collection<ScreenAnnotationExecutor> annotationExecutors = getScreenAnnotationExecutors();
return (Collection<ScreenAnnotationExecutor>) getSupportedAnnotationExecutors(annotationExecutors, annotation);
}

protected Collection<ScreenFieldAnnotationExecutor> getSupportedScreenFieldAnnotationExecutors(Annotation annotation) {
Collection<ScreenFieldAnnotationExecutor> annotationExecutors = getScreenFieldAnnotationExecutors();
return (Collection<ScreenFieldAnnotationExecutor>) getSupportedAnnotationExecutors(annotationExecutors, annotation);
}

protected Collection<ScreenAnnotationExecutor> getScreenAnnotationExecutors() {
return AppBeans.getAll(ScreenAnnotationExecutor.class).values();
}


protected Collection<ScreenFieldAnnotationExecutor> getScreenFieldAnnotationExecutors() {
return AppBeans.getAll(ScreenFieldAnnotationExecutor.class).values();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.balvi.cuba.declarativecontrollers.web.standardbrowse;

import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.gui.screen.ScreenOptions;
import com.haulmont.cuba.gui.screen.StandardLookup;
import com.haulmont.cuba.gui.screen.Subscribe;
import de.balvi.cuba.declarativecontrollers.web.screen.ScreenAnnotationDispatcher;

import javax.inject.Inject;

public class AnnotatableStandardLookup<T extends Entity> extends StandardLookup<T> {

@Inject
protected ScreenAnnotationDispatcher screenAnnotationDispatcher;
private ScreenOptions screenOptions;

public AnnotatableStandardLookup(){
addInitListener(this::onInitEvent);
addBeforeShowListener(this::onBeforeShowEvent);
}

public void onInitEvent(InitEvent event) {
this.screenOptions = event.getOptions();
screenAnnotationDispatcher.executeInit(this, screenOptions);
}

public void onBeforeShowEvent(BeforeShowEvent event) {
screenAnnotationDispatcher.executeBeforeShow(this, screenOptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.balvi.cuba.declarativecontrollers.web.standardeditor;

import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.gui.screen.ScreenOptions;
import com.haulmont.cuba.gui.screen.StandardEditor;
import com.haulmont.cuba.gui.screen.Subscribe;
import de.balvi.cuba.declarativecontrollers.web.screen.ScreenAnnotationDispatcher;

import javax.inject.Inject;

public class AnnotatableStandardEditor<T extends Entity> extends StandardEditor<T> {

@Inject
protected ScreenAnnotationDispatcher screenAnnotationDispatcher;
private ScreenOptions screenOptions;

public AnnotatableStandardEditor(){
addInitListener(this::onInitEvent);
addBeforeShowListener(this::onBeforeShowEvent);
}

public void onInitEvent(InitEvent event) {
this.screenOptions = event.getOptions();
screenAnnotationDispatcher.executeInit(this, screenOptions);
}

public void onBeforeShowEvent(BeforeShowEvent event) {
screenAnnotationDispatcher.executeBeforeShow(this, screenOptions);
}
}
Loading