3333
3434import java .util .Arrays ;
3535import java .util .List ;
36+ import java .util .Map ;
37+ import java .util .WeakHashMap ;
3638
3739import org .scijava .AbstractContextual ;
3840import org .scijava .Context ;
3941import org .scijava .ItemVisibility ;
42+ import org .scijava .convert .ConvertService ;
4043import org .scijava .log .LogService ;
4144import org .scijava .module .MethodCallException ;
4245import org .scijava .module .Module ;
@@ -59,10 +62,14 @@ public class WidgetModel extends AbstractContextual {
5962 private final Module module ;
6063 private final ModuleItem <?> item ;
6164 private final List <?> objectPool ;
65+ private final Map <Object , Object > convertedObjectPool ;
6266
6367 @ Parameter
6468 private ThreadService threadService ;
6569
70+ @ Parameter
71+ private ConvertService convertService ;
72+
6673 @ Parameter (required = false )
6774 private LogService log ;
6875
@@ -76,6 +83,7 @@ public WidgetModel(final Context context, final InputPanel<?, ?> inputPanel,
7683 this .module = module ;
7784 this .item = item ;
7885 this .objectPool = objectPool ;
86+ convertedObjectPool = new WeakHashMap <Object , Object >();
7987 }
8088
8189 /** Gets the input panel intended to house the widget. */
@@ -159,7 +167,25 @@ public Object getValue() {
159167 public void setValue (final Object value ) {
160168 final String name = item .getName ();
161169 if (MiscUtils .equal (item .getValue (module ), value )) return ; // no change
162- module .setInput (name , value );
170+
171+ // Check if a converted value is present
172+ Object convertedInput = convertedObjectPool .get (value );
173+ if (convertedInput != null &&
174+ MiscUtils .equal (item .getValue (module ), convertedInput ))
175+ {
176+ return ; // no change
177+ }
178+
179+ // Pass the vale through the convertService
180+ convertedInput = convertService .convert (value , item .getType ());
181+
182+ // If we get a different (covnerted) value back, cache it weakly.
183+ if (convertedInput != value ) {
184+ convertedObjectPool .put (value , convertedInput );
185+ }
186+
187+ module .setInput (name , convertedInput );
188+
163189 if (initialized ) {
164190 threadService .run (new Runnable () {
165191
@@ -363,6 +389,11 @@ private Object ensureValidObject(final Object value) {
363389 private Object ensureValid (final Object value , final List <?> list ) {
364390 for (final Object o : list ) {
365391 if (o .equals (value )) return value ; // value is valid
392+ // check if value was converted and cached
393+ final Object convertedValue = convertedObjectPool .get (o );
394+ if (convertedValue != null && value .equals (convertedValue )) {
395+ return convertedValue ;
396+ }
366397 }
367398
368399 // value is not valid; override with the first item on the list instead
0 commit comments