KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > gui > OppListDialog


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

13 /**
14  * This class implements the opportunity list
15  * dialog which a user will be shown their list
16  * of opportunities and from which they can add
17  * or delete opportunities.
18  */

19 public class OppListDialog extends JDialog implements GUIChars {
20
21     private JPopupMenu popup = new JPopupMenu();
22
23     private Whiteboard wb;
24     private JDialog thisDialog = null;
25     private final JList list = new JList();
26     private JButton openButton = new JButton("Open");
27     private JButton closeButton = new JButton("Close");
28     private JMenuItem addMenuButton = new JMenuItem("Add");
29     private JMenuItem delMenuButton = new JMenuItem("Delete ");
30     private JPanel mainPanel = new JPanel(new BorderLayout());
31     private ArrayList oppNames;
32
33     /**
34      * construct the opp list dialog
35      */

36     public OppListDialog() {
37         super();
38
39         thisDialog = this;
40         wb = MainWindow.getWhiteboard();
41
42         setTitle(wb.getLang().getString("opps"));
43
44         setSize(450, 300);
45
46         setFonts();
47         setLang();
48
49         JScrollPane pane = new JScrollPane(list);
50
51         mainPanel.add(pane, BorderLayout.CENTER);
52
53         JPanel buttonPanel = new JPanel();
54         buttonPanel.add(openButton);
55         buttonPanel.add(closeButton);
56         
57         popup.add(addMenuButton);
58         popup.add(delMenuButton);
59
60         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
61
62         getContentPane().add(mainPanel);
63
64         WindowListener l = new WindowAdapter() {
65             public void windowClosed(WindowEvent e) {
66             }
67
68             public void windowClosing(WindowEvent e) {
69                 hide();
70             }
71         };
72  
73         addWindowListener(l);
74
75         closeButton.addActionListener(
76             new ActionListener() {
77                 public void actionPerformed(ActionEvent e) {
78                     thisDialog.hide();
79                 }
80             }
81         );
82
83         delMenuButton.addActionListener(
84             new ActionListener() {
85                 public void actionPerformed(ActionEvent e) {
86                     int listIndex = list.getSelectedIndex();
87                     System.out.println("selected index =" + listIndex);
88                     try {
89                         ArrayList oppIndex = wb.getOpportunityIndex();
90                         OppIndex oi = (OppIndex)oppIndex.get(listIndex);
91                         int option = JOptionPane.showConfirmDialog(thisDialog, "Delete "+ oi.getName() + " ?");
92                         if (option != JOptionPane.YES_OPTION)
93                             return;
94
95                         thisDialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
96                         wb.deleteOpportunity(oi.getPK());
97                         if (oi.getPK() == wb.getCurrentOpportunity().getPK())
98                             MainWindow.clearOpportunity();
99                         ArrayList names = wb.getOpportunityNames();
100                         setData(names.toArray());
101                         thisDialog.setCursor(Cursor.getDefaultCursor());
102                     } catch (Exception JavaDoc f) {
103                         ErrorHandler.show(thisDialog, f);
104                     }
105                     thisDialog.hide();
106                 }
107             }
108         );
109
110         addMenuButton.addActionListener(
111             new ActionListener() {
112                 public void actionPerformed(ActionEvent e) {
113                     try {
114                         ArrayList groups = wb.getGroupsForUser();
115                         UserGroup ug;
116                         ArrayList groupNames = new ArrayList();
117                         for (int i=0;i<groups.size();i++) {
118                             ug = (UserGroup)groups.get(i);
119                             groupNames.add(ug.getName());
120                         }
121                         Object JavaDoc[] groupNamesList = groupNames.toArray();
122
123                         String JavaDoc val=JOptionPane.showInputDialog(wb.getLang().getString("enterOpp"));
124                         Opportunity opp = new Opportunity(val, wb.getCurrentUser().getAddress().getFormattedName());
125
126                         String JavaDoc gName = (String JavaDoc)JOptionPane.showInputDialog(
127                             thisDialog,
128                             wb.getLang().getString("chooseGroup"),
129                             wb.getLang().getString("selectGroup"),
130                             JOptionPane.PLAIN_MESSAGE,
131                             null,
132                             groupNamesList,
133                             groupNamesList[0]);
134
135                         opp.setGroupName(gName);
136                         //the following takes some defaults, these could be added to a wizard later on
137
opp.setPrimeSalesPerson(wb.getCurrentUser());
138
139                         TreeMap custNames = wb.getAllCustNames(true);
140                         Object JavaDoc[] custNamesList = custNames.values().toArray();
141
142                         if (custNamesList.length == 0) {
143                             JOptionPane.showMessageDialog(
144                                 thisDialog,
145                                 "There are no Customers defined, please add a customer first.",
146                                 "ERROR",
147                                 JOptionPane.ERROR_MESSAGE);
148                                 return;
149                         }
150
151                         String JavaDoc custName = (String JavaDoc)JOptionPane.showInputDialog(
152                             thisDialog,
153                             wb.getLang().getString("chooseCustomer"),
154                             wb.getLang().getString("selectCustomer"),
155                             JOptionPane.PLAIN_MESSAGE,
156                             null,
157                             custNamesList,
158                             custNamesList[0]);
159
160                         Customer cust = wb.getCustomer(custName);
161
162                         opp.setCustomer(cust);
163
164                         thisDialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
165                         wb.addOpportunity(opp);
166                         thisDialog.setCursor(Cursor.getDefaultCursor());
167                         wb.setCurrentOpportunity(opp);
168                         MainWindow.refreshOpportunity();
169                     } catch (Exception JavaDoc f) {
170                         ErrorHandler.show(thisDialog, f);
171                     }
172                     thisDialog.hide();
173                 }
174             }
175         );
176
177         list.addMouseListener(
178             new MouseAdapter() {
179                 public void mousePressed(MouseEvent e) {
180                     if (e.isPopupTrigger())
181                         popup.show(list, e.getX(), e.getY());
182                 }
183                 public void mouseReleased(MouseEvent e) {
184                     if (e.isPopupTrigger())
185                         popup.show(list, e.getX(), e.getY());
186                 }
187
188                 public void mouseClicked(MouseEvent e) {
189                     if (e.isPopupTrigger())
190                         popup.show(list, e.getX(), e.getY());
191                     else
192                     if (e.getClickCount() == 2) {
193                         clickOpp();
194                     }
195                 }
196             }
197         );
198         openButton.addActionListener(
199             new ActionListener() {
200                 public void actionPerformed(ActionEvent e) {
201                     clickOpp();
202                 }
203             }
204         );
205
206         getRootPane().setDefaultButton(openButton);
207
208     }
209
210     /**
211      * handle the case of an opportunity being
212      * clicked on
213      */

214     public final void clickOpp() {
215         String JavaDoc oppName = (String JavaDoc)list.getSelectedValue();
216         try {
217             Opportunity opp = wb.getOpportunityByName(oppName);
218             wb.setCurrentOpportunity(opp);
219             MainWindow.refreshOpportunity();
220             MainWindow.setItemsOn();
221             thisDialog.hide();
222         } catch (AngError e) {
223             ErrorHandler.show(thisDialog, e);
224         }
225     }
226
227     /**
228      * load a user's opp names
229      */

230     public final void loadUser() {
231         try {
232
233             oppNames = wb.getOpportunityNames();
234
235             setData(oppNames.toArray());
236         } catch (Exception JavaDoc e) {
237             ErrorHandler.show(thisDialog, e);
238         }
239     }
240
241     /**
242      * set the opp list data
243      * @param items the list of opps
244      */

245     public final void setData(Object JavaDoc[] items) {
246         DefaultListModel model = new DefaultListModel();
247         for (int i=0;i<items.length;i++)
248             model.addElement(items[i]);
249
250         list.setModel(model);
251
252         if (items.length > 0)
253             list.setSelectedIndex(0);
254     }
255
256     /**
257      * add a customer name to the list model
258      * @param c the Customer to add info from
259      */

260     public final void addCustomer(Customer c) {
261         DefaultListModel model = (DefaultListModel)list.getModel();
262         model.addElement(c.getName());
263     }
264
265     /**
266      * set the screen's fonts
267      */

268     public final void setFonts() {
269         list.setFont(MainWindow.FIELD_FONT);
270         openButton.setFont(MainWindow.LABEL_FONT);
271         closeButton.setFont(MainWindow.LABEL_FONT);
272         addMenuButton.setFont(MainWindow.LABEL_FONT);
273         delMenuButton.setFont(MainWindow.LABEL_FONT);
274     }
275
276     public void setColors() {}
277
278     /**
279      * set the screen's language
280      */

281     public final void setLang() {
282         setTitle(wb.getLang().getString("opps"));
283         openButton.setText(wb.getLang().getString("open"));
284         closeButton.setText(wb.getLang().getString("close"));
285         addMenuButton.setText(wb.getLang().getString("add"));
286         delMenuButton.setText(wb.getLang().getString("delete"));
287     }
288 }
289
Popular Tags