]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d/src/org/simantics/g3d/property/DoublePropertyManipulator.java
Eliminate rounding errors in property tabs.
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / DoublePropertyManipulator.java
index 7f120b277b24b870786f98b5a6073ffe93c6a000..46f892f3c849b3c0171981c56965f0f6d7dcf523 100644 (file)
@@ -1,69 +1,85 @@
-package org.simantics.g3d.property;\r
-\r
-import java.lang.reflect.Method;\r
-\r
-public class DoublePropertyManipulator implements PropertyManipulator {\r
-       \r
-       ValueProvider provider;\r
-       Object input;\r
-       \r
-       boolean editMode;\r
-       String editValue = null;\r
-       \r
-       public DoublePropertyManipulator(ValueProvider provider, Object input) {\r
-               this.provider = provider;\r
-               this.input = input;\r
-       }\r
-       \r
-       @Override\r
-       public int getValueCount() {\r
-               return 1;\r
-       }\r
-       \r
-       @Override\r
-       public String getDescription(int i) {\r
-               if (i == 0)\r
-                       return "Value";\r
-               return null;\r
-       }\r
-       \r
-       @Override\r
-       public String getValue(int i) {\r
-               if (editMode)\r
-                       return editValue;\r
-               try {\r
-                       return provider.getValue(input).toString();\r
-               } catch (Exception e) {\r
-                       return null;\r
-               }\r
-       }\r
-       \r
-       @Override\r
-       public String setValue(String value, int i) {\r
-               try {\r
-                       editValue = value;\r
-                       provider.setValue(input, Double.parseDouble(value));\r
-               } catch (Exception e) {\r
-                       return e.getMessage();\r
-               }\r
-               return null;\r
-       }\r
-\r
-       @Override\r
-       public boolean getEditMode() {\r
-               return editMode;\r
-       }\r
-       \r
-       @Override\r
-       public void setEditMode(boolean b) {\r
-               editMode = b;\r
-               if (editMode) {\r
-                       try {\r
-                               editValue = provider.getValue(input).toString();\r
-                       } catch (Exception e) {\r
-                               \r
-                       }\r
-               }\r
-               \r
-       }\r
-}\r
+/*******************************************************************************
+ * 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;
+
+import org.simantics.g3d.math.MathTools;
+
+public class DoublePropertyManipulator implements PropertyManipulator {
+       
+       ValueProvider provider;
+       Object input;
+       
+       boolean editMode;
+       String editValue = null;
+       
+       public DoublePropertyManipulator(ValueProvider provider, Object input) {
+               this.provider = provider;
+               this.input = input;
+       }
+       
+       @Override
+       public int getValueCount() {
+               return 1;
+       }
+       
+       @Override
+       public String getDescription(int i) {
+               if (i == 0)
+                       return "Value";
+               return null;
+       }
+       
+       @Override
+       public String getValue(int i) {
+               if (editMode)
+                       return editValue;
+               try {
+                       Object value = provider.getValue(input);
+                       if (value == null) return null;
+                       if (value instanceof Double)
+                               return Double.toString(MathTools.round((Double)value, 10));
+                       return value.toString();
+               } catch (Exception e) {
+                       return null;
+               }
+       }
+       
+       @Override
+       public String setValue(String value, int i) {
+               try {
+                       editValue = value;
+                       provider.setValue(input, Double.parseDouble(value));
+               } 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 {
+                               Object value = provider.getValue(input);
+                               editValue = value.toString();
+                       } catch (Exception e) {
+                               
+                       }
+               }
+               
+       }
+}