xapi.ui.autoui.api.BeanValueProvider 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.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import xapi.collect.impl.ArrayIterable;
import xapi.util.api.ConvertsValue;
public class BeanValueProvider {
private static Object getValue(String name, Object object, Map> map) {
if (!map.containsKey(name)){
illegalArg(name);
}
return map.get(name).convert(object);
}
protected static Object illegalArg(String name) {
throw new IllegalArgumentException("Value for field named "+name+" not found");
}
private class Converter {
protected Object valueOf(String name, Object object) {
return BeanValueProvider.this.valueOf(name, object);
}
}
private class RebasedBeanValueProvider extends BeanValueProvider {
private final String name;
private final Converter converter;
public RebasedBeanValueProvider(String name, Converter converter) {
this.name = name;
this.converter = converter;
}
@Override
public Object getValue(String name, String context, Object object) {
// assert context.equals(this.name);
if ("$name".equals(name) || "this.name()".equals(name) || (this.name+".name()").equals(name)) {
return this.name;
}
if ("$value".equals(name) || "this".equals(name) || this.name.equals(name)) {
return converter.valueOf(this.name, object);
}
return converter.valueOf(this.name+"."+name, object);
}
}
private class RebaseAllBeanValueProvider extends BeanValueProvider {
private final Converter converter;
private RebaseAllBeanValueProvider(Converter converter) {
this.converter = converter;
}
@Override
public Object getValue(String name, String context, Object object) {
if ("$name".equals(name) || "this.name()".equals(name)) {
return context;
}
if ("$value".equals(name) || "this".equals(name)) {
return object;
}
if (context.length()>0) {
context += ".";
}
return converter.valueOf(context+name, object);
}
}
private Map> map = new LinkedHashMap>();
private String[] childKeys;
public void addProvider(String key, final String name, ConvertsValue