]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator.java
Eliminate rounding errors in property tabs.
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / DoubleArrayPropertyManipulator.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 java.util.Arrays;
15
16 import org.simantics.g3d.math.MathTools;
17
18 public class DoubleArrayPropertyManipulator implements PropertyManipulator {
19         
20         ValueProvider provider;
21         Object input;
22         
23         boolean editMode;
24         double[] editValue = null;
25         
26         public DoubleArrayPropertyManipulator(ValueProvider provider, Object input) {
27                 this.provider = provider;
28                 this.input = input;
29         }
30         
31         @Override
32     public int getValueCount() {
33         return 1;
34     }
35         
36         @Override
37     public String getDescription(int i) {
38         return "Value";
39     }
40         
41         private double[] getValue() {
42         try {
43             return (double[])provider.getValue(input);
44         } catch (Exception e) {
45             return new double[0];
46         }
47     }
48         
49         @Override
50         public String getValue(int i) {
51                 if (editMode) {
52                     return Arrays.toString(editValue);
53                 }
54                 try {
55                     double[] val = getValue();
56                         if (val == null) return null;
57                     return Double.toString(MathTools.round(val[i], 10));
58                 } catch (Exception e) {
59                         return null;
60                 }
61         }
62         
63         @Override
64         public String setValue(String value, int i) {
65                 try {
66                     value = value.trim();
67                         if (value.startsWith("[") && value.endsWith("]")) {
68                             value = value.substring(1,value.length()-1);
69                             String vals[] = value.split(",");
70                             double val[] = new double[vals.length];
71                             for (int j = 0; j < vals.length; j++) {
72                                 val[j] = Double.parseDouble(vals[j]);
73                             }
74                             provider.setValue(input, val);    
75                         } else {
76                             return "Input must be values between [] characters separated with ,";
77                         }
78                         
79                 } catch (Exception e) {
80                         return e.getMessage();
81                 }
82                 return null;
83         }
84
85         @Override
86         public boolean getEditMode() {
87                 return editMode;
88         }
89         
90         @Override
91         public void setEditMode(boolean b) {
92                 editMode = b;
93                 if (editMode) {
94                         try {
95                                 editValue = getValue();
96                         } catch (Exception e) {
97                                 
98                         }
99                 }
100                 
101         }
102 }