Changeset 416 in tmcsimulator for trunk/src/tmcsim/simulationmanager/actions
- Timestamp:
- 06/18/2019 10:56:55 AM (7 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/tmcsim/simulationmanager/actions/ExportAction.java
r2 r416 4 4 import java.io.File; 5 5 import java.util.Vector; 6 import java.util.logging.Level; 7 import java.util.logging.Logger; 6 8 7 9 import javax.swing.AbstractAction; 8 10 import javax.swing.JFileChooser; 9 11 import javax.swing.JOptionPane; 12 import javax.swing.table.DefaultTableModel; 10 13 import javax.xml.parsers.SAXParserFactory; 11 14 … … 20 23 21 24 /** 22 * AddIncidentAction is an AbstractAction that is used for adding an incident 23 * into the current simulation. When the action is performed, a file chooser 24 * is shown and the user is prompted to select which file they want to load 25 * script incidents from. If the user selects a file, the script file is parsed 26 * and available incidents are displayed in the AddIncidentDialog. When the dialog 27 * window is closed, the action checks for selected incidents. If a selected incident 28 * has a conflicting log number with a current incident, or if the scheduled time 29 * is prior to the current simulation time, the dialog is reshown with an error. 30 * Once the user has selected 0 or more incidents that can be added to the 31 * simulation, the SimulationManagerModel is called with the new incidents to add. 32 * If incidents were added and the simulation has not been started, the 33 * ScriptStatus is set to SCRIPT_STOPPED_NOT_STARTED. 25 * ExportAction is an AbstractAction that exports Incident data from the 26 * current simulation. (Initially, just shows data in popup window). 27 * @author jdalbey 34 28 */ 35 29 @SuppressWarnings("serial") 36 public class AddIncidentAction extends AbstractAction {30 public class ExportAction extends AbstractAction { 37 31 38 32 /** Reference to the SimulationManagerView object. */ 39 33 private SimulationManagerView theSimManagerView = null; 40 41 /** AddIncidentDialog used for adding new incidents into the simulation. */42 private AddIncidentDialog theAddIncidentDialog;43 34 44 35 /** … … 46 37 * @param view View class object for the Simulation Manager. 47 38 */ 48 public AddIncidentAction(SimulationManagerView view) { 49 super("Add New Incident"); 50 39 public ExportAction(SimulationManagerView view) { 40 super("Export Incident Notes"); 51 41 theSimManagerView = view; 52 53 theAddIncidentDialog = new AddIncidentDialog(54 theSimManagerView);55 42 } 56 57 public void actionPerformed(ActionEvent evt) { 58 Runnable addRunnable = new Runnable(){ 59 public void run() { 60 try{ 61 JFileChooser chooser = new JFileChooser( 62 SimulationManagerView.SCRIPT_DIR); 43 /** Perform the action of exporting. */ 44 public void actionPerformed(ActionEvent evt) 45 { 46 /** Create a process that can run in a separate thread so the 47 * simulation can get back to work. 48 */ 49 Runnable addRunnable = new Runnable() 50 { 51 public void run() 52 { 53 try { 54 // Retrieve the current incidents from the simulation mgr. 55 Vector<Incident> currIncidents = theSimManagerView.getModel().getIncidentList(); 63 56 64 chooser.setDialogTitle("Open Simulation Script File"); 65 chooser.setMultiSelectionEnabled(false); 66 67 if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 68 69 File selectedFile = chooser.getSelectedFile(); 70 ScriptHandler sh = null; 71 72 try { 73 sh = new ScriptHandler(); 74 SAXParserFactory.newInstance().newSAXParser().parse(selectedFile, sh); 75 76 } catch (Exception e) { 77 throw new SimulationException(SimulationException.INVALID_SCRIPT_FILE); 57 StringBuffer sb = new StringBuffer(); 58 // For each incident in the list 59 for (Incident inc : currIncidents) 60 { 61 sb.append("Incident # " + inc.logNum + "\n"); 62 // Retrieve the table of comments/notes the users created 63 DefaultTableModel notesTable = inc.getCommentsNotesTable(); 64 // Retrieve the notes chronologically (Most recent is in first row) 65 for (int row=notesTable.getRowCount()-1; row >=0; row--) 66 { 67 // Combine the fields into one export entry 68 sb.append(notesTable.getValueAt(row,1) + " "); // time 69 String initials = (String) notesTable.getValueAt(row,2); // initials 70 // If there are no user intials, it's a scripted item 71 if (initials.length() == 0) 72 { 73 initials = "Script"; 74 } 75 sb.append(initials + " "); 76 sb.append(notesTable.getValueAt(row,4) + "\n"); // notes 78 77 } 79 80 boolean incidentsAdded = false; 81 boolean incidentsValidated = true; 82 Vector<Incident> currIncidents = theSimManagerView.getModel().getIncidentList(); 83 Vector<Incident> parsedIncidents = sh.getIncidents(); 84 Vector<Integer> duplicateIncNum = new Vector<Integer>(); 85 Vector<Integer> invalidIncTime = new Vector<Integer>(); 86 87 //Show the dialog with the initialized list of incidents. 88 theAddIncidentDialog.setModelData(parsedIncidents); 89 90 //Loop until the user selects a list(may be empty) of valid incidents. 91 do { 92 incidentsValidated = true; 93 theAddIncidentDialog.showDialog(); 94 95 duplicateIncNum.clear(); 96 invalidIncTime.clear(); 97 98 //Validate the selected incidents. Validation fails if: 99 // + A selected incident's log number matches a log number already in the simulation. 100 // + A selected incident has been scheduled to occur at a time that has passed in the simulation. 101 for(Integer incidentNum : theAddIncidentDialog.getSelectedIncidentTimes().keySet()) { 102 103 for(Incident inc : currIncidents) { 104 if(inc.logNum.equals(incidentNum)) { 105 duplicateIncNum.add(incidentNum); 106 incidentsValidated = false; 107 } 108 } 109 110 long incSchedTime = theAddIncidentDialog.getSelectedIncidentTimes().get(incidentNum); 111 if(incSchedTime < theSimManagerView.getCurrentSimTime()) 112 { 113 invalidIncTime.add(incidentNum); 114 incidentsValidated = false; 115 } 116 } 117 118 if(duplicateIncNum.size() > 0) { 119 JOptionPane.showMessageDialog(null, 120 "Duplicate incidents selected: " + 121 duplicateIncNum.toString().substring(1, 122 duplicateIncNum.toString().length()-1), 123 "Error", 124 JOptionPane.ERROR_MESSAGE); 125 } 126 else if(invalidIncTime.size() > 0) { 127 JOptionPane.showMessageDialog(null, 128 "Simulation time already passed for incidents: " + 129 invalidIncTime.toString().substring(1, 130 invalidIncTime.toString().length()-1), 131 "Error", 132 JOptionPane.ERROR_MESSAGE); 133 } 134 135 } while(!incidentsValidated); 136 137 138 //Loop through all selected incidents, set the new start time, and add the Incident 139 //to simulation. 140 for(Integer incidentNum : theAddIncidentDialog.getSelectedIncidentTimes().keySet()) { 141 for(Incident inc : parsedIncidents) { 142 if(inc.logNum.equals(incidentNum)) { 143 incidentsAdded = true; 144 inc.setSecondsToStart(theAddIncidentDialog 145 .getSelectedIncidentTimes().get(incidentNum)); 146 147 theSimManagerView.getModel().addIncident(inc); 148 } 149 } 150 } 151 152 153 if(incidentsAdded && !theSimManagerView.isSimulationStarted()) 154 { 155 theSimManagerView.setScriptStatus( 156 SCRIPT_STATUS.SCRIPT_STOPPED_NOT_STARTED); 157 } 158 } 159 } 160 catch (SimulationException se) { 161 theSimManagerView.SimulationExceptionHandler(se); 78 System.out.println(sb); 79 } 80 JOptionPane.showMessageDialog(theSimManagerView, sb, "Export", JOptionPane.INFORMATION_MESSAGE); 81 } catch (SimulationException ex) { 82 Logger.getLogger(ExportAction.class.getName()).log(Level.SEVERE, null, ex); 162 83 } 163 84 } 164 85 }; 165 86 166 87 Thread theThread = new Thread(addRunnable); 167 theThread.start();88 theThread.start(); 168 89 169 90 }
Note: See TracChangeset
for help on using the changeset viewer.
