columnNames.add("Time");
columnNames.add("From");
columnNames.add("To");
columnNames.add("Data");
final AbstractTableModel model = new AbstractTableModel() {
public String getColumnName(int col) {
return columnNames.get(col).toString();
}
public int getRowCount() {
return rowData.size();
}
public int getColumnCount() {
return columnNames.size();
}
public Object getValueAt(int row, int col) {
if (col == COLUMN_TIME) {
return rowData.get(row)[DATAPOS_TIME];
} else if (col == COLUMN_FROM) {
return ((RadioConnection)rowData.get(row)[DATAPOS_CONNECTION]).getSource().getMote();
} else if (col == COLUMN_TO) {
return "[" + ((RadioConnection)rowData.get(row)[DATAPOS_CONNECTION]).getDestinations().length + " motes]";
} else if (col == COLUMN_DATA) {
return transformDataToString((byte[]) rowData.get(row)[DATAPOS_DATA]);
} else {
logger.fatal("Bad row/col: " + row + "/" + col);
}
return null;
}
public boolean isCellEditable(int row, int col) {
if (col == COLUMN_FROM) {
// Try to highligt selected mote
gui.signalMoteHighlight(
((RadioConnection)rowData.get(row)[DATAPOS_CONNECTION]).getSource().getMote()
);
}
if (col == COLUMN_TO) {
return true;
}
return false;
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
};
final JComboBox comboBox = new JComboBox();
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getItem() instanceof Mote) {
gui.signalMoteHighlight((Mote) e.getItem());
}
}
}
});
dataTable = new JTable(model) {
public TableCellEditor getCellEditor(int row, int column) {
// Populate combo box
comboBox.removeAllItems();
if (row < 0 || row >= rowData.size()) {
return super.getCellEditor(row, column);
}
RadioConnection conn = (RadioConnection) rowData.get(row)[DATAPOS_CONNECTION];
if (conn == null) {
return super.getCellEditor(row, column);
}
for (Radio destRadio: conn.getDestinations()) {
if (destRadio.getMote() != null) {
comboBox.addItem(destRadio.getMote());
} else {
comboBox.addItem("[standalone radio]");
}
}
return super.getCellEditor(row, column);
}
public String getToolTipText(MouseEvent e) {
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
int realColumnIndex = convertColumnIndexToModel(colIndex);
String tip = "";
if (realColumnIndex == COLUMN_DATA) {
byte[] data = (byte[]) rowData.get(rowIndex)[DATAPOS_DATA];
Packet packet = analyzePacket(data);
if (packet != null) {
tip = packet.getToolTip();
}
} else {
tip = super.getToolTipText(e);
}
return tip;
}
};
// Set data column width greedy
dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
dataTable.getColumnModel().getColumn(COLUMN_TIME).setPreferredWidth(55);
dataTable.getColumnModel().getColumn(COLUMN_FROM).setPreferredWidth(130);
dataTable.getColumnModel().getColumn(COLUMN_TO).setPreferredWidth(100);
dataTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
dataTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int selectedRowIndex = dataTable.getSelectedRow();
if (selectedRowIndex < 0) {
return;
}
Object[] row = rowData.get(selectedRowIndex);
if (row == null) {
return;
}
RadioConnection conn = (RadioConnection) row[DATAPOS_CONNECTION];
if (conn == null) {
return;
}
Mote mote = conn.getSource().getMote();
if (mote == null) {
return;
}
gui.signalMoteHighlight(mote);
}
});
TableColumn destColumn = dataTable.getColumnModel().getColumn(COLUMN_TO);
destColumn.setCellEditor(new DefaultCellEditor(comboBox));
final JScrollPane scrollPane = new JScrollPane(dataTable);
// dataTable.setFillsViewportHeight(true);
add(scrollPane);
simulation.getRadioMedium().addRadioMediumObserver(radioMediumObserver = new Observer() {
public void update(Observable obs, Object obj) {
RadioConnection[] newConnections = radioMedium.getLastTickConnections();
if (newConnections == null) {
return;
}
for (RadioConnection newConnection: newConnections) {
Object[] data = new Object[3];
data[DATAPOS_TIME] = new Integer(simulation.getSimulationTime());
data[DATAPOS_CONNECTION] = newConnection;
if (newConnection.getSource() instanceof PacketRadio) {
data[DATAPOS_DATA] = ((PacketRadio) newConnection.getSource()).getLastPacketTransmitted();
} else {
data[DATAPOS_DATA] = null;
}
rowData.add(data);
}
model.fireTableRowsInserted(rowData.size() - newConnections.length + 1, rowData.size());
setTitle("Radio Logger: " + rowData.size() + " packets");
}
});
try {