]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java
Eliminate rounding errors in property tabs.
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / DoubleArrayPropertyManipulator2.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.property;
13
14 import org.simantics.g3d.math.MathTools;
15
16 public class DoubleArrayPropertyManipulator2 implements PropertyManipulator {
17         
18         ValueProvider provider;
19         Object input;
20         
21         boolean editMode;
22         double[] editValue = null;
23         
24         public DoubleArrayPropertyManipulator2(ValueProvider provider, Object input) {
25                 this.provider = provider;
26                 this.input = input;
27         }
28         
29         @Override
30     public int getValueCount() {
31         return getValue().length+1;
32     }
33         
34         @Override
35     public String getDescription(int i) {
36         return "Value " + i;
37     }
38         
39         private double[] getValue() {
40         try {
41             return (double[])provider.getValue(input);
42         } catch (Exception e) {
43             return new double[0];
44         }
45     }
46         
47         @Override
48         public String getValue(int i) {
49                 if (editMode) {
50                     if (i < editValue.length)
51                         return Double.toString(editValue[i]);
52                         return "";
53                 }
54                 try {
55                     double[] val = getValue();
56                     if (val == null)
57                         return null;
58                     if (val.length == i)
59                         return "New";
60             if (val.length < i)
61                 return null;
62             return Double.toString(MathTools.round(val[i], 10));
63                 } catch (Exception e) {
64                         return null;
65                 }
66         }
67         
68         @Override
69         public String setValue(String value, int i) {
70                 try {
71                         double[] val = editValue;
72                         if (value.length() == 0 && i == val.length -1) {
73                             double[] newVal = new double[val.length-1];
74                 System.arraycopy(val, 0, newVal, 0, val.length-1);
75                 val = newVal;
76                         } else if (i < val.length)
77                             val[i] = Double.parseDouble(value);
78                         else if (i == val.length) {
79                             double[] newVal = new double[val.length+1];
80                             System.arraycopy(val, 0, newVal, 0, val.length);
81                             val = newVal;
82                         }
83                         provider.setValue(input, val);
84                 } catch (Exception e) {
85                         return e.getMessage();
86                 }
87                 return null;
88         }
89
90         @Override
91         public boolean getEditMode() {
92                 return editMode;
93         }
94         
95         @Override
96         public void setEditMode(boolean b) {
97                 editMode = b;
98                 if (editMode) {
99                         try {
100                                 editValue = getValue();
101                         } catch (Exception e) {
102                                 
103                         }
104                 }
105                 
106         }
107 }