| 1 | package tmcsim.simulationmanager.actions; |
|---|
| 2 | |
|---|
| 3 | import java.awt.event.ActionEvent; |
|---|
| 4 | import java.io.File; |
|---|
| 5 | import java.util.Vector; |
|---|
| 6 | |
|---|
| 7 | import javax.swing.AbstractAction; |
|---|
| 8 | import javax.swing.JFileChooser; |
|---|
| 9 | import javax.swing.JOptionPane; |
|---|
| 10 | import javax.xml.parsers.SAXParserFactory; |
|---|
| 11 | |
|---|
| 12 | import tmcsim.client.cadclientgui.data.Incident; |
|---|
| 13 | import tmcsim.common.ScriptException; |
|---|
| 14 | import tmcsim.client.cadclientgui.ScriptHandler; |
|---|
| 15 | import tmcsim.common.SimulationException; |
|---|
| 16 | import tmcsim.common.CADEnums.SCRIPT_STATUS; |
|---|
| 17 | import tmcsim.simulationmanager.SimulationManager; |
|---|
| 18 | import tmcsim.simulationmanager.SimulationManagerView; |
|---|
| 19 | import tmcsim.simulationmanager.dialogs.AddIncidentDialog; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * AddIncidentAction is an AbstractAction that is used for adding an incident |
|---|
| 24 | * into the current simulation. When the action is performed, a file chooser |
|---|
| 25 | * is shown and the user is prompted to select which file they want to load |
|---|
| 26 | * script incidents from. If the user selects a file, the script file is parsed |
|---|
| 27 | * and available incidents are displayed in the AddIncidentDialog. When the dialog |
|---|
| 28 | * window is closed, the action checks for selected incidents. If a selected incident |
|---|
| 29 | * has a conflicting log number with a current incident, or if the scheduled time |
|---|
| 30 | * is prior to the current simulation time, the dialog is reshown with an error. |
|---|
| 31 | * Once the user has selected 0 or more incidents that can be added to the |
|---|
| 32 | * simulation, the SimulationManagerModel is called with the new incidents to add. |
|---|
| 33 | * If incidents were added and the simulation has not been started, the |
|---|
| 34 | * ScriptStatus is set to SCRIPT_STOPPED_NOT_STARTED. |
|---|
| 35 | */ |
|---|
| 36 | @SuppressWarnings("serial") |
|---|
| 37 | public class AddIncidentAction extends AbstractAction { |
|---|
| 38 | |
|---|
| 39 | /** Reference to the SimulationManagerView object. */ |
|---|
| 40 | private SimulationManagerView theSimManagerView = null; |
|---|
| 41 | |
|---|
| 42 | /** AddIncidentDialog used for adding new incidents into the simulation. */ |
|---|
| 43 | private AddIncidentDialog theAddIncidentDialog; |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Constructor. |
|---|
| 47 | * @param view View class object for the Simulation Manager. |
|---|
| 48 | */ |
|---|
| 49 | public AddIncidentAction(SimulationManagerView view) { |
|---|
| 50 | super("Add New Incident"); |
|---|
| 51 | |
|---|
| 52 | theSimManagerView = view; |
|---|
| 53 | |
|---|
| 54 | theAddIncidentDialog = new AddIncidentDialog( |
|---|
| 55 | theSimManagerView); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | public void actionPerformed(ActionEvent evt) { |
|---|
| 59 | Runnable addRunnable = new Runnable(){ |
|---|
| 60 | public void run() { |
|---|
| 61 | try{ |
|---|
| 62 | JFileChooser chooser = new JFileChooser( |
|---|
| 63 | SimulationManager.SCENARIOS_DIR); |
|---|
| 64 | |
|---|
| 65 | chooser.setDialogTitle("Open Simulation Script File"); |
|---|
| 66 | chooser.setMultiSelectionEnabled(false); |
|---|
| 67 | |
|---|
| 68 | if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { |
|---|
| 69 | |
|---|
| 70 | File selectedFile = chooser.getSelectedFile(); |
|---|
| 71 | ScriptHandler sh = null; |
|---|
| 72 | |
|---|
| 73 | try { |
|---|
| 74 | sh = new ScriptHandler(); |
|---|
| 75 | SAXParserFactory.newInstance().newSAXParser().parse(selectedFile, sh); |
|---|
| 76 | |
|---|
| 77 | } catch (Exception e) { |
|---|
| 78 | throw new SimulationException(SimulationException.INVALID_SCRIPT_FILE); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | boolean incidentsAdded = false; |
|---|
| 82 | boolean incidentsValidated = true; |
|---|
| 83 | Vector<Incident> currIncidents = theSimManagerView.getModel().getIncidentList(); |
|---|
| 84 | Vector<Incident> parsedIncidents = sh.getIncidents(); |
|---|
| 85 | Vector<Integer> duplicateIncNum = new Vector<Integer>(); |
|---|
| 86 | Vector<Integer> invalidIncTime = new Vector<Integer>(); |
|---|
| 87 | |
|---|
| 88 | //Show the dialog with the initialized list of incidents. |
|---|
| 89 | theAddIncidentDialog.setModelData(parsedIncidents); |
|---|
| 90 | |
|---|
| 91 | //Loop until the user selects a list(may be empty) of valid incidents. |
|---|
| 92 | do { |
|---|
| 93 | incidentsValidated = true; |
|---|
| 94 | theAddIncidentDialog.showDialog(); |
|---|
| 95 | |
|---|
| 96 | duplicateIncNum.clear(); |
|---|
| 97 | invalidIncTime.clear(); |
|---|
| 98 | |
|---|
| 99 | //Validate the selected incidents. Validation fails if: |
|---|
| 100 | // + A selected incident's log number matches a log number already in the simulation. |
|---|
| 101 | // + A selected incident has been scheduled to occur at a time that has passed in the simulation. |
|---|
| 102 | for(Integer incidentNum : theAddIncidentDialog.getSelectedIncidentTimes().keySet()) { |
|---|
| 103 | |
|---|
| 104 | for(Incident inc : currIncidents) { |
|---|
| 105 | if(inc.logNum.equals(incidentNum)) { |
|---|
| 106 | duplicateIncNum.add(incidentNum); |
|---|
| 107 | incidentsValidated = false; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | long incSchedTime = theAddIncidentDialog.getSelectedIncidentTimes().get(incidentNum); |
|---|
| 112 | if(incSchedTime < theSimManagerView.getCurrentSimTime()) |
|---|
| 113 | { |
|---|
| 114 | invalidIncTime.add(incidentNum); |
|---|
| 115 | incidentsValidated = false; |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | if(duplicateIncNum.size() > 0) { |
|---|
| 120 | JOptionPane.showMessageDialog(null, |
|---|
| 121 | "Duplicate incidents selected: " + |
|---|
| 122 | duplicateIncNum.toString().substring(1, |
|---|
| 123 | duplicateIncNum.toString().length()-1), |
|---|
| 124 | "Error", |
|---|
| 125 | JOptionPane.ERROR_MESSAGE); |
|---|
| 126 | } |
|---|
| 127 | else if(invalidIncTime.size() > 0) { |
|---|
| 128 | JOptionPane.showMessageDialog(null, |
|---|
| 129 | "Simulation time already passed for incidents: " + |
|---|
| 130 | invalidIncTime.toString().substring(1, |
|---|
| 131 | invalidIncTime.toString().length()-1), |
|---|
| 132 | "Error", |
|---|
| 133 | JOptionPane.ERROR_MESSAGE); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | } while(!incidentsValidated); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | //Loop through all selected incidents, set the new start time, and add the Incident |
|---|
| 140 | //to simulation. |
|---|
| 141 | for(Integer incidentNum : theAddIncidentDialog.getSelectedIncidentTimes().keySet()) { |
|---|
| 142 | for(Incident inc : parsedIncidents) { |
|---|
| 143 | if(inc.logNum.equals(incidentNum)) { |
|---|
| 144 | incidentsAdded = true; |
|---|
| 145 | inc.setSecondsToStart(theAddIncidentDialog |
|---|
| 146 | .getSelectedIncidentTimes().get(incidentNum)); |
|---|
| 147 | |
|---|
| 148 | theSimManagerView.getModel().addIncident(inc); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | if(incidentsAdded && !theSimManagerView.isSimulationStarted()) |
|---|
| 155 | { |
|---|
| 156 | theSimManagerView.setScriptStatus( |
|---|
| 157 | SCRIPT_STATUS.SCRIPT_STOPPED_NOT_STARTED); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | catch (SimulationException se) { |
|---|
| 162 | theSimManagerView.SimulationExceptionHandler(se); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | }; |
|---|
| 166 | |
|---|
| 167 | Thread theThread = new Thread(addRunnable); |
|---|
| 168 | theThread.start(); |
|---|
| 169 | |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | } |
|---|