|
| 1 | +package ui.helper.methodinput; |
| 2 | + |
| 3 | +import java.awt.BorderLayout; |
| 4 | +import java.awt.event.ActionEvent; |
| 5 | +import java.awt.event.ActionListener; |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.util.ArrayList; |
| 8 | + |
| 9 | +import javax.swing.DefaultListSelectionModel; |
| 10 | +import javax.swing.JButton; |
| 11 | +import javax.swing.JFrame; |
| 12 | +import javax.swing.JOptionPane; |
| 13 | +import javax.swing.JPanel; |
| 14 | +import javax.swing.JScrollPane; |
| 15 | +import javax.swing.JTable; |
| 16 | +import javax.swing.JTextField; |
| 17 | +import javax.swing.ListSelectionModel; |
| 18 | +import javax.swing.SwingUtilities; |
| 19 | +import javax.swing.event.DocumentEvent; |
| 20 | +import javax.swing.event.DocumentListener; |
| 21 | +import javax.swing.table.DefaultTableModel; |
| 22 | + |
| 23 | +/** |
| 24 | + * similar implementation as class search frame |
| 25 | + */ |
| 26 | +public class MethodInputFrame extends JFrame implements ActionListener, DocumentListener { |
| 27 | + |
| 28 | + private JTextField searchingTextField; |
| 29 | + private JTable resultsTable; |
| 30 | + private MethodSearch searchUtil; |
| 31 | + private DefaultTableModel defaultTableModel; |
| 32 | + private MethodSearchFrameDelegateInterface delegate; |
| 33 | + |
| 34 | + private Thread runningThread; |
| 35 | + private JButton btnCancel, btnConfirm; |
| 36 | + private JPanel panel; |
| 37 | + private JScrollPane scrollPane; |
| 38 | + |
| 39 | + |
| 40 | + public MethodInputFrame(Class c) { |
| 41 | + // set the defaultTableModel to non editable by user clicking around |
| 42 | + defaultTableModel = new DefaultTableModel(0, 1) { |
| 43 | + @Override |
| 44 | + public boolean isCellEditable(int row, int column) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + // big JPanel window |
| 50 | + panel = new JPanel(); |
| 51 | + getContentPane().add(panel, BorderLayout.CENTER); |
| 52 | + panel.setLayout(new BorderLayout(0, 0)); |
| 53 | + |
| 54 | + // search text field on top |
| 55 | + searchUtil = new MethodSearch(c); |
| 56 | + searchingTextField = new JTextField(); |
| 57 | + getContentPane().add(searchingTextField, BorderLayout.NORTH); |
| 58 | + searchingTextField.setColumns(10); |
| 59 | + |
| 60 | + // show result |
| 61 | + resultsTable = new JTable(); |
| 62 | + resultsTable.setModel(defaultTableModel); |
| 63 | + resultsTable.setSelectionModel(new ForcedListSelectionModel()); |
| 64 | + |
| 65 | + // scroll option |
| 66 | + scrollPane = new JScrollPane(resultsTable); |
| 67 | + panel.add(scrollPane, BorderLayout.CENTER); |
| 68 | + |
| 69 | + // add small JPanel for Cancel and Confirm buttons |
| 70 | + JPanel panel_1 = new JPanel(); |
| 71 | + getContentPane().add(panel_1, BorderLayout.SOUTH); |
| 72 | + |
| 73 | + btnCancel = new JButton("Cancel"); |
| 74 | + panel_1.add(btnCancel); |
| 75 | + btnCancel.addActionListener(this); |
| 76 | + |
| 77 | + btnConfirm = new JButton("Confirm"); |
| 78 | + panel_1.add(btnConfirm); |
| 79 | + btnConfirm.addActionListener(this); |
| 80 | + |
| 81 | + // set up connections |
| 82 | + searchingTextField.addActionListener(this); |
| 83 | + searchingTextField.getDocument().addDocumentListener(this); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /* |
| 89 | + * Forcely remove single selection in Jtable |
| 90 | + */ |
| 91 | + private static class ForcedListSelectionModel |
| 92 | + extends |
| 93 | + DefaultListSelectionModel { |
| 94 | + public ForcedListSelectionModel() { |
| 95 | + this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 96 | + }; |
| 97 | + /* |
| 98 | + * potentially better solve the selection problem, but not needed at the |
| 99 | + * moment |
| 100 | + * |
| 101 | + * @Override public void clearSelection() { } |
| 102 | + * |
| 103 | + * @Override public void removeSelectionInterval(int index0, int index1) |
| 104 | + * { } |
| 105 | + */ |
| 106 | + } |
| 107 | + |
| 108 | + private void updateMethodList() { |
| 109 | + if (runningThread != null) { |
| 110 | + runningThread.interrupt(); |
| 111 | + } |
| 112 | + runningThread = new Thread(new Runnable() { |
| 113 | + |
| 114 | + @Override |
| 115 | + public void run() { |
| 116 | + |
| 117 | + String searchText; |
| 118 | + synchronized (searchingTextField) { |
| 119 | + searchText = searchingTextField.getText(); |
| 120 | + } |
| 121 | + // start Search |
| 122 | + ArrayList<String> classesForName = searchUtil.methodsForName(searchText); |
| 123 | + |
| 124 | + boolean equals; |
| 125 | + synchronized (searchingTextField) { |
| 126 | + equals = |
| 127 | + // if text has not changed |
| 128 | + searchingTextField.getText().equals(searchText); |
| 129 | + } |
| 130 | + |
| 131 | + if (equals) { |
| 132 | + SwingUtilities.invokeLater(new Runnable() { |
| 133 | + |
| 134 | + @Override |
| 135 | + public void run() { |
| 136 | + defaultTableModel.setRowCount(0); |
| 137 | + for (String string : classesForName) { |
| 138 | + defaultTableModel.addRow(new String[]{string}); |
| 139 | + } |
| 140 | + defaultTableModel.fireTableDataChanged(); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + // It is buggy with the synchronized searching |
| 146 | + // resultsTable.setRowSelectionInterval(0, 0); |
| 147 | + } |
| 148 | + }); |
| 149 | + runningThread.start(); |
| 150 | + } |
| 151 | + |
| 152 | + public void setDelegate(MethodSearchFrameDelegateInterface delegate) { |
| 153 | + this.delegate = delegate; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void changedUpdate(DocumentEvent e) { |
| 158 | + // TODO Auto-generated method stub |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void insertUpdate(DocumentEvent e) { |
| 164 | + // TODO Auto-generated method stub |
| 165 | + updateMethodList(); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void removeUpdate(DocumentEvent e) { |
| 170 | + // TODO Auto-generated method stub |
| 171 | + updateMethodList(); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void actionPerformed(ActionEvent e) { |
| 176 | + // User cancel the class search |
| 177 | + |
| 178 | + if (e.getSource() == btnCancel) { |
| 179 | + setVisible(false); |
| 180 | + dispose(); |
| 181 | + } else if (e.getSource() == btnConfirm) { |
| 182 | + |
| 183 | + // return the selected (or the first if no selected) class if the |
| 184 | + // table is not empty |
| 185 | + if (resultsTable.getRowCount() != 0) { |
| 186 | + |
| 187 | + int selectedRow = resultsTable.getSelectedRow(); |
| 188 | + if (selectedRow == -1) { |
| 189 | + selectedRow = 0; |
| 190 | + } |
| 191 | + // TODO get selected/highlighted class, not just the first one |
| 192 | + delegate.didSelectMethod( |
| 193 | + (String) resultsTable.getValueAt(selectedRow, 0)); |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + setVisible(false); |
| 198 | + dispose(); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments