]> gerrit.simantics Code Review - simantics/3d.git/blob
e1b8d2d3aff981fb3c7826af5167547969938666
[simantics/3d.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.objmap.structural.annotations.factories;\r
13 \r
14 import java.lang.annotation.Annotation;\r
15 import java.lang.reflect.Method;\r
16 \r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.layer0.Layer0;\r
21 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
22 import org.simantics.objmap.graph.annotations.RelatedGetValue;\r
23 import org.simantics.objmap.graph.annotations.RelatedSetValue;\r
24 import org.simantics.objmap.graph.rules.ValueRule;\r
25 import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;\r
26 import org.simantics.objmap.graph.rules.adapters.ValueAdapter;\r
27 import org.simantics.objmap.structural.StructuralResource;\r
28 import org.simantics.objmap.structural.rules.domain.RelatedValueAccessor;\r
29 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;\r
30 import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;\r
31 import org.simantics.objmap.graph.rules.range.GetSetValueAccessor;\r
32 import org.simantics.objmap.graph.rules.range.IRangeAccessor;\r
33 \r
34 /**\r
35  * Rule factory for mapped value using Getter/Setter-methods.\r
36  * \r
37  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
38  *\r
39  */\r
40 public class RelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<StructuralResource,Range> {\r
41         \r
42         @Override\r
43         public IBidirectionalMappingRule<StructuralResource, Range> create(ReadGraph g, Annotation annotation,\r
44                         Method getter, Method setter)\r
45                         throws DatabaseException {\r
46                 RelatedGetValue getterAnn = (RelatedGetValue)annotation;\r
47                 \r
48                 Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();\r
49                  IRangeAccessor<Range,Object> rangeAccessor = new GetSetValueAccessor<Range,Object>(getter, setter);\r
50         Resource valueType;\r
51         if (adapterClass == IdentityAdapter.class) {\r
52             valueType = dataTypeOfClass(g, getter.getReturnType());\r
53         } else {\r
54                 try{\r
55                          ValueAdapter adapter = adapterClass.newInstance();\r
56                  rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);\r
57                  valueType = adapter.rangeTypeToDomainType(g, getter.getReturnType());\r
58              } catch (InstantiationException e) {\r
59                  throw new RuntimeException(e);\r
60              } catch (IllegalAccessException e) {\r
61                  throw new RuntimeException(e);\r
62              }\r
63         }\r
64                 return new ValueRule<StructuralResource,Range>(new RelatedValueAccessor(g.getResource(getterAnn.value()), valueType,false,true,false),\r
65                                         rangeAccessor);\r
66         }\r
67         \r
68         @Override\r
69         public boolean isSetter(Annotation getterAnnotation, Annotation annotation) {\r
70                 RelatedGetValue getterAnn = (RelatedGetValue)getterAnnotation;\r
71                 RelatedSetValue setterAnn = (RelatedSetValue)annotation;\r
72                 return getterAnn.value().equals(setterAnn.value());\r
73         }\r
74         \r
75         public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {\r
76         Layer0 b = Layer0.getInstance(g);\r
77         if(clazz.equals(Double.class) || clazz.equals(double.class))\r
78             return b.Double;\r
79         else if(clazz.equals(String.class))\r
80             return b.String;\r
81         else if(clazz.equals(Integer.class) || clazz.equals(int.class))\r
82             return b.Integer;\r
83         else if(clazz.equals(Float.class) || clazz.equals(float.class))\r
84             return b.Float;\r
85         else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))\r
86             return b.Boolean;\r
87         else if(clazz.equals(Long.class) || clazz.equals(long.class))\r
88             return b.Long;\r
89         else if(clazz.equals(Byte.class) || clazz.equals(byte.class))\r
90             return b.Byte;\r
91         \r
92         else if(clazz.equals(double[].class))\r
93             return b.DoubleArray;\r
94         else if(clazz.equals(int[].class))\r
95             return b.IntegerArray;\r
96         else if(clazz.equals(byte[].class))\r
97             return b.ByteArray;\r
98         else if(clazz.equals(float[].class))\r
99             return b.FloatArray;\r
100         else if(clazz.equals(boolean[].class))\r
101             return b.BooleanArray;\r
102         else if(clazz.equals(String[].class))\r
103             return b.StringArray;\r
104         else if(clazz.equals(long[].class))\r
105             return b.LongArray;\r
106         else {\r
107                 System.out.println("Couldn't find a data type for " + clazz);\r
108             return null;\r
109         }\r
110     }\r
111 \r
112 }\r