Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1795 | - | 1 | package dongfang.mk.ui; |
2 | |||
3 | import java.awt.BorderLayout; |
||
4 | import java.awt.Component; |
||
5 | |||
6 | import javax.swing.JFrame; |
||
7 | import javax.swing.JPanel; |
||
8 | import javax.swing.JTable; |
||
9 | import javax.swing.table.AbstractTableModel; |
||
10 | import javax.swing.table.TableCellRenderer; |
||
11 | |||
12 | public class DebugTable extends JPanel { |
||
13 | private static final int NUMCHANNELS = 32; |
||
14 | private static final int NUMCOLSETS = 1; |
||
15 | private static final int NUMROWS = NUMCHANNELS / NUMCOLSETS; |
||
16 | private static final int NUMCOLS = NUMCOLSETS * 3; |
||
17 | private SelectionListener selectionListener; |
||
18 | |||
19 | private static final int LABELCOL=0; |
||
20 | private static final int VALUECOL=1; |
||
21 | private static final int SELECTCOL=2; |
||
22 | |||
23 | class DebugtableModel extends AbstractTableModel { |
||
24 | private String[] labels = new String[NUMCHANNELS]; |
||
25 | private Integer[] values = new Integer[NUMCHANNELS]; |
||
26 | private boolean[] selected = new boolean[NUMCHANNELS]; |
||
27 | |||
28 | @Override |
||
29 | public Class<?> getColumnClass(int columnIndex) { |
||
30 | switch(columnIndex % 3) { |
||
31 | case LABELCOL: return String.class; |
||
32 | case VALUECOL: return Integer.class; |
||
33 | case SELECTCOL: return Boolean.class; |
||
34 | } |
||
35 | return null; |
||
36 | } |
||
37 | |||
38 | @Override |
||
39 | public String getColumnName(int column) { |
||
40 | switch(column % 3) { |
||
41 | case LABELCOL: return "Name"; |
||
42 | case VALUECOL: return "Value"; |
||
43 | case SELECTCOL: return "Show"; |
||
44 | } |
||
45 | return null; |
||
46 | } |
||
47 | |||
48 | @Override |
||
49 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
||
50 | return columnIndex % 3 == SELECTCOL; |
||
51 | } |
||
52 | |||
53 | @Override |
||
54 | public void setValueAt(Object value, int rowIndex, int columnIndex) { |
||
55 | if (columnIndex % 3 == SELECTCOL) { |
||
56 | int colnum = (columnIndex / 3); |
||
57 | setSelected(rowIndex + colnum * NUMROWS, (Boolean)value); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | void setLabel(int index, String label) { |
||
62 | labels[index] = label; |
||
63 | int colnum = LABELCOL + (index / NUMROWS) * 3; |
||
64 | fireTableCellUpdated(index % NUMROWS, colnum); |
||
65 | } |
||
66 | |||
67 | void setValue(int index, int value) { |
||
68 | values[index] = value; |
||
69 | int colnum = VALUECOL + (index / NUMROWS) * 3; |
||
70 | fireTableCellUpdated(index % NUMROWS, colnum); |
||
71 | } |
||
72 | |||
73 | void setValues(int[] values) { |
||
74 | for (int i=0; i<values.length; i++) { |
||
75 | this.values[i] = values[i]; |
||
76 | int colnum = VALUECOL + (i / NUMROWS) * 3; |
||
77 | fireTableCellUpdated(i % NUMROWS, colnum); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | void setSelected(int index, boolean selected) { |
||
82 | this.selected[index] = selected; |
||
83 | notifySelectionListener(this.selected); |
||
84 | } |
||
85 | |||
86 | boolean isSelected(int index) { |
||
87 | return selected[index]; |
||
88 | } |
||
89 | |||
90 | public int getColumnCount() { |
||
91 | return NUMCOLS; |
||
92 | } |
||
93 | |||
94 | public int getRowCount() { |
||
95 | return NUMROWS; |
||
96 | } |
||
97 | |||
98 | public Object getValueAt(int rowIndex, int columnIndex) { |
||
99 | int colnum = (columnIndex / 3); |
||
100 | switch(columnIndex % 3) { |
||
101 | case LABELCOL: return labels[rowIndex + colnum * NUMROWS]; |
||
102 | case VALUECOL: return values[rowIndex + colnum * NUMROWS]; |
||
103 | case SELECTCOL: return selected[rowIndex + colnum * NUMROWS]; |
||
104 | } |
||
105 | return null; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | private DebugtableModel model = new DebugtableModel(); |
||
110 | |||
111 | private JTable table = new JTable() { |
||
112 | public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { |
||
113 | Component c = super.prepareRenderer(renderer, row, column); |
||
114 | if (column % 3 == SELECTCOL) |
||
115 | c.setBackground(DebugColors.getDebugColor(row)); |
||
116 | return c; |
||
117 | } |
||
118 | }; |
||
119 | |||
120 | public DebugTable() { |
||
121 | table.setModel(model); |
||
122 | setLayout(new BorderLayout()); |
||
123 | add(table.getTableHeader(), BorderLayout.PAGE_START); |
||
124 | add(table, BorderLayout.CENTER); |
||
125 | } |
||
126 | |||
127 | |||
128 | |||
129 | public void setLabel(int index, String label) { |
||
130 | this.model.setLabel(index, label); |
||
131 | } |
||
132 | |||
133 | public void setValue(int index, int value) { |
||
134 | this.model.setValue(index, value); |
||
135 | } |
||
136 | |||
137 | public void setValues(int[] values) { |
||
138 | this.model.setValues(values); |
||
139 | } |
||
140 | |||
141 | public void setSelected(int index, boolean value) { |
||
142 | this.model.setSelected(index, value); |
||
143 | } |
||
144 | |||
145 | public boolean isSelected(int index) { |
||
146 | return this.model.isSelected(index); |
||
147 | } |
||
148 | |||
149 | public void setSelectionListener(SelectionListener l) { |
||
150 | this.selectionListener = l; |
||
151 | } |
||
152 | |||
153 | private void notifySelectionListener(boolean[] selected) { |
||
154 | if (selectionListener != null) |
||
155 | selectionListener.notifySelected(selected); |
||
156 | } |
||
157 | |||
158 | public static void main(String[] args) { |
||
159 | JFrame f = new JFrame(); |
||
160 | DebugTable t = new DebugTable(); |
||
161 | |||
162 | for (int i=0; i<NUMCHANNELS; i++) { |
||
163 | t.setLabel(i, "label "+i); |
||
164 | t.setValue(i, (int)(Math.random() * 10000)); |
||
165 | t.setSelected(i, false); |
||
166 | } |
||
167 | |||
168 | f.getContentPane().add(t); |
||
169 | f.pack(); |
||
170 | f.setVisible(true); |
||
171 | } |
||
172 | } |