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

Revision 310, 13.4 KB checked in by jdalbey, 7 years ago (diff)

Fix defect #102. Also add revision number to filenames of deploy jars.

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     * Add a single new incident to the list of incidents. Fixes #102
159     * @param incident a single new incident to be added
160     * @author jdalbey
161     */
162    public void addIncident(Incident incident)
163    {
164        incidents.add(incident);
165    }
166   
167    /**
168     * Returns the specified unitNum
169     */
170    public Unit getUnit(String unitNum)
171    {
172        for (int i = 0; i < units.size(); i++)
173        {
174            if (units.get(i).getUnitNum().equals(unitNum))
175            {
176                return units.get(i);
177            }
178        }
179        return null;
180    }
181
182    /**
183     * Check if it contains an incident with the specified Id.
184     */
185    public boolean checkForValidId(int incidentId)
186    {
187        for (int i = 0; i < incidents.size(); i++)
188        {
189            if (incidents.get(i).getLogNum() == incidentId)
190            {
191                return true;
192            }
193        }
194        return false;
195    }
196
197    /**
198     * Uses an Incident's masterInc to look up its corresponding ID.
199     *
200     * @param masterInc the Incident's masterInc
201     * @return the Incident's ID, -1 if invalid masterInc
202     */
203    public int getIncidentId(String masterInc)
204    {
205        for (int i = 0; i < incidents.size(); i++)
206        {
207            if (incidents.get(i).getMasterInc().equals(masterInc))
208            {
209                return incidents.get(i).getLogNum();
210            }
211        }
212        return -1;
213    }
214
215    /**
216     * Returns the specified incident
217     */
218    public Incident getIncident(int incidentId)
219    {
220        for (int i = 0; i < incidents.size(); i++)
221        {
222            if (incidents.get(i).getLogNum() == incidentId)
223            {
224                return incidents.get(i);
225            }
226        }
227        logger.logp(Level.INFO, "CADData",
228                "getIncident", "id=" + incidentId, "incident not found.");
229        return new Incident(0, "", 0);
230    }
231
232    /**
233     * Sends all the objects for UnitStatus
234     *
235     * @return DefaultTableModel for UnitStatus
236     */
237    public DefaultTableModel tableForUnitStatus()
238    {
239        toUnitTableVector.clear();
240        for (int i = 0; i < units.size(); i++)
241        {
242            toUnitTableVector.add(units.get(i).toVector());
243        }
244        // Exception occurred here: http://pastebin.com/fvifM5i8
245        unitStatusTableModel
246                .setDataVector(toUnitTableVector, unitStatusHeaders);
247        return unitStatusTableModel;
248    }
249
250    /**
251     * Sends all the objects for AssignedIncidents
252     *
253     * @return DefaultTableModel for AssignedIncidents
254     */
255    public DefaultTableModel tableForAssignedIncidents()
256    {
257        toAssignedTableVector.clear();
258        for (int i = 0; i < incidents.size(); i++)
259        {
260            if (incidents.get(i).hasOccured()
261                    && incidents.get(i).getIncidentStatus() == IncidentEnums.Assigned)
262            {
263                toAssignedTableVector.add(incidents.get(i)
264                        .toVectorForAssignedIncidents());
265            }
266        }
267        assignedIncidentsTableModel.setDataVector(toAssignedTableVector,
268                assignedIncidentsHeaders);
269        return assignedIncidentsTableModel;
270    }
271
272    /**
273     * Sends all the objects for PendingIncidents
274     *
275     * @return DefaultTableModel for PendingIncidents
276     */
277    public DefaultTableModel tableForPendingIncidents()
278    {
279        toPendingTableVector.clear();
280        for (int i = 0; i < incidents.size(); i++)
281        {
282            if (incidents.get(i).hasOccured()
283                    && incidents.get(i).getIncidentStatus() == IncidentEnums.Pending)
284            {
285                toPendingTableVector.add(incidents.get(i)
286                        .toVectorForPendingIncidents());
287            }
288        }
289        pendingIncidentsTableModel.setDataVector(toPendingTableVector,
290                pendingIncidentsHeaders);
291        return pendingIncidentsTableModel;
292    }
293
294    /**
295     * Sends all the objects for IncidentEditor
296     *
297     * @return DefaultTableModel for IncidentEditor
298     */
299    public DefaultTableModel tableForIncidentEditor()
300    {
301        toIncidentEditorVector.clear();
302        for (int i = 0; i < incidents.size(); i++)
303        {
304            if (incidents.get(i).hasOccured())
305            {
306                toIncidentEditorVector.add(incidents.get(i)
307                        .toVectorForIncidentEditor());
308            }
309        }
310        incidentEditorModel.setDataVector(toIncidentEditorVector,
311                incidentEditorHeaders);
312        return incidentEditorModel;
313    }
314
315    /**
316     * Sets up a timer to subtract one from the unit's timer every second.
317     */
318    public void handleUpdateTimes()
319    {
320        Timer timer = new Timer(ONE_SECOND, new ActionListener()
321        {
322            public void actionPerformed(ActionEvent e)
323            {
324                for (int i = 0; i < units.size(); i++)
325                {
326                    units.get(i).TimerMinusSecond();
327                }
328            }
329        });
330        timer.start();
331    }
332
333    /**
334     * Assigns the specified unit to the specified incident and updates
335     * information in both screens
336     *
337     * @param unitNum the unit being assigned
338     * @param incidentNum the incident being assigned to
339     * @param primary whether or not the assigned unit is the primary unit
340     */
341    public void unitAssignedToIncident(String unitNum, int incidentNum, boolean primary)
342    {
343        if (primary)
344        {
345            getIncident(incidentNum).setPrimaryAssignedUnitNum(unitNum);
346            getIncident(incidentNum).setIncidentStatus(IncidentEnums.Assigned);
347        }
348        else
349        {
350            getIncident(incidentNum).addAssignedUnitNum(unitNum);
351        }
352
353        getUnit(unitNum).setAssignedIncidentId(incidentNum);
354        getUnit(unitNum).setMasterInc(getIncident(incidentNum).getMasterInc());
355        getUnit(unitNum).setStatus("ENRT");
356        getUnit(unitNum).setUnitStatus(UnitStatusEnums.Enroute);
357        getUnit(unitNum).setType(getIncident(incidentNum).getAdditionalInfo().getType());
358        getUnit(unitNum).setDestination(getIncident(incidentNum).getIncidentLocation().getAddress());
359        getUnit(unitNum).setArea(getIncident(incidentNum).getIncidentLocation().getArea());
360        getUnit(unitNum).setP(getIncident(incidentNum).getP());
361    }
362
363    /**
364     * Updates information in both incident and unit screens for when a unit has
365     * arrived at scene aka that the unit status is "10-97"
366     *
367     * @param unitNum the unit which arrived at the scene
368     */
369    public void unitArrivedAtIncidentScene(String unitNum, int incidentNum, boolean primary)
370    {
371        if (getUnit(unitNum).getAssignedIncidentId() != incidentNum)
372        {
373            if (primary)
374            {
375                getIncident(incidentNum).setPrimaryAssignedUnitNum(unitNum);
376                getIncident(incidentNum).setIncidentStatus(IncidentEnums.Assigned);
377            }
378            else
379            {
380                getIncident(incidentNum).addAssignedUnitNum(unitNum);
381            }
382            getUnit(unitNum).setAssignedIncidentId(incidentNum);
383            getUnit(unitNum).setMasterInc(getIncident(incidentNum).getMasterInc());
384            getUnit(unitNum).setType(getIncident(incidentNum).getAdditionalInfo().getType());
385            getUnit(unitNum).setDestination(getIncident(incidentNum).getIncidentLocation().getAddress());
386            getUnit(unitNum).setArea(getIncident(incidentNum).getIncidentLocation().getArea());
387            getUnit(unitNum).setP(getIncident(incidentNum).getP());
388        }
389        getUnit(unitNum).setStatus("10-97");
390        getUnit(unitNum).setUnitStatus(UnitStatusEnums.Arrived);
391        getUnit(unitNum).setCurrentLocation(getUnit(unitNum).getDestination());
392    }
393
394    /**
395     * Updates information in both incident and unit screens for when a unit has
396     * become available again aka that the unit status is "10-98"
397     *
398     * @param unitNum the unit which arrived at the scene
399     */
400    public void unitAvailable(String unitNum)
401    {
402        getIncident(getUnit(unitNum).getAssignedIncidentId()).removeAssignedUnitNum(unitNum);
403
404        getUnit(unitNum).setStatus("10-98");
405        getUnit(unitNum).setUnitStatus(UnitStatusEnums.Assignable);
406        getUnit(unitNum).setAssignedIncidentId(-1);
407        getUnit(unitNum).setMasterInc("");
408        getUnit(unitNum).setType("");
409        getUnit(unitNum).setDestination("");
410        getUnit(unitNum).setP("");
411
412    }
413}
Note: See TracBrowser for help on using the repository browser.