-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathOSPControlTable.java
More file actions
426 lines (394 loc) · 13.7 KB
/
OSPControlTable.java
File metadata and controls
426 lines (394 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.controls;
import java.awt.Color;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import org.opensourcephysics.numerics.DoubleArray;
import org.opensourcephysics.numerics.IntegerArray;
/**
*
* OSPControlTable automaitally converts strings, such "pi" or "sqrt(2)" to numbers when
* getDouble and getInt are invoked.
*
* @author W. Christian
* @version 1.1
*/
public class OSPControlTable extends XMLTable implements Control {
static Color ERROR_COLOR = Color.PINK;
private HashMap<String, Double> valueCache = new HashMap<String, Double>();
private boolean lockValues = false;
private DecimalFormat format;
/**
* Constructs OSPControlTable and creates an XMLControlElement.
*/
public OSPControlTable() {
this(new XMLControlElement());
}
/**
* Constructs OSPControlTable with the given control.
* @param control XMLControlElement
*/
public OSPControlTable(XMLControlElement control) {
super(control);
}
/**
* Locks the control's interface. Values sent to the control will not
* update the display until the control is unlocked.
*
* @param lock boolean
*/
public void setLockValues(boolean lock) {
tableModel.control.setLockValues(lock);
lockValues = lock;
if(!lockValues) {
refresh();
}
}
/**
* Adds a parameter to the input display.
*
* @param par the parameter name
* @param val the initial parameter value
*/
public void setValue(String par, Object val) {
if(getBackgroundColor(par)==ERROR_COLOR) {
setBackgroundColor(par, Color.WHITE);
}
tableModel.control.setValue(par, val);
if(!lockValues) {
refresh();
}
}
/**
* Sets the format pattern used for floating point numbers.
* @param pattern String
*/
public void setDecimalFormat(String pattern) {
if(pattern==null) {
format = null;
} else {
format = new DecimalFormat(pattern);
}
}
/**
* Adds an initial value of a parameter to the input display.
*
* @param par the parameter name
* @param val the initial parameter value
*/
public void setValue(String par, double val) {
if(format==null) {
setValue(par, Double.toString(val));
} else {
setValue(par, format.format(val));
}
if(!Double.isNaN(val)) {
valueCache.put(par, new Double(val)); // store last good value
}
}
/**
* Adds an initial value of a parameter to the input display.
*
* @param par the parameter name
* @param val the initial parameter value
*/
public void setValue(String par, int val) {
setValue(par, Integer.toString(val));
valueCache.put(par, new Double(val)); // store last good value
}
public void setValue(String par, boolean val) {
if(getBackgroundColor(par)==ERROR_COLOR) {
setBackgroundColor(par, Color.WHITE);
}
tableModel.control.setValue(par, val);
}
/**
* Reads a parameter value from the input display.
*
* @param par
* @return int the value of of the parameter
*/
public int getInt(String par) {
String str = tableModel.control.getString(par);
if(str==null) {
str = getObject(par).toString();
}
// special handling for OSPCombo
if(tableModel.control.getPropertyType(par).equals("object")) { //$NON-NLS-1$
XMLControl c = tableModel.control.getChildControl(par);
if(c.getObjectClass()==OSPCombo.class) {
OSPCombo combo = (OSPCombo) c.loadObject(null);
return combo.getSelectedIndex();
}
}
if(str==null) {
setBackgroundColor(par, ERROR_COLOR);
refresh();
if(valueCache.containsKey(par)) {
return(int) valueCache.get(par).doubleValue();
}
return 0;
}
Color color = cellColors.get(par);
boolean editable = isEditable(par);
try {
int val = Integer.parseInt(par);
if(editable&&(color!=Color.WHITE)) { // background is not correct so change it
setBackgroundColor(par, Color.WHITE);
refresh();
} else if(!editable&&(color!=Control.NOT_EDITABLE_BACKGROUND)) { // background is not correct so change it
setBackgroundColor(par, Control.NOT_EDITABLE_BACKGROUND);
refresh();
}
valueCache.put(par, new Double(val));
return val;
} catch(NumberFormatException ex) {}
try {
int val = (int) Double.parseDouble(par);
if(editable&&(color!=Color.WHITE)) { // background is not correct so change it
setBackgroundColor(par, Color.WHITE);
refresh();
} else if(!editable&&(color!=Control.NOT_EDITABLE_BACKGROUND)) { // background is not correct so change it
setBackgroundColor(par, Control.NOT_EDITABLE_BACKGROUND);
refresh();
}
valueCache.put(par, new Double(val));
return val;
} catch(NumberFormatException ex) {}
double dval = org.opensourcephysics.numerics.Util.evalMath(str);
if(Double.isNaN(dval)&&(color!=ERROR_COLOR)) {
setBackgroundColor(par, ERROR_COLOR);
refresh();
if(valueCache.containsKey(par)) {
return(int) valueCache.get(par).doubleValue();
}
return 0;
}
if(editable&&(color!=Color.WHITE)) { // background is not correct so change it
setBackgroundColor(par, Color.WHITE);
refresh();
} else if(!editable&&(color!=Control.NOT_EDITABLE_BACKGROUND)) { // background is not correct so change it
setBackgroundColor(par, Control.NOT_EDITABLE_BACKGROUND);
refresh();
}
valueCache.put(par, new Double(dval));
return(int) dval;
}
/**
* Test if the last "get" method produced an input error.
*
* @param par String
* @return boolean
*/
public boolean inputError(String par) {
return getBackgroundColor(par)==ERROR_COLOR;
}
/**
* Reads a double value from the table.
*
* @param par String the parameter key
* @return double the value of of the parameter
*/
public double getDouble(String par) {
String str = tableModel.control.getString(par);
if(str==null) {
str = getObject(par).toString();
}
if(str==null) {
setBackgroundColor(par, ERROR_COLOR);
refresh();
if(valueCache.containsKey(par)) {
return valueCache.get(par).doubleValue();
}
return 0;
}
Color color = cellColors.get(par);
boolean editable = isEditable(par);
try {
double val = Double.parseDouble(str);
if(editable&&(color!=Color.WHITE)) { // background is not correct so change it
setBackgroundColor(par, Color.WHITE);
refresh();
} else if(!editable&&(color!=Control.NOT_EDITABLE_BACKGROUND)) { // background is not correct so change it
setBackgroundColor(par, Control.NOT_EDITABLE_BACKGROUND);
refresh();
}
valueCache.put(par, new Double(val));
return val;
} catch(NumberFormatException ex) {}
double val = org.opensourcephysics.numerics.Util.evalMath(str);
if(Double.isNaN(val)&&(color!=ERROR_COLOR)) { // string is not a valid number
setBackgroundColor(par, ERROR_COLOR);
refresh();
} else if(editable&&(color!=Color.WHITE)) { // background is not correct so change it
setBackgroundColor(par, Color.WHITE);
refresh();
} else if(!editable&&(color!=Control.NOT_EDITABLE_BACKGROUND)) { // background is not correct so change it
setBackgroundColor(par, Control.NOT_EDITABLE_BACKGROUND);
refresh();
}
if(Double.isNaN(val)&&valueCache.containsKey(par)) {
val = valueCache.get(par).doubleValue();
} else {
valueCache.put(par, new Double(val));
}
return val;
}
/**
* Gets the object with the specified property name.
* Throws an UnsupportedOperationException if the named object has not been stored.
*
* @param par
* @return the object
*/
public Object getObject(String par) throws UnsupportedOperationException {
return tableModel.control.getObject(par);
}
public String getString(String par) {
return tableModel.control.getString(par);
}
public boolean getBoolean(String par) {
return tableModel.control.getBoolean(par);
}
public Collection<String> getPropertyNames() {
return tableModel.control.getPropertyNames();
}
/**
* Removes a parameter from the table.
*
* @param par the parameter name
*/
public void removeParameter(String par) {
tableModel.control.setValue(par, null);
setBackgroundColor(par, Color.WHITE);
}
public void println(String s) {
tableModel.control.println(s);
}
public void println() {
tableModel.control.println();
}
public void print(String s) {
tableModel.control.print(s);
}
public void clearMessages() {
tableModel.control.clearMessages();
}
public void clearValues() {
tableModel.control.clearValues();
}
public void calculationDone(String message) {
if(message!=null) {
tableModel.control.calculationDone(message);
}
}
/**
* Returns an XML.ObjectLoader to save and load data for this object.
*
* @return the object loader
*/
public static XML.ObjectLoader getLoader() {
return new OSPControlTableLoader();
}
/**
* A class to save and load data for OSPControls.
*/
static class OSPControlTableLoader implements XML.ObjectLoader {
/**
* Saves object data to an XMLControl.
*
* @param prefsXMLControl the control to save to
* @param obj the object to save
*/
public void saveObject(XMLControl xmlControl, Object obj) {
OSPControlTable controlTable = (OSPControlTable) obj;
Iterator<String> it = controlTable.getPropertyNames().iterator();
while(it.hasNext()) {
String name = it.next();
Object val = controlTable.getObject(name);
if(val.getClass()==DoubleArray.class) {
xmlControl.setValue(name, ((DoubleArray) val).getArray());
} else if(val.getClass()==IntegerArray.class) {
xmlControl.setValue(name, ((IntegerArray) val).getArray());
} else if(val.getClass()==Boolean.class) {
xmlControl.setValue(name, ((Boolean) val).booleanValue());
} else if(val.getClass()==Double.class) {
xmlControl.setValue(name, ((Double) val).doubleValue());
} else if(val.getClass()==Integer.class) {
xmlControl.setValue(name, ((Integer) val).intValue());
} else if(val.getClass().isArray()) {
xmlControl.setValue(name, val);
} else {
xmlControl.setValue(name, val);
}
}
}
/**
* Creates an OSPControlTable object.
*
* @param control the control
* @return the newly created object
*/
public Object createObject(XMLControl control) {
return new OSPControlTable();
}
/**
* Loads an object with data from an XMLControl.
*
* @param control the control
* @param obj the object
* @return the loaded object
*/
public Object loadObject(XMLControl control, Object obj) {
OSPControlTable controlTable = (OSPControlTable) obj;
// iterate over properties and add them to table model
Iterator<String> it = control.getPropertyNames().iterator();
controlTable.setLockValues(true);
while(it.hasNext()) {
String name = it.next();
if(control.getPropertyType(name).equals("string")) { //$NON-NLS-1$
controlTable.setValue(name, control.getString(name));
} else if(control.getPropertyType(name).equals("int")) { //$NON-NLS-1$
controlTable.setValue(name, control.getInt(name));
} else if(control.getPropertyType(name).equals("double")) { //$NON-NLS-1$
controlTable.setValue(name, control.getDouble(name));
} else if(control.getPropertyType(name).equals("boolean")) { //$NON-NLS-1$
controlTable.setValue(name, control.getBoolean(name));
} else {
controlTable.setValue(name, control.getObject(name));
}
}
controlTable.setLockValues(false);
return obj;
}
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/