|
| 1 | +package net.imagej.ui.swing.widget; |
| 2 | + |
| 3 | +import org.scijava.module.MethodCallException; |
| 4 | +import org.scijava.plugin.Plugin; |
| 5 | +import org.scijava.ui.swing.widget.SwingInputWidget; |
| 6 | +import org.scijava.widget.InputWidget; |
| 7 | +import org.scijava.widget.WidgetModel; |
| 8 | + |
| 9 | +import javax.swing.*; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author Matthias Arzt |
| 13 | + */ |
| 14 | +@Plugin(type = InputWidget.class) |
| 15 | +public class SwingMutableChoicesWidget<T> extends SwingInputWidget<MutableChoices<T>> { |
| 16 | + |
| 17 | + private JComboBox<ComboBoxItem<T>> comboBox; |
| 18 | + private DefaultComboBoxModel<ComboBoxItem<T>> comboBoxModel; |
| 19 | + private MyMutableChoices<T> value; |
| 20 | + |
| 21 | + @Override |
| 22 | + protected void doRefresh() { |
| 23 | + comboBox.setSelectedItem(value.value); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public void set(final WidgetModel model) { |
| 28 | + super.set(model); |
| 29 | + initializeTableComboBox(); |
| 30 | + initializeMutableChoices(model); |
| 31 | + } |
| 32 | + |
| 33 | + private void initializeTableComboBox() { |
| 34 | + comboBoxModel = new DefaultComboBoxModel<>(); |
| 35 | + comboBox = new JComboBox<>(comboBoxModel); |
| 36 | + comboBox.addActionListener(e -> updateSelection()); |
| 37 | + getComponent().add(comboBox); |
| 38 | + } |
| 39 | + |
| 40 | + private void initializeMutableChoices(WidgetModel model) { |
| 41 | + value = new MyMutableChoices<>(this); |
| 42 | + model.setValue(value); |
| 43 | + try { |
| 44 | + model.getItem().initialize(model.getModule()); |
| 45 | + model.getPanel().refresh(); |
| 46 | + } catch (MethodCallException e) { |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private void updateSelection() { |
| 52 | + get().setValue(getValue()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean supports(final WidgetModel model) { |
| 57 | + return model.isType(MutableChoices.class); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public MutableChoices<T> getValue() { |
| 62 | + ComboBoxItem<T> selectedItem = (ComboBoxItem<T>) comboBox.getSelectedItem(); |
| 63 | + value.value = (selectedItem != null) ? selectedItem.get() : null; |
| 64 | + return value; |
| 65 | + } |
| 66 | + |
| 67 | + public void setChoices(Iterable<T> choices, MutableChoices.PrettyPrinter printer) { |
| 68 | + comboBoxModel.removeAllElements(); |
| 69 | + if(! get().getItem().isRequired()) |
| 70 | + comboBoxModel.addElement(new ComboBoxItem<T>("", null)); |
| 71 | + for(T choice : choices) |
| 72 | + comboBoxModel.addElement(new ComboBoxItem<T>(printer.toString(choice), choice)); |
| 73 | + } |
| 74 | + |
| 75 | + static private class MyMutableChoices<T> implements MutableChoices<T> { |
| 76 | + |
| 77 | + SwingMutableChoicesWidget<T> widget; |
| 78 | + private T value; |
| 79 | + private Iterable<T> choices; |
| 80 | + |
| 81 | + MyMutableChoices(SwingMutableChoicesWidget<T> widget) { |
| 82 | + this.widget = widget; |
| 83 | + } |
| 84 | + |
| 85 | + void detach() { |
| 86 | + widget = null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void setChoices(Iterable<T> choices, PrettyPrinter<T> printer) { |
| 91 | + if (widget != null) |
| 92 | + widget.setChoices(choices, printer); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public T get() { |
| 97 | + return value; |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + static private class ComboBoxItem<E> { |
| 103 | + |
| 104 | + final String label; |
| 105 | + final E value; |
| 106 | + |
| 107 | + ComboBoxItem(String label, E value) { |
| 108 | + this.label = label; |
| 109 | + this.value = value; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String toString() { return label; } |
| 114 | + |
| 115 | + public E get() { return value; } |
| 116 | + |
| 117 | + } |
| 118 | +} |
0 commit comments