| 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 | |
|---|
| 9 | import javax.swing.event.TableModelEvent; |
|---|
| 10 | import javax.swing.table.DefaultTableModel; |
|---|
| 11 | |
|---|
| 12 | import tmcsim.cadmodels.CMSDiversion; |
|---|
| 13 | import tmcsim.cadmodels.CMSInfo; |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * AppliedDiversionTableModel is a DefaultTableModel used to display the |
|---|
| 17 | * list of current diversions in the Simulation Manager. The columns in |
|---|
| 18 | * this table show the divesion cms id, old path, new path, diversion pct, |
|---|
| 19 | * and simulation time it was applied. |
|---|
| 20 | * |
|---|
| 21 | * @author Matthew Cechini |
|---|
| 22 | * @version |
|---|
| 23 | */ |
|---|
| 24 | @SuppressWarnings("serial") |
|---|
| 25 | public class AppliedDiversionTableModel extends DefaultTableModel implements |
|---|
| 26 | Comparator<AppliedDiversionTableItem> { |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * Enumeration of columns for this table. |
|---|
| 30 | * @author Matthew Cechini |
|---|
| 31 | */ |
|---|
| 32 | public static enum APPLIED_DIV_COLUMNS { |
|---|
| 33 | CMS_ID_COL ("ID", 0, 140, 280, 180), |
|---|
| 34 | OLD_PATH_COL ("Old Path", 1, 70, 120, 70), |
|---|
| 35 | NEW_PATH_COL ("New Path", 2, 70, 120, 70), |
|---|
| 36 | PERCENT_COL ("%", 3, 30, 60, 30), |
|---|
| 37 | APPLIED_COL ("Applied", 4, 70, 120, 70); |
|---|
| 38 | |
|---|
| 39 | public String colName; |
|---|
| 40 | public int colNum; |
|---|
| 41 | public int colMinWidth; |
|---|
| 42 | public int colMaxWidth; |
|---|
| 43 | public int colPrefWidth; |
|---|
| 44 | |
|---|
| 45 | private APPLIED_DIV_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(APPLIED_DIV_COLUMNS column : APPLIED_DIV_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(APPLIED_DIV_COLUMNS column : APPLIED_DIV_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(APPLIED_DIV_COLUMNS column : APPLIED_DIV_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(APPLIED_DIV_COLUMNS column : APPLIED_DIV_COLUMNS.values()) { |
|---|
| 90 | if(column.colNum == num) |
|---|
| 91 | return column.colPrefWidth; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | return 0; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | public static APPLIED_DIV_COLUMNS fromColNum(int column) { |
|---|
| 98 | for(APPLIED_DIV_COLUMNS c : APPLIED_DIV_COLUMNS.values()) { |
|---|
| 99 | if(c.colNum == column) |
|---|
| 100 | return c; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | return CMS_ID_COL; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | /** List of data displayed in the table. */ |
|---|
| 109 | private List<AppliedDiversionTableItem> diversion_list; |
|---|
| 110 | |
|---|
| 111 | /** Constructor. */ |
|---|
| 112 | public AppliedDiversionTableModel() { |
|---|
| 113 | diversion_list = new ArrayList<AppliedDiversionTableItem>(); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | public String getColumnName(int col) { |
|---|
| 117 | return APPLIED_DIV_COLUMNS.columnName(col); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | public int getRowCount() { |
|---|
| 121 | if(diversion_list == null) { |
|---|
| 122 | return 0; |
|---|
| 123 | } |
|---|
| 124 | else |
|---|
| 125 | return diversion_list.size(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | public int getColumnCount() { |
|---|
| 129 | return APPLIED_DIV_COLUMNS.colCount(); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | public int getColumnMinWidth(int c){ |
|---|
| 133 | return APPLIED_DIV_COLUMNS.columnMinWidth(c); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | public int getColumnMaxWidth(int c){ |
|---|
| 137 | return APPLIED_DIV_COLUMNS.columnMaxWidth(c); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public int getColumnPrefWidth(int c){ |
|---|
| 141 | return APPLIED_DIV_COLUMNS.columnPrefWidth(c); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | public boolean isCellEditable(int row, int col) { |
|---|
| 145 | return false; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | public Object getValueAt(int row, int col) { |
|---|
| 149 | switch (APPLIED_DIV_COLUMNS.fromColNum(col)) { |
|---|
| 150 | case APPLIED_COL: |
|---|
| 151 | return diversion_list.get(row).timeApplied; |
|---|
| 152 | case NEW_PATH_COL: |
|---|
| 153 | return diversion_list.get(row).newPath; |
|---|
| 154 | case CMS_ID_COL: |
|---|
| 155 | return diversion_list.get(row).cmsID; |
|---|
| 156 | case OLD_PATH_COL: |
|---|
| 157 | return diversion_list.get(row).originalPath; |
|---|
| 158 | case PERCENT_COL: |
|---|
| 159 | return diversion_list.get(row).currentDiv; |
|---|
| 160 | default: |
|---|
| 161 | return ""; |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | public int compare(AppliedDiversionTableItem item0, AppliedDiversionTableItem item1) { |
|---|
| 166 | return item0.compareTo(item1); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * Find the insertion point for the argument in a sorted list. |
|---|
| 171 | * |
|---|
| 172 | * @param element find this object's insertion point in the sorted list |
|---|
| 173 | * @return the index of the insertion point |
|---|
| 174 | */ |
|---|
| 175 | private int findInsertionPoint(AppliedDiversionTableItem element) { |
|---|
| 176 | |
|---|
| 177 | // Find the new element's insertion point. |
|---|
| 178 | int insertionPoint = Collections.binarySearch(diversion_list, element, this); |
|---|
| 179 | if (insertionPoint < 0) { |
|---|
| 180 | insertionPoint = -(insertionPoint + 1); |
|---|
| 181 | } |
|---|
| 182 | return insertionPoint; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /** |
|---|
| 186 | * Method adds a new diversion into the table. |
|---|
| 187 | * @param theCMSInfo CMSInfo that the diversion is set for. |
|---|
| 188 | * @param theDiversion The new diversion data. |
|---|
| 189 | */ |
|---|
| 190 | public void addDiversion(CMSInfo theCMSInfo, CMSDiversion theDiversion) { |
|---|
| 191 | |
|---|
| 192 | AppliedDiversionTableItem newDiv = new AppliedDiversionTableItem( |
|---|
| 193 | theCMSInfo.cmsID, |
|---|
| 194 | theDiversion.originalPath, |
|---|
| 195 | theDiversion.newPath, |
|---|
| 196 | theDiversion.getCurrDiv(), |
|---|
| 197 | theDiversion.timeApplied); |
|---|
| 198 | |
|---|
| 199 | diversion_list.add(findInsertionPoint(newDiv), newDiv); |
|---|
| 200 | |
|---|
| 201 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 202 | |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * Method removes a diversion from the table. |
|---|
| 207 | * @param theCMSInfo CMSInfo that the diversion is set for. |
|---|
| 208 | * @param theDiversion The new diversion data. |
|---|
| 209 | */ |
|---|
| 210 | public void removeDiversion(CMSInfo theCMSInfo, CMSDiversion theDiversion) { |
|---|
| 211 | |
|---|
| 212 | //loop through all rows. Move down a row until the cmsID for the current diversion |
|---|
| 213 | //is greater ("04 - CMS 4" > "03 - CMS 3") than the current row, or the matching diversion row is found |
|---|
| 214 | //If the matching row is found, delete the current row for the new info will be added below. |
|---|
| 215 | |
|---|
| 216 | AppliedDiversionTableItem divInfo = null; |
|---|
| 217 | for(Iterator<AppliedDiversionTableItem> divInfoIter = diversion_list.iterator(); |
|---|
| 218 | divInfoIter.hasNext();) { |
|---|
| 219 | |
|---|
| 220 | divInfo = divInfoIter.next(); |
|---|
| 221 | |
|---|
| 222 | if(divInfo.cmsID.equals(theCMSInfo.cmsID)) { |
|---|
| 223 | if(divInfo.originalPath.equals(theDiversion.originalPath) && |
|---|
| 224 | divInfo.newPath.equals(theDiversion.newPath)) { |
|---|
| 225 | divInfoIter.remove(); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | * Remove all diversions from the table's data. |
|---|
| 235 | */ |
|---|
| 236 | public void clearModelData() { |
|---|
| 237 | diversion_list.clear(); |
|---|
| 238 | fireTableChanged(new TableModelEvent(this)); |
|---|
| 239 | } |
|---|
| 240 | } |
|---|