forked from scijava/scijava-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultWidgetModel.java
More file actions
343 lines (290 loc) · 9.01 KB
/
DefaultWidgetModel.java
File metadata and controls
343 lines (290 loc) · 9.01 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
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2020 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.widget;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.WeakHashMap;
import org.scijava.AbstractContextual;
import org.scijava.Context;
import org.scijava.ItemVisibility;
import org.scijava.convert.ConvertService;
import org.scijava.log.LogService;
import org.scijava.module.MethodCallException;
import org.scijava.module.Module;
import org.scijava.module.ModuleItem;
import org.scijava.module.ModuleService;
import org.scijava.object.ObjectService;
import org.scijava.plugin.Parameter;
import org.scijava.thread.ThreadService;
import org.scijava.util.NumberUtils;
import org.scijava.util.Types;
/**
* The backing data model for a particular {@link InputWidget}.
*
* @author Curtis Rueden
*/
public class DefaultWidgetModel extends AbstractContextual implements WidgetModel {
private final InputPanel<?, ?> inputPanel;
private final Module module;
private final ModuleItem<?> item;
private final List<?> objectPool;
private final Map<Object, Object> convertedObjects;
@Parameter
private ThreadService threadService;
@Parameter
private ConvertService convertService;
@Parameter
private ModuleService moduleService;
@Parameter
private ObjectService objectService;
@Parameter(required = false)
private LogService log;
private boolean initialized;
public DefaultWidgetModel(final Context context, final InputPanel<?, ?> inputPanel,
final Module module, final ModuleItem<?> item, final List<?> objectPool)
{
setContext(context);
this.inputPanel = inputPanel;
this.module = module;
this.item = item;
this.objectPool = objectPool;
convertedObjects = new WeakHashMap<>();
if (item.getValue(module) == null) {
// assign the item's default value as the current value
setValue(moduleService.getDefaultValue(item));
}
}
@Override
public InputPanel<?, ?> getPanel() {
return inputPanel;
}
@Override
public Module getModule() {
return module;
}
@Override
public ModuleItem<?> getItem() {
return item;
}
@Override
public List<?> getObjectPool() {
return objectPool;
}
@Override
public String getWidgetLabel() {
// Do this dynamically. Don't cache this result.
// Some controls change their labels at runtime.
final String label = item.getLabel();
if (label != null && !label.isEmpty()) return label;
final String name = item.getName();
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
@Override
public boolean isStyle(final String style) {
final String widgetStyle = getItem().getWidgetStyle();
if (widgetStyle == null) return style == null;
for (final String s : widgetStyle.split(",")) {
if (s.equals(style)) return true;
}
return false;
}
@Override
public Object getValue() {
final Object value = item.getValue(module);
if (isMultipleChoice()) return ensureValidChoice(value);
if (getObjectPool().size() > 0) return ensureValidObject(value);
return value;
}
@Override
public void setValue(final Object value) {
final String name = item.getName();
if (Objects.equals(item.getValue(module), value)) return; // no change
// Check if a converted value is present
Object convertedInput = convertedObjects.get(value);
if (convertedInput != null &&
Objects.equals(item.getValue(module), convertedInput))
{
return; // no change
}
// Pass the value through the convertService
convertedInput = convertService.convert(value, item.getType());
// If we get a different (converted) value back, cache it weakly.
if (convertedInput != value) {
convertedObjects.put(value, convertedInput);
}
module.setInput(name, convertedInput);
if (initialized) {
threadService.queue(() -> {
callback();
inputPanel.refresh(); // must be on AWT thread?
module.preview();
});
}
}
@Override
public void callback() {
try {
item.callback(module);
}
catch (final MethodCallException exc) {
if (log != null) log.error(exc);
}
}
@Override
public Number getMin() {
final Number min = toNumber(item.getMinimumValue());
if (min != null) return min;
return NumberUtils.getMinimumNumber(item.getType());
}
@Override
public Number getMax() {
final Number max = toNumber(item.getMaximumValue());
if (max != null) return max;
return NumberUtils.getMaximumNumber(item.getType());
}
@Override
public Number getSoftMin() {
final Number softMin = toNumber(item.getSoftMinimum());
if (softMin != null) return softMin;
return getMin();
}
@Override
public Number getSoftMax() {
final Number softMax = toNumber(item.getSoftMaximum());
if (softMax != null) return softMax;
return getMax();
}
@Override
public Number getStepSize() {
final Number stepSize = toNumber(item.getStepSize());
if (stepSize != null) return stepSize;
return NumberUtils.toNumber("1", item.getType());
}
@Override
public String[] getChoices() {
final List<?> choicesList = item.getChoices();
final String[] choices = new String[choicesList.size()];
for (int i = 0; i < choices.length; i++) {
choices[i] = objectService.getName(choicesList.get(i));
}
return choices;
}
@Override
public String getText() {
final Object value = getValue();
if (value == null) return "";
final String text = value.toString();
if (text.equals("\0")) return ""; // render null character as empty
return text;
}
@Override
public boolean isMessage() {
return getItem().getVisibility() == ItemVisibility.MESSAGE;
}
@Override
public boolean isText() {
return Types.isText(getItem().getType());
}
@Override
public boolean isCharacter() {
return Types.isCharacter(getItem().getType());
}
@Override
public boolean isNumber() {
return Types.isNumber(getItem().getType());
}
@Override
public String getFormat() {
return item.getFormat();
}
@Override
public boolean isBoolean() {
return Types.isBoolean(getItem().getType());
}
@Override
public boolean isMultipleChoice() {
final List<?> choices = item.getChoices();
return choices != null && !choices.isEmpty();
}
@Override
public boolean isType(final Class<?> type) {
return type.isAssignableFrom(getItem().getType());
}
@Override
public void setInitialized(final boolean initialized) {
this.initialized = initialized;
}
@Override
public boolean isInitialized() {
return initialized;
}
// -- Helper methods --
/**
* For multiple choice widgets, ensures the value is a valid choice.
*
* @see #getChoices()
* @see ChoiceWidget
*/
private Object ensureValidChoice(final Object value) {
return ensureValid(value, Arrays.asList(getChoices()));
}
/**
* For object widgets, ensures the value is a valid object.
*
* @see #getObjectPool()
* @see ObjectWidget
*/
private Object ensureValidObject(final Object value) {
return ensureValid(value, getObjectPool());
}
/** Ensures the value is on the given list. */
private Object ensureValid(final Object value, final List<?> list) {
for (final Object o : list) {
if (o.equals(value)) return value; // value is valid
// check if value was converted and cached
final Object convertedValue = convertedObjects.get(o);
if (convertedValue != null && value.equals(convertedValue)) {
return convertedValue;
}
}
// value is not valid; override with the first item on the list instead
final Object validValue = list.get(0);
// CTR TODO: Mutating the model in a getter is dirty. Find a better way?
setValue(validValue);
return validValue;
}
/** Converts the given object to a number matching the input type. */
private Number toNumber(final Object value) {
final Class<?> type = item.getType();
final Class<?> saneType = Types.box(type);
return NumberUtils.toNumber(value, saneType);
}
}