| 1 | package tmcsim.paramicscommunicator.gui; |
|---|
| 2 | |
|---|
| 3 | import java.text.DateFormat; |
|---|
| 4 | import java.util.ArrayList; |
|---|
| 5 | import java.util.Date; |
|---|
| 6 | import java.util.List; |
|---|
| 7 | |
|---|
| 8 | import javax.swing.event.TableModelEvent; |
|---|
| 9 | import javax.swing.table.DefaultTableModel; |
|---|
| 10 | |
|---|
| 11 | import tmcsim.paramicscommunicator.FileIOUpdate; |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * FileIOTableModel is a DefaultTableModel used to display the |
|---|
| 15 | * list of I/O operations that have been performed by a |
|---|
| 16 | * Paramics FileWriter or FileReader. The columns in this table |
|---|
| 17 | * show the I/O time and bytes written or read. |
|---|
| 18 | * |
|---|
| 19 | * @author Matthew Cechini |
|---|
| 20 | * @version |
|---|
| 21 | */ |
|---|
| 22 | @SuppressWarnings("serial") |
|---|
| 23 | public class FileIOTableModel extends DefaultTableModel { |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Enumeration of columns for this table. |
|---|
| 27 | * @author Matthew Cechini |
|---|
| 28 | */ |
|---|
| 29 | public static enum FILE_IO_COLUMNS { |
|---|
| 30 | TIME_COL ("Time", 0, 100), |
|---|
| 31 | NUM_BYTES_COL ("Num Bytes", 1, 60); |
|---|
| 32 | |
|---|
| 33 | public String colName; |
|---|
| 34 | public int colNum; |
|---|
| 35 | public int colWidth; |
|---|
| 36 | |
|---|
| 37 | private FILE_IO_COLUMNS (String name, int num, int width) { |
|---|
| 38 | colName = name; |
|---|
| 39 | colNum = num; |
|---|
| 40 | colWidth = width; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | public static int colCount() { |
|---|
| 44 | return values().length; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public static String columnName(int num) { |
|---|
| 48 | |
|---|
| 49 | for(FILE_IO_COLUMNS column : FILE_IO_COLUMNS.values()) { |
|---|
| 50 | if(column.colNum == num) |
|---|
| 51 | return column.colName; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | return ""; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | public static int columnWidth(int num) { |
|---|
| 58 | |
|---|
| 59 | for(FILE_IO_COLUMNS column : FILE_IO_COLUMNS.values()) { |
|---|
| 60 | if(column.colNum == num) |
|---|
| 61 | return column.colWidth; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | return 0; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Container class to hold information displayed in the table. |
|---|
| 70 | * @author Matthew Cechini |
|---|
| 71 | */ |
|---|
| 72 | protected class FileIOTableItem { |
|---|
| 73 | String ioTime; |
|---|
| 74 | Long ioBytes; |
|---|
| 75 | |
|---|
| 76 | public FileIOTableItem(String time, Long bytes) { |
|---|
| 77 | ioTime = time; |
|---|
| 78 | ioBytes = bytes; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /** List of data displayed in the table. */ |
|---|
| 84 | protected List<FileIOTableItem> tableData; |
|---|
| 85 | |
|---|
| 86 | /** DateFormat used to format time table values. */ |
|---|
| 87 | protected DateFormat timeFormatter; |
|---|
| 88 | |
|---|
| 89 | /** Constructor. */ |
|---|
| 90 | public FileIOTableModel() { |
|---|
| 91 | tableData = new ArrayList<FileIOTableItem>(); |
|---|
| 92 | |
|---|
| 93 | timeFormatter = DateFormat.getTimeInstance(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Gets the rowCount attribute of the RideTableModel object |
|---|
| 98 | * |
|---|
| 99 | *@return The rowCount value |
|---|
| 100 | */ |
|---|
| 101 | public int getRowCount() { |
|---|
| 102 | if(tableData == null) |
|---|
| 103 | return 0; |
|---|
| 104 | else |
|---|
| 105 | return tableData.size(); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Gets the columnCount attribute of the RideTableModel object |
|---|
| 112 | * |
|---|
| 113 | *@return The columnCount value |
|---|
| 114 | */ |
|---|
| 115 | public int getColumnCount() { |
|---|
| 116 | return FILE_IO_COLUMNS.colCount(); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Gets the valueAt attribute of the RideTableModel object |
|---|
| 123 | * |
|---|
| 124 | *@param row Description of the Parameter |
|---|
| 125 | *@param col Description of the Parameter |
|---|
| 126 | *@return The valueAt value |
|---|
| 127 | */ |
|---|
| 128 | public Object getValueAt(int row, int col) { |
|---|
| 129 | |
|---|
| 130 | if(col == FILE_IO_COLUMNS.TIME_COL.colNum) |
|---|
| 131 | return tableData.get(row).ioTime; |
|---|
| 132 | else if(col == FILE_IO_COLUMNS.NUM_BYTES_COL.colNum) |
|---|
| 133 | return tableData.get(row).ioBytes; |
|---|
| 134 | else |
|---|
| 135 | return ""; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | public String getColumnName(int col) { |
|---|
| 140 | return FILE_IO_COLUMNS.columnName(col); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | public int getColumnWidth(int col){ |
|---|
| 144 | return FILE_IO_COLUMNS.columnWidth(col); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | * Gets the cellEditable attribute of the RideTableModel object |
|---|
| 149 | * |
|---|
| 150 | *@param row Description of the Parameter |
|---|
| 151 | *@param col Description of the Parameter |
|---|
| 152 | *@return The cellEditable value |
|---|
| 153 | */ |
|---|
| 154 | public boolean isCellEditable(int row, int col) { |
|---|
| 155 | return false; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * Add a new IOUpdate into the table model. Update the table. |
|---|
| 161 | * @param update New update object. |
|---|
| 162 | */ |
|---|
| 163 | public void addIOUpdate(FileIOUpdate update) { |
|---|
| 164 | |
|---|
| 165 | tableData.add(new FileIOTableItem(timeFormatter.format(new Date()), |
|---|
| 166 | update.ioBytes)); |
|---|
| 167 | |
|---|
| 168 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | } |
|---|