Warning: Can't use blame annotator:
svn blame failed on branches/ScriptBuilder4/src/event/editor/GenericTable.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator-scriptbuilder/branches/ScriptBuilder4/src/event/editor/GenericTable.java @ 6

Revision 6, 4.2 KB checked in by jdalbey, 9 years ago (diff)

Add original prototype to branch

RevLine 
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 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}
Note: See TracBrowser for help on using the repository browser.