]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaFloat.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / java / JavaFloat.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.FloatAccessor;\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.FloatInterestSet;\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.FloatBinding;\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.FloatType;\r
33
34 public class JavaFloat extends JavaObject implements FloatAccessor {
35         
36         public JavaFloat(Accessor parent, FloatBinding binding, Object object, AccessorParams params) {
37                 super(parent, binding, object, params);
38         }
39         
40         public FloatBinding getBinding() {
41                 return (FloatBinding) binding;
42         }
43
44         @Override
45         public FloatType type() {
46                 return getBinding().type();
47         }
48
49         @Override
50         public float 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(float newValue) throws AccessorException {
63                 if (newValue == getValue()) return;\r
64                 writeLock();\r
65                 try {
66                         if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {\r
67                                 JavaObject jo = (JavaObject) parent;\r
68                                 ArrayBinding ab = (ArrayBinding) jo.binding;\r
69                                 Object nv = getBinding().create(newValue);\r
70                                 ab.set(jo.object, (Integer)keyInParent, nv);\r
71                                 this.object = nv;\r
72                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {\r
73                                 JavaObject jo = (JavaObject) parent;\r
74                                 RecordBinding rb = (RecordBinding) jo.binding;\r
75                                 Object nv = getBinding().create(newValue);\r
76                                 rb.setComponent(jo.object, (Integer)keyInParent, nv);\r
77                                 this.object = nv;\r
78                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {\r
79                                 JavaObject jo = (JavaObject) parent;\r
80                                 VariantBinding vb = (VariantBinding) jo.binding;\r
81                                 Object nv = getBinding().create(newValue);\r
82                                 vb.setContent(jo.object, getBinding(), nv);\r
83                                 this.object = nv;\r
84                         } else {                        \r
85                                 // Set value\r
86                                 getBinding().setValue(object, newValue);\r
87                         }\r
88 \r
89                         
90                         // Notify
91                         ListenerEntry le = listeners;
92                         while (le!=null) {
93                                 FloatInterestSet is = le.getInterestSet();
94                                 if (is.inNotifications()) {
95                                         Event e = new ValueAssigned( Bindings.FLOAT, is.inValues() ? newValue : null );
96                                         emitEvent(le, e);
97                                 }
98                                 le = le.next;
99                         }
100                         
101                 } catch (BindingException e) {
102                         throw new AccessorException(e);
103                 } finally {\r
104                         writeUnlock();\r
105                 }
106         }
107
108         @Override
109         public void setValue(Binding binding, Object newValue) throws AccessorException {
110                 try {
111                         float nv = ((FloatBinding)binding).getValue_(newValue);
112                         setValue(nv);
113                 } catch (BindingException e) {
114                         throw new AccessorException(e);
115                 }
116         }
117
118         @SuppressWarnings("unchecked")
119         @Override
120         public <T extends Accessor> T getComponent(ChildReference reference)
121         throws AccessorConstructionException {
122                 if (reference==null) return (T) this;
123
124                 throw new ReferenceException(reference.getClass()+" is not a subreference of BooleanType");     
125         }
126
127         @Override
128         Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
129                 Event rollback = makeRollback ? new ValueAssigned( Bindings.FLOAT, getValue() ) : null;                 \r
130                 if (e instanceof ValueAssigned) {\r
131                         ValueAssigned va = (ValueAssigned) e;\r
132                         if (va.newValue == null) throw new AccessorException("Float value expected, got null");                 \r
133                         setValue(va.newValue.getBinding(), va.newValue.getValue());\r
134                         return rollback;\r
135                 } else {\r
136                         throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Float");                        \r
137                 }
138         }
139
140 }