Skip to content

Commit

Permalink
Merge pull request #482 from scijava/limit-widget-inputs
Browse files Browse the repository at this point in the history
Fix duplicate entries in input selectors by a) preferring existing objects registered in ObjectService and b) de-duplicating based on toString representation
  • Loading branch information
hinerm authored Sep 13, 2024
2 parents 0d95340 + f5230f6 commit ee1e7c1
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/main/java/org/scijava/widget/AbstractInputHarvester.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
package org.scijava.widget;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.scijava.AbstractContextual;
import org.scijava.convert.ConvertService;
Expand Down Expand Up @@ -129,9 +130,24 @@ private <T> WidgetModel addInput(final InputPanel<P, W> inputPanel,

/** Asks the object service and convert service for valid choices */
private List<Object> getObjects(final Class<?> type) {
Set<Object> compatibleInputs =
new HashSet<>(convertService.getCompatibleInputs(type));
compatibleInputs.addAll(objectService.getObjects(type));
return new ArrayList<>(compatibleInputs);
// Start with the known, unconverted objects of the desired type
List<Object> objects = new ArrayList<>(objectService.getObjects(type));

// Get all the known objects that can be converted to the destination type
Collection<Object> compatibleInputs = convertService.getCompatibleInputs(type);

// HACK: Add each convertible object that doesn't share a name with any other object
// Our goal here is to de-duplicate by avoiding similar inputs that could be converted
// to the same effective output (e.g. an ImageDisplay and a Dataset that map to the same
// ImgPlus)
Set<String> knownNames = objects.stream().map(Object::toString).collect(Collectors.toSet());
for (Object o : compatibleInputs) {
final String s = o.toString();
if (!knownNames.contains(s)) {
objects.add(o);
knownNames.add(s);
}
}
return objects;
}
}

0 comments on commit ee1e7c1

Please sign in to comment.