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.BooleanAccessor;
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.BooleanInterestSet;
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.BooleanBinding;
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.BooleanType;
35 * Accessor to a Java Object of Boolean Type.
37 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
39 public class JavaBoolean extends JavaObject implements BooleanAccessor {
41 public JavaBoolean(Accessor parent, BooleanBinding binding, Object object, AccessorParams params) {
42 super(parent, binding, object, params);
45 public BooleanBinding getBinding() {
46 return (BooleanBinding) binding;
50 public BooleanType type() {
51 return getBinding().type();
55 public boolean getValue() throws AccessorException {
58 return getBinding().getValue_(object);
59 } catch (BindingException e) {
60 throw new AccessorException(e);
67 public void setValue(boolean 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) ((JavaVariant)parent).binding;
86 Object nv = getBinding().create(newValue);
87 vb.setContent(jo.object, getBinding(), nv);
91 getBinding().setValue(object, newValue);
95 ListenerEntry le = listeners;
97 BooleanInterestSet is = le.getInterestSet();
98 if (is.inNotifications()) {
99 Event e = new ValueAssigned( Bindings.BOOLEAN, is.inValues() ? newValue : null );
105 } catch (BindingException e) {
106 throw new AccessorException(e);
113 public void setValue(Binding binding, Object newValue) throws AccessorException {
115 boolean nv = ((BooleanBinding)binding).getValue_(newValue);
117 } catch (BindingException e) {
118 throw new AccessorException(e);
122 @SuppressWarnings("unchecked")
123 public <T extends Accessor> T getComponent(ChildReference reference)
124 throws AccessorConstructionException {
125 if (reference==null) return (T) this;
126 throw new ReferenceException(reference.getClass()+" is not a subreference of BooleanType");
130 Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
131 Event rollback = null;
132 rollback = makeRollback ? new ValueAssigned( Bindings.BOOLEAN, getValue() ) : null;
133 if (e instanceof ValueAssigned) {
134 ValueAssigned va = (ValueAssigned) e;
135 if (va.newValue == null) throw new AccessorException("Boolean value expected, got null");
136 setValue(va.newValue.getBinding(), va.newValue.getValue());
138 } else throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Boolean");