io.mateu.mdd.vaadin.components.fieldBuilders.JPACodeFieldBuilder Maven / Gradle / Ivy
package io.mateu.mdd.vaadin.components.fieldBuilders;
import com.vaadin.data.HasValue;
import com.vaadin.data.ValidationResult;
import com.vaadin.data.Validator;
import com.vaadin.data.ValueContext;
import com.vaadin.data.validator.BeanValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.UserError;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ValoTheme;
import io.mateu.mdd.core.app.AbstractAction;
import io.mateu.mdd.core.interfaces.AbstractStylist;
import io.mateu.mdd.core.ui.MDDUIAccessor;
import io.mateu.mdd.shared.annotations.Code;
import io.mateu.mdd.shared.annotations.RequestFocus;
import io.mateu.mdd.shared.reflection.FieldInterfaced;
import io.mateu.mdd.vaadin.data.MDDBinder;
import io.mateu.reflection.ReflectionHelper;
import org.vaadin.aceeditor.AceEditor;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class JPACodeFieldBuilder extends JPAStringFieldBuilder {
public boolean isSupported(FieldInterfaced field) {
return field.isAnnotationPresent(Code.class);
}
@Override
public Component build(VerticalLayout fieldGroup, HorizontalLayout fieldGroupHeader, FieldInterfaced field, Object object, Layout container, MDDBinder binder, Map> validators, AbstractStylist stylist, Map allFieldContainers, boolean forSearchFilter, Map> attachedActions) {
Component r = null;
if (!forSearchFilter) {
HorizontalLayout l = new HorizontalLayout();
AceEditor tf;
l.addComponent(tf = new AceEditor());
tf.setMode(field.getAnnotation(Code.class).mode());
tf.setTheme(field.getAnnotation(Code.class).theme());
// Use worker (if available for the current mode)
tf.setUseWorker(true);
if (field.isAnnotationPresent(RequestFocus.class)) tf.focus();
Button b;
l.addComponent(b = new Button(VaadinIcons.EXPAND_SQUARE));
b.addStyleName(ValoTheme.BUTTON_QUIET);
b.addClickListener(e -> MDDUIAccessor.go(field.getName()));
container.addComponent(l);
if (allFieldContainers != null) allFieldContainers.put(field, tf);
if (container.getComponentCount() > 0) l.setCaption(ReflectionHelper.getCaption(field));
validators.put(tf, new ArrayList<>());
tf.addValueChangeListener(new HasValue.ValueChangeListener() {
@Override
public void valueChange(HasValue.ValueChangeEvent valueChangeEvent) {
ValidationResult result = null;
for (Validator v : validators.get(tf)) {
result = v.apply(valueChangeEvent.getValue(), new ValueContext(tf));
if (result.isError()) break;
}
if (result != null && result.isError()) {
tf.setComponentError(new UserError(result.getErrorMessage()));
} else {
tf.setComponentError(null);
}
}
});
tf.setRequiredIndicatorVisible(field.isAnnotationPresent(NotNull.class));
//if (field.isAnnotationPresent(Help.class) && !Strings.isNullOrEmpty(field.getAnnotation(Help.class).value())) tf.setDescription(field.getAnnotation(Help.class).value());
if (field.isAnnotationPresent(NotNull.class)) validators.get(tf).add(new StringLengthValidator("Required field", 1, Integer.MAX_VALUE));
BeanValidator bv = new BeanValidator(field.getDeclaringClass(), field.getName());
validators.get(tf).add(new Validator() {
@Override
public ValidationResult apply(Object o, ValueContext valueContext) {
return bv.apply(o, valueContext);
}
@Override
public Object apply(Object o, Object o2) {
return null;
}
});
addValidators(validators.get(tf));
addErrorHandler(field, tf);
r = tf;
/*
tf.setDescription();
tf.setPlaceholder();
*/
completeBinding(tf, binder, field);
}
return r;
}
public void addValidators(List validators) {
}
}