Blame |
Last modification |
View Log
| RSS feed
package dongfang.mk.ui;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
public class DebugTable
extends JPanel {
private static final int NUMCHANNELS =
32;
private static final int NUMCOLSETS =
1;
private static final int NUMROWS = NUMCHANNELS / NUMCOLSETS
;
private static final int NUMCOLS = NUMCOLSETS
* 3;
private SelectionListener selectionListener
;
private static final int LABELCOL=
0;
private static final int VALUECOL=
1;
private static final int SELECTCOL=
2;
class DebugtableModel
extends AbstractTableModel {
private String[] labels =
new String[NUMCHANNELS
];
private Integer[] values =
new Integer[NUMCHANNELS
];
private boolean[] selected =
new boolean[NUMCHANNELS
];
@
Override
public Class<?> getColumnClass
(int columnIndex
) {
switch(columnIndex
% 3) {
case LABELCOL:
return String.
class;
case VALUECOL:
return Integer.
class;
case SELECTCOL:
return Boolean.
class;
}
return null;
}
@
Override
public String getColumnName
(int column
) {
switch(column
% 3) {
case LABELCOL:
return "Name";
case VALUECOL:
return "Value";
case SELECTCOL:
return "Show";
}
return null;
}
@
Override
public boolean isCellEditable
(int rowIndex,
int columnIndex
) {
return columnIndex
% 3 == SELECTCOL
;
}
@
Override
public void setValueAt
(Object value,
int rowIndex,
int columnIndex
) {
if (columnIndex
% 3 == SELECTCOL
) {
int colnum =
(columnIndex /
3);
setSelected
(rowIndex + colnum
* NUMROWS,
(Boolean)value
);
}
}
void setLabel
(int index,
String label
) {
labels
[index
] = label
;
int colnum = LABELCOL +
(index / NUMROWS
) * 3;
fireTableCellUpdated
(index
% NUMROWS, colnum
);
}
void setValue
(int index,
int value
) {
values
[index
] = value
;
int colnum = VALUECOL +
(index / NUMROWS
) * 3;
fireTableCellUpdated
(index
% NUMROWS, colnum
);
}
void setValues
(int[] values
) {
for (int i=
0; i
<values.
length; i++
) {
this.
values[i
] = values
[i
];
int colnum = VALUECOL +
(i / NUMROWS
) * 3;
fireTableCellUpdated
(i
% NUMROWS, colnum
);
}
}
void setSelected
(int index,
boolean selected
) {
this.
selected[index
] = selected
;
notifySelectionListener
(this.
selected);
}
boolean isSelected
(int index
) {
return selected
[index
];
}
public int getColumnCount
() {
return NUMCOLS
;
}
public int getRowCount
() {
return NUMROWS
;
}
public Object getValueAt
(int rowIndex,
int columnIndex
) {
int colnum =
(columnIndex /
3);
switch(columnIndex
% 3) {
case LABELCOL:
return labels
[rowIndex + colnum
* NUMROWS
];
case VALUECOL:
return values
[rowIndex + colnum
* NUMROWS
];
case SELECTCOL:
return selected
[rowIndex + colnum
* NUMROWS
];
}
return null;
}
}
private DebugtableModel model =
new DebugtableModel
();
private JTable table =
new JTable() {
public Component prepareRenderer
(TableCellRenderer renderer,
int row,
int column
) {
Component c =
super.
prepareRenderer(renderer, row, column
);
if (column
% 3 == SELECTCOL
)
c.
setBackground(DebugColors.
getDebugColor(row
));
return c
;
}
};
public DebugTable
() {
table.
setModel(model
);
setLayout
(new BorderLayout());
add
(table.
getTableHeader(),
BorderLayout.
PAGE_START);
add
(table,
BorderLayout.
CENTER);
}
public void setLabel
(int index,
String label
) {
this.
model.
setLabel(index, label
);
}
public void setValue
(int index,
int value
) {
this.
model.
setValue(index, value
);
}
public void setValues
(int[] values
) {
this.
model.
setValues(values
);
}
public void setSelected
(int index,
boolean value
) {
this.
model.
setSelected(index, value
);
}
public boolean isSelected
(int index
) {
return this.
model.
isSelected(index
);
}
public void setSelectionListener
(SelectionListener l
) {
this.
selectionListener = l
;
}
private void notifySelectionListener
(boolean[] selected
) {
if (selectionListener
!=
null)
selectionListener.
notifySelected(selected
);
}
public static void main
(String[] args
) {
JFrame f =
new JFrame();
DebugTable t =
new DebugTable
();
for (int i=
0; i
<NUMCHANNELS
; i++
) {
t.
setLabel(i,
"label "+i
);
t.
setValue(i,
(int)(Math.
random() * 10000));
t.
setSelected(i,
false);
}
f.
getContentPane().
add(t
);
f.
pack();
f.
setVisible(true);
}
}