|
| 1 | +package org.scijava.ui.swing.widget; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import javax.swing.JComboBox; |
| 10 | + |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.scijava.Context; |
| 15 | +import org.scijava.Initializable; |
| 16 | +import org.scijava.command.Command; |
| 17 | +import org.scijava.command.CommandInfo; |
| 18 | +import org.scijava.command.CommandModuleItem; |
| 19 | +import org.scijava.command.CommandService; |
| 20 | +import org.scijava.command.DynamicCommand; |
| 21 | +import org.scijava.command.DynamicCommandInfo; |
| 22 | +import org.scijava.log.LogService; |
| 23 | +import org.scijava.module.Module; |
| 24 | +import org.scijava.module.ModuleService; |
| 25 | +import org.scijava.module.MutableModule; |
| 26 | +import org.scijava.module.MutableModuleItem; |
| 27 | +import org.scijava.plugin.Parameter; |
| 28 | +import org.scijava.plugin.Plugin; |
| 29 | +import org.scijava.widget.InputWidget; |
| 30 | +import org.scijava.widget.WidgetModel; |
| 31 | +import org.scijava.widget.WidgetService; |
| 32 | + |
| 33 | +public class SwingChoiceWidgetTest { |
| 34 | + |
| 35 | + private Context context; |
| 36 | + private CommandService commandService; |
| 37 | + private ModuleService moduleService; |
| 38 | + private WidgetService widgetService; |
| 39 | + |
| 40 | + private static String[] INITIAL_THING_CHOICES = { "a", "b", "c" }; |
| 41 | + private static String[] ANIMAL_CHOICES = { "Lion", "Tiger", "Bear" }; |
| 42 | + private static String[] VEGETABLE_CHOICES = { "Sage", "Rosemary", "Thyme" }; |
| 43 | + private static String[] MINERAL_CHOICES = { "Diamond", "Emerald", "Ruby" }; |
| 44 | + private static String ANIMAL = "Animal"; |
| 45 | + private static String VEGETABLE = "Vegetable"; |
| 46 | + private static String MINERAL = "Mineral"; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setUp() { |
| 50 | + context = new Context(); |
| 51 | + commandService = context.service(CommandService.class); |
| 52 | + moduleService = context.service(ModuleService.class); |
| 53 | + widgetService = context.service(WidgetService.class); |
| 54 | + } |
| 55 | + |
| 56 | + @After |
| 57 | + public void tearDown() { |
| 58 | + context.dispose(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testDynamicCallbacks() { |
| 63 | + CommandInfo info = commandService.getCommand(DynamicCallbacks.class); |
| 64 | + DynamicCommandInfo moduleInfo = new DynamicCommandInfo(info, DynamicCallbacks.class); |
| 65 | + MutableModule module = (MutableModule) moduleService.createModule(moduleInfo); |
| 66 | + MutableModuleItem<String> kindOfThingItem = moduleInfo.getMutableInput("kindOfThing", String.class); |
| 67 | + MutableModuleItem<String> thingItem = moduleInfo.getMutableInput("thing", String.class); |
| 68 | + |
| 69 | + SwingInputPanel panel = new SwingInputPanel(); |
| 70 | + WidgetModel thingWidgetModel = widgetService.createModel(panel, module, thingItem, null); |
| 71 | + SwingInputWidget<String> thingWidget = (SwingInputWidget<String>) widgetService.create(thingWidgetModel); |
| 72 | + WidgetModel kindOfThingWidgetModel = widgetService.createModel(panel, module, kindOfThingItem, null); |
| 73 | + SwingInputWidget<String> kindOfThingWidget = (SwingInputWidget<String>) widgetService.create(kindOfThingWidgetModel); |
| 74 | + panel.refresh(); |
| 75 | + |
| 76 | + assertArrayEquals(INITIAL_THING_CHOICES, thingWidgetModel.getChoices()); |
| 77 | + |
| 78 | + //JComboBox<String> comboBox = (JComboBox<String>) kindOfThingWidget.getComponent().getComponents()[0]; |
| 79 | + //comboBox.setSelectedIndex(1); |
| 80 | + kindOfThingWidgetModel.setValue(ANIMAL); |
| 81 | + panel.refresh(); |
| 82 | + //kindOfThingWidget.updateModel(); |
| 83 | + kindOfThingWidgetModel.callback(); |
| 84 | + panel.refresh(); |
| 85 | + thingWidget.updateModel(); |
| 86 | + panel.refresh(); |
| 87 | + thingWidget.refreshWidget(); |
| 88 | + panel.refresh(); |
| 89 | + System.err.println("First choice now: " + thingItem.getChoices().get(0)); |
| 90 | + System.err.println("First choice now: " + thingWidget.get().getChoices()[0]); |
| 91 | + System.err.println(kindOfThingWidget.getComponent().getComponents()[0]); |
| 92 | + System.err.println(thingWidget.getComponent().getComponents()[0]); |
| 93 | + //thingWidget.refreshWidget(); |
| 94 | + |
| 95 | + assertArrayEquals(ANIMAL_CHOICES, thingWidgetModel.getChoices()); |
| 96 | + } |
| 97 | + |
| 98 | + @Plugin(type = Command.class) |
| 99 | + public static class DynamicCallbacks extends DynamicCommand { |
| 100 | + |
| 101 | + @Parameter(callback = "kindOfThingChanged", // |
| 102 | + choices = { "Animal", "Vegetable", "Mineral" }) |
| 103 | + private String kindOfThing = "Animal"; |
| 104 | + |
| 105 | + @Parameter(choices = { "a", "b", "c" }) |
| 106 | + private String thing = "a"; |
| 107 | + |
| 108 | + @SuppressWarnings("unused") |
| 109 | + private void kindOfThingChanged() { |
| 110 | + context().service(LogService.class).error("callback called with value " + kindOfThing); |
| 111 | + final MutableModuleItem<String> thingItem = // |
| 112 | + getInfo().getMutableInput("thing", String.class); |
| 113 | + switch (kindOfThing) { |
| 114 | + case "Animal": |
| 115 | + thingItem.setChoices(Arrays.asList("Lion", "Tiger", "Bear")); |
| 116 | + break; |
| 117 | + case "Vegetable": |
| 118 | + thingItem.setChoices(Arrays.asList("Sage", "Rosemary", "Thyme")); |
| 119 | + break; |
| 120 | + case "Mineral": |
| 121 | + thingItem.setChoices(Arrays.asList("Diamond", "Emerald", "Ruby")); |
| 122 | + break; |
| 123 | + default: |
| 124 | + thingItem.setChoices(Arrays.asList("???", "WAT", "OHNOEZ")); |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void dummy() { |
| 131 | + List<Double> aList = new ArrayList<>(); |
| 132 | + aList.add(4.5); |
| 133 | + try { |
| 134 | + List nList = aList.getClass().newInstance(); |
| 135 | + } catch (InstantiationException e) { |
| 136 | + // TODO Auto-generated catch block |
| 137 | + e.printStackTrace(); |
| 138 | + } catch (IllegalAccessException e) { |
| 139 | + // TODO Auto-generated catch block |
| 140 | + e.printStackTrace(); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | +} |
0 commit comments