From 971ec96226cf7a146ff484cb58f21df6611269d0 Mon Sep 17 00:00:00 2001 From: Jan Eglinger Date: Sat, 19 Dec 2020 14:15:53 +0100 Subject: [PATCH] Improve error message for missing required inputs Human-friendly names can be registered at runtime by calling ObjectService#addHumanFriendlyName(Class, String). --- .../scijava/object/DefaultObjectService.java | 17 +++++++++++++++++ .../java/org/scijava/object/ObjectService.java | 4 ++++ .../scijava/widget/AbstractInputHarvester.java | 4 ++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/scijava/object/DefaultObjectService.java b/src/main/java/org/scijava/object/DefaultObjectService.java index c72dcf434..04caf2be9 100644 --- a/src/main/java/org/scijava/object/DefaultObjectService.java +++ b/src/main/java/org/scijava/object/DefaultObjectService.java @@ -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; @@ -56,6 +59,7 @@ public final class DefaultObjectService extends AbstractService implements ObjectService { + private static String DEFAULT_OBJECT_NAME = "an object"; @Parameter private EventService eventService; @@ -63,6 +67,9 @@ public final class DefaultObjectService extends AbstractService implements /** Index of registered objects. */ private NamedObjectIndex objectIndex; + /** Map of human-friendly names */ + private Map, String> aliasMap = new HashMap<>(); + // -- ObjectService methods -- @Override @@ -75,6 +82,16 @@ public NamedObjectIndex 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 diff --git a/src/main/java/org/scijava/object/ObjectService.java b/src/main/java/org/scijava/object/ObjectService.java index e68eaf1c4..5922d2e98 100644 --- a/src/main/java/org/scijava/object/ObjectService.java +++ b/src/main/java/org/scijava/object/ObjectService.java @@ -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. */ diff --git a/src/main/java/org/scijava/widget/AbstractInputHarvester.java b/src/main/java/org/scijava/widget/AbstractInputHarvester.java index 582b72b77..80ca7e445 100644 --- a/src/main/java/org/scijava/widget/AbstractInputHarvester.java +++ b/src/main/java/org/scijava/widget/AbstractInputHarvester.java @@ -119,8 +119,8 @@ private WidgetModel addInput(final InputPanel 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