Skip to content

Commit 304178c

Browse files
authored
Merge pull request #72 from NicoKiaru/master
Fixes interactive command NPEs raised in SwingObjectWidget
2 parents 9bf5def + 87fe1e4 commit 304178c

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

src/main/java/org/scijava/ui/swing/widget/SwingObjectWidget.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public void set(final WidgetModel model) {
9696

9797
@Override
9898
public boolean supports(final WidgetModel model) {
99-
return super.supports(model) && (model.getChoices() != null ||
100-
(model.getObjectPool() != null && model.getObjectPool().size() > 0));
99+
return super.supports(model) && ((model.getChoices() != null && model.getChoices().length>0) ||
100+
((model.getObjectPool() != null) && (model.getObjectPool().size() > 0)));
101101
}
102102

103103
// -- AbstractUIInputWidget methods ---
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.scijava.ui.swing.command;
2+
3+
import org.scijava.Context;
4+
import org.scijava.command.Command;
5+
import org.scijava.command.CommandService;
6+
import org.scijava.command.InteractiveCommand;
7+
import org.scijava.plugin.Parameter;
8+
import org.scijava.plugin.Plugin;
9+
import org.scijava.ui.UIService;
10+
11+
@Plugin(type = Command.class, menuPath = "Test>Test Interactive Command")
12+
public class InteractiveCommandDemo extends InteractiveCommand {
13+
14+
@Parameter
15+
String a_string;
16+
17+
@Parameter(choices={"A", "B", "C"})
18+
String another_string;
19+
20+
@Override
21+
public void run() {
22+
// nothing
23+
}
24+
25+
public static void main(String... args) throws Exception {
26+
27+
Context context = new Context();
28+
context.service(UIService.class).showUI();
29+
context.service(CommandService.class).run(InteractiveCommandDemo.class, true);
30+
31+
}
32+
}

src/test/java/org/scijava/ui/swing/widget/SwingObjectWidgetTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.scijava.Context;
4040
import org.scijava.command.CommandInfo;
4141
import org.scijava.command.ContextCommand;
42+
import org.scijava.command.InteractiveCommand;
4243
import org.scijava.module.Module;
4344
import org.scijava.module.ModuleItem;
4445
import org.scijava.module.ModuleService;
@@ -67,7 +68,7 @@ public void tearDown() {
6768
}
6869

6970
@Test
70-
public void testObjects() {
71+
public void testObjectsCommand() {
7172
Thing a = new Thing();
7273
Thing b = new Thing();
7374

@@ -93,6 +94,33 @@ public void testObjects() {
9394
assertTrue(choicesWidget instanceof SwingObjectWidget);
9495
}
9596

97+
@Test
98+
public void testObjectsInteractiveCommand() {
99+
Thing a = new Thing();
100+
Thing b = new Thing();
101+
102+
CommandInfo commandInfo = new CommandInfo(MyInteractiveCommand.class);
103+
Module module = moduleService.createModule(commandInfo);
104+
InputPanel<?,?> panel = new SwingInputPanel();
105+
106+
ModuleItem<Thing> thingInput = moduleService.getSingleInput(module, Thing.class);
107+
ModuleItem<Nothing> nothingInput = moduleService.getSingleInput(module, Nothing.class);
108+
ModuleItem<Choices> choicesInput = moduleService.getSingleInput(module, Choices.class);
109+
110+
WidgetModel thingModel = widgetService.createModel(panel, module, thingInput, Arrays.asList(a, b));
111+
WidgetModel nothingModel = widgetService.createModel(panel, module, nothingInput, null);
112+
WidgetModel choicesModel = widgetService.createModel(panel, module, choicesInput, null);
113+
114+
InputWidget<?, ?> thingWidget = widgetService.create(thingModel);
115+
assertTrue(thingWidget instanceof SwingObjectWidget);
116+
117+
InputWidget<?, ?> nothingWidget = widgetService.create(nothingModel);
118+
assertFalse(nothingWidget instanceof SwingObjectWidget);
119+
120+
InputWidget<?, ?> choicesWidget = widgetService.create(choicesModel);
121+
assertTrue(choicesWidget instanceof SwingObjectWidget);
122+
}
123+
96124
private class Thing {
97125
// dummy class
98126
}
@@ -121,4 +149,22 @@ public void run() {
121149
}
122150

123151
}
152+
153+
public static class MyInteractiveCommand extends InteractiveCommand {
154+
@Parameter
155+
private Thing thing;
156+
157+
@Parameter
158+
private Nothing nothing;
159+
160+
@Parameter
161+
private Choices choices;
162+
163+
@Override
164+
public void run() {
165+
// nothing to do
166+
}
167+
168+
}
169+
124170
}

0 commit comments

Comments
 (0)