]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/DataTypeUtils.java
Use type reflection tools from databoard in objmap2.
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / annotations / factories / DataTypeUtils.java
index cd745a5230bd97b0c7bec4631dee01d4d822a61b..6d3f33a1b23e98c1932f1a167b004b3ead6d73c2 100644 (file)
  *******************************************************************************/
 package org.simantics.objmap.graph.annotations.factories;
 
  *******************************************************************************/
 package org.simantics.objmap.graph.annotations.factories;
 
+import org.simantics.databoard.Datatypes;
+import org.simantics.databoard.binding.error.DatatypeConstructionException;
+import org.simantics.databoard.type.ArrayType;
+import org.simantics.databoard.type.BooleanType;
+import org.simantics.databoard.type.ByteType;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.type.DoubleType;
+import org.simantics.databoard.type.FloatType;
+import org.simantics.databoard.type.IntegerType;
+import org.simantics.databoard.type.LongType;
+import org.simantics.databoard.type.OptionalType;
+import org.simantics.databoard.type.StringType;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.layer0.Layer0;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.layer0.Layer0;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class DataTypeUtils {
     
 
 public class DataTypeUtils {
     
+    private static final Logger LOGGER = LoggerFactory.getLogger(DataTypeUtils.class);
+
     public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {
         Layer0 b = Layer0.getInstance(g);
         if(clazz.equals(Double.class) || clazz.equals(double.class))
     public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {
         Layer0 b = Layer0.getInstance(g);
         if(clazz.equals(Double.class) || clazz.equals(double.class))
@@ -49,9 +65,59 @@ public class DataTypeUtils {
         else if(clazz.equals(long[].class))
             return b.LongArray;
         else {
         else if(clazz.equals(long[].class))
             return b.LongArray;
         else {
-               System.out.println("Couldn't find a data type for " + clazz);
+            try {
+                Datatype type = Datatypes.getDatatype(clazz);
+                final Resource result = dataTypeOfDatatype(g, type);
+                if (result != null)
+                    return result;
+            } catch (DatatypeConstructionException e) {
+            }
+            
+            LOGGER.error("No literal type found for class {}", clazz);
             return null;
         }
     }
             return null;
         }
     }
-    
+
+    public static Resource dataTypeOfDatatype(ReadGraph g, Datatype type) {
+        if (type instanceof OptionalType)
+            return dataTypeOfDatatype(g, ((OptionalType) type).getComponentType());
+        
+        Layer0 b = Layer0.getInstance(g);
+        if (type instanceof DoubleType)
+            return b.Double;
+        else if(type instanceof StringType)
+            return b.String;
+        else if(type instanceof IntegerType)
+            return b.Integer;
+        else if(type instanceof FloatType)
+            return b.Float;
+        else if(type instanceof BooleanType)
+            return b.Boolean;
+        else if(type instanceof LongType)
+            return b.Long;
+        else if(type instanceof ByteType)
+            return b.Byte;
+        
+        else if (type instanceof ArrayType) {
+            type = ((ArrayType) type).componentType();
+            
+            if (type instanceof DoubleType)
+                return b.DoubleArray;
+            else if(type instanceof IntegerType)
+                return b.IntegerArray;
+            else if(type instanceof ByteType)
+                return b.ByteArray;
+            else if(type instanceof FloatType)
+                return b.FloatArray;
+            else if(type instanceof BooleanType)
+                return b.BooleanArray;
+            else if(type instanceof StringType)
+                return b.StringArray;
+            else if(type instanceof LongType)
+                return b.LongArray;
+        }
+        
+        LOGGER.error("No literal type found for data type {}", type);
+        return null;
+    }
 }
 }