]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java
Double array properties
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / DoubleArrayPropertyManipulator2.java
diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java b/org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java
new file mode 100644 (file)
index 0000000..82e9a06
--- /dev/null
@@ -0,0 +1,103 @@
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.property;
+
+public class DoubleArrayPropertyManipulator2 implements PropertyManipulator {
+       
+       ValueProvider provider;
+       Object input;
+       
+       boolean editMode;
+       double[] editValue = null;
+       
+       public DoubleArrayPropertyManipulator2(ValueProvider provider, Object input) {
+               this.provider = provider;
+               this.input = input;
+       }
+       
+       @Override
+    public int getValueCount() {
+        return getValue().length+1;
+    }
+       
+       @Override
+    public String getDescription(int i) {
+        return "Value " + i;
+    }
+       
+       private double[] getValue() {
+        try {
+            return (double[])provider.getValue(input);
+        } catch (Exception e) {
+            return new double[0];
+        }
+    }
+       
+       @Override
+       public String getValue(int i) {
+               if (editMode) {
+                   if (i < editValue.length)
+                       return Double.toString(editValue[i]);
+                       return "";
+               }
+               try {
+                   double[] val = getValue();
+                   if (val.length == i)
+                       return "New";
+            if (val.length < i)
+                return null;
+            return Double.toString(val[i]);
+               } catch (Exception e) {
+                       return null;
+               }
+       }
+       
+       @Override
+       public String setValue(String value, int i) {
+               try {
+                       double[] val = editValue;
+                       if (value.length() == 0 && i == val.length -1) {
+                           double[] newVal = new double[val.length-1];
+                System.arraycopy(val, 0, newVal, 0, val.length-1);
+                val = newVal;
+                       } else if (i < val.length)
+                           val[i] = Double.parseDouble(value);
+                       else if (i == val.length) {
+                           double[] newVal = new double[val.length+1];
+                           System.arraycopy(val, 0, newVal, 0, val.length);
+                           val = newVal;
+                       }
+                       provider.setValue(input, val);
+               } catch (Exception e) {
+                       return e.getMessage();
+               }
+               return null;
+       }
+
+       @Override
+       public boolean getEditMode() {
+               return editMode;
+       }
+       
+       @Override
+       public void setEditMode(boolean b) {
+               editMode = b;
+               if (editMode) {
+                       try {
+                               editValue = getValue();
+                       } catch (Exception e) {
+                               
+                       }
+               }
+               
+       }
+}