KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > gui > ForecastPanel


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

18 /**
19  * This class implements the SellWin Forecast Panel which
20  * displays in the Forecast tab in the GUI. All information
21  * about an opportunity's set of Forecasts is displayed
22  * in this window.
23  */

24 public class ForecastPanel extends JPanel implements GUIChars {
25
26     private JComboBox scenarios = new JComboBox(Forecast.SCENARIOS);
27
28     private ForecastPanel thisPanel = null;
29     private JPopupMenu popup = new JPopupMenu();
30
31     private JPanel jPanel1;
32     private JButton applyButton;
33     private JMenuItem addMenuItem;
34     private JMenuItem copyMenuItem;
35     private JMenuItem deleteMenuItem;
36     private JMenuItem submitMenuItem;
37     private final javax.swing.JPanel JavaDoc mainPanel = new JPanel();
38     private JLabel label1;
39     private JTextField oppName;
40     private JLabel custLabel;
41     private JTextField custName;
42     private JLabel tableLabel;
43     private JPanel forecastsTablePanel = new JPanel();
44     private JScrollPane tableScrollPane;
45     private final javax.swing.JTable JavaDoc table = new JTable();
46
47     private Whiteboard wb;
48     private Opportunity currentOpp;
49     private ForecastTableModel model = null;
50
51     /**
52      * construct a forecast panel
53      */

54     public ForecastPanel() {
55         super();
56
57         thisPanel = this;
58
59         wb = MainWindow.getWhiteboard();
60     
61         initComponents();
62
63         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
64         table.getSelectionModel().addListSelectionListener(
65             new ListSelectionListener() {
66                 public void valueChanged(ListSelectionEvent e) {
67                     if (e.getValueIsAdjusting()) return; //ignore these extra msgs
68
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
69                 }
70             }
71         );
72
73         //start with a blank table
74
model = new ForecastTableModel(new ArrayList());
75         table.setModel(model);
76         initializeScenarioColumn();
77         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
78
79
80         submitMenuItem.addActionListener(
81             new ActionListener() {
82                 public void actionPerformed(ActionEvent e) {
83                     int opt = JOptionPane.showConfirmDialog(thisPanel,
84                         "Continue with submit?");
85                     if (opt != JOptionPane.YES_OPTION) return;
86
87                     ListSelectionModel m = table.getSelectionModel();
88                     if (m.getLeadSelectionIndex() >= 0) {
89                         Forecast row = model.getForecast(m.getLeadSelectionIndex());
90                         row.setSubmitted(new Boolean JavaDoc(true));
91                         table.tableChanged(new TableModelEvent(model));
92                         try {
93                             wb.updateForecast(row);
94                         } catch (AngError x) {
95                             ErrorHandler.show(thisPanel, x);
96                         }
97                     } else
98                         JOptionPane.showMessageDialog(thisPanel,
99                             "User Error",
100                             "Please select a row first.",
101                             JOptionPane.INFORMATION_MESSAGE);
102                 }
103             }
104         );
105
106         copyMenuItem.addActionListener(
107             new ActionListener() {
108                 public void actionPerformed(ActionEvent e) {
109                     ListSelectionModel m = table.getSelectionModel();
110                     if (m.getLeadSelectionIndex() >= 0) {
111                         Forecast row = model.getForecast(m.getLeadSelectionIndex());
112                         Forecast copyForecast = row.copy();
113                         copyForecast.setName(copyForecast.getName() + "_copy");
114                         try {
115                             wb.addForecast(wb.getCurrentOpportunity().getPK(),
116                                 copyForecast);
117                             table.tableChanged(new TableModelEvent(model));
118                         } catch (Exception JavaDoc g) {
119                             ErrorHandler.show(thisPanel, g);
120                         }
121                     }
122                 }
123             }
124         );
125
126         deleteMenuItem.addActionListener(
127             new ActionListener() {
128                 public void actionPerformed(ActionEvent e) {
129                     ListSelectionModel m = table.getSelectionModel();
130                     if (m.getLeadSelectionIndex() >= 0) {
131                         Forecast row = model.getForecast(m.getLeadSelectionIndex());
132                         try {
133                             int option = JOptionPane.showConfirmDialog(
134                                 thisPanel, "Continue with delete?");
135                             if (option != JOptionPane.YES_OPTION)
136                                 return;
137                             wb.deleteForecast(
138                                 wb.getCurrentOpportunity().getPK(),
139                                 row.getPK());
140                             JOptionPane.showMessageDialog( thisPanel,
141                                 "Delete Successful", "Delete Successful",
142                                 JOptionPane.INFORMATION_MESSAGE);
143                             model.deleteForecast(m.getLeadSelectionIndex());
144                             table.tableChanged(new TableModelEvent(model));
145                         } catch (AngError x) {
146                             ErrorHandler.show(thisPanel, x);
147                         }
148                     }
149                 }
150             }
151         );
152
153         addMenuItem.addActionListener(
154             new ActionListener() {
155                 public void actionPerformed(ActionEvent e) {
156                     Opportunity opp = wb.getCurrentOpportunity();
157                     if (opp == null) {
158                         JOptionPane.showMessageDialog(thisPanel,
159                             wb.getLang().getString("selectOpp"),
160                             wb.getLang().getString("error"),
161                             JOptionPane.INFORMATION_MESSAGE);
162                         return;
163                     }
164                     String JavaDoc name = JOptionPane.showInputDialog("Please enter a forecast name.");
165                     Forecast f = new Forecast();
166                     f.setName(name);
167                     f.setCloseDate(opp.getCloseDate());
168                     try {
169                         wb.addForecast(opp.getPK(), f);
170                         model = new ForecastTableModel(opp.getForecasts());
171                         table.setModel(model);
172                         initializeScenarioColumn();
173                         table.tableChanged(new TableModelEvent(model));
174                     } catch (Exception JavaDoc g) {
175                         ErrorHandler.show(thisPanel, g);
176                     }
177                 }
178             }
179         );
180
181         setFonts();
182         setColors();
183
184         createPopup();
185         table.addMouseListener(new MouseAdapter() {
186             public void mousePressed(MouseEvent e) {
187                 if (e.isPopupTrigger())
188                     popup.show(table, e.getX(), e.getY());
189             }
190
191             public void mouseReleased(MouseEvent e) {
192                 if (e.isPopupTrigger())
193                     popup.show(table, e.getX(), e.getY());
194             }
195
196             public void mouseClicked(MouseEvent e) {
197                 if (e.isPopupTrigger())
198                     popup.show(table, e.getX(), e.getY());
199             }
200         });
201
202         table.getTableHeader().addMouseListener(new MouseAdapter() {
203             public void mousePressed(MouseEvent e) {
204                 if (e.isPopupTrigger())
205                     popup.show(table, e.getX(), e.getY());
206             }
207
208             public void mouseReleased(MouseEvent e) {
209                 if (e.isPopupTrigger())
210                     popup.show(table, e.getX(), e.getY());
211             }
212
213             public void mouseClicked(MouseEvent e) {
214                 if (e.isPopupTrigger())
215                     popup.show(table, e.getX(), e.getY());
216             }
217         });
218
219         tableScrollPane.addMouseListener(new MouseAdapter() {
220             public void mousePressed(MouseEvent e) {
221                 if (e.isPopupTrigger())
222                     popup.show(table, e.getX(), e.getY());
223             }
224
225             public void mouseReleased(MouseEvent e) {
226                 if (e.isPopupTrigger())
227                     popup.show(table, e.getX(), e.getY());
228             }
229
230             public void mouseClicked(MouseEvent e) {
231                 if (e.isPopupTrigger())
232                     popup.show(table, e.getX(), e.getY());
233             }
234         });
235
236         applyButton.addActionListener(
237             new ActionListener() {
238                 public void actionPerformed(ActionEvent e) {
239                     int opt = JOptionPane.showConfirmDialog(thisPanel,
240                         "Continue with update?");
241                     if (opt != JOptionPane.YES_OPTION) return;
242
243                     ListSelectionModel m = table.getSelectionModel();
244                     if (m.getLeadSelectionIndex() >= 0) {
245                         Forecast row = model.getForecast(m.getLeadSelectionIndex());
246                         table.tableChanged(new TableModelEvent(model));
247                         try {
248                             wb.updateForecast(row);
249                         } catch (AngError x) {
250                             ErrorHandler.show(thisPanel, x);
251                         }
252                     } else
253                         JOptionPane.showMessageDialog(thisPanel,
254                             "User Error",
255                             "Please select a row first.",
256                             JOptionPane.INFORMATION_MESSAGE);
257                 }
258             }
259         );
260
261         oppName.setEditable(false);
262         custName.setEditable(false);
263
264         scenarios.addActionListener(
265             new ActionListener() {
266                 public void actionPerformed(ActionEvent e) {
267                     ListSelectionModel m = table.getSelectionModel();
268                     if (m.getLeadSelectionIndex() >= 0) {
269                         Forecast row = model.getForecast(m.getLeadSelectionIndex());
270                         row.factorChange();
271                     }
272                 }
273             }
274         );
275
276         initTableColumnSize();
277         setLang();
278     }
279
280     /**
281      * create a menu popup that will hover over the table
282      */

283     private final void createPopup() {
284         popup.add(addMenuItem);
285         popup.add(copyMenuItem);
286         popup.add(deleteMenuItem);
287         popup.add(submitMenuItem);
288     }
289
290     /**
291      * refresh the data on the screen using the current
292      * opportunity
293      */

294     public final void refreshOpportunity() {
295         currentOpp = wb.getCurrentOpportunity();
296
297         setSecurity();
298
299         Utility.strSet(custName, currentOpp.getCustomer().getName());
300         Utility.strSet(oppName, currentOpp.getName());
301         ArrayList rows=null;
302         if (currentOpp.getForecasts().size() == 0)
303             rows = new ArrayList();
304         else
305             rows = currentOpp.getForecasts();
306
307         model = new ForecastTableModel(rows);
308         table.setModel(model);
309         initializeScenarioColumn();
310         initializeDateColumn();
311         table.setDefaultRenderer(Double JavaDoc.class, new MoneyRenderer());
312         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
313     }
314
315     /**
316      * set the screen's colors
317      */

318     public final void setColors() {
319         label1.setForeground(MainWindow.LETTERS);
320         custLabel.setForeground(MainWindow.LETTERS);
321         tableLabel.setForeground(MainWindow.LETTERS);
322         table.setBackground(Color.white);
323     }
324
325
326     /**
327      * set the screen's fonts
328      */

329     public final void setFonts() {
330         applyButton.setFont(MainWindow.LABEL_FONT);
331         addMenuItem.setFont(MainWindow.LABEL_FONT);
332         copyMenuItem.setFont(MainWindow.LABEL_FONT);
333         deleteMenuItem.setFont(MainWindow.LABEL_FONT);
334         submitMenuItem.setFont(MainWindow.LABEL_FONT);
335         label1.setFont(MainWindow.LABEL_FONT);
336         oppName.setFont(MainWindow.FIELD_FONT);
337         custLabel.setFont(MainWindow.LABEL_FONT);
338         custName.setFont(MainWindow.FIELD_FONT);
339         tableLabel.setFont(MainWindow.LABEL_FONT);
340         table.setFont(MainWindow.FIELD_FONT);
341     }
342
343     /**
344      * build and initialize the screen's components
345      */

346     private final void initComponents() {
347         jPanel1 = new JPanel();
348         applyButton = new JButton("Apply");
349         addMenuItem = new JMenuItem();
350         copyMenuItem = new JMenuItem();
351         deleteMenuItem = new JMenuItem();
352         submitMenuItem = new JMenuItem();
353         label1 = new JLabel();
354         oppName = new JTextField();
355         custLabel = new JLabel();
356         custName = new JTextField();
357         tableLabel = new JLabel();
358         forecastsTablePanel.setMinimumSize(new Dimension(700,220));
359         forecastsTablePanel.setPreferredSize(new Dimension(700,220));
360         forecastsTablePanel.setMaximumSize(new Dimension(700,220));
361         tableScrollPane = new JScrollPane();
362         tableScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
363         tableScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
364         tableScrollPane.setMinimumSize(new Dimension(650,180));
365         tableScrollPane.setPreferredSize(new Dimension(650,180));
366         tableScrollPane.setMaximumSize(new Dimension(650,180));
367
368
369         setLayout(new java.awt.BorderLayout JavaDoc());
370
371         addMenuItem.setText("Add");
372         copyMenuItem.setText("Copy");
373         deleteMenuItem.setText("Delete");
374         submitMenuItem.setText("Submit");
375
376         jPanel1.add(applyButton);
377         add(jPanel1, java.awt.BorderLayout.SOUTH);
378
379         mainPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
380         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
381
382         mainPanel.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
383         label1.setText("Opportunity");
384         label1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
385         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
386         gridBagConstraints1.gridx = 0;
387         gridBagConstraints1.gridy = 0;
388         gridBagConstraints1.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT);
389         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
390         mainPanel.add(label1, gridBagConstraints1);
391
392         oppName.setText("");
393         oppName.setMinimumSize(new Dimension(180,Prefs.FIELD_HEIGHT));
394         oppName.setPreferredSize(new Dimension(180,Prefs.FIELD_HEIGHT));
395         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
396         gridBagConstraints1.gridx = 1;
397         gridBagConstraints1.gridy = 0;
398         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
399         gridBagConstraints1.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT);
400         mainPanel.add(oppName, gridBagConstraints1);
401
402         custLabel.setText("Customer");
403         custLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
404         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
405         gridBagConstraints1.gridx = 0;
406         gridBagConstraints1.gridy = 1;
407         gridBagConstraints1.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT);
408         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
409         mainPanel.add(custLabel, gridBagConstraints1);
410
411         custName.setText("");
412         custName.setMinimumSize(new Dimension(180,Prefs.FIELD_HEIGHT));
413         custName.setPreferredSize(new Dimension(180,Prefs.FIELD_HEIGHT));
414         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
415         gridBagConstraints1.gridx = 1;
416         gridBagConstraints1.gridy = 1;
417         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
418         gridBagConstraints1.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT);
419         mainPanel.add(custName, gridBagConstraints1);
420
421
422         tableScrollPane.setViewportView(table);
423
424         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
425         gridBagConstraints1.gridx = 0;
426         gridBagConstraints1.gridy = 4;
427         gridBagConstraints1.gridwidth = 3;
428         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
429         gridBagConstraints1.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT);
430         mainPanel.add(tableScrollPane, gridBagConstraints1);
431
432         add(mainPanel, java.awt.BorderLayout.CENTER);
433
434     }
435
436     /**
437      * initialize the scenario column's renderers and
438      * editors of the table
439      */

440     private void initializeScenarioColumn() {
441         TableColumnModel colModel = table.getColumnModel();
442         TableColumn col = colModel.getColumn(1);
443         col.setCellEditor(new DefaultCellEditor(scenarios));
444         DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
445         col.setCellRenderer(renderer);
446     }
447
448     /**
449      * initialize the date column - close date
450      */

451     private final void initializeDateColumn() {
452         final JButton button = new JButton();
453
454         final DateEditor editor = new DateEditor(button, table);
455         table.setDefaultEditor(Date.class, editor);
456
457         //pass in the editor so that we can pass back the entered date
458
final CalendarDialog editDialog = new CalendarDialog(MainWindow.getMainParent(), editor, true);
459
460         //code that brings up the date editor dialog
461
button.addActionListener(new ActionListener() {
462             public void actionPerformed(ActionEvent e) {
463                 editDialog.setDate(editor.getDate());
464                 editDialog.show();
465                 button.setText(Prefs.dateTimeFormat.format(editor.getDate()));
466             }
467         });
468
469         table.setDefaultRenderer(Date.class, new DateTimeRenderer());
470     }
471
472
473     /**
474      * enable or disable fields based upon the security
475      * settings
476      */

477     private final void setSecurity() {
478         try {
479             if (wb.getLogin().forecastWritable()) {
480                 applyButton.setEnabled(true);
481                 addMenuItem.setEnabled(true);
482                 copyMenuItem.setEnabled(true);
483                 deleteMenuItem.setEnabled(true);
484                 submitMenuItem.setEnabled(true);
485             } else {
486                 applyButton.setEnabled(false);
487                 addMenuItem.setEnabled(false);
488                 copyMenuItem.setEnabled(false);
489                 deleteMenuItem.setEnabled(false);
490                 submitMenuItem.setEnabled(false);
491             }
492         } catch (AngError e) {
493             ErrorHandler.show(thisPanel, e);
494         }
495     }
496
497     /**
498      * clear the screen's components
499      */

500     public final void clearOpportunity() {
501         Utility.strSet(custName, null);
502         Utility.strSet(oppName, null);
503         ArrayList rows = new ArrayList();
504         model = new ForecastTableModel(rows);
505         table.setModel(model);
506         initializeScenarioColumn();
507     }
508
509     /**
510      * set the screen's language
511      */

512     public final void setLang() {
513         model.setLang();
514         addMenuItem.setText(wb.getLang().getString("add"));
515         copyMenuItem.setText(wb.getLang().getString("copy"));
516         deleteMenuItem.setText(wb.getLang().getString("delete"));
517         submitMenuItem.setText(wb.getLang().getString("submit"));
518         label1.setText(wb.getLang().getString("opportunity"));
519         custLabel.setText(wb.getLang().getString("customer"));
520         applyButton.setText(wb.getLang().getString("apply"));
521     }
522
523     private final void initTableColumnSize() {
524         TableColumn col;
525
526         for (int i=0;i<ForecastTableModel.columnNames.length;i++) {
527             col = table.getColumnModel().getColumn(i);
528             col.setPreferredWidth(100);
529         }
530     }
531
532 }
533
Popular Tags