source: tmcsimulator-scriptbuilder/trunk/src/event/editor/GenericTable.java @ 1

Revision 1, 4.2 KB checked in by bmcguffin, 9 years ago (diff)

2017/07/18: Uploaded entire prototype to SVN repo.

Line 
1package event.editor;
2
3import javax.swing.*;
4import java.util.*;
5import javax.swing.table.*;
6
7
8class 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 */
56public 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 JTable 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        return dialogTable;
104    }
105
106    public static JTable genericizeTable(JScrollPane scrollPane,
107            final JComboBox types, JButton addButton, JButton removeButton,
108            String header1, String header2)
109    {
110        MyTableModel.columnNames[0] = header1;
111        MyTableModel.columnNames[1] = header2;
112
113        return GenericTable.genericizeTable(scrollPane, types, addButton, removeButton);
114    }
115
116    public static JTable genericizeNumberedTable(JScrollPane scrollPane, JButton addButton, JButton removeButton)
117    {
118        MyTableModel.columnNames[0] = "#";
119        final JTable dialogTable = genericizeTable(scrollPane, new HashMap<JButton, String>(), removeButton);
120
121        addButton.addMouseListener(new java.awt.event.MouseAdapter() {
122            private int counter = 1;
123            @Override
124            public void mouseClicked(java.awt.event.MouseEvent evt)
125            {   
126                ((MyTableModel) dialogTable.getModel()).addRow("" + counter++, "");;
127            }
128        });
129        return dialogTable;
130    }
131}
Note: See TracBrowser for help on using the repository browser.