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