source: tmcsimulator/trunk/src/tmcsim/client/cadclientgui/data/CADData.java @ 66

Revision 66, 13.2 KB checked in by jdalbey, 9 years ago (diff)

Import Paramics Simulator.

Line 
1package tmcsim.client.cadclientgui.data;
2
3import java.awt.event.ActionEvent;
4import java.awt.event.ActionListener;
5import java.io.Serializable;
6import java.util.Collections;
7import java.util.Vector;
8import java.util.logging.Level;
9import java.util.logging.Logger;
10import javax.swing.Timer;
11import javax.swing.table.DefaultTableModel;
12import tmcsim.client.cadclientgui.enums.IncidentEnums;
13import tmcsim.client.cadclientgui.enums.TableHeaders;
14import tmcsim.client.cadclientgui.enums.UnitStatusEnums;
15
16/**
17 * This class holds the data for all the units and incidents. It is responsible
18 * for sending out data to all the screens.
19 *
20 * @author Vincent
21 *
22 */
23public class CADData implements Serializable
24{
25    private final int ONE_SECOND = 1000;
26    private static Vector<Unit> units;
27    private static Vector<Incident> incidents;
28    private static Vector<String> assignedIncidentsHeaders;
29    private static Vector<String> unitStatusHeaders;
30    private static Vector<String> pendingIncidentsHeaders;
31    private static Vector<String> incidentEditorHeaders;
32    private static Vector<Object> toUnitTableVector;
33    private static Vector<Object> toPendingTableVector;
34    private static Vector<Object> toAssignedTableVector;
35    private static Vector<Object> toIncidentEditorVector;
36    private static Vector<Object> toCommentsNotesVector;
37    private static DefaultTableModel pendingIncidentsTableModel;
38    private static DefaultTableModel unitStatusTableModel;
39    private static DefaultTableModel assignedIncidentsTableModel;
40    private static DefaultTableModel incidentEditorModel;
41    private static Logger logger = Logger.getLogger("tmcsim.client");
42
43    public CADData()
44    {
45        units = new Vector<Unit>();
46        incidents = new Vector<Incident>();
47
48        toUnitTableVector = new Vector<Object>();
49        toPendingTableVector = new Vector<Object>();
50        toAssignedTableVector = new Vector<Object>();
51        toIncidentEditorVector = new Vector<Object>();
52        toCommentsNotesVector = new Vector<Object>();
53        assignedIncidentsHeaders = new Vector<String>();
54        pendingIncidentsHeaders = new Vector<String>();
55        unitStatusHeaders = new Vector<String>();
56        incidentEditorHeaders = new Vector<String>();
57        initializeAssignedIncidentsSettings();
58        initializeUnitStatusSettings();
59        initializePendingIncidentsSettings();
60        initializeIncidentEditorSettings();
61
62        handleUpdateTimes();
63    }
64
65    public void clearData()
66    {
67        units = new Vector<Unit>();
68        incidents = new Vector<Incident>();
69    }
70
71    public void resetSimulation()
72    {
73        for (Incident inc : incidents)
74        {
75            inc.resetCADDataSimulation();
76        }
77        for (Unit unit : units)
78        {
79            unit.resetCADDataSimulation();
80        }
81    }
82
83    private void initializeAssignedIncidentsSettings()
84    {
85        Collections
86                .addAll(assignedIncidentsHeaders, TableHeaders.ASSIGNED_INCIDENTS_HEADERS);
87        assignedIncidentsTableModel = new DefaultTableModel()
88        {
89            public boolean isCellEditable(int row, int column)
90            {
91                return false;// This causes all cells to be not editable
92            }
93        };
94        assignedIncidentsTableModel
95                .setColumnIdentifiers(TableHeaders.ASSIGNED_INCIDENTS_HEADERS);
96    }
97
98    private void initializeUnitStatusSettings()
99    {
100        Collections.addAll(unitStatusHeaders, TableHeaders.UNIT_STATUS_HEADERS);
101        unitStatusTableModel = new DefaultTableModel()
102        {
103            public boolean isCellEditable(int row, int column)
104            {
105                return false;// This causes all cells to be not editable
106            }
107        };
108        unitStatusTableModel.setColumnIdentifiers(TableHeaders.UNIT_STATUS_HEADERS);
109    }
110
111    private void initializeIncidentEditorSettings()
112    {
113        Collections.addAll(incidentEditorHeaders, TableHeaders.INCIDENT_EDITOR_HEADERS);
114        incidentEditorModel = new DefaultTableModel()
115        {
116            public boolean isCellEditable(int row, int column)
117            {
118                return false;// This causes all cells to be not editable
119            }
120        };
121        incidentEditorModel.setColumnIdentifiers(TableHeaders.INCIDENT_EDITOR_HEADERS);
122    }
123
124    private void initializePendingIncidentsSettings()
125    {
126        Collections.addAll(pendingIncidentsHeaders, TableHeaders.PENDING_INCIDENTS_HEADERS);
127        pendingIncidentsTableModel = new DefaultTableModel()
128        {
129            public boolean isCellEditable(int row, int column)
130            {
131                return false;// This causes all cells to be not editable
132            }
133        };
134        pendingIncidentsTableModel
135                .setColumnIdentifiers(TableHeaders.PENDING_INCIDENTS_HEADERS);
136    }
137
138    /**
139     * Obtains the list of units from the XML.
140     *
141     * @param unit the new list of units.
142     */
143    public void setUnitsFromXML(Vector<Unit> unit)
144    {
145        units = unit;
146    }
147
148    /**
149     * Obtains the list of incidents from the XML.
150     *
151     * @param incident the new list of incidents.
152     */
153    public void setIncidentsFromXML(Vector<Incident> incident)
154    {
155        incidents = incident;
156    }
157
158    /**
159     * Returns the specified unitNum
160     */
161    public Unit getUnit(String unitNum)
162    {
163        for (int i = 0; i < units.size(); i++)
164        {
165            if (units.get(i).getUnitNum().equals(unitNum))
166            {
167                return units.get(i);
168            }
169        }
170        return null;
171    }
172
173    /**
174     * Check if it contains an incident with the specified Id.
175     */
176    public boolean checkForValidId(int incidentId)
177    {
178        for (int i = 0; i < incidents.size(); i++)
179        {
180            if (incidents.get(i).getLogNum() == incidentId)
181            {
182                return true;
183            }
184        }
185        return false;
186    }
187
188    /**
189     * Uses an Incident's masterInc to look up its corresponding ID.
190     *
191     * @param masterInc the Incident's masterInc
192     * @return the Incident's ID, -1 if invalid masterInc
193     */
194    public int getIncidentId(String masterInc)
195    {
196        for (int i = 0; i < incidents.size(); i++)
197        {
198            if (incidents.get(i).getMasterInc().equals(masterInc))
199            {
200                return incidents.get(i).getLogNum();
201            }
202        }
203        return -1;
204    }
205
206    /**
207     * Returns the specified incident
208     */
209    public Incident getIncident(int incidentId)
210    {
211        for (int i = 0; i < incidents.size(); i++)
212        {
213            if (incidents.get(i).getLogNum() == incidentId)
214            {
215                return incidents.get(i);
216            }
217        }
218        logger.logp(Level.INFO, "CADData",
219                "getIncident", "id=" + incidentId, "incident not found.");
220        return new Incident(0, "", 0);
221    }
222
223    /**
224     * Sends all the objects for UnitStatus
225     *
226     * @return DefaultTableModel for UnitStatus
227     */
228    public DefaultTableModel tableForUnitStatus()
229    {
230        toUnitTableVector.clear();
231        for (int i = 0; i < units.size(); i++)
232        {
233            toUnitTableVector.add(units.get(i).toVector());
234        }
235        // Exception occurred here: http://pastebin.com/fvifM5i8
236        unitStatusTableModel
237                .setDataVector(toUnitTableVector, unitStatusHeaders);
238        return unitStatusTableModel;
239    }
240
241    /**
242     * Sends all the objects for AssignedIncidents
243     *
244     * @return DefaultTableModel for AssignedIncidents
245     */
246    public DefaultTableModel tableForAssignedIncidents()
247    {
248        toAssignedTableVector.clear();
249        for (int i = 0; i < incidents.size(); i++)
250        {
251            if (incidents.get(i).hasOccured()
252                    && incidents.get(i).getIncidentStatus() == IncidentEnums.Assigned)
253            {
254                toAssignedTableVector.add(incidents.get(i)
255                        .toVectorForAssignedIncidents());
256            }
257        }
258        assignedIncidentsTableModel.setDataVector(toAssignedTableVector,
259                assignedIncidentsHeaders);
260        return assignedIncidentsTableModel;
261    }
262
263    /**
264     * Sends all the objects for PendingIncidents
265     *
266     * @return DefaultTableModel for PendingIncidents
267     */
268    public DefaultTableModel tableForPendingIncidents()
269    {
270        toPendingTableVector.clear();
271        for (int i = 0; i < incidents.size(); i++)
272        {
273            if (incidents.get(i).hasOccured()
274                    && incidents.get(i).getIncidentStatus() == IncidentEnums.Pending)
275            {
276                toPendingTableVector.add(incidents.get(i)
277                        .toVectorForPendingIncidents());
278            }
279        }
280        pendingIncidentsTableModel.setDataVector(toPendingTableVector,
281                pendingIncidentsHeaders);
282        return pendingIncidentsTableModel;
283    }
284
285    /**
286     * Sends all the objects for IncidentEditor
287     *
288     * @return DefaultTableModel for IncidentEditor
289     */
290    public DefaultTableModel tableForIncidentEditor()
291    {
292        toIncidentEditorVector.clear();
293        for (int i = 0; i < incidents.size(); i++)
294        {
295            if (incidents.get(i).hasOccured())
296            {
297                toIncidentEditorVector.add(incidents.get(i)
298                        .toVectorForIncidentEditor());
299            }
300        }
301        incidentEditorModel.setDataVector(toIncidentEditorVector,
302                incidentEditorHeaders);
303        return incidentEditorModel;
304    }
305
306    /**
307     * Sets up a timer to subtract one from the unit's timer every second.
308     */
309    public void handleUpdateTimes()
310    {
311        Timer timer = new Timer(ONE_SECOND, new ActionListener()
312        {
313            public void actionPerformed(ActionEvent e)
314            {
315                for (int i = 0; i < units.size(); i++)
316                {
317                    units.get(i).TimerMinusSecond();
318                }
319            }
320        });
321        timer.start();
322    }
323
324    /**
325     * Assigns the specified unit to the specified incident and updates
326     * information in both screens
327     *
328     * @param unitNum the unit being assigned
329     * @param incidentNum the incident being assigned to
330     * @param primary whether or not the assigned unit is the primary unit
331     */
332    public void unitAssignedToIncident(String unitNum, int incidentNum, boolean primary)
333    {
334        if (primary)
335        {
336            getIncident(incidentNum).setPrimaryAssignedUnitNum(unitNum);
337            getIncident(incidentNum).setIncidentStatus(IncidentEnums.Assigned);
338        }
339        else
340        {
341            getIncident(incidentNum).addAssignedUnitNum(unitNum);
342        }
343
344        getUnit(unitNum).setAssignedIncidentId(incidentNum);
345        getUnit(unitNum).setMasterInc(getIncident(incidentNum).getMasterInc());
346        getUnit(unitNum).setStatus("ENRT");
347        getUnit(unitNum).setUnitStatus(UnitStatusEnums.Enroute);
348        getUnit(unitNum).setType(getIncident(incidentNum).getAdditionalInfo().getType());
349        getUnit(unitNum).setDestination(getIncident(incidentNum).getIncidentLocation().getAddress());
350        getUnit(unitNum).setArea(getIncident(incidentNum).getIncidentLocation().getArea());
351        getUnit(unitNum).setP(getIncident(incidentNum).getP());
352    }
353
354    /**
355     * Updates information in both incident and unit screens for when a unit has
356     * arrived at scene aka that the unit status is "10-97"
357     *
358     * @param unitNum the unit which arrived at the scene
359     */
360    public void unitArrivedAtIncidentScene(String unitNum, int incidentNum, boolean primary)
361    {
362        if (getUnit(unitNum).getAssignedIncidentId() != incidentNum)
363        {
364            if (primary)
365            {
366                getIncident(incidentNum).setPrimaryAssignedUnitNum(unitNum);
367                getIncident(incidentNum).setIncidentStatus(IncidentEnums.Assigned);
368            }
369            else
370            {
371                getIncident(incidentNum).addAssignedUnitNum(unitNum);
372            }
373            getUnit(unitNum).setAssignedIncidentId(incidentNum);
374            getUnit(unitNum).setMasterInc(getIncident(incidentNum).getMasterInc());
375            getUnit(unitNum).setType(getIncident(incidentNum).getAdditionalInfo().getType());
376            getUnit(unitNum).setDestination(getIncident(incidentNum).getIncidentLocation().getAddress());
377            getUnit(unitNum).setArea(getIncident(incidentNum).getIncidentLocation().getArea());
378            getUnit(unitNum).setP(getIncident(incidentNum).getP());
379        }
380        getUnit(unitNum).setStatus("10-97");
381        getUnit(unitNum).setUnitStatus(UnitStatusEnums.Arrived);
382        getUnit(unitNum).setCurrentLocation(getUnit(unitNum).getDestination());
383    }
384
385    /**
386     * Updates information in both incident and unit screens for when a unit has
387     * become available again aka that the unit status is "10-98"
388     *
389     * @param unitNum the unit which arrived at the scene
390     */
391    public void unitAvailable(String unitNum)
392    {
393        getIncident(getUnit(unitNum).getAssignedIncidentId()).removeAssignedUnitNum(unitNum);
394
395        getUnit(unitNum).setStatus("10-98");
396        getUnit(unitNum).setUnitStatus(UnitStatusEnums.Assignable);
397        getUnit(unitNum).setAssignedIncidentId(-1);
398        getUnit(unitNum).setMasterInc("");
399        getUnit(unitNum).setType("");
400        getUnit(unitNum).setDestination("");
401        getUnit(unitNum).setP("");
402
403    }
404}
Note: See TracBrowser for help on using the repository browser.