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

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

Revision 65, 13.1 KB checked in by jdalbey, 9 years ago (diff)

CADData.java: Change error return value from null to new Incident()

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