]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/reflection/MyBinding.java
Fixing several binding-related bugs
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / reflection / MyBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.reflection;
13
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.databoard.binding.FloatBinding;
16 import org.simantics.databoard.binding.LongBinding;
17 import org.simantics.databoard.binding.error.BindingConstructionException;
18 import org.simantics.databoard.binding.error.BindingException;
19 import org.simantics.databoard.type.NumberType;
20 import org.simantics.databoard.type.RecordType;
21
22 public class MyBinding extends ClassBinding {
23         
24         public MyBinding(RecordType type) throws BindingConstructionException {
25                 super( ClassInfo.getInfo(MyClass.class) );
26                 this.type = type;
27         }
28
29 //      @Override
30         public Object getComponent(Object obj, int index) throws BindingException {
31                 MyClass x = (MyClass) obj;
32                 switch ( index ) {
33                 case 0: return x.field0;
34                 case 1: return x.field1;
35                 case 2: return x.field2;
36                 case 3: return x.field3;
37                 case 4: return x.field4;
38                 case 5: return x.field5;
39                 case 6: return x.field6;
40                 case 7: return x.field7;
41                 case 8: return x.field8;
42                 case 9: return x.field9;
43                 case 10: return x.field10;
44                 case 11: return x.field11;
45                 case 12: return x.field12;
46                 case 13: return x.field13;
47                 case 14: return x.field14;
48                 case 15: return x.field15;
49                 case 16: return x.field16;
50                 default: throw new BindingException("Illegal field index");
51                 }
52         }
53
54         public Object getComponent2(Object obj, int index) throws BindingException {
55                 MyClass x = (MyClass) obj;
56                 switch ( index ) {
57                 case 0: return x.getField0();
58                 case 1: return x.getField1();
59                 case 2: return x.getField2();
60                 case 3: return x.getField3();
61                 case 4: return x.getField4();
62                 case 5: return x.getField5();
63                 case 6: return x.getField6();
64                 case 7: return x.getField7();
65                 case 8: return x.getField8();
66                 case 9: return x.getField9();
67                 case 10: return x.getField10();
68                 case 11: return x.getField11();
69                 case 12: return x.getField12();
70                 case 13: return x.getField13();
71                 case 14: return x.getField14();
72                 case 15: return x.getField15();
73                 case 16: return x.getField16();
74                 default: throw new BindingException("Illegal field index");
75                 }
76         }
77         
78 //      @Override
79         public Object create(Object... values) throws BindingException {
80 //              return new MyClass(
81 //                              (Byte) values[0],
82 //                              (Integer) values[1],
83 //                              (Short) values[2],
84 //                              (Long) values[3],
85 //                              (Float) values[4],
86 //                              (Boolean) values[5],
87 //                              (Double) values[6],
88 //                              (NumberType) values[7],
89 //                              (byte[]) values[8],
90 //                              (int[]) values[9],
91 //                              (short[]) values[10],
92 //                              (long[]) values[11],
93 //                              (float[]) values[12],
94 //                              (boolean[]) values[13],
95 //                              (double[]) values[14],
96 //                              values[15],
97 //                              (Integer) values[16]
98 //              );
99                 return null;
100         }
101         
102         public Object create2(Object... values) throws BindingException {
103                 MyClass x = new MyClass(this);
104                 setComponents(x, values);
105                 return x;
106         }       
107         
108 //      @Override
109         public Object createPartial() throws BindingException {
110                 return new MyClass(this);
111         }
112
113         public Object createPartial2() throws BindingException {
114                 Object values[] = new Object[15];
115                 for (int i=0; i<values.length; i++) {
116                         Binding fb = componentBindings[i];
117                         values[i] = fb.createDefault();
118                 }                               
119                 return create(values);
120         }
121         
122 //      @Override
123         public void setComponents(Object obj, Object... value)
124                         throws BindingException {
125                 MyClass x = (MyClass) obj;
126                 // 0: Write field
127                 x.field0 = (Byte) value[0];
128                 x.field1 = (Integer) value[1];
129                 x.field2 = (Short) value[2];
130                 x.field3 = (Long) value[3];
131                 x.field4 = (Float) value[4];
132                 x.field5 = (Boolean) value[5];
133                 x.field6 = (Double) value[6];
134                 x.field7 = (NumberType) value[7];
135                 x.field8 = (byte[]) value[8];
136                 x.field9 = (int[]) value[9];
137                 x.field10 = (short[]) value[10];
138                 x.field11 = (long[]) value[11];
139                 x.field12 = (float[]) value[12];
140                 x.field13 = (boolean[]) value[13];
141                 x.field14 = (double[]) value[14];
142                 x.field15 = value[15];
143                 x.field16 = (Integer) value[16];
144         }
145
146         public void setComponents2(Object obj, Object... value)
147                         throws BindingException {
148                 MyClass x = (MyClass) obj;
149                 // Set with setters
150                 x.setField0((Byte) value[0]);
151                 x.setField1((Integer) value[1]);
152                 x.setField2((Short) value[2]);
153                 x.setField3((Long) value[3]);
154                 x.setField4((Float) value[4]);
155                 x.setField5((Boolean) value[5]);
156                 x.setField6((Double) value[6]);
157                 x.setField7((NumberType) value[7]);
158                 x.setField8((byte[]) value[8]);
159                 x.setField9((int[]) value[9]);
160                 x.setField10((short[]) value[10]);
161                 x.setField11((long[]) value[11]);
162                 x.setField12((float[]) value[12]);
163                 x.setField13((boolean[]) value[13]);
164                 x.setField14((double[]) value[14]);
165                 x.setField15(value[15]);
166                 x.setField16((Integer)value[16]);
167         }
168
169 //      @Override
170         public void setComponent(Object obj, int index, Object value)
171                         throws BindingException {
172                 MyClass x = (MyClass) obj;
173                 switch ( index ) {
174                 case 0: x.field0 = (Byte) value;
175                 case 1: x.field1 = (Integer) value;
176                 case 2: x.field2 = (Short) value;
177                 case 3: x.field3 = (Long) value;
178                 case 4: x.field4 = (Float) value;
179                 case 5: x.field5 = (Boolean) value;
180                 case 6: x.field6 = (Double) value;
181                 case 7: x.field7 = (NumberType) value;
182                 case 8: x.field8 = (byte[]) value;
183                 case 9: x.field9 = (int[]) value;
184                 case 10: x.field10 = (short[]) value;
185                 case 11: x.field11 = (long[]) value;
186                 case 12: x.field12 = (float[]) value;
187                 case 13: x.field13 = (boolean[]) value;
188                 case 14: x.field14 = (double[]) value;
189                 case 15: x.field15 = value;
190                 default: throw new BindingException("Illegal field index");                     
191                 }
192         }
193
194         public void setComponent2(Object obj, int index, Object value)
195                         throws BindingException {
196                 MyClass x = (MyClass) obj;
197                 switch ( index ) {
198                 case 0: x.setField0( (Byte) value );
199                 case 1: x.setField1( (Integer) value );
200                 case 2: x.setField2( (Short) value );
201                 case 3: x.setField3( (Long) value );
202                 case 4: x.setField4( (Float) value );
203                 case 5: x.setField5( (Boolean) value );
204                 case 6: x.setField6( (Double) value );
205                 case 7: x.setField7( (NumberType) value );
206                 case 8: x.setField8( (byte[]) value );
207                 case 9: x.setField9( (int[]) value );
208                 case 10: x.setField10( (short[]) value );
209                 case 11: x.setField11( (long[]) value );
210                 case 12: x.setField12( (float[]) value );
211                 case 13: x.setField13( (boolean[]) value );
212                 case 14: x.setField14( (double[]) value );
213                 case 15: x.setField15( value );
214                 default: throw new BindingException("Illegal field index");                     
215                 }
216         }
217
218 //      @Override
219         public boolean isInstance(Object obj) {
220                 return obj instanceof MyClass;
221         }
222         
223 //      @Override
224         public boolean isImmutable() {
225                 return false;
226         }
227
228         public void setBoolean(Object r, int index, boolean z) throws BindingException
229         {
230                 MyClass x = (MyClass) r;
231                 switch ( index ) {
232                 case 5: x.field5 = z; break;
233                 case 19: x.field19 = z; break;
234                 default: throw new BindingException("Field is not boolean");                    
235                 }
236         }
237
238         public void setBoolean2(Object r, int index, boolean z) throws BindingException
239         {
240                 MyClass x = (MyClass) r;
241                 switch ( index ) {
242                 case 5: x.field5 = z; break;
243                 case 19: x.field19 = z; break;
244                 default: throw new BindingException("Field is not boolean");                    
245                 }
246         }
247
248         public void setBoolean3(Object r, int index, boolean z) throws BindingException
249         {
250                 MyClass x = (MyClass) r;
251                 if ( index==5 ) x.field5 = z; 
252                 else if ( index==19 ) x.field19 = z; 
253                 //default: throw new BindingException("Field is not boolean");                  
254         
255         }
256         
257         public boolean getBoolean(Object r, int index) throws BindingException
258         {
259                 MyClass x = (MyClass) r;
260                 switch ( index ) {
261                 case 5: return x.field5;
262                 case 19: return x.getField5();
263                 default: throw new BindingException("Field is not boolean");                    
264                 }
265         }
266         
267         public void setByte(Object r, int index, byte z) throws BindingException
268         {
269                 MyClass x = (MyClass) r;
270                 switch ( index ) {
271                 case 6: x.field6 = z; break;
272                 default: throw new BindingException("Field is not boolean");                    
273                 }
274         }
275         
276         public byte getByte(Object r, int index) throws BindingException
277         {
278                 try {
279                         return ci.fields[index].getByte(r);
280                 } catch (IllegalArgumentException e) {
281                         throw new BindingException(e);
282                 } catch (IllegalAccessException e) {
283                         throw new BindingException(e);
284                 }
285         }
286
287         public void setInt(Object r, int index, int x) throws BindingException
288         {
289                 try {
290                         ci.fields[index].setInt(r, x);
291                 } catch (IllegalArgumentException e) {
292                         throw new BindingException(e);
293                 } catch (IllegalAccessException e) {
294                         throw new BindingException(e);
295                 }
296         }
297         
298         public int getInt(Object r, int index) throws BindingException
299         {
300                 try {
301                         return ci.fields[index].getInt(r);
302                 } catch (IllegalArgumentException e) {
303                         throw new BindingException(e);
304                 } catch (IllegalAccessException e) {
305                         throw new BindingException(e);
306                 }
307         }
308         
309         public void setLong(Object r, int index, long z) throws BindingException
310         {
311                 MyClass x = (MyClass) r;
312                 switch ( index ) {
313                 case 6: x.field3 = z; break;
314                 case 8: x.setField3(z); break;
315                 default: setComponent(r, index, ((LongBinding)componentBindings[index]).create(z));                     
316                 }
317         }
318         
319         public long getLong(Object r, int index) throws BindingException
320         {
321                 MyClass x = (MyClass) r;
322                 switch ( index ) {
323                 case 3: return x.field3;
324                 case 4: return x.field3;
325                 default: return ((LongBinding)componentBindings[index]).getValue_( getComponent(r, index) );
326                 }
327         }
328         
329         public void setFloat(Object r, int index, float z) throws BindingException
330         {
331                 MyClass x = (MyClass) r;
332                 switch ( index ) {
333 //              case 6: x.field4 = z; break;
334                 //case 8: x.; break;
335                 default: setComponent(r, index, ((FloatBinding)componentBindings[index]).create(z));                    
336                 }
337         }
338         
339         public float getFloat(Object r, int index) throws BindingException
340         {
341                 try {
342                         return ci.fields[index].getFloat(r);
343                 } catch (IllegalArgumentException e) {
344                         throw new BindingException(e);
345                 } catch (IllegalAccessException e) {
346                         throw new BindingException(e);
347                 }
348         }
349         
350         public void setDouble(Object r, int index, double z) throws BindingException
351         {
352                 MyClass x = (MyClass) r;
353                 switch ( index ) {
354                 case 6: x.field6 = z; break;
355                 //case 8: x.; break;
356                 default: throw new BindingException("Field is not boolean");                    
357                 }
358         }
359         
360         public double getDouble(Object r, int index) throws BindingException
361         {
362                 MyClass x = (MyClass) r;
363                 switch ( index ) {
364 //              case 3: return x.field6;
365                 case 4: return x.getField6_();
366                 case 5: return x.field16;
367                 default: throw new BindingException("Field is not boolean");                    
368                 }
369         }
370         
371         
372 }