|
| 1 | +package org.vcell.N5.reduction.GUI; |
| 2 | + |
| 3 | +import ij.gui.Roi; |
| 4 | +import ij.plugin.frame.RoiManager; |
| 5 | + |
| 6 | +import javax.swing.*; |
| 7 | +import javax.swing.table.DefaultTableModel; |
| 8 | +import java.awt.event.ActionEvent; |
| 9 | +import java.awt.event.ActionListener; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | + |
| 13 | +public class AvailableROIs extends JDialog implements ActionListener { |
| 14 | + private final JButton okayButton = new JButton("Okay"); |
| 15 | + private final JButton cancelButton = new JButton("Cancel"); |
| 16 | + private final ROIDataModel roiDataModel = new ROIDataModel(); |
| 17 | + private final JTable table = new JTable(roiDataModel); |
| 18 | + private int[] selectedRows = new int[0]; |
| 19 | + |
| 20 | + public AvailableROIs(){ |
| 21 | + JScrollPane scrollPane = new JScrollPane(table); |
| 22 | + |
| 23 | + JPanel buttonPanel = new JPanel(); |
| 24 | + okayButton.addActionListener(this); |
| 25 | + cancelButton.addActionListener(this); |
| 26 | + buttonPanel.add(okayButton); |
| 27 | + buttonPanel.add(cancelButton); |
| 28 | + |
| 29 | + this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS)); |
| 30 | + |
| 31 | + this.add(scrollPane); |
| 32 | + this.add(buttonPanel); |
| 33 | + this.setSize(300, 300); |
| 34 | + this.setModal(true); |
| 35 | + this.setVisible(true); |
| 36 | + |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + public ArrayList<Roi> getSelectedRows(){ |
| 41 | + ArrayList<Roi> selectedRois = new ArrayList<>(); |
| 42 | + for (int selectedRow : selectedRows){ |
| 43 | + selectedRois.add(roiDataModel.rois.get(selectedRow)); |
| 44 | + } |
| 45 | + return selectedRois; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void actionPerformed(ActionEvent e) { |
| 50 | + if (e.getSource().equals(okayButton)){ |
| 51 | + selectedRows = table.getSelectedRows(); |
| 52 | + this.dispose(); |
| 53 | + } else if (e.getSource().equals(cancelButton)){ |
| 54 | + this.dispose(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static class ROIDataModel extends DefaultTableModel{ |
| 59 | + private final ArrayList<Roi> rois; |
| 60 | + public ROIDataModel(){ |
| 61 | + rois = new ArrayList<>(); |
| 62 | + rois.addAll(Arrays.asList(RoiManager.getRoiManager().getRoisAsArray())); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public int getRowCount() { |
| 67 | + if (rois == null){ |
| 68 | + return 0; |
| 69 | + } |
| 70 | + return rois.size(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String getColumnName(int column) { |
| 75 | + return "Available ROI's From ROI Manager"; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public int getColumnCount() { |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Object getValueAt(int rowIndex, int columnIndex) { |
| 85 | + return rois.get(rowIndex).getName(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments