KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ist > coach > coachEmfClientComponents > gui > LogTableModel


1 package ist.coach.coachEmfClientComponents.gui;
2
3 import javax.swing.table.AbstractTableModel JavaDoc;
4 import java.util.Vector JavaDoc;
5 import ist.coach.coachEmfServices.EmfBasicLog.LogRecord;
6 import ist.coach.coachEmfServices.EmfBasicLog.LogSeverity;
7 import java.text.DateFormat JavaDoc;
8 import org.omg.CORBA.Any JavaDoc;
9
10 class LogTableModel extends AbstractTableModel JavaDoc {
11
12         final String JavaDoc[] columnNames = {" Time ",
13                     " Log Type ",
14                     " Source Type ",
15                     " Source ",
16                     " Log Id ",
17                     " Details "
18         };
19
20         Vector JavaDoc data;
21         protected static int NUM_COLUMNS = 6;
22         protected static int TIME_C = 0;
23
24         protected static int EVENT_C = 1;
25         protected static int SOURCE_T_C = 2;
26         protected static int SOURCE_C = 3;
27         protected static int ID_C = 4;
28         protected static int DETAILS_C = 5;
29
30         private int nextEmptyRow = 0;
31         private static int START_NUM_ROWS = 30; //20
32
int numRows = 0;
33
34         LogTableModel() {
35
36             data = new Vector JavaDoc();
37         }
38
39         public int getColumnCount() {
40             return columnNames.length;
41         }
42
43         public int getRowCount() {
44
45             if (numRows < START_NUM_ROWS)
46                 return START_NUM_ROWS;
47             else
48                 return numRows;
49         }
50
51         public String JavaDoc getColumnName(int col) {
52
53             return columnNames[col];
54         }
55
56         public Object JavaDoc getValueAt(int row, int col) {
57
58             try {
59                 LogDisplayRecord logR = (LogDisplayRecord) data.elementAt(row);
60
61                 switch(col) {
62                 case 0:
63                     return logR.timeR;
64                 case 1:
65                     return logR.eventType;
66                 case 2:
67                     return logR.sourceClass;
68                 case 3:
69                     return logR.source;
70                 case 4:
71                     return logR.notifId;
72                 case 5:
73                     return logR.details;
74                 }
75             }
76             catch(Exception JavaDoc e) {}
77
78             return new String JavaDoc();
79
80         }
81
82         /*
83          * JTable uses this method to determine the default renderer/
84          * editor for each cell. If we didn't implement this method,
85          * then the last column would contain text ("true"/"false"),
86          * rather than a check box.
87          */

88         public Class JavaDoc getColumnClass(int c) {
89             return getValueAt(0, c).getClass();
90         }
91
92         /*
93          * Don't need to implement this method unless your table's
94          * editable.
95          */

96         public boolean isCellEditable(int row, int col) {
97                 return false;
98         }
99
100     public synchronized void updateLogTable(String JavaDoc dateR, String JavaDoc eventType,
101                         String JavaDoc source, String JavaDoc sourceClass,
102                         String JavaDoc notifId, String JavaDoc details) {
103
104          // System.err.println("LogTableModel> updated called!!!!!");
105
LogDisplayRecord logR = new LogDisplayRecord(dateR, eventType, source,
106                                             sourceClass,notifId, details);
107
108             data.addElement(logR);
109             if (data.size() == data.capacity())
110                 data.ensureCapacity(data.size() + 100);
111
112             numRows++;
113             fireTableRowsInserted(nextEmptyRow, nextEmptyRow);
114             nextEmptyRow++;
115             // System.err.println("LogTableModel> updated returns....");
116
}
117
118         public synchronized void updateLogArchives(LogRecord[] records) {
119
120           DateFormat JavaDoc df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT);
121
122           java.util.Date JavaDoc log_record_date;
123           String JavaDoc log_record_date_str = new String JavaDoc();
124           String JavaDoc log_record_id = new String JavaDoc();
125           String JavaDoc log_record_source= new String JavaDoc();
126           String JavaDoc log_record_class= new String JavaDoc();
127           String JavaDoc log_record_type= new String JavaDoc();
128           String JavaDoc log_record_details= new String JavaDoc();
129
130           for (int i = 0; i < records.length; i++) {
131             log_record_date = new java.util.Date JavaDoc(records[i].time);
132             log_record_date_str = df.format(log_record_date);
133             log_record_id = String.valueOf(records[i].id);
134             log_record_details = records[i].info.extract_string();
135
136             for(int j = 0; j < records[i].attr_list.length; j++) {
137
138               if (records[i].attr_list[j].name.equals("Source"))
139                log_record_source = records[i].attr_list[j].value.extract_string();
140               else
141               if (records[i].attr_list[j].name.equals("SourceClass"))
142                 log_record_class = records[i].attr_list[j].value.extract_string();
143               else
144               if (records[i].attr_list[j].name.equals("EventType")) {
145
146                 log_record_type = GuiMessages.info_log;
147
148                 switch(records[i].attr_list[j].value.extract_short()) {
149                  case LogSeverity._Error :
150                     log_record_type = GuiMessages.error_log;
151                     break;
152                  case LogSeverity._FatalError :
153                     log_record_type = GuiMessages.fatal_error_log;
154                     break;
155                 case LogSeverity._Warning :
156                     log_record_type = GuiMessages.warning_log;
157                     break;
158               };
159
160             }
161           }
162           updateLogTable(log_record_date_str, log_record_type,
163                         log_record_source, log_record_class,
164                         log_record_id, log_record_details);
165         }
166       }
167
168         public synchronized void clear() {
169
170             int oldNumRows = numRows;
171
172             numRows = START_NUM_ROWS;
173             data.removeAllElements();
174             nextEmptyRow = 0;
175
176             if (oldNumRows > START_NUM_ROWS)
177                 fireTableRowsDeleted(START_NUM_ROWS, oldNumRows - 1);
178
179             fireTableRowsUpdated(0, START_NUM_ROWS - 1);
180         }
181
182         public void printDebugData() {
183             int numRows = getRowCount();
184
185             LogDisplayRecord record ;
186             for (int i=0; i < numRows; i++) {
187                 System.err.println("------> LogDisplayRecord" + i + " <-----------");
188                 record = (LogDisplayRecord) data.get(i);
189                 record.print();
190             }
191         }
192
193     private class LogDisplayRecord {
194
195         String JavaDoc timeR = null;
196         String JavaDoc notifId = null;
197         String JavaDoc eventType = null;
198         String JavaDoc source = null;
199         String JavaDoc sourceClass = null;
200         String JavaDoc details = null;
201
202         LogDisplayRecord() {}
203         LogDisplayRecord(String JavaDoc timeR, String JavaDoc eventType, String JavaDoc source,
204                     String JavaDoc sourceClass, String JavaDoc notifId, String JavaDoc details) {
205
206
207             this.timeR = timeR;
208             this.notifId = notifId;
209             this.eventType = eventType;
210             this.source = source;
211             this.sourceClass = sourceClass;
212             this.details = details;
213         }
214
215         void print() {
216             System.err.println("Time =" + timeR +
217                                 "\tEvent Type =" + eventType +
218                                 "\tSource = " + source +
219                                 "\tSourceClass = " + sourceClass +
220                                 "\tId = " + notifId +
221                                 "\tDetails = " + details);
222         }
223
224     }
225 }
226
227
Popular Tags