Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate entries in input selectors #482

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading