]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator.java
a9971605af500f1433678bc20701f4b232c76734
[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 public class DoubleArrayPropertyManipulator implements PropertyManipulator {
17         
18         ValueProvider provider;
19         Object input;
20         
21         boolean editMode;
22         double[] editValue = null;
23         
24         public DoubleArrayPropertyManipulator(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         return "Value";
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                     return Arrays.toString(editValue);
51                 }
52                 try {
53                     double[] val = getValue();
54                         if (val == null) return null;
55                     return Arrays.toString(val);
56                 } catch (Exception e) {
57                         return null;
58                 }
59         }
60         
61         @Override
62         public String setValue(String value, int i) {
63                 try {
64                     value = value.trim();
65                         if (value.startsWith("[") && value.endsWith("]")) {
66                             value = value.substring(1,value.length()-1);
67                             String vals[] = value.split(",");
68                             double val[] = new double[vals.length];
69                             for (int j = 0; j < vals.length; j++) {
70                                 val[j] = Double.parseDouble(vals[j]);
71                             }
72                             provider.setValue(input, val);    
73                         } else {
74                             return "Input must be values between [] characters separated with ,";
75                         }
76                         
77                 } catch (Exception e) {
78                         return e.getMessage();
79                 }
80                 return null;
81         }
82
83         @Override
84         public boolean getEditMode() {
85                 return editMode;
86         }
87         
88         @Override
89         public void setEditMode(boolean b) {
90                 editMode = b;
91                 if (editMode) {
92                         try {
93                                 editValue = getValue();
94                         } catch (Exception e) {
95                                 
96                         }
97                 }
98                 
99         }
100 }