| 1 | package tmcsim.simulationmanager.actions; |
|---|
| 2 | |
|---|
| 3 | import java.awt.event.ActionEvent; |
|---|
| 4 | import java.util.StringTokenizer; |
|---|
| 5 | import java.util.logging.Level; |
|---|
| 6 | import java.util.logging.Logger; |
|---|
| 7 | |
|---|
| 8 | import javax.swing.AbstractAction; |
|---|
| 9 | import javax.swing.JTable; |
|---|
| 10 | |
|---|
| 11 | import tmcsim.common.ScriptException; |
|---|
| 12 | import tmcsim.common.SimulationException; |
|---|
| 13 | import tmcsim.simulationmanager.SimulationManagerView; |
|---|
| 14 | import tmcsim.simulationmanager.model.IncidentTimeSpinnerModel; |
|---|
| 15 | import tmcsim.simulationmanager.model.IncidentListTableModel.INCIDENT_LIST_COLUMNS; |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * RescheduleIncidentAction is an AbstractAction that is used for rescheduling |
|---|
| 20 | * an incident that has been loaded into the simulation, but not started. |
|---|
| 21 | * When the action is performed, the incident selected in the incident list |
|---|
| 22 | * table on the SimulationManagerView is chosen for triggering. If the user has |
|---|
| 23 | * not selected an incident to trigger, an information window is shown to prompt |
|---|
| 24 | * the user to select an incident. If an incident has been selected, the |
|---|
| 25 | * action calls the SimulationManagerModel with the Incident Log Number and |
|---|
| 26 | * the time parsed from the IncidentTimeModel object. |
|---|
| 27 | * <br/> |
|---|
| 28 | * <br/> |
|---|
| 29 | * The INCIDENT_LIST_TABLE key should be assigned the IncidentListTable object |
|---|
| 30 | * from the SimulationManagerView object. |
|---|
| 31 | * <br/> |
|---|
| 32 | * <br/> |
|---|
| 33 | * The INCIDENT_TIME_MODEL key should be assigned the IncidentTimeModel object |
|---|
| 34 | * from the SimulationManagerView object. |
|---|
| 35 | * @author Matthew Cechini |
|---|
| 36 | */ |
|---|
| 37 | @SuppressWarnings("serial") |
|---|
| 38 | public class RescheduleIncidentAction extends AbstractAction { |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * This key string is used to reference the IncidentListTable object |
|---|
| 42 | * from the SimulationManagerView object. |
|---|
| 43 | */ |
|---|
| 44 | public static final String INCIDENT_LIST_TABLE = "INCIDENT_LIST_TABLE"; |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * This key string is used to reference the IncidentTimeModel object |
|---|
| 48 | * from the SimulationManagerView object. |
|---|
| 49 | */ |
|---|
| 50 | public static final String INCIDENT_TIME_MODEL = "INCIDENT_TIME_MODEL"; |
|---|
| 51 | |
|---|
| 52 | /** Reference to the SimulationManagerView object. */ |
|---|
| 53 | private SimulationManagerView theSimManagerView = null; |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Constructor. |
|---|
| 57 | * @param view View class object for the Simulation Manager. |
|---|
| 58 | */ |
|---|
| 59 | public RescheduleIncidentAction(SimulationManagerView view) { |
|---|
| 60 | super("Reschedule"); |
|---|
| 61 | |
|---|
| 62 | theSimManagerView = view; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public void actionPerformed(ActionEvent evt) { |
|---|
| 66 | if(getValue(INCIDENT_LIST_TABLE) == null) { |
|---|
| 67 | Logger.getLogger("tmcsim.simulationmanager.actions").logp( |
|---|
| 68 | Level.WARNING, "RescheduleIncidentAction", |
|---|
| 69 | "actionPerformed", |
|---|
| 70 | "Object for INCIDENT_LIST_TABLE is null."); |
|---|
| 71 | return; |
|---|
| 72 | } |
|---|
| 73 | if(getValue(INCIDENT_TIME_MODEL) == null) { |
|---|
| 74 | Logger.getLogger("tmcsim.simulationmanager.actions").logp( |
|---|
| 75 | Level.WARNING, "RescheduleIncidentAction", |
|---|
| 76 | "actionPerformed", |
|---|
| 77 | "Object for INCIDENT_TIME_MODEL is null."); |
|---|
| 78 | return; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | Runnable reschedRunnable = new Runnable(){ |
|---|
| 82 | public void run() { |
|---|
| 83 | try { |
|---|
| 84 | if(((JTable)getValue(INCIDENT_LIST_TABLE)).getSelectedRowCount() == 0) |
|---|
| 85 | throw new ScriptException("Please select the incident you wish to reschedule."); |
|---|
| 86 | |
|---|
| 87 | int selectedRow = ((JTable)getValue(INCIDENT_LIST_TABLE)).getSelectedRow(); |
|---|
| 88 | Integer incidentLogNum = (Integer)((JTable)getValue(INCIDENT_LIST_TABLE)).getValueAt( |
|---|
| 89 | selectedRow, INCIDENT_LIST_COLUMNS.LOG_NUM_COL.colNum); |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | StringTokenizer strTok = new StringTokenizer( |
|---|
| 93 | (String)((IncidentTimeSpinnerModel)getValue(INCIDENT_TIME_MODEL)).getValue(), ":"); |
|---|
| 94 | |
|---|
| 95 | Long newTime = Long.parseLong(strTok.nextToken()) * 3600 + |
|---|
| 96 | Long.parseLong(strTok.nextToken()) * 60 + |
|---|
| 97 | Long.parseLong(strTok.nextToken()); |
|---|
| 98 | |
|---|
| 99 | theSimManagerView.getModel().rescheduleIncident(newTime, incidentLogNum); |
|---|
| 100 | |
|---|
| 101 | ((JTable)getValue(INCIDENT_LIST_TABLE)).setValueAt(newTime, selectedRow, |
|---|
| 102 | INCIDENT_LIST_COLUMNS.SCHEDULED_COL.colNum); |
|---|
| 103 | |
|---|
| 104 | } |
|---|
| 105 | catch (ScriptException se) { |
|---|
| 106 | theSimManagerView.ScriptExceptionHandler(se); |
|---|
| 107 | } |
|---|
| 108 | catch (SimulationException se) { |
|---|
| 109 | theSimManagerView.SimulationExceptionHandler(se); |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | Thread theThread = new Thread(reschedRunnable); |
|---|
| 115 | theThread.start(); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | } |
|---|