xapi.ui.autoui.api.UiRenderingContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xapi-core-ui-autoui Show documentation
Show all versions of xapi-core-ui-autoui Show documentation
The core API for generating user interfaces from data models.
package xapi.ui.autoui.api;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Provider;
import xapi.source.write.MappedTemplate;
import xapi.util.impl.ImmutableProvider;
public class UiRenderingContext {
private static final MappedTemplate DEFAULT_TEMPLATE = new MappedTemplate("");
@SuppressWarnings("rawtypes")
private static final Validator[] DEFAULT_VALIDATORS = new Validator[0];
private static final Object[] EMPTY_MESSAGES = new Object[0];
private boolean head = false;
private final Provider> renderProvider;
private UiRendererSelector selector = UiRendererSelector.ALWAYS_TRUE;
private boolean tail = false;
private MappedTemplate template = DEFAULT_TEMPLATE;
@SuppressWarnings("rawtypes")
private Validator[] validators = DEFAULT_VALIDATORS;
private boolean wrapper = false;
private BeanValueProvider valueProvider;
private String name = "";
public UiRenderingContext(Provider> renderProvider) {
this.renderProvider = renderProvider;
}
public UiRenderingContext(UiRenderer> renderer) {
this(new ImmutableProvider>(renderer));
}
public String getName() {
return name;
}
@SuppressWarnings("rawtypes")
public final UiRenderer getRenderer() {
return renderProvider.get();
}
public UiRendererSelector getSelector() {
return selector;
}
public MappedTemplate getTemplate() {
return template;
}
public String applyTemplate(String name, Object val) {
Map map = new HashMap();
BeanValueProvider bean = getBeanValueProvider();
if ("".equals(name)) {
for (String key : bean.childKeys()) {
String k = "${"+key+"}";
if (template.hasKey(k)) {
map.put(k, bean.getValue(key, "", val));
}
k = "${"+key+".name()}";
if (template.hasKey(k)) {
map.put(k, key);
}
}
} else {
val = bean.getValue(name, "", val);
map.put("$value", val);
map.put("$name", name);
map.put("${"+name+"}", val);
map.put("${"+name+".name()}", name);
// We should also have a means to do deep name resolving on rebased bean items...
}
return getTemplate().applyMap(map);
}
@SuppressWarnings("rawtypes")
public Validator[] getValidators() {
return validators;
}
public boolean isHead() {
return head;
}
public boolean isTail() {
return tail;
}
public boolean isTemplateSet() {
return template != DEFAULT_TEMPLATE;
}
public boolean isValid(Object o) {
if (validators.length == 0) {
return true;
}
for (Validator> validator : validators) {
Object error = validator.isValid(o);
if (!(Boolean.TRUE.equals(error) || error == null)) {
return false;
}
}
return true;
}
public boolean isWrapper() {
return wrapper;
}
public UiRenderingContext setHead(boolean head) {
this.head = head;
return this;
}
public UiRenderingContext setName(String name) {
this.name = name;
return this;
}
public UiRenderingContext setSelector(UiRendererSelector selector) {
assert selector != null : "You must supply a valid UiRendererSelector; "
+ "Use UiRendererSelector.AlwaysTrue instead of null";
this.selector = selector;
return this;
}
public UiRenderingContext setTail(boolean tail) {
this.tail = tail;
return this;
}
public UiRenderingContext setTemplate(MappedTemplate template) {
this.template = template;
return this;
}
@SuppressWarnings("rawtypes")
public UiRenderingContext setValidators(Validator[] validators) {
this.validators = validators;
return this;
}
public UiRenderingContext setWrapper(boolean wrapper) {
this.wrapper = wrapper;
return this;
}
public Object[] validate(Object o) {
if (validators.length == 0) {
return EMPTY_MESSAGES;
}
List