import java.util.List;
+import org.simantics.utils.datastructures.Arrays;
+
public class ComboPropertyManipulator implements PropertyManipulator{
ComboValueProvider provider;
@Override
public String getValue(int i) {
try {
- Integer value = (Integer)provider.getValue(input);
- if (value == null) return null;
- return getItems()[value];
+ Object rawValue = provider.getValue(input);
+ if (rawValue == null) {
+ return null;
+ }
+ else if (rawValue instanceof Integer) {
+ Integer value = (Integer)rawValue;
+ return getItems()[value];
+ }
+ else if (rawValue instanceof Enum) {
+ return rawValue.toString();
+ }
+ else {
+ return null;
+ }
} catch (Exception e) {
return null;
}
@Override
public String setValue(String value, int i) {
try {
- provider.setValue(input, Integer.parseInt(value));
+ int intValue = Integer.parseInt(value);
+ Class<?> valueType = provider.getValueType();
+ if (Enum.class.isAssignableFrom(valueType)) {
+ provider.setValue(input, valueType.getEnumConstants()[intValue]);
+ }
+ else {
+ provider.setValue(input, intValue);
+ }
} catch (Exception e) {
return e.getMessage();
}
public Integer getValueIndex() {
try {
- Integer value = (Integer)provider.getValue(input);
- return value;
+ Object value = provider.getValue(input);
+ Class<?> valueType = provider.getValueType();
+ if (valueType.isEnum()) {
+ return Arrays.indexOf(valueType.getEnumConstants(), value);
+ }
+ else {
+ return (Integer) value;
+ }
} catch (Exception e) {
return null;
}
return BooleanPropertyManipulator.class;
if (double[].class.equals(returnValue))
return DoubleArrayPropertyManipulator.class;
+ if (returnValue.isEnum())
+ return ComboPropertyManipulator.class;
throw new RuntimeException("Cannot handle value " + returnValue.getName() + " for method " + get);
}
package org.simantics.g3d.property;
import java.lang.reflect.Method;
+import java.util.ArrayList;
import java.util.List;
+import org.simantics.utils.datastructures.Arrays;
+
public class MethodComboValueProvider implements ComboValueProvider {
Method getter;
@Override
public Object getValue(Object obj) throws Exception{
- return getter.invoke(obj);
+ if (getValueType().isEnum()) {
+ Object value = getter.invoke(obj);
+ Object[] vs = getValueType().getEnumConstants();
+ return (Integer) Arrays.indexOf(vs, value);
+ }
+ else {
+ return getter.invoke(obj);
+ }
}
@Override
public void setValue(Object obj, Object value) throws Exception {
setter.invoke(obj,value);
}
+ @SuppressWarnings("unchecked")
public List<Object> getValues(Object obj) throws Exception {
- return (List<Object>)values.invoke(obj);
+ if (values != null) {
+ return (List<Object>)values.invoke(obj);
+ }
+ else if (getValueType().isEnum()) {
+ Object[] enumConstants = getValueType().getEnumConstants();
+ List<Object> result = new ArrayList<>(enumConstants.length);
+ for (Object c : enumConstants)
+ result.add(c);
+ return result;
+ }
+ else {
+ throw new IllegalStateException("No value list defined for " + getter.toString());
+ }
+ }
+
+ @Override
+ public Class<?> getValueType() {
+ return getter.getReturnType();
}
}
public Object getValue(Object obj) throws Exception{\r
return getter.invoke(obj);\r
}\r
+ \r
@Override\r
public void setValue(Object obj, Object value) throws Exception {\r
setter.invoke(obj,value);\r
}\r
+ \r
+ @Override\r
+ public Class<?> getValueType() {\r
+ return getter.getReturnType();\r
+ }\r
}\r
package org.simantics.g3d.property;\r
\r
import java.lang.reflect.Method;\r
+import java.lang.reflect.ParameterizedType;\r
+import java.lang.reflect.Type;\r
import java.util.HashMap;\r
import java.util.Map;\r
\r
Map<String,Object> map = (Map<String, Object>)getter.invoke(obj);\r
return map.get(key);\r
}\r
+ \r
@SuppressWarnings("unchecked")\r
@Override\r
public void setValue(Object obj, Object value) throws Exception {\r
map.put(key, value);\r
setter.invoke(obj,map);\r
}\r
+ \r
+ @Override\r
+ public Class<?> getValueType() {\r
+ // The return type of the getter should be a java.util.Map.\r
+ Type[] typeArguments = ((ParameterizedType)getter.getGenericReturnType()).getActualTypeArguments();\r
+ return (Class<?>) typeArguments[1];\r
+ }\r
}\r
\r
Object getValue(Object obj) throws Exception;\r
void setValue(Object obj, Object value) throws Exception;\r
-\r
+ Class<?> getValueType();\r
}\r