]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Add optional parameter to RelatedValue-annotation for providing an adapter class...
authorhoharri <hoharri@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 10 May 2011 09:23:34 +0000 (09:23 +0000)
committerhoharri <hoharri@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 10 May 2011 09:23:34 +0000 (09:23 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20851 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.objmap/META-INF/MANIFEST.MF
org.simantics.objmap/src/org/simantics/objmap/annotations/RelatedValue.java
org.simantics.objmap/src/org/simantics/objmap/annotations/factories/RelatedValueRuleFactory.java
org.simantics.objmap/src/org/simantics/objmap/rules/adapters/IdentityAdapter.java
org.simantics.objmap/src/org/simantics/objmap/rules/adapters/ValueAdapter.java

index 89cfd50b035d03a158fa6df085d32e48fda1d0e3..52d144d76b0b6aff8762e6431e06719b84e2b7c8 100644 (file)
@@ -12,6 +12,7 @@ Export-Package: org.simantics.objmap,
  org.simantics.objmap.annotations,
  org.simantics.objmap.annotations.meta,
  org.simantics.objmap.rules,
+ org.simantics.objmap.rules.adapters,
  org.simantics.objmap.rules.domain,
  org.simantics.objmap.rules.factory,
  org.simantics.objmap.rules.range,
index 98dfcca4f9b6ce3691fa4e2fcbd74f9dd6a3016b..23d3e9a8d4d6c7cfdae3fbb7bc15f8010a46c564 100644 (file)
@@ -18,6 +18,8 @@ import java.lang.annotation.Target;
 \r
 import org.simantics.objmap.annotations.factories.RelatedValueRuleFactory;\r
 import org.simantics.objmap.annotations.meta.HasFieldRuleFactory;\r
+import org.simantics.objmap.rules.adapters.IdentityAdapter;\r
+import org.simantics.objmap.rules.adapters.ValueAdapter;\r
 \r
 /**\r
  * Specifies a correspondence between a field and \r
@@ -28,5 +30,6 @@ import org.simantics.objmap.annotations.meta.HasFieldRuleFactory;
 @Target(ElementType.FIELD)\r
 @HasFieldRuleFactory(RelatedValueRuleFactory.class)\r
 public @interface RelatedValue {\r
-       String value();\r
+    String value();\r
+    Class<? extends ValueAdapter> adapter() default IdentityAdapter.class;\r
 }\r
index 5843fb1b519a5a8146d7ec5c24ac8c81f78c2d76..37cd981291ed0adcaa34902153448a48830d8c96 100644 (file)
@@ -15,29 +15,46 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;\r
 \r
 import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
 import org.simantics.db.exception.ResourceNotFoundException;\r
 import org.simantics.db.exception.ServiceException;\r
 import org.simantics.db.exception.ValidationException;\r
 import org.simantics.objmap.IMappingRule;\r
 import org.simantics.objmap.annotations.RelatedValue;\r
 import org.simantics.objmap.rules.ValueRule;\r
+import org.simantics.objmap.rules.adapters.IdentityAdapter;\r
+import org.simantics.objmap.rules.adapters.ValueAdapter;\r
 import org.simantics.objmap.rules.domain.RelatedValueAccessor;\r
 import org.simantics.objmap.rules.factory.IFieldRuleFactory;\r
+import org.simantics.objmap.rules.range.AdaptedRangeAccessor;\r
 import org.simantics.objmap.rules.range.FieldAccessor;\r
+import org.simantics.objmap.rules.range.IRangeAccessor;\r
 \r
 public class RelatedValueRuleFactory implements IFieldRuleFactory {\r
 \r
     @Override\r
     public IMappingRule create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException, ValidationException, ServiceException {\r
         RelatedValue annotation = (RelatedValue)_annotation;\r
-        //Builtins b = g.getBuiltins();\r
+        Class<? extends ValueAdapter> adapterClass = annotation.adapter();\r
+        IRangeAccessor<Object> rangeAccessor = new FieldAccessor<Object>(field);\r
+        Resource valueType = DataTypeUtils.dataTypeOfClass(g, field.getType());\r
+        if(adapterClass != IdentityAdapter.class)\r
+            try {\r
+                ValueAdapter adapter = adapterClass.newInstance();\r
+                rangeAccessor = new AdaptedRangeAccessor(rangeAccessor, adapter);\r
+                valueType = adapter.rangeTypeToDomainType(g, field.getType());\r
+            } catch (InstantiationException e) {\r
+                throw new RuntimeException(e);\r
+            } catch (IllegalAccessException e) {\r
+                throw new RuntimeException(e);\r
+            }            \r
         return new ValueRule(\r
                 new RelatedValueAccessor(\r
                         g.getResource(annotation.value()), \r
-                        DataTypeUtils.dataTypeOfClass(g, field.getType())\r
-                    ),\r
-                new FieldAccessor<Object>(field)\r
-                );\r
+                        valueType\r
+                ),\r
+                rangeAccessor\r
+        );\r
     }\r
 \r
 }\r
index 3d4b6913033a5ffac8e6566196f83a4b7e8281c8..913357b28251162882c19207a75e9d384698357f 100644 (file)
@@ -1,5 +1,8 @@
 package org.simantics.objmap.rules.adapters;\r
 \r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+\r
 public enum IdentityAdapter implements ValueAdapter {\r
     INSTANCE;\r
 \r
@@ -12,4 +15,9 @@ public enum IdentityAdapter implements ValueAdapter {
     public Object rangeToDomain(Object rangeValue) {\r
         return rangeValue;\r
     }\r
+\r
+    @Override\r
+    public Resource rangeTypeToDomainType(ReadGraph graph, Class<?> rangeType) {\r
+        return null;\r
+    }\r
 }\r
index c8d0d2d0970e8e030138d7e90544dff3a8bf0fd2..85e4016981c2606a63f83d4baf6c169ae6059f9a 100644 (file)
@@ -1,6 +1,10 @@
 package org.simantics.objmap.rules.adapters;\r
 \r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+\r
 public interface ValueAdapter {\r
+    Resource rangeTypeToDomainType(ReadGraph graph, Class<?> rangeType);\r
     Object domainToRange(Object domainValue);\r
     Object rangeToDomain(Object rangeValue);\r
 }\r