Skip to content

Commit

Permalink
Merge branch 'enum-choices'
Browse files Browse the repository at this point in the history
This is work I started doing years ago to clean up logic related to
multiple-choice parameters, to make it more compatible with enums.
  • Loading branch information
ctrueden committed Oct 12, 2023
2 parents fa5133b + fcffe20 commit 664b3ab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
9 changes: 3 additions & 6 deletions src/main/java/org/scijava/widget/AbstractInputHarvester.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,10 @@ private <T> WidgetModel addInput(final InputPanel<P, W> inputPanel,
}

/** Asks the object service and convert service for valid choices */
@SuppressWarnings("unchecked")
private List<?> getObjects(final Class<?> type) {
@SuppressWarnings("rawtypes")
Set compatibleInputs =
new HashSet(convertService.getCompatibleInputs(type));
private List<Object> getObjects(final Class<?> type) {
Set<Object> compatibleInputs =
new HashSet<>(convertService.getCompatibleInputs(type));
compatibleInputs.addAll(objectService.getObjects(type));
return new ArrayList<>(compatibleInputs);
}

}
16 changes: 2 additions & 14 deletions src/main/java/org/scijava/widget/DefaultWidgetModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

package org.scijava.widget;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -156,6 +155,7 @@ public void setValue(final Object value) {

// Pass the value through the convertService
convertedInput = convertService.convert(value, item.getType());
if (convertedInput == null) convertedInput = value;

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

@Override
public String[] getChoices() {
final List<?> choicesList = item.getChoices();
if (choicesList == null) return null;
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();
Expand Down Expand Up @@ -289,11 +278,10 @@ public boolean isInitialized() {
/**
* 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()));
return ensureValid(value, getItem().getChoices());
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/scijava/widget/WidgetModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ public interface WidgetModel extends Contextual {
*/
Number getStepSize();

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

/**
* Gets the input's value rendered as a string.
Expand Down

0 comments on commit 664b3ab

Please sign in to comment.