+/*******************************************************************************\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.binding.impl;
+
+import java.lang.reflect.Array;\r
+import java.util.Iterator;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.adapter.Adapter;\r
+import org.simantics.databoard.adapter.AdapterConstructionException;\r
+import org.simantics.databoard.binding.ArrayBinding;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.ArrayType;\r
+\r
+/**\r
+ * Binds to Object[]\r
+ *\r
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
+ */
+public class ObjectArrayBinding extends ArrayBinding {
+
+ public ObjectArrayBinding(ArrayType type, Binding componentBinding) {\r
+ super(type, componentBinding);\r
+ if (type == null)\r
+ throw new IllegalArgumentException("null arg");\r
+ this.type = type;\r
+ }\r
+\r
+ public ObjectArrayBinding(Binding componentBinding) {\r
+ this(new ArrayType(componentBinding.type()), componentBinding);\r
+ }\r
+\r
+ @Override
+ public Object create(int length, Iterator<Object> values) {
+ Object[] array = new Object[length];
+ for (int i = 0; i < length; ++i)
+ array[i] = values.next();
+ return array;
+ }
+
+ @Override
+ public Object create() {
+ return new Object[0];
+ }
+
+ public Object create(Object[] values) {
+ return values.clone();
+ }
+\r
+ @Override\r
+ public void readFrom(Binding srcBinding, Object src, Object dst)\r
+ throws BindingException {\r
+ // Src Binding\r
+ ArrayBinding sb = (ArrayBinding) srcBinding;\r
+ // Src Component Binding\r
+ Binding scb = sb.getComponentBinding();\r
+ // Dst component binding\r
+ Binding dcb = getComponentBinding();\r
+ // Dst array\r
+ Object[] d = (Object[]) dst;\r
+ if (d.length != sb.size(src)) throw new BindingException("Object[] is length immutable");\r
+ \r
+ for (int i=0; i<d.length; i++) {\r
+ Object sc = sb.get(src, i);\r
+ Object dc = d[i];\r
+ d[i] = dcb.readFromTry(scb, sc, dc);\r
+ }\r
+ } \r
+ \r
+ @Override\r
+ public Object readFromTry(Binding srcBinding, Object src, Object dst) throws BindingException {\r
+ // Src Binding\r
+ ArrayBinding sb = (ArrayBinding) srcBinding;\r
+ // Src Component Binding\r
+ Binding scb = sb.getComponentBinding();\r
+ // Dst component binding\r
+ Binding dcb = getComponentBinding();\r
+ // Dst array\r
+ Object[] d = (Object[]) dst;\r
+ int srcSize = sb.size(src);\r
+ if (d.length != srcSize) {\r
+ Object[] oldD = (Object[]) dst;\r
+ d = new Object[ srcSize ];\r
+ int X = Math.min(oldD.length, srcSize);\r
+ for (int i=0; i<X; i++) {\r
+ Object sc = sb.get(src, i);\r
+ Object dc = d[i];\r
+ d[i] = dcb.readFromTry(scb, sc, dc);\r
+ }\r
+ if (X<srcSize) {\r
+ try {\r
+ Adapter cloner = Bindings.adapterFactory.getAdapter(scb, dcb, false, true);\r
+ for (int i=X; i<srcSize; i++) {\r
+ Object sc = sb.get(src, i);\r
+ Object dc = cloner.adapt( sc );\r
+ d[i] = dc;\r
+ }\r
+ } catch (AdaptException e) {\r
+ throw new BindingException( e );\r
+ } catch (AdapterConstructionException e) {\r
+ throw new BindingException( e );\r
+ }\r
+ }\r
+ } else {\r
+ for (int i=0; i<d.length; i++) {\r
+ Object sc = sb.get(src, i);\r
+ Object dc = d[i];\r
+ d[i] = dcb.readFromTry(scb, sc, dc); \r
+ }\r
+ }\r
+ return d;\r
+ } \r
+ \r
+ @Override
+ public void add(Object array, int index, Object element)
+ throws BindingException, IndexOutOfBoundsException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void remove(Object array, int index, int count) throws BindingException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void getAll(Object array, Object[] result) throws BindingException {
+ Object[] list = (Object[]) array;
+ System.arraycopy(list, 0, result, 0, list.length);
+ }
+
+ @Override
+ public Object get(Object array, int index) throws BindingException {
+ if (!isInstance(array))
+ throw new BindingException("Unexpected class "
+ + array.getClass().getSimpleName() + ", Object[] expected");
+ return ((Object[]) array)[index];
+ }
+
+ @Override
+ public void set(Object array, int index, Object value)
+ throws BindingException {
+ Object[] list = (Object[]) array;
+ list[index] = value;
+ }
+
+ @Override
+ public int size(Object array) throws BindingException {
+ if (!isInstance(array))
+ throw new BindingException("Unexpected class "
+ + array.getClass().getSimpleName() + ", Object[] expected");
+ return ((Object[]) array).length;
+ }
+
+ @Override
+ public boolean isInstance(Object obj) {
+ return obj instanceof Object[];
+ }
+
+ @Override
+ public boolean isImmutable() {
+ return false;
+ }\r
+\r
+ @Override\r
+ public void setSize(Object array, int newSize) throws BindingException {\r
+ int oldSize = Array.getLength(array);\r
+ if (oldSize==newSize) return;\r
+ throw new BindingException("Object[] is length immutable");\r
+ } \r
+ \r
+ @Override\r
+ public boolean isResizable() {\r
+ return false;\r
+ }\r
+
+}