|
| 1 | +package net.imagej.ui.swing.widget; |
| 2 | + |
| 3 | +import net.imagej.table.Table; |
| 4 | +import net.imagej.table.TableDisplay; |
| 5 | +import org.scijava.display.DisplayService; |
| 6 | +import org.scijava.plugin.Parameter; |
| 7 | +import org.scijava.plugin.Plugin; |
| 8 | +import org.scijava.ui.swing.widget.SwingInputWidget; |
| 9 | +import org.scijava.widget.InputWidget; |
| 10 | +import org.scijava.widget.WidgetModel; |
| 11 | + |
| 12 | +import javax.swing.*; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Vector; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Matthias Arzt |
| 18 | + */ |
| 19 | +@Plugin(type = InputWidget.class) |
| 20 | +public class SwingTableWidget extends SwingInputWidget<Table<?,?>>{ |
| 21 | + |
| 22 | + @Parameter |
| 23 | + DisplayService displayService; |
| 24 | + |
| 25 | + private JComboBox<TableItem> tableComboBox; |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void doRefresh() { |
| 29 | + get().setValue(getValue()); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void set(final WidgetModel model) { |
| 34 | + super.set(model); |
| 35 | + initializeTableComboBox(); |
| 36 | + updateSelectedTable(); |
| 37 | + } |
| 38 | + |
| 39 | + private void initializeTableComboBox() { |
| 40 | + List<TableDisplay> tableDisplays = displayService.getDisplaysOfType(TableDisplay.class); |
| 41 | + Vector<TableItem> tables = new Vector<>(); |
| 42 | + for(TableDisplay tableDisplay : tableDisplays) |
| 43 | + tables.add(new TableItem(tableDisplay)); |
| 44 | + tableComboBox = new JComboBox<>(tables); |
| 45 | + tableComboBox.addActionListener(e -> updateSelectedTable()); |
| 46 | + getComponent().add(tableComboBox); |
| 47 | + } |
| 48 | + |
| 49 | + private void updateSelectedTable() { |
| 50 | + WidgetModel model = get(); |
| 51 | + model.setValue(getValue()); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean supports(final WidgetModel model) { return model.isType(Table.class); } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Table<?, ?> getValue() { |
| 59 | + TableItem selected = (TableItem) tableComboBox.getSelectedItem(); |
| 60 | + return (selected != null) ? selected.get() : null; |
| 61 | + } |
| 62 | + |
| 63 | + private static class ComboBoxItem<E> { |
| 64 | + |
| 65 | + final String label; |
| 66 | + final E value; |
| 67 | + |
| 68 | + ComboBoxItem(String label, E value) { |
| 69 | + this.label = label; |
| 70 | + this.value = value; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String toString() { return label; } |
| 75 | + |
| 76 | + public E get() { return value; } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + private static class TableItem extends ComboBoxItem<Table<?,?>> { |
| 81 | + TableItem(TableDisplay display) { super(display.getName(), display.get(0)); } |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments