]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/VectorPropertyManipulator.java
Eliminate rounding errors in property tabs.
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / VectorPropertyManipulator.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 javax.vecmath.Vector3d;
15
16 import org.simantics.g3d.math.MathTools;
17
18 public class VectorPropertyManipulator implements PropertyManipulator {
19         
20         ValueProvider provider;
21         protected Object input;
22         
23         boolean editMode;
24         Vector3d editValue = null;
25         
26         public VectorPropertyManipulator(ValueProvider provider, Object input) {
27                 this.provider = provider;
28                 this.input = input;
29         }
30         
31         @Override
32         public int getValueCount() {
33                 return 3;
34         }
35         
36         @Override
37         public String getDescription(int i) {
38                 if (i == 0)
39                         return "X";
40                 if (i == 1)
41                         return "Y";
42                 if (i == 2)
43                         return "Z";
44                 return null;
45         }
46         
47         @Override
48         public String getValue(int i) {
49                 try {
50                         Vector3d v = _getValue();
51                         if (v == null)
52                                 return null;
53                         if (i == 0)
54                                 return Double.toString(MathTools.round(v.x, 10));
55                         if (i == 1)
56                                 return Double.toString(MathTools.round(v.y, 10));
57                         if (i == 2)
58                                 return Double.toString(MathTools.round(v.z, 10));
59                         return null;
60                 } catch (Exception e) {
61                         return null;
62                 }
63         }
64         
65         @Override
66         public String setValue(String value, int i) {
67                 try {
68                         Double d = Double.parseDouble(value);
69                         Vector3d v = _getValue();
70                         v = new Vector3d(v.x, v.y, v.z);
71                         if (i == 0)
72                                 v.x = d;
73                         if (i == 1)
74                                 v.y = d;
75                         if (i == 2)
76                                 v.z = d;
77                         editValue = v;
78                         setValue(v);
79                 } catch (Exception e) {
80                         return e.getMessage();
81                 }
82                 return null;
83         }
84         
85         protected void setValue(Vector3d v) throws Exception {
86                 provider.setValue(input, v);
87         }
88         
89         private Vector3d _getValue() throws Exception{
90                 if (editMode)
91                         return editValue;
92                 return (Vector3d) provider.getValue(input);
93         }
94         
95         @Override
96         public boolean getEditMode() {
97                 return editMode;
98         }
99         
100         @Override
101         public void setEditMode(boolean b) {
102                 editMode = b;
103                 if (editMode) {
104                         try {
105                                 editValue = (Vector3d) provider.getValue(input);
106                         } catch (Exception e) {
107                                 
108                         }
109                 }
110                 
111         }
112
113 }