Skip to content

Commit

Permalink
Improve error message for missing required inputs
Browse files Browse the repository at this point in the history
Human-friendly names can be registered at runtime by calling
ObjectService#addHumanFriendlyName(Class, String).
  • Loading branch information
imagejan committed Dec 19, 2020
1 parent afe795a commit bccddf1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/main/java/org/scijava/object/DefaultObjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

package org.scijava.object;

import java.util.HashMap;
import java.util.Map;

import org.scijava.event.EventHandler;
import org.scijava.event.EventService;
import org.scijava.object.event.ObjectCreatedEvent;
Expand Down Expand Up @@ -56,13 +59,17 @@
public final class DefaultObjectService extends AbstractService implements
ObjectService
{
private static String DEFAULT_OBJECT_NAME = "an object";

@Parameter
private EventService eventService;

/** Index of registered objects. */
private NamedObjectIndex<Object> objectIndex;

/** Map of human-friendly names */
private Map<Class<?>, String> aliasMap = new HashMap<>();

// -- ObjectService methods --

@Override
Expand All @@ -75,6 +82,16 @@ public NamedObjectIndex<Object> getIndex() {
return objectIndex;
}

@Override
public String getHumanFriendlyName(Class<?> c) {
return aliasMap.getOrDefault(c, DEFAULT_OBJECT_NAME);
}

@Override
public void addHumanFriendlyName(Class<?> c, String name) {
aliasMap.put(c, name);
}

// -- Service methods --

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/scijava/object/ObjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ default void removeObject(final Object obj) {
eventService().publish(new ObjectsRemovedEvent(obj));
}

String getHumanFriendlyName(Class<?> c);

void addHumanFriendlyName(Class<?> c, String name);

// -- Deprecated methods --

/** @deprecated Use {@link #eventService()} instead. */
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/scijava/widget/AbstractInputHarvester.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ private <T> WidgetModel addInput(final InputPanel<P, W> inputPanel,
}

if (item.isRequired()) {
throw new ModuleException("A " + type.getSimpleName() +
" is required but none exist.");
throw new ModuleException("This operation requires " + objectService.getHumanFriendlyName(type)
+ " of type '" + type.getSimpleName() + "', but none was found.");
}

// item is not required; we can skip it
Expand Down

0 comments on commit bccddf1

Please sign in to comment.