| 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.DVDTitleUpdate; |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * DVDStatusTableModel extends from DefaultTableModel to display |
|---|
| 15 | * DVDStatusUpdates that are received from a DVDController. The |
|---|
| 16 | * addStatusUpdate() method is used to add new update objects. |
|---|
| 17 | * The table may be cleared through the clearModelData() method. |
|---|
| 18 | * |
|---|
| 19 | * @author Matthew |
|---|
| 20 | * @version |
|---|
| 21 | */ |
|---|
| 22 | @SuppressWarnings("serial") |
|---|
| 23 | public class DVDTitleTableModel extends DefaultTableModel { |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Enumeration containing column information for all columns shown in the |
|---|
| 28 | * DVDTitle table. |
|---|
| 29 | * @author Matthew Cechini |
|---|
| 30 | */ |
|---|
| 31 | public static enum TITLE_COLUMNS { |
|---|
| 32 | TIME_COL ("Time", 0, 60, 100, 80), |
|---|
| 33 | UPDATE_INFO_COL ("Title 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 TITLE_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(TITLE_COLUMNS column : TITLE_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(TITLE_COLUMNS column : TITLE_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(TITLE_COLUMNS column : TITLE_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(TITLE_COLUMNS column : TITLE_COLUMNS.values()) { |
|---|
| 86 | if(column.colNum == num) |
|---|
| 87 | return column.colPrefWidth; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | return 0; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | public static TITLE_COLUMNS fromColNum(int column) { |
|---|
| 94 | for(TITLE_COLUMNS c : TITLE_COLUMNS.values()) { |
|---|
| 95 | if(c.colNum == column) |
|---|
| 96 | return c; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | return TIME_COL; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Container class to hold information displayed in the table. |
|---|
| 105 | * @author Matthew Cechini |
|---|
| 106 | */ |
|---|
| 107 | private class TitleUpdateInfo { |
|---|
| 108 | |
|---|
| 109 | /** Formatted time text of when the title update was received. */ |
|---|
| 110 | public String time; |
|---|
| 111 | |
|---|
| 112 | /** Text for title update. */ |
|---|
| 113 | public String titleUpdate; |
|---|
| 114 | |
|---|
| 115 | public TitleUpdateInfo(String t, String u) { |
|---|
| 116 | time = t; |
|---|
| 117 | titleUpdate = u; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** List of data displayed in the table. */ |
|---|
| 122 | private List<TitleUpdateInfo> title_update_list; |
|---|
| 123 | |
|---|
| 124 | /** DateFormat object used to format time formatted text for display. */ |
|---|
| 125 | private DateFormat timeFormatter; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | /** Constructor. Intialize the DateFormat object. */ |
|---|
| 129 | public DVDTitleTableModel() { |
|---|
| 130 | title_update_list = new ArrayList<TitleUpdateInfo>(); |
|---|
| 131 | timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | public int getRowCount() { |
|---|
| 135 | if(title_update_list == null) { |
|---|
| 136 | return 0; |
|---|
| 137 | } |
|---|
| 138 | else |
|---|
| 139 | return title_update_list.size(); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | public int getColumnCount() { |
|---|
| 143 | return TITLE_COLUMNS.colCount(); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | public int getColumnMinWidth(int c){ |
|---|
| 147 | return TITLE_COLUMNS.columnMinWidth(c); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | public int getColumnMaxWidth(int c){ |
|---|
| 152 | return TITLE_COLUMNS.columnMaxWidth(c); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | public int getColumnPrefWidth(int c){ |
|---|
| 157 | return TITLE_COLUMNS.columnPrefWidth(c); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | public Object getValueAt(int row, int col) { |
|---|
| 162 | switch (TITLE_COLUMNS.fromColNum(col)) { |
|---|
| 163 | case TIME_COL: |
|---|
| 164 | return title_update_list.get(row).time; |
|---|
| 165 | case UPDATE_INFO_COL: |
|---|
| 166 | return title_update_list.get(row).titleUpdate; |
|---|
| 167 | default: |
|---|
| 168 | return ""; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | @SuppressWarnings("unchecked") |
|---|
| 173 | public void setValueAt(Object value, int row, int col) { |
|---|
| 174 | switch (TITLE_COLUMNS.fromColNum(col)) { |
|---|
| 175 | /* |
|---|
| 176 | case NAME_COL: |
|---|
| 177 | reader_list.get(row).readerName; |
|---|
| 178 | case ADDRESS_COL: |
|---|
| 179 | reader_list.get(row).readerAddress; |
|---|
| 180 | case READER_TYPE_COL: |
|---|
| 181 | reader_list.get(row).readerFullType; |
|---|
| 182 | case CONN_TYPE_COL: |
|---|
| 183 | reader_list.get(row).connType; |
|---|
| 184 | default: |
|---|
| 185 | return ""; |
|---|
| 186 | */ |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | public String getColumnName(int col) { |
|---|
| 191 | return TITLE_COLUMNS.columnName(col); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | public Class<?> getColumnClass(int c) { |
|---|
| 195 | return getValueAt(0, c).getClass(); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | public boolean isCellEditable(int row, int col) { |
|---|
| 199 | return false; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /** |
|---|
| 203 | * This method adds a new title update to the table. If the title has |
|---|
| 204 | * been repeated, the text will be "Repeating title #<Number> <Addl Info>". |
|---|
| 205 | * If the title is being played for the first time, the text will be |
|---|
| 206 | * "Playing title #<Number> <Addl Info>". If the title is being played |
|---|
| 207 | * for an incident, the <Addl Info> will be "for Incident #<Number>". |
|---|
| 208 | * Else, the title is being played for a speed range and the <Addl Info> |
|---|
| 209 | * will be "for Range <Min Speed> - <Max Speed> mph (<duration> secs)". |
|---|
| 210 | * |
|---|
| 211 | * @param update New update object. |
|---|
| 212 | */ |
|---|
| 213 | public void addTitleUpdate(DVDTitleUpdate update) { |
|---|
| 214 | |
|---|
| 215 | StringBuffer updateBuf = new StringBuffer(); |
|---|
| 216 | |
|---|
| 217 | if(update.isRepeat) |
|---|
| 218 | updateBuf.append("Repeating title #"); |
|---|
| 219 | else |
|---|
| 220 | updateBuf.append("Playing title #"); |
|---|
| 221 | |
|---|
| 222 | if(update.isPlayingIncident) { |
|---|
| 223 | updateBuf.append(update.currentIncident.dvdTitle + " for Incident #"); |
|---|
| 224 | updateBuf.append(update.currentIncident.incidentNumber); |
|---|
| 225 | } |
|---|
| 226 | else { |
|---|
| 227 | updateBuf.append(update.currentRange.dvdTitle + " for Range "); |
|---|
| 228 | updateBuf.append(update.currentRange.minSpeed + " - "); |
|---|
| 229 | updateBuf.append(update.currentRange.maxSpeed + " mph"); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | title_update_list.add(new TitleUpdateInfo( |
|---|
| 233 | timeFormatter.format(new Date()), |
|---|
| 234 | updateBuf.toString())); |
|---|
| 235 | |
|---|
| 236 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | /** |
|---|
| 240 | * Remove all Status Updates from the table's data. |
|---|
| 241 | */ |
|---|
| 242 | public void clearModelData() { |
|---|
| 243 | title_update_list.clear(); |
|---|
| 244 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | } |
|---|