| 1 | package tmcsim.cadsimulator.viewer.model; |
|---|
| 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.cadsimulator.videocontrol.DVDStatusUpdate; |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * DVDStatusTableModel extends from DefaultTableModel to display |
|---|
| 16 | * DVDStatusUpdates that are received from a DVDController. The |
|---|
| 17 | * addStatusUpdate() method is used to add new update objects. |
|---|
| 18 | * The table may be cleared through the clearModelData() method. |
|---|
| 19 | * |
|---|
| 20 | * @author Matthew Cechini |
|---|
| 21 | * @version |
|---|
| 22 | */ |
|---|
| 23 | @SuppressWarnings("serial") |
|---|
| 24 | public class DVDStatusTableModel extends DefaultTableModel { |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Enumeration containing column information for all columns shown in the |
|---|
| 28 | * DVDStatus table. |
|---|
| 29 | * @author Matthew Cechini |
|---|
| 30 | */ |
|---|
| 31 | public static enum STATUS_COLUMNS { |
|---|
| 32 | TIME_COL ("Time", 0, 60, 100, 80), |
|---|
| 33 | UPDATE_INFO_COL ("Status Info", 1, 140, 400, 420); |
|---|
| 34 | |
|---|
| 35 | public String colName; |
|---|
| 36 | public int colNum; |
|---|
| 37 | public int colMinWidth; |
|---|
| 38 | public int colMaxWidth; |
|---|
| 39 | public int colPrefWidth; |
|---|
| 40 | |
|---|
| 41 | private STATUS_COLUMNS (String name, int num, int min, int max, int pref) { |
|---|
| 42 | colName = name; |
|---|
| 43 | colNum = num; |
|---|
| 44 | colMinWidth = min; |
|---|
| 45 | colMaxWidth = max; |
|---|
| 46 | colPrefWidth = pref; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | public static int colCount() { |
|---|
| 50 | return values().length; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | public static String columnName(int num) { |
|---|
| 54 | |
|---|
| 55 | for(STATUS_COLUMNS column : STATUS_COLUMNS.values()) { |
|---|
| 56 | if(column.colNum == num) |
|---|
| 57 | return column.colName; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | return ""; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | public static int columnMinWidth(int num) { |
|---|
| 64 | |
|---|
| 65 | for(STATUS_COLUMNS column : STATUS_COLUMNS.values()) { |
|---|
| 66 | if(column.colNum == num) |
|---|
| 67 | return column.colMinWidth; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | return 0; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public static int columnMaxWidth(int num) { |
|---|
| 74 | |
|---|
| 75 | for(STATUS_COLUMNS column : STATUS_COLUMNS.values()) { |
|---|
| 76 | if(column.colNum == num) |
|---|
| 77 | return column.colMaxWidth; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | return 0; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | public static int columnPrefWidth(int num) { |
|---|
| 84 | |
|---|
| 85 | for(STATUS_COLUMNS column : STATUS_COLUMNS.values()) { |
|---|
| 86 | if(column.colNum == num) |
|---|
| 87 | return column.colPrefWidth; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | return 0; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | public static STATUS_COLUMNS fromColNum(int column) { |
|---|
| 94 | for(STATUS_COLUMNS c : STATUS_COLUMNS.values()) { |
|---|
| 95 | if(c.colNum == column) |
|---|
| 96 | return c; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | return TIME_COL; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * Container class to hold information displayed in the table. |
|---|
| 106 | * @author Matthew Cechini |
|---|
| 107 | */ |
|---|
| 108 | private class StatusUpdateInfo { |
|---|
| 109 | |
|---|
| 110 | /** Formatted time text of when the status update was received. */ |
|---|
| 111 | public String time; |
|---|
| 112 | |
|---|
| 113 | /** Text of status update. */ |
|---|
| 114 | public String statusText; |
|---|
| 115 | |
|---|
| 116 | public StatusUpdateInfo(String t, String s) { |
|---|
| 117 | time = t; |
|---|
| 118 | statusText = s; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | /** List of data displayed in the table. */ |
|---|
| 124 | private List<StatusUpdateInfo> status_update_list; |
|---|
| 125 | |
|---|
| 126 | /** DateFormat object used to format time formatted text for display. */ |
|---|
| 127 | private DateFormat timeFormatter; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | /** Constructor. Intialize the DateFormat object. */ |
|---|
| 131 | public DVDStatusTableModel() { |
|---|
| 132 | status_update_list = new ArrayList<StatusUpdateInfo>(); |
|---|
| 133 | timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | public int getRowCount() { |
|---|
| 137 | if(status_update_list == null) { |
|---|
| 138 | return 0; |
|---|
| 139 | } |
|---|
| 140 | else |
|---|
| 141 | return status_update_list.size(); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | public int getColumnCount() { |
|---|
| 145 | return STATUS_COLUMNS.colCount(); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | public int getColumnMinWidth(int c){ |
|---|
| 149 | return STATUS_COLUMNS.columnMinWidth(c); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | public int getColumnMaxWidth(int c){ |
|---|
| 154 | return STATUS_COLUMNS.columnMaxWidth(c); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | public int getColumnPrefWidth(int c){ |
|---|
| 159 | return STATUS_COLUMNS.columnPrefWidth(c); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | public Object getValueAt(int row, int col) { |
|---|
| 163 | switch (STATUS_COLUMNS.fromColNum(col)) { |
|---|
| 164 | case TIME_COL: |
|---|
| 165 | return status_update_list.get(row).time; |
|---|
| 166 | case UPDATE_INFO_COL: |
|---|
| 167 | return status_update_list.get(row).statusText; |
|---|
| 168 | default: |
|---|
| 169 | return ""; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | @SuppressWarnings("unchecked") |
|---|
| 174 | public void setValueAt(Object value, int row, int col) { |
|---|
| 175 | switch (STATUS_COLUMNS.fromColNum(col)) { |
|---|
| 176 | /* |
|---|
| 177 | case NAME_COL: |
|---|
| 178 | reader_list.get(row).readerName; |
|---|
| 179 | case ADDRESS_COL: |
|---|
| 180 | reader_list.get(row).readerAddress; |
|---|
| 181 | case READER_TYPE_COL: |
|---|
| 182 | reader_list.get(row).readerFullType; |
|---|
| 183 | case CONN_TYPE_COL: |
|---|
| 184 | reader_list.get(row).connType; |
|---|
| 185 | default: |
|---|
| 186 | return ""; |
|---|
| 187 | */ |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | public String getColumnName(int col) { |
|---|
| 192 | return STATUS_COLUMNS.columnName(col); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | public Class<?> getColumnClass(int c) { |
|---|
| 196 | return getValueAt(0, c).getClass(); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | public boolean isCellEditable(int row, int col) { |
|---|
| 200 | return false; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * This method adds a new status update to the table. If the parameter |
|---|
| 205 | * update contains an exception, the status text will be "Exception: |
|---|
| 206 | * <Message>". The status text will be "Connected" or "Disconnected" |
|---|
| 207 | * depending on the value of the isConnected data member. The current |
|---|
| 208 | * time is used to time stamp the received update. |
|---|
| 209 | * |
|---|
| 210 | * @param update New update object. |
|---|
| 211 | */ |
|---|
| 212 | public void addStatusUpdate(DVDStatusUpdate update) { |
|---|
| 213 | |
|---|
| 214 | StringBuffer updateBuf = new StringBuffer(); |
|---|
| 215 | |
|---|
| 216 | if(update.exception != null) { |
|---|
| 217 | updateBuf.append("Exception: " + update.exception.getMessage()); |
|---|
| 218 | } |
|---|
| 219 | else if(update.isConnected) { |
|---|
| 220 | updateBuf.append("Connected"); |
|---|
| 221 | } |
|---|
| 222 | else { |
|---|
| 223 | updateBuf.append("Disconnected"); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | status_update_list.add(new StatusUpdateInfo( |
|---|
| 227 | timeFormatter.format(new Date()), |
|---|
| 228 | updateBuf.toString())); |
|---|
| 229 | |
|---|
| 230 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | * Remove all Status Updates from the table's data. |
|---|
| 235 | */ |
|---|
| 236 | public void clearModelData() { |
|---|
| 237 | status_update_list.clear(); |
|---|
| 238 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | } |
|---|