source: tmcsimulator/trunk/src/tmcsim/simulationmanager/model/IncidentHistoryTableModel.java @ 2

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

Initial Import of project files

Line 
1package tmcsim.simulationmanager.model;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import javax.swing.event.TableModelEvent;
7import javax.swing.table.DefaultTableModel;
8
9import tmcsim.cadmodels.IncidentInquiryDetails;
10import tmcsim.client.cadclientgui.data.IncidentEvent;
11
12/**
13 * IncidentHistoryTableModel is a DefaultTableModel used to display the
14 * list of current incident events in the Event History portion of the
15 * Simulation Manager.  The columns in this table show the event source,
16 * the simulation time the event occured, and the event's description.
17 *
18 * @author Matthew Cechini
19 * @version
20 */
21@SuppressWarnings("serial")
22public class IncidentHistoryTableModel extends DefaultTableModel {
23   
24    /**
25     * Enumeration of columns for this table.
26     * @author Matthew Cechini
27     */ 
28    public static enum EVENT_HIST_COLUMNS {
29        POSITION_COL    ("Position", 0, 70, 110, 80),
30        TIME_COL        ("Time", 1, 50, 100, 60),
31        EVENT_DESC_COL  ("Event Description", 2, 250, 600, 300);
32       
33        public String colName;
34        public int colNum;
35        public int colMinWidth;
36        public int colMaxWidth;
37        public int colPrefWidth;
38       
39        private EVENT_HIST_COLUMNS (String name, int num, int min, int max, int pref) {
40            colName      = name;
41            colNum       = num;
42            colMinWidth  = min;
43            colMaxWidth  = max;
44            colPrefWidth = pref;
45        }
46       
47        public static int colCount() {
48            return values().length;
49        }
50       
51        public static String columnName(int num) {
52           
53            for(EVENT_HIST_COLUMNS column : EVENT_HIST_COLUMNS.values()) {
54                if(column.colNum == num)
55                    return column.colName;
56            }
57           
58            return "";
59        }
60       
61        public static int columnMinWidth(int num) {
62           
63            for(EVENT_HIST_COLUMNS column : EVENT_HIST_COLUMNS.values()) {
64                if(column.colNum == num)
65                    return column.colMinWidth;
66            }
67           
68            return 0;
69        }
70       
71        public static int columnMaxWidth(int num) {
72           
73            for(EVENT_HIST_COLUMNS column : EVENT_HIST_COLUMNS.values()) {
74                if(column.colNum == num)
75                    return column.colMaxWidth;
76            }
77           
78            return 0;
79        }
80       
81        public static int columnPrefWidth(int num) {
82           
83            for(EVENT_HIST_COLUMNS column : EVENT_HIST_COLUMNS.values()) {
84                if(column.colNum == num)
85                    return column.colPrefWidth;
86            }
87           
88            return 0;
89        }
90       
91        public static EVENT_HIST_COLUMNS fromColNum(int column) {
92            for(EVENT_HIST_COLUMNS c : EVENT_HIST_COLUMNS.values()) {
93                if(c.colNum == column)
94                    return c;
95            }
96           
97            return POSITION_COL;
98        }
99    } 
100   
101    /**
102     * Container class to hold information displayed in the table.
103     * @author Matthew Cechini
104     */
105    protected class EventHistoryInfo {
106
107        public String source;
108        public Long   secondsOccured;
109        public String details;
110       
111        public EventHistoryInfo(String src, Long seconds, String d) {
112            source         = src;
113            secondsOccured = seconds;
114            details        = d;
115        }
116    }
117   
118
119    /** List of data displayed in the table. */
120    private List<EventHistoryInfo> event_list;
121   
122    /** Constructor. */
123    public IncidentHistoryTableModel() {
124        event_list = new ArrayList<EventHistoryInfo>();
125    }
126
127    public int getRowCount() {
128        if(event_list == null) {
129            return 0;
130        }
131        else
132            return event_list.size();
133    }
134
135    public int getColumnCount() {
136        return EVENT_HIST_COLUMNS.colCount();
137    }
138
139    public String getColumnName(int col) {
140        return EVENT_HIST_COLUMNS.columnName(col);
141    }
142
143    public int getColumnMinWidth(int c){
144        return EVENT_HIST_COLUMNS.columnMinWidth(c);
145    }
146
147    public int getColumnMaxWidth(int c){
148        return EVENT_HIST_COLUMNS.columnMaxWidth(c);
149    }
150
151    public int getColumnPrefWidth(int c){
152        return EVENT_HIST_COLUMNS.columnPrefWidth(c);
153    }
154
155    public boolean isCellEditable(int row, int col) {
156        return false;
157    }   
158   
159    public Object getValueAt(int row, int col) {
160        switch (EVENT_HIST_COLUMNS.fromColNum(col)) {
161            case EVENT_DESC_COL:
162                return event_list.get(row).details;
163            case POSITION_COL:
164                return event_list.get(row).source;
165            case TIME_COL:
166                return event_list.get(row).secondsOccured;
167            default:
168                return "";
169        }       
170    }
171   
172    /**
173     * Method adds a new incident event into the table.
174     * @param logNumber Incident log number corresponding to the parameter event.
175     * @param newEvent Incident event object to add.
176     */   
177    public void addEvent(IncidentEvent newEvent) {
178       
179        for(IncidentInquiryDetails detail : newEvent.eventInfo.getDetails()) {
180            event_list.add(new EventHistoryInfo(
181                    newEvent.eventInfo.getSource(),
182                    newEvent.secondsOccuredInSimulation,
183                    detail.details));       
184        }
185        fireTableChanged(new TableModelEvent(this));
186    }
187   
188    /**
189     * Remove all events from the table's data.
190     */   
191    public void clearModelData() {
192        event_list.clear();
193        fireTableChanged(new TableModelEvent(this));
194    }
195   
196}
Note: See TracBrowser for help on using the repository browser.