/*******************************************************************
SIMBUCK LIMITED
THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
********************************************************************
File Description:
Change History:
Rev Description
---- -----------------------------------------
1.0 First release
*******************************************************************/
package org.simbuck.plugins.mydevice;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
/**
* DataTable Class for the MyDevice
*
* @author rjones
*/
public class MyDeviceDataTable implements TableModelListener {
/** use a JTable for the presentation of the board data
* make it package-private
*/
JTable simbuckDeviceDataTable;
/** use a Specific TableModel for the presentation of the board data
* make it package-private
*/
MyDeviceTableModel simbuckdevicetablemodel;
// turn off table selection until the user
// knows what she wants to do
private final boolean COLUMN_SELECTION = false;
private final boolean ROW_SELECTION = false;
/**
* Default Constructor
*/
public MyDeviceDataTable() {
}
/**
* create a table for device specific data.
*
* @param names ArrayList
* @param records ArrayList
* @return JScrollPane - ScrollPane containing the table view
*/
public JScrollPane generateTableView(ArrayList names, ArrayList records) {
simbuckDeviceDataTable = new JTable();
// select one row at a time
simbuckDeviceDataTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// select contiguous rows
//--//simbuckDeviceDataTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
// select any set of elements
//--//simbuckDeviceDataTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// make everything selectable
simbuckDeviceDataTable.setRowSelectionAllowed(ROW_SELECTION);
simbuckDeviceDataTable.setColumnSelectionAllowed(COLUMN_SELECTION);
//simbuckDeviceDataTable.setCellSelectionEnabled(ROW_SELECTION);
simbuckDeviceDataTable.setPreferredScrollableViewportSize(new Dimension(500, 80));
simbuckDeviceDataTable.setModel(createSimbuckDeviceDataTable(names));
simbuckdevicetablemodel = (MyDeviceTableModel) simbuckDeviceDataTable.getModel();
if (!simbuckdevicetablemodel.hasEmptyRow()) {
for (int i = 1; i <= 4; i++) {
simbuckdevicetablemodel.addEmptyRow();
}
//--// we are not using any table listener(yet!)
simbuckdevicetablemodel.addTableModelListener(this);
}
// place holder for further development.
//simbuckDeviceDataTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (simbuckDeviceDataTable.getRowSelectionAllowed()) {
// true by default
ListSelectionModel rowSM = simbuckDeviceDataTable.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
//--// This is currently redundant as we have not
@Override
public void valueChanged(ListSelectionEvent e) {
//--//Ignore the event.
//if (e.getValueIsAdjusting()) {
// return;
//}
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if (lsm.isSelectionEmpty()) {
// debug
System.out.println("No rows are selected.");
} else {
int selectedRow = lsm.getMinSelectionIndex();
// debug
System.out.println("Row " + selectedRow + " is now selected.");
}
}
});
} else {
// Do Nothing
}
if (simbuckDeviceDataTable.getColumnSelectionAllowed()) {
// false by default
if (simbuckDeviceDataTable.getRowSelectionAllowed()) {
//We allow both row and column selection, which
//implies that we *really* want to allow individual
//cell selection.
simbuckDeviceDataTable.setCellSelectionEnabled(true);
}
ListSelectionModel colSM = simbuckDeviceDataTable.getColumnModel().getSelectionModel();
colSM.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
//--//Ignore the event.
//if (e.getValueIsAdjusting()) {
// return;
//}
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if (lsm.isSelectionEmpty()) {
// debug
System.out.println("No columns are selected.");
} else {
int selectedCol = lsm.getMinSelectionIndex();
//debug
System.out.println("Column " + selectedCol + " is now selected.");
}
}
});
}
simbuckDeviceDataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int i = 0; i < simbuckDeviceDataTable.getColumnCount(); i++) {
DefaultTableColumnModel colModel = (DefaultTableColumnModel) simbuckDeviceDataTable.getColumnModel();
TableColumn col = colModel.getColumn(i);
int width = 0;
TableCellRenderer renderer = col.getHeaderRenderer();
for (int r = 0; r < simbuckDeviceDataTable.getRowCount(); r++) {
renderer = simbuckDeviceDataTable.getCellRenderer(r, i);
Component comp = renderer.getTableCellRendererComponent(simbuckDeviceDataTable, simbuckDeviceDataTable.getValueAt(r, i),
false, false, r, i);
width = Math.max(width, comp.getPreferredSize().width);
}
col.setPreferredWidth(width * 12);
}
// create a pre-sized table
simbuckdevicetablemodel.prepareTable(records.size());
// populate the table with known data
for (int i = 0; i < records.size(); i++) {
simbuckdevicetablemodel.setValueAt(records.get(i), i, 0);
}
// make sure we can see the data
simbuckDeviceDataTable.repaint();
// put it into a scrollpane if it gets bigger than the allocated space
JScrollPane scrollPane = new JScrollPane(simbuckDeviceDataTable);
// return the component
return scrollPane;
}
/**
* construct a board specific table
*
* @param names - column names
* @return TableModel - Device Specific table model
*/
private MyDeviceTableModel createSimbuckDeviceDataTable(ArrayList names) {
String[] columnNames = new String[names.size()];
for (int i = 0; i < names.size(); i++) {
columnNames[i] = names.get(i).toString();
}
MyDeviceTableModel tm = new MyDeviceTableModel(columnNames);
return tm;
}
/**
* Provide package-private access to the table model
* @return MyDeviceTableModel - tablemodel
*/
MyDeviceTableModel getTableModel() {
return simbuckdevicetablemodel;
}
@Override
public void tableChanged(TableModelEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}