| 1 | package event.editor; |
|---|
| 2 | |
|---|
| 3 | import javax.swing.*; |
|---|
| 4 | import java.util.*; |
|---|
| 5 | import javax.swing.table.*; |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | class MyTableModel extends AbstractTableModel |
|---|
| 9 | { |
|---|
| 10 | private JTable table; |
|---|
| 11 | |
|---|
| 12 | protected static String[] columnNames = {"Role", "Dialog"}; |
|---|
| 13 | private Vector<Vector<String>> rowData = new Vector<Vector<String>>(); |
|---|
| 14 | |
|---|
| 15 | public String getColumnName(int col) { |
|---|
| 16 | return columnNames[col].toString(); |
|---|
| 17 | } |
|---|
| 18 | public int getRowCount() { return rowData.size(); } |
|---|
| 19 | public int getColumnCount() { return columnNames.length; } |
|---|
| 20 | public Object getValueAt(int row, int col) { |
|---|
| 21 | return rowData.get(row).get(col); |
|---|
| 22 | } |
|---|
| 23 | public boolean isCellEditable(int row, int col) |
|---|
| 24 | { return col != 0; } |
|---|
| 25 | |
|---|
| 26 | public void setValueAt(Object value, int row, int col) { |
|---|
| 27 | rowData.get(row).set(col, (String) value); |
|---|
| 28 | fireTableCellUpdated(row, col); |
|---|
| 29 | } |
|---|
| 30 | public void setTable(JTable theTable) |
|---|
| 31 | { |
|---|
| 32 | table = theTable; |
|---|
| 33 | } |
|---|
| 34 | public void addRow(String role, String dialog) |
|---|
| 35 | { |
|---|
| 36 | Vector<String> theRow = new Vector<String>(); |
|---|
| 37 | |
|---|
| 38 | theRow.add(role); |
|---|
| 39 | theRow.add(dialog); |
|---|
| 40 | rowData.add(theRow); |
|---|
| 41 | this.fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1 ); |
|---|
| 42 | } |
|---|
| 43 | public void deleteRow(int row) |
|---|
| 44 | { |
|---|
| 45 | if (row >= 0) |
|---|
| 46 | { |
|---|
| 47 | rowData.remove(row); |
|---|
| 48 | this.fireTableRowsDeleted(row, row); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | /** |
|---|
| 53 | * |
|---|
| 54 | * @author nathaniellehrer |
|---|
| 55 | */ |
|---|
| 56 | public class GenericTable { |
|---|
| 57 | |
|---|
| 58 | public static JTable genericizeTable(JScrollPane scrollPane, |
|---|
| 59 | final HashMap<JButton, String> types, JButton removeButton) |
|---|
| 60 | { |
|---|
| 61 | final JTable dialogTable = new JTable(new MyTableModel()); |
|---|
| 62 | ((MyTableModel) dialogTable.getModel()).setTable(dialogTable); |
|---|
| 63 | dialogTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
|---|
| 64 | dialogTable.getColumnModel().getColumn(0).setPreferredWidth(80); |
|---|
| 65 | dialogTable.getColumnModel().getColumn(1).setPreferredWidth(733); |
|---|
| 66 | scrollPane.setViewportView(dialogTable); |
|---|
| 67 | |
|---|
| 68 | for (final JButton button : types.keySet()) |
|---|
| 69 | { |
|---|
| 70 | button.addMouseListener(new java.awt.event.MouseAdapter() { |
|---|
| 71 | @Override |
|---|
| 72 | public void mouseClicked(java.awt.event.MouseEvent evt) |
|---|
| 73 | { |
|---|
| 74 | ((MyTableModel) dialogTable.getModel()).addRow(types.get(button), ""); |
|---|
| 75 | } |
|---|
| 76 | }); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | removeButton.addMouseListener(new java.awt.event.MouseAdapter() { |
|---|
| 80 | @Override |
|---|
| 81 | public void mouseClicked(java.awt.event.MouseEvent evt) |
|---|
| 82 | { |
|---|
| 83 | ((MyTableModel) dialogTable.getModel()).deleteRow(dialogTable.getSelectedRow()); |
|---|
| 84 | } |
|---|
| 85 | }); |
|---|
| 86 | |
|---|
| 87 | return dialogTable; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | public static void genericizeTable(JScrollPane scrollPane, |
|---|
| 92 | final JComboBox types, JButton addButton, JButton removeButton) |
|---|
| 93 | { |
|---|
| 94 | final JTable dialogTable = genericizeTable(scrollPane, new HashMap<JButton, String>(), removeButton); |
|---|
| 95 | |
|---|
| 96 | addButton.addMouseListener(new java.awt.event.MouseAdapter() { |
|---|
| 97 | @Override |
|---|
| 98 | public void mouseClicked(java.awt.event.MouseEvent evt) |
|---|
| 99 | { |
|---|
| 100 | ((MyTableModel) dialogTable.getModel()).addRow((String) types.getSelectedItem(), "");; |
|---|
| 101 | } |
|---|
| 102 | }); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | public static void genericizeTable(JScrollPane scrollPane, |
|---|
| 106 | final JComboBox types, JButton addButton, JButton removeButton, |
|---|
| 107 | String header1, String header2) |
|---|
| 108 | { |
|---|
| 109 | MyTableModel.columnNames[0] = header1; |
|---|
| 110 | MyTableModel.columnNames[1] = header2; |
|---|
| 111 | |
|---|
| 112 | GenericTable.genericizeTable(scrollPane, types, addButton, removeButton); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | public static void genericizeNumberedTable(JScrollPane scrollPane, JButton addButton, JButton removeButton) |
|---|
| 116 | { |
|---|
| 117 | MyTableModel.columnNames[0] = "#"; |
|---|
| 118 | final JTable dialogTable = genericizeTable(scrollPane, new HashMap<JButton, String>(), removeButton); |
|---|
| 119 | |
|---|
| 120 | addButton.addMouseListener(new java.awt.event.MouseAdapter() { |
|---|
| 121 | private int counter = 1; |
|---|
| 122 | @Override |
|---|
| 123 | public void mouseClicked(java.awt.event.MouseEvent evt) |
|---|
| 124 | { |
|---|
| 125 | ((MyTableModel) dialogTable.getModel()).addRow("" + counter++, "");; |
|---|
| 126 | } |
|---|
| 127 | }); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|