|
| 1 | +package ui.helper.historyui; |
| 2 | + |
| 3 | +import java.awt.BorderLayout; |
| 4 | +import java.awt.event.ActionEvent; |
| 5 | +import java.awt.event.ActionListener; |
| 6 | + |
| 7 | + |
| 8 | +import javax.swing.JFrame; |
| 9 | +import javax.swing.JPanel; |
| 10 | +import javax.swing.JScrollPane; |
| 11 | +import javax.swing.JTable; |
| 12 | +import javax.swing.ListSelectionModel; |
| 13 | +import javax.swing.table.DefaultTableModel; |
| 14 | + |
| 15 | + |
| 16 | +import javax.swing.DefaultListSelectionModel; |
| 17 | +import javax.swing.JButton; |
| 18 | + |
| 19 | +public class HistoryUI extends JFrame |
| 20 | + { |
| 21 | + private static final long serialVersionUID = 8141494344180865577L; |
| 22 | + private JTable resultsTable; |
| 23 | + private DefaultTableModel defaultTableModel; |
| 24 | + private HistoryUIInterface delegate; |
| 25 | + |
| 26 | + |
| 27 | + private JPanel panel; |
| 28 | + private JScrollPane scrollPane; |
| 29 | + |
| 30 | + |
| 31 | + /* |
| 32 | + * Setup class finders |
| 33 | + */ |
| 34 | + public HistoryUI(String[] titles) { |
| 35 | + |
| 36 | + // set the defaultTableModel to non editable by user clicking around |
| 37 | + defaultTableModel = new DefaultTableModel(0, 1) { |
| 38 | + private static final long serialVersionUID = 1L; |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean isCellEditable(int row, int column) { |
| 42 | + return false; |
| 43 | + |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + // big JPanel window |
| 48 | + panel = new JPanel(); |
| 49 | + getContentPane().add(panel, BorderLayout.CENTER); |
| 50 | + panel.setLayout(new BorderLayout(0, 0)); |
| 51 | + |
| 52 | + // show result |
| 53 | + resultsTable = new JTable(); |
| 54 | + resultsTable.setModel(defaultTableModel); |
| 55 | + resultsTable.setSelectionModel(new ForcedListSelectionModel()); |
| 56 | + |
| 57 | + // scroll option |
| 58 | + scrollPane = new JScrollPane(resultsTable); |
| 59 | + panel.add(scrollPane, BorderLayout.CENTER); |
| 60 | + |
| 61 | + // add small JPanel for Cancel and Confirm buttons |
| 62 | + JPanel buttom_panel = new JPanel(); |
| 63 | + getContentPane().add(buttom_panel, BorderLayout.SOUTH); |
| 64 | + |
| 65 | + //create buttons from array list |
| 66 | + for (String title : titles){ |
| 67 | + JButton button = new JButton(title); |
| 68 | + buttom_panel.add(button); |
| 69 | + button.addActionListener(new ActionListener() { |
| 70 | + |
| 71 | + @Override |
| 72 | + public void actionPerformed(ActionEvent e) { |
| 73 | + delegate.didPressButton(title, resultsTable.getSelectedRow()); |
| 74 | + |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + //add a row to the end of table |
| 81 | + public void insert(HistoryDataObject e){ |
| 82 | + defaultTableModel.addRow(new Object[] {e.toString()}); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + public void setDelegate(HistoryUIInterface delegate) { |
| 88 | + this.delegate = delegate; |
| 89 | + } |
| 90 | + |
| 91 | + /* |
| 92 | + * Forcedly remove single selection in JTable |
| 93 | + */ |
| 94 | + private static class ForcedListSelectionModel |
| 95 | + extends |
| 96 | + DefaultListSelectionModel { |
| 97 | + private static final long serialVersionUID = 1L; |
| 98 | + |
| 99 | + public ForcedListSelectionModel() { |
| 100 | + this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments