1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.accessor.java;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.accessor.Accessor;
16 import org.simantics.databoard.accessor.FloatAccessor;
17 import org.simantics.databoard.accessor.error.AccessorConstructionException;
18 import org.simantics.databoard.accessor.error.AccessorException;
19 import org.simantics.databoard.accessor.error.ReferenceException;
20 import org.simantics.databoard.accessor.event.Event;
21 import org.simantics.databoard.accessor.event.ValueAssigned;
22 import org.simantics.databoard.accessor.impl.AccessorParams;
23 import org.simantics.databoard.accessor.impl.ListenerEntry;
24 import org.simantics.databoard.accessor.interestset.FloatInterestSet;
25 import org.simantics.databoard.accessor.reference.ChildReference;
26 import org.simantics.databoard.binding.ArrayBinding;
27 import org.simantics.databoard.binding.Binding;
28 import org.simantics.databoard.binding.FloatBinding;
29 import org.simantics.databoard.binding.RecordBinding;
30 import org.simantics.databoard.binding.VariantBinding;
31 import org.simantics.databoard.binding.error.BindingException;
32 import org.simantics.databoard.type.FloatType;
34 public class JavaFloat extends JavaObject implements FloatAccessor {
36 public JavaFloat(Accessor parent, FloatBinding binding, Object object, AccessorParams params) {
37 super(parent, binding, object, params);
40 public FloatBinding getBinding() {
41 return (FloatBinding) binding;
45 public FloatType type() {
46 return getBinding().type();
50 public float getValue() throws AccessorException {
53 return getBinding().getValue_(object);
54 } catch (BindingException e) {
55 throw new AccessorException(e);
62 public void setValue(float newValue) throws AccessorException {
63 if (newValue == getValue()) return;
66 if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {
67 JavaObject jo = (JavaObject) parent;
68 ArrayBinding ab = (ArrayBinding) jo.binding;
69 Object nv = getBinding().create(newValue);
70 ab.set(jo.object, (Integer)keyInParent, nv);
72 } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {
73 JavaObject jo = (JavaObject) parent;
74 RecordBinding rb = (RecordBinding) jo.binding;
75 Object nv = getBinding().create(newValue);
76 rb.setComponent(jo.object, (Integer)keyInParent, nv);
78 } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {
79 JavaObject jo = (JavaObject) parent;
80 VariantBinding vb = (VariantBinding) jo.binding;
81 Object nv = getBinding().create(newValue);
82 vb.setContent(jo.object, getBinding(), nv);
86 getBinding().setValue(object, newValue);
91 ListenerEntry le = listeners;
93 FloatInterestSet is = le.getInterestSet();
94 if (is.inNotifications()) {
95 Event e = new ValueAssigned( Bindings.FLOAT, is.inValues() ? newValue : null );
101 } catch (BindingException e) {
102 throw new AccessorException(e);
109 public void setValue(Binding binding, Object newValue) throws AccessorException {
111 float nv = ((FloatBinding)binding).getValue_(newValue);
113 } catch (BindingException e) {
114 throw new AccessorException(e);
118 @SuppressWarnings("unchecked")
120 public <T extends Accessor> T getComponent(ChildReference reference)
121 throws AccessorConstructionException {
122 if (reference==null) return (T) this;
124 throw new ReferenceException(reference.getClass()+" is not a subreference of BooleanType");
128 Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
129 Event rollback = makeRollback ? new ValueAssigned( Bindings.FLOAT, getValue() ) : null;
130 if (e instanceof ValueAssigned) {
131 ValueAssigned va = (ValueAssigned) e;
132 if (va.newValue == null) throw new AccessorException("Float value expected, got null");
133 setValue(va.newValue.getBinding(), va.newValue.getValue());
136 throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Float");