]> gerrit.simantics Code Review - simantics/3d.git/blob
05dd6783350908a33306b98901e860ed39b86e43
[simantics/3d.git] /
1 package org.simantics.objmap.structural.annotations.factories;\r
2 \r
3 import java.lang.annotation.Annotation;\r
4 import java.lang.reflect.Method;\r
5 \r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.layer0.Layer0;\r
10 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
11 import org.simantics.objmap.graph.rules.ValueRule;\r
12 import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;\r
13 import org.simantics.objmap.graph.rules.adapters.ValueAdapter;\r
14 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;\r
15 import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;\r
16 import org.simantics.objmap.graph.rules.range.GetSetValueAccessor;\r
17 import org.simantics.objmap.graph.rules.range.IRangeAccessor;\r
18 import org.simantics.objmap.structural.StructuralResource;\r
19 import org.simantics.objmap.structural.annotations.TypeRelatedGetValue;\r
20 import org.simantics.objmap.structural.annotations.TypeRelatedSetValue;\r
21 import org.simantics.objmap.structural.rules.domain.RelatedValueAccessor;\r
22 \r
23 /**\r
24  * Rule factory for mapped value using Getter/Setter-methods.\r
25  * \r
26  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
27  *\r
28  */\r
29 public class TypeRelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<StructuralResource,Range> {\r
30         \r
31         @Override\r
32         public IBidirectionalMappingRule<StructuralResource, Range> create(ReadGraph g, Annotation annotation,\r
33                         Method getter, Method setter)\r
34                         throws DatabaseException {\r
35                 TypeRelatedGetValue getterAnn = (TypeRelatedGetValue)annotation;\r
36                 \r
37                 Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();\r
38                  IRangeAccessor<Range,Object> rangeAccessor = new GetSetValueAccessor<Range,Object>(getter, setter);\r
39         Resource valueType;\r
40         if (adapterClass == IdentityAdapter.class) {\r
41             valueType = dataTypeOfClass(g, getter.getReturnType());\r
42         } else {\r
43                 try{\r
44                          ValueAdapter adapter = adapterClass.newInstance();\r
45                  rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);\r
46                  valueType = adapter.rangeTypeToDomainType(g, getter.getReturnType());\r
47              } catch (InstantiationException e) {\r
48                  throw new RuntimeException(e);\r
49              } catch (IllegalAccessException e) {\r
50                  throw new RuntimeException(e);\r
51              }\r
52         }\r
53                 return new ValueRule<StructuralResource,Range>(new RelatedValueAccessor(g.getResource(getterAnn.value()), valueType,true,true,true),\r
54                                         rangeAccessor);\r
55         }\r
56         \r
57         @Override\r
58         public boolean isSetter(Annotation getterAnnotation, Annotation annotation) {\r
59                 TypeRelatedGetValue getterAnn = (TypeRelatedGetValue)getterAnnotation;\r
60                 TypeRelatedSetValue setterAnn = (TypeRelatedSetValue)annotation;\r
61                 return getterAnn.value().equals(setterAnn.value());\r
62         }\r
63         \r
64         public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {\r
65         Layer0 b = Layer0.getInstance(g);\r
66         if(clazz.equals(Double.class) || clazz.equals(double.class))\r
67             return b.Double;\r
68         else if(clazz.equals(String.class))\r
69             return b.String;\r
70         else if(clazz.equals(Integer.class) || clazz.equals(int.class))\r
71             return b.Integer;\r
72         else if(clazz.equals(Float.class) || clazz.equals(float.class))\r
73             return b.Float;\r
74         else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))\r
75             return b.Boolean;\r
76         else if(clazz.equals(Long.class) || clazz.equals(long.class))\r
77             return b.Long;\r
78         else if(clazz.equals(Byte.class) || clazz.equals(byte.class))\r
79             return b.Byte;\r
80         \r
81         else if(clazz.equals(double[].class))\r
82             return b.DoubleArray;\r
83         else if(clazz.equals(int[].class))\r
84             return b.IntegerArray;\r
85         else if(clazz.equals(byte[].class))\r
86             return b.ByteArray;\r
87         else if(clazz.equals(float[].class))\r
88             return b.FloatArray;\r
89         else if(clazz.equals(boolean[].class))\r
90             return b.BooleanArray;\r
91         else if(clazz.equals(String[].class))\r
92             return b.StringArray;\r
93         else if(clazz.equals(long[].class))\r
94             return b.LongArray;\r
95         else {\r
96                 System.out.println("Couldn't find a data type for " + clazz);\r
97             return null;\r
98         }\r
99     }\r
100 \r
101 }\r