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

Revision 475, 13.7 KB checked in by jdalbey, 7 years ago (diff)

Coordinator.java, AssignedIncidents?.java, UnitStatus?.java, CADMenu.java: Add comments and error messages. Add revision number to CAD Menu.

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     * Accessor to the Unit Status table model
234     *
235     * @return DefaultTableModel for UnitStatus
236     */
237    synchronized
238    public DefaultTableModel tableForUnitStatus()
239    {
240        Vector<Object> toUnitTableVector = new Vector<Object>();
241
242        // build a new unit table
243        for (Unit unit: units)
244        {
245            toUnitTableVector.add(unit.toVector());
246        }
247        // Exception occurred here: http://pastebin.com/fvifM5i8
248        // Probably a distributed object concurrency problem.
249        // Assign the unit status table from the newly created vector
250        unitStatusTableModel
251                .setDataVector(toUnitTableVector, unitStatusHeaders);
252        // return a reference to the updated table
253        return unitStatusTableModel;
254    }
255    /**
256     * Sends all the objects for AssignedIncidents
257     *
258     * @return DefaultTableModel for AssignedIncidents
259     */
260    public DefaultTableModel tableForAssignedIncidents()
261    {
262        toAssignedTableVector.clear();
263        for (int i = 0; i < incidents.size(); i++)
264        {
265            if (incidents.get(i).hasOccured()
266                    && incidents.get(i).getIncidentStatus() == IncidentEnums.Assigned)
267            {
268                toAssignedTableVector.add(incidents.get(i)
269                        .toVectorForAssignedIncidents());
270            }
271        }
272        assignedIncidentsTableModel.setDataVector(toAssignedTableVector,
273                assignedIncidentsHeaders);
274        return assignedIncidentsTableModel;
275    }
276
277    /**
278     * Sends all the objects for PendingIncidents
279     *
280     * @return DefaultTableModel for PendingIncidents
281     */
282    public DefaultTableModel tableForPendingIncidents()
283    {
284        toPendingTableVector.clear();
285        for (int i = 0; i < incidents.size(); i++)
286        {
287            if (incidents.get(i).hasOccured()
288                    && incidents.get(i).getIncidentStatus() == IncidentEnums.Pending)
289            {
290                toPendingTableVector.add(incidents.get(i)
291                        .toVectorForPendingIncidents());
292            }
293        }
294        pendingIncidentsTableModel.setDataVector(toPendingTableVector,
295                pendingIncidentsHeaders);
296        return pendingIncidentsTableModel;
297    }
298
299    /**
300     * Sends all the objects for IncidentEditor
301     *
302     * @return DefaultTableModel for IncidentEditor
303     */
304    public DefaultTableModel tableForIncidentEditor()
305    {
306        toIncidentEditorVector.clear();
307        for (int i = 0; i < incidents.size(); i++)
308        {
309            if (incidents.get(i).hasOccured())
310            {
311                toIncidentEditorVector.add(incidents.get(i)
312                        .toVectorForIncidentEditor());
313            }
314        }
315        incidentEditorModel.setDataVector(toIncidentEditorVector,
316                incidentEditorHeaders);
317        return incidentEditorModel;
318    }
319
320    /**
321     * Sets up a timer to subtract one from the unit's timer every second.
322     */
323    public void handleUpdateTimes()
324    {
325        Timer timer = new Timer(ONE_SECOND, new ActionListener()
326        {
327            public void actionPerformed(ActionEvent e)
328            {
329                for (int i = 0; i < units.size(); i++)
330                {
331                    units.get(i).TimerMinusSecond();
332                }
333            }
334        });
335        timer.start();
336    }
337
338    /**
339     * Assigns the specified unit to the specified incident and updates
340     * information in both screens
341     *
342     * @param unitNum the unit being assigned
343     * @param incidentNum the incident being assigned to
344     * @param primary whether or not the assigned unit is the primary unit
345     */
346    public void unitAssignedToIncident(String unitNum, int incidentNum, boolean primary)
347    {
348        if (primary)
349        {
350            getIncident(incidentNum).setPrimaryAssignedUnitNum(unitNum);
351            getIncident(incidentNum).setIncidentStatus(IncidentEnums.Assigned);
352        }
353        else
354        {
355            getIncident(incidentNum).addAssignedUnitNum(unitNum);
356        }
357
358        getUnit(unitNum).setAssignedIncidentId(incidentNum);
359        getUnit(unitNum).setMasterInc(getIncident(incidentNum).getMasterInc());
360        getUnit(unitNum).setStatus("ENRT");
361        getUnit(unitNum).setUnitStatus(UnitStatusEnums.Enroute);
362        getUnit(unitNum).setType(getIncident(incidentNum).getAdditionalInfo().getType());
363        getUnit(unitNum).setDestination(getIncident(incidentNum).getIncidentLocation().getAddress());
364        getUnit(unitNum).setArea(getIncident(incidentNum).getIncidentLocation().getArea());
365        getUnit(unitNum).setP(getIncident(incidentNum).getP());
366    }
367
368    /**
369     * Updates information in both incident and unit screens for when a unit has
370     * arrived at scene aka that the unit status is "10-97"
371     *
372     * @param unitNum the unit which arrived at the scene
373     */
374    public void unitArrivedAtIncidentScene(String unitNum, int incidentNum, boolean primary)
375    {
376        if (getUnit(unitNum).getAssignedIncidentId() != incidentNum)
377        {
378            if (primary)
379            {
380                getIncident(incidentNum).setPrimaryAssignedUnitNum(unitNum);
381                getIncident(incidentNum).setIncidentStatus(IncidentEnums.Assigned);
382            }
383            else
384            {
385                getIncident(incidentNum).addAssignedUnitNum(unitNum);
386            }
387            getUnit(unitNum).setAssignedIncidentId(incidentNum);
388            getUnit(unitNum).setMasterInc(getIncident(incidentNum).getMasterInc());
389            getUnit(unitNum).setType(getIncident(incidentNum).getAdditionalInfo().getType());
390            getUnit(unitNum).setDestination(getIncident(incidentNum).getIncidentLocation().getAddress());
391            getUnit(unitNum).setArea(getIncident(incidentNum).getIncidentLocation().getArea());
392            getUnit(unitNum).setP(getIncident(incidentNum).getP());
393        }
394        getUnit(unitNum).setStatus("10-97");
395        getUnit(unitNum).setUnitStatus(UnitStatusEnums.Arrived);
396        getUnit(unitNum).setCurrentLocation(getUnit(unitNum).getDestination());
397    }
398
399    /**
400     * Updates information in both incident and unit screens for when a unit has
401     * become available again aka that the unit status is "10-98"
402     *
403     * @param unitNum the unit which arrived at the scene
404     */
405    public void unitAvailable(String unitNum)
406    {
407        getIncident(getUnit(unitNum).getAssignedIncidentId()).removeAssignedUnitNum(unitNum);
408
409        getUnit(unitNum).setStatus("10-98");
410        getUnit(unitNum).setUnitStatus(UnitStatusEnums.Assignable);
411        getUnit(unitNum).setAssignedIncidentId(-1);
412        getUnit(unitNum).setMasterInc("");
413        getUnit(unitNum).setType("");
414        getUnit(unitNum).setDestination("");
415        getUnit(unitNum).setP("");
416
417    }
418}
Note: See TracBrowser for help on using the repository browser.