| 1 | package tmcsim.simulationmanager.model; |
|---|
| 2 | |
|---|
| 3 | import java.util.ArrayList; |
|---|
| 4 | import java.util.Collections; |
|---|
| 5 | import java.util.Comparator; |
|---|
| 6 | import java.util.Iterator; |
|---|
| 7 | import java.util.List; |
|---|
| 8 | import java.util.Vector; |
|---|
| 9 | |
|---|
| 10 | import javax.swing.event.TableModelEvent; |
|---|
| 11 | import javax.swing.table.DefaultTableModel; |
|---|
| 12 | |
|---|
| 13 | import tmcsim.client.cadclientgui.data.Incident; |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * IncidentListTableModel is a DefaultTableModel used to display the |
|---|
| 17 | * list of current incidents in the Simulation Manager. The columns in |
|---|
| 18 | * this table show the incident log number, it's schedule time, and the |
|---|
| 19 | * incident description. The startIncident() method is used to set |
|---|
| 20 | * an incident in the table to 'started.' See the method description for |
|---|
| 21 | * more information. |
|---|
| 22 | * |
|---|
| 23 | * @author Matthew Cechini |
|---|
| 24 | * @version |
|---|
| 25 | */ |
|---|
| 26 | @SuppressWarnings("serial") |
|---|
| 27 | public class IncidentListTableModel extends DefaultTableModel implements |
|---|
| 28 | Comparator<IncidentListTableItem> { |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Enumeration of columns for this table. |
|---|
| 32 | * @author Matthew Cechini |
|---|
| 33 | */ |
|---|
| 34 | public static enum INCIDENT_LIST_COLUMNS { |
|---|
| 35 | LOG_NUM_COL ("Log #", 0, 70, 100, 70), |
|---|
| 36 | SCHEDULED_COL ("Scheduled", 1, 50, 80, 80), |
|---|
| 37 | INC_DESC_COL ("Incident Description", 2, 130, 460, 200); |
|---|
| 38 | |
|---|
| 39 | public String colName; |
|---|
| 40 | public int colNum; |
|---|
| 41 | public int colMinWidth; |
|---|
| 42 | public int colMaxWidth; |
|---|
| 43 | public int colPrefWidth; |
|---|
| 44 | |
|---|
| 45 | private INCIDENT_LIST_COLUMNS (String name, int num, int min, int max, int pref) { |
|---|
| 46 | colName = name; |
|---|
| 47 | colNum = num; |
|---|
| 48 | colMinWidth = min; |
|---|
| 49 | colMaxWidth = max; |
|---|
| 50 | colPrefWidth = pref; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | public static int colCount() { |
|---|
| 54 | return values().length; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | public static String columnName(int num) { |
|---|
| 58 | |
|---|
| 59 | for(INCIDENT_LIST_COLUMNS column : INCIDENT_LIST_COLUMNS.values()) { |
|---|
| 60 | if(column.colNum == num) |
|---|
| 61 | return column.colName; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | return ""; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public static int columnMinWidth(int num) { |
|---|
| 68 | |
|---|
| 69 | for(INCIDENT_LIST_COLUMNS column : INCIDENT_LIST_COLUMNS.values()) { |
|---|
| 70 | if(column.colNum == num) |
|---|
| 71 | return column.colMinWidth; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | return 0; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public static int columnMaxWidth(int num) { |
|---|
| 78 | |
|---|
| 79 | for(INCIDENT_LIST_COLUMNS column : INCIDENT_LIST_COLUMNS.values()) { |
|---|
| 80 | if(column.colNum == num) |
|---|
| 81 | return column.colMaxWidth; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | return 0; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | public static int columnPrefWidth(int num) { |
|---|
| 88 | |
|---|
| 89 | for(INCIDENT_LIST_COLUMNS column : INCIDENT_LIST_COLUMNS.values()) { |
|---|
| 90 | if(column.colNum == num) |
|---|
| 91 | return column.colPrefWidth; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | return 0; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | public static INCIDENT_LIST_COLUMNS fromColNum(int column) { |
|---|
| 98 | for(INCIDENT_LIST_COLUMNS c : INCIDENT_LIST_COLUMNS.values()) { |
|---|
| 99 | if(c.colNum == column) |
|---|
| 100 | return c; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | return LOG_NUM_COL; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | /** List of data displayed in the table. */ |
|---|
| 109 | private List<IncidentListTableItem> event_list; |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Constructor. |
|---|
| 113 | */ |
|---|
| 114 | public IncidentListTableModel() { |
|---|
| 115 | event_list = new ArrayList<IncidentListTableItem>(); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | public String getColumnName(int col) { |
|---|
| 119 | return INCIDENT_LIST_COLUMNS.columnName(col); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | public int getRowCount() { |
|---|
| 123 | if(event_list == null) { |
|---|
| 124 | return 0; |
|---|
| 125 | } |
|---|
| 126 | else |
|---|
| 127 | return event_list.size(); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | public int getColumnCount() { |
|---|
| 131 | return INCIDENT_LIST_COLUMNS.colCount(); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | public int getColumnMinWidth(int c){ |
|---|
| 136 | return INCIDENT_LIST_COLUMNS.columnMinWidth(c); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | public int getColumnMaxWidth(int c){ |
|---|
| 141 | return INCIDENT_LIST_COLUMNS.columnMaxWidth(c); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | public int getColumnPrefWidth(int c){ |
|---|
| 146 | return INCIDENT_LIST_COLUMNS.columnPrefWidth(c); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | public boolean isCellEditable(int row, int col) { |
|---|
| 150 | return false; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | public Object getValueAt(int row, int col) { |
|---|
| 154 | switch (INCIDENT_LIST_COLUMNS.fromColNum(col)) { |
|---|
| 155 | case INC_DESC_COL: |
|---|
| 156 | return event_list.get(row).description; |
|---|
| 157 | case LOG_NUM_COL: |
|---|
| 158 | return event_list.get(row).logNumber; |
|---|
| 159 | case SCHEDULED_COL: |
|---|
| 160 | return event_list.get(row).scheduleTime; |
|---|
| 161 | default: |
|---|
| 162 | return ""; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | public void setValueAt(Object arg, int row, int col) { |
|---|
| 168 | switch (INCIDENT_LIST_COLUMNS.fromColNum(col)) { |
|---|
| 169 | case INC_DESC_COL: |
|---|
| 170 | event_list.get(row).description = (String)arg; |
|---|
| 171 | break; |
|---|
| 172 | case LOG_NUM_COL: |
|---|
| 173 | event_list.get(row).logNumber = (Integer)arg; |
|---|
| 174 | break; |
|---|
| 175 | case SCHEDULED_COL: |
|---|
| 176 | event_list.get(row).scheduleTime = (Long)arg; |
|---|
| 177 | break; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | public int compare(IncidentListTableItem item0, IncidentListTableItem item1) { |
|---|
| 184 | return item0.compareTo(item1); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * Find the insertion point for the argument in a sorted list. |
|---|
| 189 | * |
|---|
| 190 | * @param element find this object's insertion point in the sorted list |
|---|
| 191 | * @return the index of the insertion point |
|---|
| 192 | */ |
|---|
| 193 | private int findInsertionPoint(IncidentListTableItem element) { |
|---|
| 194 | |
|---|
| 195 | // Find the new element's insertion point. |
|---|
| 196 | int insertionPoint = Collections.binarySearch(event_list, element, this); |
|---|
| 197 | if (insertionPoint < 0) { |
|---|
| 198 | insertionPoint = -(insertionPoint + 1); |
|---|
| 199 | } |
|---|
| 200 | return insertionPoint; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * Method 'starts' the incident from the table whose log number |
|---|
| 205 | * matches the parameter value. When an incident is 'started,' its |
|---|
| 206 | * scheduled time is set to -1. This specific value is used by |
|---|
| 207 | * the CellRenderer to display "Started" in the table. |
|---|
| 208 | * @param logNumber Unique log number of an incident to 'start.' |
|---|
| 209 | */ |
|---|
| 210 | public void startIncident(Integer logNumber) { |
|---|
| 211 | for(IncidentListTableItem eventInfo : event_list) { |
|---|
| 212 | if(eventInfo.logNumber.equals(logNumber)) { |
|---|
| 213 | eventInfo.scheduleTime = -1L; |
|---|
| 214 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /** |
|---|
| 220 | * Method adds a new incident into the table. |
|---|
| 221 | * @param newIncident Incident object to add. |
|---|
| 222 | */ |
|---|
| 223 | public void addIncident(Incident newIncident) { |
|---|
| 224 | |
|---|
| 225 | IncidentListTableItem newItem = new IncidentListTableItem( |
|---|
| 226 | newIncident.getLogNumber(), |
|---|
| 227 | newIncident.getSecondsToStart(), |
|---|
| 228 | newIncident.description); |
|---|
| 229 | |
|---|
| 230 | event_list.add(findInsertionPoint(newItem), newItem); |
|---|
| 231 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | /** |
|---|
| 235 | * Method removes the incident from the table whose log number |
|---|
| 236 | * matches the parameter value. |
|---|
| 237 | * @param logNumber Unique log number of incident to remove. |
|---|
| 238 | */ |
|---|
| 239 | public void removeIncident(Integer logNumber) { |
|---|
| 240 | for(Iterator<IncidentListTableItem> eventInfoIter = event_list.iterator(); |
|---|
| 241 | eventInfoIter.hasNext();) { |
|---|
| 242 | |
|---|
| 243 | if(eventInfoIter.next().logNumber.equals(logNumber)) { |
|---|
| 244 | eventInfoIter.remove(); |
|---|
| 245 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | /** |
|---|
| 251 | * Remove all incidents from the table's data. |
|---|
| 252 | */ |
|---|
| 253 | public void clearModelData() { |
|---|
| 254 | event_list.clear(); |
|---|
| 255 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | } |
|---|