]> gerrit.simantics Code Review - simantics/3d.git/blob - 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
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 DoublePropertyManipulator implements PropertyManipulator {
17         
18         ValueProvider provider;
19         Object input;
20         
21         boolean editMode;
22         String editValue = null;
23         
24         public DoublePropertyManipulator(ValueProvider provider, Object input) {
25                 this.provider = provider;
26                 this.input = input;
27         }
28         
29         @Override
30         public int getValueCount() {
31                 return 1;
32         }
33         
34         @Override
35         public String getDescription(int i) {
36                 if (i == 0)
37                         return "Value";
38                 return null;
39         }
40         
41         @Override
42         public String getValue(int i) {
43                 if (editMode)
44                         return editValue;
45                 try {
46                         Object value = provider.getValue(input);
47                         if (value == null) return null;
48                         if (value instanceof Double)
49                                 return Double.toString(MathTools.round((Double)value, 10));
50                         return value.toString();
51                 } catch (Exception e) {
52                         return null;
53                 }
54         }
55         
56         @Override
57         public String setValue(String value, int i) {
58                 try {
59                         editValue = value;
60                         provider.setValue(input, Double.parseDouble(value));
61                 } catch (Exception e) {
62                         return e.getMessage();
63                 }
64                 return null;
65         }
66
67         @Override
68         public boolean getEditMode() {
69                 return editMode;
70         }
71         
72         @Override
73         public void setEditMode(boolean b) {
74                 editMode = b;
75                 if (editMode) {
76                         try {
77                                 Object value = provider.getValue(input);
78                                 editValue = value.toString();
79                         } catch (Exception e) {
80                                 
81                         }
82                 }
83                 
84         }
85 }