]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Reflection-based handling of Enum property editing. 29/3629/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 25 Nov 2019 11:50:14 +0000 (13:50 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Wed, 27 Nov 2019 13:41:07 +0000 (13:41 +0000)
gitlab #57

Change-Id: Ifab41fbf38d1e56b36fc561763ce9200cd18c358
(cherry picked from commit 2041d6564531fe794072ddfe345773de1cab60a3)

org.simantics.g3d/src/org/simantics/g3d/property/ComboPropertyManipulator.java
org.simantics.g3d/src/org/simantics/g3d/property/DefaultPropertyManipulatorFactory.java
org.simantics.g3d/src/org/simantics/g3d/property/MethodComboValueProvider.java
org.simantics.g3d/src/org/simantics/g3d/property/MethodValueProvider.java
org.simantics.g3d/src/org/simantics/g3d/property/MethodWithMapValueProvider.java
org.simantics.g3d/src/org/simantics/g3d/property/ValueProvider.java

index 45a3b99adb3f3ec2a5284eb36b2133f6791fbee6..a1ea44c33260bd8b151cc6974b6e91a00e5d53f8 100644 (file)
@@ -2,6 +2,8 @@ package org.simantics.g3d.property;
 
 import java.util.List;
 
+import org.simantics.utils.datastructures.Arrays;
+
 public class ComboPropertyManipulator implements PropertyManipulator{
 
     ComboValueProvider provider;
@@ -29,9 +31,20 @@ public class ComboPropertyManipulator implements PropertyManipulator{
     @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;
         }
@@ -40,7 +53,14 @@ public class ComboPropertyManipulator implements PropertyManipulator{
     @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();
         }
@@ -72,8 +92,14 @@ public class ComboPropertyManipulator implements PropertyManipulator{
     
     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;
         }
index 1ad899458fcd8edc4cd373e86c8decae1fea37a9..5d7e121d134a9ce2983733b1e9c70ba18e63deec 100644 (file)
@@ -45,6 +45,8 @@ public class DefaultPropertyManipulatorFactory implements PropertyManipulatorFac
                        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);
        }
 
index c1e21e071d6ed0d5e7b9c5895b0179fca7377c97..0408235f3ca5d81824046748b18f32c5f11231f7 100644 (file)
 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; 
@@ -28,14 +31,39 @@ public class MethodComboValueProvider implements ComboValueProvider {
        
        @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();
        }
 }
index 016e1fe30d2f57d9aa96e5549d1c2c4bd2fcc22c..212ff36b16f4d204d00e295bb1a3860d53e8828c 100644 (file)
@@ -27,8 +27,14 @@ public class MethodValueProvider implements ValueProvider {
        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
index eeae0dc89499dab28966d6083af0b74f977cfb5d..8fad88979690dbf266f796982b52c314bd4d1221 100644 (file)
@@ -12,6 +12,8 @@
 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
@@ -33,6 +35,7 @@ public class MethodWithMapValueProvider implements ValueProvider {
                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
@@ -41,4 +44,11 @@ public class MethodWithMapValueProvider implements ValueProvider {
                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
index 94169617e80a118a0c7e594ec16615db1f3ad92d..2471d5f357a8b40cf1356f6bec8e8760114ed61f 100644 (file)
@@ -16,5 +16,5 @@ public interface ValueProvider {
        \r
        Object getValue(Object obj) throws Exception;\r
        void setValue(Object obj, Object value) throws Exception;\r
-\r
+       Class<?> getValueType();\r
 }\r