Skip to content

Commit 664b3ab

Browse files
committed
Merge branch 'enum-choices'
This is work I started doing years ago to clean up logic related to multiple-choice parameters, to make it more compatible with enums.
2 parents fa5133b + fcffe20 commit 664b3ab

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

src/main/java/org/scijava/widget/AbstractInputHarvester.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,10 @@ private <T> WidgetModel addInput(final InputPanel<P, W> inputPanel,
128128
}
129129

130130
/** Asks the object service and convert service for valid choices */
131-
@SuppressWarnings("unchecked")
132-
private List<?> getObjects(final Class<?> type) {
133-
@SuppressWarnings("rawtypes")
134-
Set compatibleInputs =
135-
new HashSet(convertService.getCompatibleInputs(type));
131+
private List<Object> getObjects(final Class<?> type) {
132+
Set<Object> compatibleInputs =
133+
new HashSet<>(convertService.getCompatibleInputs(type));
136134
compatibleInputs.addAll(objectService.getObjects(type));
137135
return new ArrayList<>(compatibleInputs);
138136
}
139-
140137
}

src/main/java/org/scijava/widget/DefaultWidgetModel.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
package org.scijava.widget;
3131

32-
import java.util.Arrays;
3332
import java.util.List;
3433
import java.util.Map;
3534
import java.util.Objects;
@@ -156,6 +155,7 @@ public void setValue(final Object value) {
156155

157156
// Pass the value through the convertService
158157
convertedInput = convertService.convert(value, item.getType());
158+
if (convertedInput == null) convertedInput = value;
159159

160160
// If we get a different (converted) value back, cache it weakly.
161161
if (convertedInput != value) {
@@ -218,17 +218,6 @@ public Number getStepSize() {
218218
return NumberUtils.toNumber("1", item.getType());
219219
}
220220

221-
@Override
222-
public String[] getChoices() {
223-
final List<?> choicesList = item.getChoices();
224-
if (choicesList == null) return null;
225-
final String[] choices = new String[choicesList.size()];
226-
for (int i = 0; i < choices.length; i++) {
227-
choices[i] = objectService.getName(choicesList.get(i));
228-
}
229-
return choices;
230-
}
231-
232221
@Override
233222
public String getText() {
234223
final Object value = getValue();
@@ -289,11 +278,10 @@ public boolean isInitialized() {
289278
/**
290279
* For multiple choice widgets, ensures the value is a valid choice.
291280
*
292-
* @see #getChoices()
293281
* @see ChoiceWidget
294282
*/
295283
private Object ensureValidChoice(final Object value) {
296-
return ensureValid(value, Arrays.asList(getChoices()));
284+
return ensureValid(value, getItem().getChoices());
297285
}
298286

299287
/**

src/main/java/org/scijava/widget/WidgetModel.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ public interface WidgetModel extends Contextual {
134134
*/
135135
Number getStepSize();
136136

137-
/**
138-
* Gets the multiple choice list for the module input.
139-
*
140-
* @return The available choices, or an empty list if not multiple choice.
141-
* @see ChoiceWidget
142-
*/
143-
String[] getChoices();
137+
/** @deprecated Use {@code getItem().getChoices()} instead. */
138+
@Deprecated
139+
default String[] getChoices() {
140+
return getItem().getChoices().stream() //
141+
.map(choice -> choice.toString()) //
142+
.toArray(String[]::new);
143+
}
144144

145145
/**
146146
* Gets the input's value rendered as a string.

0 commit comments

Comments
 (0)