KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > gui > ActivPanel


1 package sellwin.gui;
2
3 import sellwin.domain.*;
4 import sellwin.utils.*;
5
6 import javax.swing.*;
7 import javax.swing.border.*;
8 import javax.swing.event.*;
9 import javax.swing.table.*;
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.util.*;
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 Activity Panel tab
20  * accessed by the users from the MainWindow ...
21  * maintenance of an Opportunities Activities is
22  * done here
23  */

24 public class ActivPanel extends JPanel implements GUIChars {
25
26     private JPopupMenu popup = new JPopupMenu();
27     private ActivityTableModel tableModel = null;
28
29     private final javax.swing.JPanel JavaDoc northPanel = new JPanel();
30     private javax.swing.JPanel JavaDoc centerPanel;
31     private JLabel activTableLabel;
32     private JTable activTable;
33     private JScrollPane activTableScrollPane;
34
35     private Whiteboard wb;
36     private JPanel thisPanel;
37
38     private JMenuItem addMenuItem = new JMenuItem("Add");
39     private JMenuItem deleteMenuItem = new JMenuItem("Delete");
40     private JMenuItem recipientsMenuItem = new JMenuItem("Add Recipient");
41     private JButton applyButton = new JButton("Apply");
42     private JButton checkButton = new JButton("Check Alarms");
43
44     /**
45      * construct the activity panel and initialize it
46      */

47     public ActivPanel() {
48         super(new BorderLayout());
49
50         thisPanel = this;
51
52         wb = MainWindow.getWhiteboard();
53
54         initComponents();
55
56         JPanel buttonPanel = new JPanel();
57         buttonPanel.add(applyButton);
58         buttonPanel.add(checkButton);
59
60         add(buttonPanel, BorderLayout.SOUTH);
61
62         recipientsMenuItem.addActionListener(
63             new ActionListener() {
64                 public void actionPerformed(ActionEvent e) {
65                     LogWrite.write("add recipient pressed");
66                 }
67             }
68         );
69
70         addMenuItem.addActionListener(
71             new ActionListener() {
72                 public void actionPerformed(ActionEvent e) {
73                     try {
74                         if (wb.getCurrentOpportunity() == null) {
75                             JOptionPane.showMessageDialog(
76                             thisPanel,
77                             wb.getLang().getString("selectOpp"),
78                             wb.getLang().getString("error"),
79                             JOptionPane.INFORMATION_MESSAGE);
80                             return;
81                         }
82
83                         String JavaDoc name = JOptionPane.showInputDialog(wb.getLang().getString("enterAct"));
84                         
85                         Activity newActivity = new Activity(name);
86                         newActivity.setSubject(name);
87                         newActivity.setModifiedBy(wb.getCurrentUser().getAddress().getFormattedName());
88
89                         newActivity.setPK(
90                             wb.addActivity(
91                                 wb.getCurrentOpportunity().getPK(),
92                                 newActivity)
93                         );
94                         activTable.tableChanged(new TableModelEvent(tableModel));
95                         wb.getCurrentOpportunity().addActivity(newActivity);
96                         int actCt = wb.getCurrentOpportunity().getActivities().size();
97                         if (actCt > 0)
98                             activTable.setRowSelectionInterval(actCt-1, actCt-1);
99                         JOptionPane.showMessageDialog(
100                             thisPanel, "Add Successful",
101                             "Add Successful",
102                             JOptionPane.INFORMATION_MESSAGE);
103                     } catch (Exception JavaDoc f) {
104                         ErrorHandler.show(thisPanel, f);
105                     }
106                 }
107             }
108         );
109
110         applyButton.addActionListener(
111             new ActionListener() {
112                 public void actionPerformed(ActionEvent e) {
113                     int index = activTable.getSelectedRow();
114                     if (index < 0) {
115                         JOptionPane.showConfirmDialog(
116                                     thisPanel,
117                                     "Please select the row you wish to update.",
118                                     "User Error",
119                                     JOptionPane.ERROR_MESSAGE);
120                                 return;
121                     }
122
123                     int opt = JOptionPane.showConfirmDialog(thisPanel,
124                         "Continue with update?", "Confirm",
125                         JOptionPane.YES_NO_OPTION);
126                     if (opt == JOptionPane.YES_OPTION) {
127                         try {
128                             //get a handle to the in-memory copy of the act
129
ArrayList acts = wb.getCurrentOpportunity().getActivities();
130                             Activity temp = (Activity)(acts.get(index));
131                             Activity actCopy = temp.copy();
132
133                             //update the activity in the database
134
wb.updateActivity(
135                                 wb.getCurrentOpportunity().getPK(),
136                                 actCopy);
137                             JOptionPane.showMessageDialog(
138                                 thisPanel, "Update Successful",
139                                 "Update Successful",
140                                 JOptionPane.INFORMATION_MESSAGE);
141
142                             //since the database update succeeded, update
143
//the in-memory activity
144
} catch (Exception JavaDoc f) {
145                             ErrorHandler.show(thisPanel, f);
146                         }
147                     }
148                 }
149             }
150         );
151     
152         deleteMenuItem.addActionListener(
153             new ActionListener() {
154                 public void actionPerformed(ActionEvent e) {
155                     int opt = JOptionPane.showConfirmDialog(thisPanel,
156                         "Continue with delete?", "Confirm",
157                         JOptionPane.YES_NO_OPTION);
158                     if (opt == JOptionPane.YES_OPTION) {
159                         try {
160                             Opportunity opp = wb.getCurrentOpportunity();
161                             int index = activTable.getSelectedRow();
162                             ArrayList acts = opp.getActivities();
163                             Activity a = (Activity)(acts.get(index));
164                             wb.deleteActivity(
165                                 opp.getPK(),
166                                 a.getPK());
167                             opp.deleteActivity(a);
168                             activTable.tableChanged(new TableModelEvent(tableModel));
169                             
170                             JOptionPane.showMessageDialog(
171                                 thisPanel, "Delete Successful",
172                                 "Delete Successful",
173                                 JOptionPane.INFORMATION_MESSAGE);
174                         } catch (Exception JavaDoc f) {
175                             ErrorHandler.show(thisPanel, f);
176                         }
177                     }
178                 }
179             }
180         );
181
182
183
184         checkButton.addActionListener(
185             new ActionListener() {
186                 public void actionPerformed(ActionEvent e) {
187                     Activity alarm;
188                     AlarmDialog ad;
189                     try {
190                         ArrayList alarms = wb.getAlarms();
191                         LogWrite.write("ActivityChecker sees "+ alarms.size() + " alarms");
192                         for (int i=0;i<alarms.size();i++) {
193                             alarm = (Activity)alarms.get(i);
194                             ad = new AlarmDialog(MainWindow.getMainParent(), false, alarm);
195                             ad.show();
196                         }
197                     } catch (AngError x) {
198                         ErrorHandler.show(MainWindow.getMainParent(), x);
199                     }
200                 }
201             }
202         );
203
204
205         setColors();
206
207         createPopup();
208
209         activTable.addMouseListener(new MouseAdapter() {
210             public void mousePressed(MouseEvent e) {
211                 if (e.isPopupTrigger())
212                     popup.show(activTable, e.getX(), e.getY());
213             }
214             public void mouseReleased(MouseEvent e) {
215                 if (e.isPopupTrigger())
216                     popup.show(activTable, e.getX(), e.getY());
217             }
218             public void mouseClicked(MouseEvent e) {
219                 if (e.isPopupTrigger())
220                     popup.show(activTable, e.getX(), e.getY());
221             }
222         });
223         activTable.getTableHeader().addMouseListener(new MouseAdapter() {
224             public void mousePressed(MouseEvent e) {
225                 if (e.isPopupTrigger())
226                     popup.show(activTable, e.getX(), e.getY());
227             }
228             public void mouseReleased(MouseEvent e) {
229                 if (e.isPopupTrigger())
230                     popup.show(activTable, e.getX(), e.getY());
231             }
232             public void mouseClicked(MouseEvent e) {
233                 if (e.isPopupTrigger())
234                     popup.show(activTable, e.getX(), e.getY());
235             }
236         });
237         activTableScrollPane.addMouseListener(new MouseAdapter() {
238             public void mousePressed(MouseEvent e) {
239                 if (e.isPopupTrigger())
240                     popup.show(activTable, e.getX(), e.getY());
241             }
242             public void mouseReleased(MouseEvent e) {
243                 if (e.isPopupTrigger())
244                     popup.show(activTable, e.getX(), e.getY());
245             }
246             public void mouseClicked(MouseEvent e) {
247                 if (e.isPopupTrigger())
248                     popup.show(activTable, e.getX(), e.getY());
249             }
250         });
251
252
253         initializeDateColumn();
254         initializeDescColumn();
255
256         setFonts();
257         tableModel = new ActivityTableModel(new ArrayList());
258         activTable.setModel(tableModel);
259         setLang();
260     }
261
262     /**
263      * set the date columns to a certain width
264      */

265     private final void initializeColumnWidths() {
266         TableColumnModel colModel = activTable.getColumnModel();
267         TableColumn col = colModel.getColumn(1); //the Start Date column
268
col.setPreferredWidth(130);
269     }
270
271     /**
272      * iniitialize the activity 'type' column ...this
273      * ties a cell renderer to a specific table column
274      */

275     private final void initializeTypeColumn() {
276         JComboBox myTypes = new JComboBox(Activity.TYPES);
277         TableColumnModel colModel = activTable.getColumnModel();
278         TableColumn col = colModel.getColumn(2); //the type column
279
col.setCellEditor(new DefaultCellEditor(myTypes));
280
281         DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
282         col.setCellRenderer(renderer);
283
284         TableCellRenderer headerRenderer = col.getHeaderRenderer();
285         if (headerRenderer instanceof DefaultTableCellRenderer) {
286             ((DefaultTableCellRenderer)headerRenderer).setToolTipText(
287                 "Click the type to see a list of choices");
288         }
289     }
290
291     /**
292      * initialize the date column in the activity table
293      */

294     private final void initializeDateColumn() {
295
296         final JButton button = new JButton();
297
298         final DateEditor editor = new DateEditor(button, activTable);
299         activTable.setDefaultEditor(Date.class, editor);
300
301         //pass in the editor so that we can pass back the entered date
302
final CalendarDialog editDialog = new CalendarDialog(MainWindow.getMainParent(), editor, true);
303
304         //code that brings up the date editor dialog
305
button.addActionListener(new ActionListener() {
306             public void actionPerformed(ActionEvent e) {
307                 editDialog.setDate(editor.getDate());
308                 editDialog.show();
309                 button.setText(Prefs.dateTimeFormat.format(editor.getDate()));
310             }
311         });
312
313         activTable.setDefaultRenderer(Date.class, new DateTimeRenderer());
314     }
315
316     /**
317      * initialize the desc column in the activity table
318      */

319     private final void initializeDescColumn() {
320
321         final JButton button = new JButton();
322
323         final DescEditor editor = new DescEditor(button, activTable);
324         activTable.setDefaultEditor(String JavaDoc.class, editor);
325
326         //pass in the editor so that we can pass back the entered string
327
final DescEditorDialog editDialog = new DescEditorDialog(
328             MainWindow.getMainParent(), editor);
329
330         //code that brings up the date editor dialog
331
button.addActionListener(new ActionListener() {
332             public void actionPerformed(ActionEvent e) {
333                 editDialog.setDesc(editor.getDesc());
334                 editDialog.show();
335                 button.setText(editor.getDesc());
336             }
337         });
338
339         activTable.setDefaultRenderer(String JavaDoc.class, new DescRenderer());
340     }
341
342
343     /**
344      * refresh the screen with the current opportunity
345      */

346     public final void refreshOpportunity() {
347         setSecurity();
348         Opportunity opp = wb.getCurrentOpportunity();
349
350         tableModel = new ActivityTableModel(opp.getActivities());
351         activTable.setModel(tableModel);
352         if (opp.getActivities().size() > 0) {
353             activTable.setRowSelectionInterval(0,0);
354         }
355
356         initializeTypeColumn();
357         initializeColumnWidths();
358     }
359
360     /**
361      * clear the screen
362      */

363     public final void clearOpportunity() {
364         tableModel = new ActivityTableModel(new ArrayList());
365         activTable.setModel(tableModel);
366     }
367
368     /**
369      * set all the screen's widgets to a color
370      */

371     public final void setColors() {
372         activTableLabel.setForeground(MainWindow.LETTERS);
373     }
374
375     /**
376      * set the screen's fields to a specific font
377      */

378     public final void setFonts() {
379         centerPanel.setFont(MainWindow.LABEL_FONT);
380         addMenuItem.setFont(MainWindow.LABEL_FONT);
381         recipientsMenuItem.setFont(MainWindow.LABEL_FONT);
382         deleteMenuItem.setFont(MainWindow.LABEL_FONT);
383         applyButton.setFont(MainWindow.LABEL_FONT);
384         checkButton.setFont(MainWindow.LABEL_FONT);
385         activTableLabel.setFont(MainWindow.LABEL_FONT);
386     }
387
388     /**
389      * create the popup menu
390      */

391     private final void createPopup() {
392         popup.add(addMenuItem);
393         popup.add(deleteMenuItem);
394         popup.add(recipientsMenuItem);
395     }
396
397     /**
398      * initialize or build the screen's components
399      */

400     private final void initComponents() {
401         centerPanel = new JPanel();
402         activTableLabel = new JLabel("Activities");
403         activTable = new JTable();
404         activTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
405         activTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
406         activTableScrollPane = new JScrollPane(activTable);
407         activTableScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
408         activTableScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
409
410         activTableScrollPane.setPreferredSize(new Dimension(600, 270));
411         activTableScrollPane.setMinimumSize(new Dimension(600, 270));
412         
413         setBorder(new javax.swing.border.EtchedBorder JavaDoc());
414         add(northPanel);
415         
416         centerPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
417         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
418         
419         centerPanel.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
420         
421        
422         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
423         gridBagConstraints1.gridx = 0;
424         gridBagConstraints1.gridy = 1;
425         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
426         gridBagConstraints1.ipadx = 9;
427         gridBagConstraints1.ipady = 9;
428         gridBagConstraints1.insets = new Insets(Prefs.I_TOP,Prefs.I_LF,Prefs.I_BOTT,Prefs.I_RT);
429         gridBagConstraints1.anchor = java.awt.GridBagConstraints.EAST;
430         centerPanel.add(activTableScrollPane, gridBagConstraints1);
431         
432         add(centerPanel);
433     }
434
435     /**
436      * set the screen's widgets to visible or not
437      * depending upon a user's security profile
438      */

439     private final void setSecurity() {
440         try {
441             if (wb.getLogin().activityWritable()) {
442                 addMenuItem.setEnabled(true);
443                 recipientsMenuItem.setEnabled(true);
444                 deleteMenuItem.setEnabled(true);
445                 applyButton.setEnabled(true);
446             } else {
447                 addMenuItem.setEnabled(false);
448                 recipientsMenuItem.setEnabled(false);
449                 deleteMenuItem.setEnabled(false);
450                 applyButton.setEnabled(false);
451             }
452         } catch (AngError e) {
453             ErrorHandler.show(thisPanel, e);
454         }
455     }
456
457     /**
458      * set the screen's language
459      */

460     public final void setLang() {
461         tableModel.setLang();
462         addMenuItem.setText(wb.getLang().getString("add"));
463         recipientsMenuItem.setText(wb.getLang().getString("recipients"));
464         deleteMenuItem.setText(wb.getLang().getString("delete"));
465         applyButton.setText(wb.getLang().getString("apply"));
466         checkButton.setText(wb.getLang().getString("checkAlarms"));
467         activTableLabel.setText(wb.getLang().getString("activities"));
468     }
469 }
470
Popular Tags