1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.accessor.java;
14 import org.simantics.databoard.Bindings;
\r
15 import org.simantics.databoard.accessor.Accessor;
\r
16 import org.simantics.databoard.accessor.ByteAccessor;
\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.ByteInterestSet;
\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.ByteBinding;
\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.ByteType;
\r
35 * Accessor to a Java Object that contains a Byte Type.
37 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
39 public class JavaByte extends JavaObject implements ByteAccessor {
41 public JavaByte(Accessor parent, ByteBinding binding, Object object, AccessorParams params) {
42 super(parent, binding, object, params);
45 public ByteBinding getBinding() {
46 return (ByteBinding) binding;
50 public ByteType type() {
51 return getBinding().type();
55 public byte getValue() throws AccessorException {
\r
58 return getBinding().getValue_(object);
59 } catch (BindingException e) {
60 throw new AccessorException(e);
67 public void setValue(byte newValue) throws AccessorException {
68 if (newValue == getValue()) return;
\r
71 if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {
\r
72 JavaObject jo = (JavaObject) parent;
\r
73 ArrayBinding ab = (ArrayBinding) jo.binding;
\r
74 Object nv = getBinding().create(newValue);
\r
75 ab.set(jo.object, (Integer)keyInParent, nv);
\r
77 } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {
\r
78 JavaObject jo = (JavaObject) parent;
\r
79 RecordBinding rb = (RecordBinding) jo.binding;
\r
80 Object nv = getBinding().create(newValue);
\r
81 rb.setComponent(jo.object, (Integer)keyInParent, nv);
\r
83 } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {
\r
84 JavaObject jo = (JavaObject) parent;
\r
85 VariantBinding vb = (VariantBinding) jo.binding;
\r
86 Object nv = getBinding().create(newValue);
\r
87 vb.setContent(jo.object, getBinding(), nv);
\r
91 getBinding().setValue(object, newValue);
\r
96 ListenerEntry le = listeners;
98 ByteInterestSet is = le.getInterestSet();
99 if (is.inNotifications()) {
100 Event e = new ValueAssigned( Bindings.BYTE, is.inValues() ? newValue : null );
106 } catch (BindingException e) {
107 throw new AccessorException(e);
114 public void setValue(Binding binding, Object newValue) throws AccessorException {
\r
116 byte nv = ((ByteBinding)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;
129 throw new ReferenceException(reference.getClass()+" is not a subreference of BooleanType");
133 Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
\r
134 Event rollback = makeRollback ? new ValueAssigned( Bindings.BYTE, getValue() ) : null;
\r
135 if (e instanceof ValueAssigned) {
\r
136 ValueAssigned va = (ValueAssigned) e;
\r
137 if (va.newValue == null) throw new AccessorException("Byte value expected, got null");
\r
138 setValue(va.newValue.getBinding(), va.newValue.getValue());
\r
141 throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Byte");
\r