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.IntegerAccessor;
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.IntegerInterestSet;
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.IntegerBinding;
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.IntegerType;
34 public class JavaInteger extends JavaObject implements IntegerAccessor {
36 public JavaInteger(Accessor parent, IntegerBinding binding, Object object, AccessorParams params) {
37 super(parent, binding, object, params);
40 public IntegerBinding getBinding() {
41 return (IntegerBinding) binding;
45 public IntegerType type() {
46 return getBinding().type();
50 public int getValue() throws AccessorException {
53 return getBinding().getValue_(object);
54 } catch (BindingException e) {
55 throw new AccessorException(e);
62 public void setValue(int newValue) throws AccessorException {
63 if (newValue == getValue()) return;
65 if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {
66 JavaObject jo = (JavaObject) parent;
67 ArrayBinding ab = (ArrayBinding) jo.binding;
68 Object nv = getBinding().create(newValue);
69 ab.set(jo.object, (Integer)keyInParent, nv);
71 } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {
72 JavaObject jo = (JavaObject) parent;
73 RecordBinding rb = (RecordBinding) jo.binding;
74 Object nv = getBinding().create(newValue);
75 rb.setComponent(jo.object, (Integer)keyInParent, nv);
77 } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {
78 JavaObject jo = (JavaObject) parent;
79 VariantBinding vb = (VariantBinding) jo.binding;
80 Object nv = getBinding().create(newValue);
81 vb.setContent(jo.object, getBinding(), nv );
85 getBinding().setValue(object, newValue);
89 ListenerEntry le = listeners;
91 IntegerInterestSet is = le.getInterestSet();
92 if (is.inNotifications()) {
93 Event e = new ValueAssigned( Bindings.INTEGER, is.inValues() ? newValue : null );
99 } catch (BindingException e) {
100 throw new AccessorException(e);
105 public void setValue(Binding binding, Object newValue) throws AccessorException {
107 int nv = ((IntegerBinding)binding).getValue_(newValue);
109 } catch (BindingException e) {
110 throw new AccessorException(e);
114 @SuppressWarnings("unchecked")
116 public <T extends Accessor> T getComponent(ChildReference reference)
117 throws AccessorConstructionException {
118 if (reference==null) return (T) this;
120 throw new ReferenceException(reference.getClass()+" is not a subreference of BooleanType");
124 Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
125 Event rollback = makeRollback ? new ValueAssigned( Bindings.INTEGER, getValue() ) : null;
126 if (e instanceof ValueAssigned) {
127 ValueAssigned va = (ValueAssigned) e;
128 if (va.newValue == null) throw new AccessorException("Integer value expected, got null");
129 setValue(va.newValue.getBinding(), va.newValue.getValue());
132 throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Integer");