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.DoubleAccessor;
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.DoubleInterestSet;
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.DoubleBinding;
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.DoubleType;
35 * Accessor to a Java Object that contains a Double Type.
37 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
39 public class JavaDouble extends JavaObject implements DoubleAccessor {
41 public JavaDouble(Accessor parent, DoubleBinding binding, Object object, AccessorParams params) {
42 super(parent, binding, object, params);
45 public DoubleBinding getBinding() {
46 return (DoubleBinding) binding;
50 public DoubleType type() {
51 return getBinding().type();
55 public double getValue() throws AccessorException {
58 return getBinding().getValue_(object);
59 } catch (BindingException e) {
60 throw new AccessorException(e);
67 public void setValue(double newValue) throws AccessorException {
68 if (newValue == getValue()) return;
71 if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {
72 JavaObject jo = (JavaObject) parent;
73 ArrayBinding ab = (ArrayBinding) jo.binding;
74 Object nv = getBinding().create(newValue);
75 ab.set(jo.object, (Integer)keyInParent, nv);
77 } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {
78 JavaObject jo = (JavaObject) parent;
79 RecordBinding rb = (RecordBinding) jo.binding;
80 Object nv = getBinding().create(newValue);
81 rb.setComponent(jo.object, (Integer)keyInParent, nv);
83 } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {
84 JavaObject jo = (JavaObject) parent;
85 VariantBinding vb = (VariantBinding) jo.binding;
86 Object nv = getBinding().create(newValue);
87 vb.setContent(jo.object, getBinding(), nv);
91 getBinding().setValue(object, newValue);
96 ListenerEntry le = listeners;
98 DoubleInterestSet is = le.getInterestSet();
99 if (is.inNotifications()) {
100 Event e = new ValueAssigned( Bindings.DOUBLE, is.inValues() ? newValue : null );
106 } catch (BindingException e) {
107 throw new AccessorException(e);
114 public void setValue(Binding binding, Object newValue) throws AccessorException {
116 double nv = ((DoubleBinding)binding).getValue_(newValue);
118 } catch (BindingException e) {
119 throw new AccessorException(e);
123 @SuppressWarnings("unchecked")
125 public <T extends Accessor> T getComponent(ChildReference reference)
126 throws AccessorConstructionException {
127 if (reference==null) return (T) this;
128 throw new ReferenceException(reference.getClass()+" is not a subreference of BooleanType");
132 Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
133 Event rollback = null;
134 if (e instanceof ValueAssigned) {
135 ValueAssigned va = (ValueAssigned) e;
136 if (va.newValue == null) throw new AccessorException("Double value expected, got null");
137 if (makeRollback) rollback = new ValueAssigned(getBinding(), getValue(getBinding()));
138 setValue(va.newValue.getBinding(), va.newValue.getValue());
141 throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Double");