]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaInteger.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / java / JavaInteger.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 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.databoard.accessor.java;
13
14 import org.simantics.databoard.Bindings;\r
15 import org.simantics.databoard.accessor.Accessor;\r
16 import org.simantics.databoard.accessor.IntegerAccessor;\r
17 import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
18 import org.simantics.databoard.accessor.error.AccessorException;\r
19 import org.simantics.databoard.accessor.error.ReferenceException;\r
20 import org.simantics.databoard.accessor.event.Event;\r
21 import org.simantics.databoard.accessor.event.ValueAssigned;\r
22 import org.simantics.databoard.accessor.impl.AccessorParams;\r
23 import org.simantics.databoard.accessor.impl.ListenerEntry;\r
24 import org.simantics.databoard.accessor.interestset.IntegerInterestSet;\r
25 import org.simantics.databoard.accessor.reference.ChildReference;\r
26 import org.simantics.databoard.binding.ArrayBinding;\r
27 import org.simantics.databoard.binding.Binding;\r
28 import org.simantics.databoard.binding.IntegerBinding;\r
29 import org.simantics.databoard.binding.RecordBinding;\r
30 import org.simantics.databoard.binding.VariantBinding;\r
31 import org.simantics.databoard.binding.error.BindingException;\r
32 import org.simantics.databoard.type.IntegerType;\r
33
34 public class JavaInteger extends JavaObject implements IntegerAccessor {
35         
36         public JavaInteger(Accessor parent, IntegerBinding binding, Object object, AccessorParams params) {
37                 super(parent, binding, object, params);
38         }
39         
40         public IntegerBinding getBinding() {
41                 return (IntegerBinding) binding;
42         }
43
44         @Override
45         public IntegerType type() {
46                 return getBinding().type();
47         }
48
49         @Override
50         public int getValue() throws AccessorException {\r
51                 readLock();
52                 try {
53                         return getBinding().getValue_(object);
54                 } catch (BindingException e) {
55                         throw new AccessorException(e);
56                 } finally {\r
57                         readUnlock();\r
58                 }
59         }
60         
61         @Override
62         public void setValue(int newValue) throws AccessorException {
63                 if (newValue == getValue()) return;\r
64                 try {
65                         if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {\r
66                                 JavaObject jo = (JavaObject) parent;\r
67                                 ArrayBinding ab = (ArrayBinding) jo.binding;\r
68                                 Object nv = getBinding().create(newValue);\r
69                                 ab.set(jo.object, (Integer)keyInParent, nv);\r
70                                 this.object = nv;\r
71                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {\r
72                                 JavaObject jo = (JavaObject) parent;\r
73                                 RecordBinding rb = (RecordBinding) jo.binding;\r
74                                 Object nv = getBinding().create(newValue);\r
75                                 rb.setComponent(jo.object, (Integer)keyInParent, nv);\r
76                                 this.object = nv;\r
77                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {\r
78                                 JavaObject jo = (JavaObject) parent;\r
79                                 VariantBinding vb = (VariantBinding) jo.binding;\r
80                                 Object nv = getBinding().create(newValue);\r
81                                 vb.setContent(jo.object, getBinding(), nv );\r
82                                 this.object = nv;\r
83                         } else {                        \r
84                                 // Set value\r
85                                 getBinding().setValue(object, newValue);\r
86                         }\r
87                         
88                         // Notify
89                         ListenerEntry le = listeners;
90                         while (le!=null) {
91                                 IntegerInterestSet is = le.getInterestSet();
92                                 if (is.inNotifications()) {
93                                         Event e = new ValueAssigned( Bindings.INTEGER, is.inValues() ? newValue : null );
94                                         emitEvent(le, e);
95                                 }
96                                 le = le.next;
97                         }
98                         
99                 } catch (BindingException e) {
100                         throw new AccessorException(e);
101                 }
102         }
103
104         @Override
105         public void setValue(Binding binding, Object newValue) throws AccessorException {
106                 try {
107                         int nv = ((IntegerBinding)binding).getValue_(newValue);
108                         setValue(nv);
109                 } catch (BindingException e) {
110                         throw new AccessorException(e);
111                 }
112         }
113
114         @SuppressWarnings("unchecked")
115         @Override
116         public <T extends Accessor> T getComponent(ChildReference reference)
117         throws AccessorConstructionException {
118                 if (reference==null) return (T) this;
119
120                 throw new ReferenceException(reference.getClass()+" is not a subreference of BooleanType");     
121         }
122         
123         @Override
124         Event applyLocal(Event e, boolean makeRollback) throws AccessorException {\r
125                 Event rollback = makeRollback ? new ValueAssigned( Bindings.INTEGER, getValue() ) : null;               \r
126                 if (e instanceof ValueAssigned) {\r
127                         ValueAssigned va = (ValueAssigned) e;\r
128                         if (va.newValue == null) throw new AccessorException("Integer value expected, got null");                       \r
129                         setValue(va.newValue.getBinding(), va.newValue.getValue());\r
130                         return rollback;\r
131                 } else {\r
132                         throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Integer");\r
133                 }
134         }
135         
136 }
137