--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.accessor.java;
+
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.Accessor;\r
+import org.simantics.databoard.accessor.StringAccessor;\r
+import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
+import org.simantics.databoard.accessor.error.AccessorException;\r
+import org.simantics.databoard.accessor.error.ReferenceException;\r
+import org.simantics.databoard.accessor.event.Event;\r
+import org.simantics.databoard.accessor.event.ValueAssigned;\r
+import org.simantics.databoard.accessor.impl.AccessorParams;\r
+import org.simantics.databoard.accessor.impl.ListenerEntry;\r
+import org.simantics.databoard.accessor.interestset.StringInterestSet;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.binding.ArrayBinding;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.RecordBinding;\r
+import org.simantics.databoard.binding.StringBinding;\r
+import org.simantics.databoard.binding.VariantBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.StringType;\r
+
+public class JavaString extends JavaObject implements StringAccessor {
+
+ public JavaString(Accessor parent, StringBinding binding, Object object, AccessorParams params) {
+ super(parent, binding, object, params);
+ }
+
+ public StringBinding getBinding() {
+ return (StringBinding) binding;
+ }
+
+ @Override
+ public StringType type() {
+ return getBinding().type();
+ }
+
+ @Override
+ public String getValue() throws AccessorException {\r
+ readLock();
+ try {
+ return getBinding().getValue(object);
+ } catch (BindingException e) {
+ throw new AccessorException(e);
+ } finally {\r
+ readUnlock();\r
+ }
+ }
+
+ @Override
+ public void setValue(String newValue) throws AccessorException {
+ if (newValue.equals( getValue() )) return;\r
+ writeLock();\r
+ try {
+ if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {\r
+ JavaObject jo = (JavaObject) parent;\r
+ ArrayBinding ab = (ArrayBinding) jo.binding;\r
+ Object nv = getBinding().create(newValue);\r
+ ab.set(jo.object, (Integer)keyInParent, nv);\r
+ this.object = nv;\r
+ } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {\r
+ JavaObject jo = (JavaObject) parent;\r
+ RecordBinding rb = (RecordBinding) jo.binding;\r
+ Object nv = getBinding().create(newValue);\r
+ rb.setComponent(jo.object, (Integer)keyInParent, nv);\r
+ this.object = nv;\r
+ } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {\r
+ JavaObject jo = (JavaObject) parent;\r
+ VariantBinding vb = (VariantBinding) jo.binding;\r
+ Object nv = getBinding().create(newValue);\r
+ vb.setContent(jo.object, getBinding(), nv);\r
+ this.object = nv;\r
+ } else { \r
+ // Set value\r
+ getBinding().setValue(object, newValue);\r
+ }\r
+\r
+
+ // Notify
+ ListenerEntry le = listeners;
+ while (le!=null) {
+ StringInterestSet is = le.getInterestSet();
+ if (is.inNotifications()) {
+ Event e = new ValueAssigned( Bindings.STRING, is.inValues() ? newValue : null );
+ emitEvent(le, e);
+ }
+ le = le.next;
+ }
+
+ } catch (BindingException e) {
+ throw new AccessorException(e);
+ } finally {\r
+ writeUnlock();\r
+ }
+ }
+
+ @Override
+ public void setValue(Binding binding, Object newValue) throws AccessorException {
+ try {
+ String nv = ((StringBinding)binding).getValue(newValue);
+ setValue(nv);
+ } catch (BindingException e) {
+ throw new AccessorException(e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T extends Accessor> T getComponent(ChildReference reference)
+ throws AccessorConstructionException {
+ if (reference==null) return (T) this;
+ throw new ReferenceException(reference.getClass()+" is not a subreference of StringType");
+ }
+
+ @Override
+ Event applyLocal(Event e, boolean makeRollback) throws AccessorException {\r
+ Event rollback = makeRollback ? new ValueAssigned( Bindings.STRING, getValue() ) : null; \r
+ if (e instanceof ValueAssigned) {\r
+ ValueAssigned va = (ValueAssigned) e;\r
+ if (va.newValue == null) throw new AccessorException("String value expected, got null");\r
+ setValue(va.newValue.getBinding(), va.newValue.getValue());\r
+ return rollback;\r
+ } else {\r
+ throw new AccessorException("Cannot apply "+e.getClass().getName()+" to String"); \r
+ }
+ }
+
+}
+
+