KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > gui > ForecastTableModel


1 package sellwin.gui;
2
3 import sellwin.domain.*;
4 import javax.swing.*;
5 import javax.swing.text.*;
6 import javax.swing.table.*;
7 import java.util.*;
8 import java.text.*;
9
10 // SellWin http://sourceforge.net/projects/sellwincrm
11
//Contact support@open-app.com for commercial help with SellWin
12
//This software is provided "AS IS", without a warranty of any kind.
13

14 /**
15  * This class the table model for the forecast table
16  */

17 public class ForecastTableModel extends AbstractTableModel {
18     public final static String JavaDoc[] columnNames =
19         { "Forecast", "Scenario", "Submitted",
20             "Total Revenue", "Total Profit", "Profit Margin", "Close Date",
21             "Jan", "Feb", "Mar", "Q1", "Apr", "May", "Jun", "Q2",
22             "Jul", "Aug", "Sep", "Q3", "Oct", "Nov", "Dec", "Q4" };
23
24     
25     private ArrayList wholeList = null;
26     private Forecast sample = new Forecast();
27
28     /**
29      * construct the table model
30      * @param wholeList the ArrayList of Forecasts to display
31      */

32     public ForecastTableModel(ArrayList wholeList) {
33
34         this.wholeList = wholeList;
35         sample.setName("hi");
36         sample.setScenario(Forecast.SCEN_BEST_CASE);
37         sample.setSubmitted(new Boolean JavaDoc(true));
38         sample.setRevenue(new Integer JavaDoc(1));
39         sample.setProfit(new Integer JavaDoc(1));
40         sample.setMargin(new Double JavaDoc(1.00));
41         sample.setCloseDate(new Date());
42         sample.setAmountJan(new Integer JavaDoc(0));
43         sample.setAmountFeb(new Integer JavaDoc(0));
44         sample.setAmountMar(new Integer JavaDoc(0));
45         sample.setQ1(new Integer JavaDoc(0));
46         sample.setAmountApr(new Integer JavaDoc(0));
47         sample.setAmountMay(new Integer JavaDoc(0));
48         sample.setAmountJun(new Integer JavaDoc(0));
49         sample.setQ2(new Integer JavaDoc(0));
50         sample.setAmountJul(new Integer JavaDoc(0));
51         sample.setAmountAug(new Integer JavaDoc(0));
52         sample.setAmountSep(new Integer JavaDoc(0));
53         sample.setQ3(new Integer JavaDoc(0));
54         sample.setAmountOct(new Integer JavaDoc(0));
55         sample.setAmountNov(new Integer JavaDoc(0));
56         sample.setAmountDec(new Integer JavaDoc(0));
57         sample.setQ4(new Integer JavaDoc(0));
58
59         setLang();
60     }
61
62     /**
63      * add a Forecast to the model
64      * @param f the Forecast to add
65      */

66     public final void addForecast(Forecast f) {
67         wholeList.add(f);
68     }
69
70     /**
71      * get the Forecast at a given index
72      * @param index the index
73      * @return the Forecast
74      */

75     public final Forecast getForecast(int index) {
76         return (Forecast)(wholeList.get(index));
77     }
78
79     /**
80      * delete a forecast from the model
81      * @param index the forecast index to delete
82      */

83     public final void deleteForecast(int index) {
84         wholeList.remove(index);
85     }
86
87     /**
88      * get the model's list of Forecasts
89      * @return the ArrayList of Forecasts
90      */

91     public final ArrayList getForecasts() {
92         return wholeList;
93     }
94
95     /**
96      * get a table cell's value
97      * @param inRow the row index
98      * @param col the column index
99      * @return the value at that cell
100      */

101     public final Object JavaDoc getValueAt(int inRow, int col) {
102         Forecast row = (Forecast)wholeList.get(inRow);
103         switch (col) {
104             case 0: //name
105
return row.getName();
106             case 1: //scenario
107
return row.getScenario();
108             case 2: //submitted
109
return row.getSubmitted();
110             case 3: //revenue
111
return row.getRevenue();
112             case 4: //profit
113
return row.getProfit();
114             case 5: //margin
115
return row.getMargin();
116             case 6: //close date
117
return row.getCloseDate();
118             case 7: //Jan amount
119
return row.getAmountJan();
120             case 8: //Feb amount
121
return row.getAmountFeb();
122             case 9: //Mar amount
123
return row.getAmountMar();
124             case 10: //Q1 amount
125
return row.getQ1();
126             case 11: //Apr amount
127
return row.getAmountApr();
128             case 12: //May amount
129
return row.getAmountMay();
130             case 13: //Jun amount
131
return row.getAmountJun();
132             case 14: //Q2 amount
133
return row.getQ2();
134             case 15: //Jul amount
135
return row.getAmountJul();
136             case 16: //Aug amount
137
return row.getAmountAug();
138             case 17: //Sep amount
139
return row.getAmountSep();
140             case 18: //Q3 amount
141
return row.getQ3();
142             case 19: //Oct amount
143
return row.getAmountOct();
144             case 20: //Nov amount
145
return row.getAmountNov();
146             case 21: //Dec amount
147
return row.getAmountDec();
148             case 22: //Q4 amount
149
return row.getQ4();
150             default:
151                 System.out.println("oops its dorked");
152                 break;
153         }
154
155         Exception JavaDoc e = new Exception JavaDoc();
156         e.printStackTrace();
157         System.exit(1);
158         return null;
159     }
160
161     /**
162      * part of the standard interface
163      */

164     public final int getRowCount() {
165         return wholeList.size();
166     }
167
168     /**
169      * part of the standard interface
170      */

171     public final int getColumnCount() {
172         return columnNames.length;
173     }
174
175     /**
176      * part of the standard interface
177      */

178     public final String JavaDoc getColumnName(int col) {
179         return columnNames[col];
180     }
181
182     /**
183      * part of the standard interface
184      */

185     public final Class JavaDoc getColumnClass(int col) {
186         switch (col) {
187             case 0: //name
188
return sample.getName().getClass();
189             case 1: //scenario
190
return sample.getScenario().getClass();
191             case 2: //submitted
192
return sample.getSubmitted().getClass();
193             case 3: //revenue
194
return sample.getRevenue().getClass();
195             case 4: //profit
196
return sample.getProfit().getClass();
197             case 5: //margin
198
return sample.getMargin().getClass();
199             case 6: //close date
200
return sample.getCloseDate().getClass();
201             case 7: //jan amounts
202
case 8: //feb amounts
203
case 9: //mar amounts
204
case 10: //Q1 amount
205
case 11: //apr amounts
206
case 12: //may amounts
207
case 13: //jun amounts
208
case 14: //Q2
209
case 15: //jul amounts
210
case 16: //aug amounts
211
case 17: //sep amounts
212
case 18: //Q3
213
case 19: //oct amounts
214
case 20: //nov amounts
215
case 21: //dec amounts
216
case 22: //Q4
217
return sample.getAmountJan().getClass();
218             default:
219                 System.out.println("oops its dorked");
220                 break;
221         }
222         Exception JavaDoc e = new Exception JavaDoc();
223         e.printStackTrace();
224         System.exit(0);
225         return null;
226     }
227     
228     /**
229      * part of the standard interface
230      */

231     public final void setValueAt(Object JavaDoc value, int row, int col) {
232         Forecast f = (Forecast)wholeList.get(row);
233         switch (col) {
234             case 0: //name
235
f.setName((String JavaDoc)value);
236                 break;
237             case 1: //scenario
238
f.setScenario((String JavaDoc)value);
239                 break;
240             case 2: //submitted
241
f.setSubmitted((Boolean JavaDoc)value);
242                 break;
243             case 3: //revenue
244
f.setRevenue((Integer JavaDoc)value);
245                 break;
246             case 4: //profit
247
f.setProfit((Integer JavaDoc)value);
248                 break;
249             case 5: //margin
250
Double JavaDoc dd = (Double JavaDoc)value;
251                 if (dd.doubleValue() > 100.00)
252                     System.out.println("setting margin too high");
253                 f.setMargin((Double JavaDoc)value);
254                 break;
255             case 6: //close date
256
if (value == null) value = new Date();
257                 f.setCloseDate((Date)value);
258                 break;
259             case 7: //jan amt
260
f.setAmountJan((Integer JavaDoc)value);
261                 break;
262             case 8: //Feb amt
263
f.setAmountFeb((Integer JavaDoc)value);
264                 break;
265             case 9: //Mar amt
266
f.setAmountMar((Integer JavaDoc)value);
267                 break;
268             case 10: //Q1
269
f.setQ1((Integer JavaDoc)value);
270                 break;
271             case 11: //Apr amt
272
f.setAmountApr((Integer JavaDoc)value);
273                 break;
274             case 12: //May amt
275
f.setAmountMay((Integer JavaDoc)value);
276                 break;
277             case 13: //Jun amt
278
f.setAmountJun((Integer JavaDoc)value);
279                 break;
280             case 14: //Q2
281
f.setQ2((Integer JavaDoc)value);
282                 break;
283             case 15: //Jul amt
284
f.setAmountJul((Integer JavaDoc)value);
285                 break;
286             case 16: //Aug amt
287
f.setAmountAug((Integer JavaDoc)value);
288                 break;
289             case 17: //Sep amt
290
f.setAmountSep((Integer JavaDoc)value);
291                 break;
292             case 18: //Q3
293
f.setQ3((Integer JavaDoc)value);
294                 break;
295             case 19: //Oct amt
296
f.setAmountOct((Integer JavaDoc)value);
297                 break;
298             case 20: //Nov amt
299
f.setAmountNov((Integer JavaDoc)value);
300                 break;
301             case 21: //dec amt
302
f.setAmountDec((Integer JavaDoc)value);
303                 break;
304             case 22: //Q4
305
f.setQ4((Integer JavaDoc)value);
306                 break;
307             default:
308                 System.out.println("oops its dorked");
309                 break;
310         }
311
312         //fireTableCellUpdated(row, col);
313
fireTableRowsUpdated(row, row);
314     }
315
316     /**
317      * part of the standard interface
318      */

319     public final boolean isCellEditable(int row, int col) {
320         //dont allow totals to be edited
321
if ((col == 3) || (col == 4))
322             return false;
323
324         //dont allow table editing of the submitted flag this forces
325
//the user to use the menu button to perform the submit, this
326
//is safer than letting them edit this table col directly
327
if (col == 2)
328             return false;
329
330         //if submitted == true then don't allow any col to be edited
331
Boolean JavaDoc submitted = (Boolean JavaDoc)(getValueAt(row, 2));
332         if (submitted.booleanValue() == true)
333             return false;
334
335     
336         //dont allow editing of the Q1-Q4 amounts
337
if ( ( col == 10) || (col == 14) || (col == 18) || (col == 22))
338             return false;
339
340         Class JavaDoc cls = getColumnClass(col);
341         String JavaDoc name = getColumnName(col);
342
343         return true;
344     }
345
346     /**
347      * set the table's language
348      */

349     public final void setLang() {
350
351         Whiteboard wb = MainWindow.getWhiteboard();
352
353         columnNames[0] = wb.getLang().getString("forecast");
354         columnNames[1] = wb.getLang().getString("scenario");
355         columnNames[2] = wb.getLang().getString("submitted");
356         columnNames[3] = wb.getLang().getString("totalRev");
357         columnNames[4] = wb.getLang().getString("totalProfit");
358         columnNames[5] = wb.getLang().getString("profitMargin");
359         columnNames[6] = wb.getLang().getString("closeDate");
360         columnNames[7] = wb.getLang().getString("jan");
361         columnNames[8] = wb.getLang().getString("feb");
362         columnNames[9] = wb.getLang().getString("mar");
363         columnNames[10] = wb.getLang().getString("Q1");
364         columnNames[11] = wb.getLang().getString("apr");
365         columnNames[12] = wb.getLang().getString("may");
366         columnNames[13] = wb.getLang().getString("jun");
367         columnNames[14] = wb.getLang().getString("Q2");
368         columnNames[15] = wb.getLang().getString("jul");
369         columnNames[16] = wb.getLang().getString("aug");
370         columnNames[17] = wb.getLang().getString("sep");
371         columnNames[18] = wb.getLang().getString("Q3");
372         columnNames[19] = wb.getLang().getString("oct");
373         columnNames[20] = wb.getLang().getString("nov");
374         columnNames[21] = wb.getLang().getString("dec");
375         columnNames[22] = wb.getLang().getString("Q4");
376     }
377 }
378
Popular Tags