Warning: Can't use blame annotator:
svn blame failed on trunk/src/tmcsim/simulationmanager/actions/AddIncidentAction.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/trunk/src/tmcsim/simulationmanager/actions/AddIncidentAction.java @ 658

Revision 658, 8.4 KB checked in by jdalbey, 4 years ago (diff)

Replace hard-code scenario folder name in Simulation Manager with config property.

RevLine 
1package tmcsim.simulationmanager.actions;
2
3import java.awt.event.ActionEvent;
4import java.io.File;
5import java.util.Vector;
6
7import javax.swing.AbstractAction;
8import javax.swing.JFileChooser;
9import javax.swing.JOptionPane;
10import javax.xml.parsers.SAXParserFactory;
11
12import tmcsim.client.cadclientgui.data.Incident;
13import tmcsim.common.ScriptException;
14import tmcsim.client.cadclientgui.ScriptHandler;
15import tmcsim.common.SimulationException;
16import tmcsim.common.CADEnums.SCRIPT_STATUS;
17import tmcsim.simulationmanager.SimulationManager;
18import tmcsim.simulationmanager.SimulationManagerView;
19import 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")
37public 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}
Note: See TracBrowser for help on using the repository browser.