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

Revision 2, 8.4 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files

Line 
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.SimulationManagerView;
18import tmcsim.simulationmanager.dialogs.AddIncidentDialog;
19
20
21/**
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.
34 */
35@SuppressWarnings("serial")
36public class AddIncidentAction extends AbstractAction {
37   
38    /** Reference to the SimulationManagerView object. */
39    private SimulationManagerView theSimManagerView = null;
40   
41    /** AddIncidentDialog used for adding new incidents into the simulation. */
42    private AddIncidentDialog theAddIncidentDialog; 
43   
44    /**
45     * Constructor.
46     * @param view View class object for the Simulation Manager.
47     */     
48    public AddIncidentAction(SimulationManagerView view) {
49        super("Add New Incident");
50       
51        theSimManagerView = view;
52
53        theAddIncidentDialog  = new AddIncidentDialog(
54                theSimManagerView);
55    }
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);
63                   
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);
78                        }
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);   
162                }
163            }
164        };
165       
166        Thread theThread = new Thread(addRunnable);
167        theThread.start();
168
169    }
170
171}
Note: See TracBrowser for help on using the repository browser.