KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > gui > ProductSelectionDialog


1 package sellwin.gui;
2
3 import sellwin.domain.*;
4 import sellwin.utils.*;
5
6 import java.util.*;
7 import java.rmi.*;
8 import javax.swing.*;
9 import javax.swing.tree.*;
10 import javax.swing.event.*;
11 import java.awt.*;
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 lets a user select a product
20  */

21 public class ProductSelectionDialog extends JDialog implements GUIChars {
22     private Whiteboard wb = null;
23     private String JavaDoc currentGroup, currentLine;
24     private ArrayList currentProds;
25     private JDialog thisDialog;
26     private InventoryListener parent = null;
27
28     private JPanel buttonPanel;
29     private JButton addButton;
30     private JButton cancelButton;
31     private JPanel mainPanel;
32     private JSplitPane splitPane;
33     private JScrollPane treeScrollPane;
34     private JTree tree;
35     private JScrollPane listScrollPane;
36     private JList list;
37     private JPanel countPanel;
38     private JLabel countLabel;
39     private JTextField count;
40
41     /** Creates new form ProductSelectionDialog */
42     public ProductSelectionDialog(InventoryListener p) {
43         super(MainWindow.getMainParent(), true);
44
45         thisDialog = this;
46         parent = p;
47
48         wb = MainWindow.getWhiteboard();
49         setSize(430, 450);
50
51         initComponents();
52         setLang();
53
54         addButton.addActionListener(
55             new ActionListener() {
56                 public void actionPerformed(ActionEvent e) {
57                     QuoteLine x = getQuoteLineFromScreen();
58                     parent.addQuoteLine(x);
59                     thisDialog.hide();
60                 }
61             });
62
63         getRootPane().setDefaultButton(addButton);
64
65         cancelButton.addActionListener(
66             new ActionListener() {
67                 public void actionPerformed(ActionEvent e) {
68                     thisDialog.hide();
69                 }
70             });
71
72         loadProducts();
73
74         tree.addTreeSelectionListener(
75             new TreeSelectionListener() {
76                 public void valueChanged(TreeSelectionEvent e) {
77                     TreePath path = e.getPath();
78                     Object JavaDoc[] nodes = path.getPath();
79                     DefaultMutableTreeNode parentNode, node;
80                     for (int i=0;i<nodes.length;i++) {
81                         node = (DefaultMutableTreeNode)nodes[i];
82                         if (node.isLeaf()) {
83                             parentNode = (DefaultMutableTreeNode)node.getParent();
84                             updateModelsList(parentNode.toString(), node.toString());
85                         }
86                     }
87                 }
88             }
89         );
90
91         pack();
92
93     }
94
95     /**
96      * update the model list on the screen
97      * @param group the product group to search with
98      * @param line the product line to search with
99      */

100     public final void updateModelsList(String JavaDoc group, String JavaDoc line) {
101         cleanModelList();
102         try {
103             //
104
//this is where some caching will be required if
105
//the user has already downloaded products for this group
106
//and line combo
107
//
108

109             currentGroup = group;
110             currentLine = line;
111
112             currentProds = wb.getProductsForLine(group, line);
113             ListModel lmodel = list.getModel();
114             if (lmodel != null) {
115                 DefaultListModel model = (DefaultListModel)lmodel;
116                 Product prod;
117                 for (int i=0;i<currentProds.size();i++) {
118                     prod = (Product)(currentProds.get(i));
119                     model.addElement(prod.getName());
120                 }
121                 if (currentProds.size() > 0)
122                     list.setSelectedIndex(0);
123             }
124
125         } catch (Exception JavaDoc e) {
126             ErrorHandler.show(this, e);
127         }
128     }
129
130     /**
131      * load the products onto the screen
132      */

133     public final void loadProducts() {
134         try {
135             ArrayList matrix = wb.getProductMatrix();
136             tree.setModel(createTreeModel(matrix));
137             cleanModelList();
138         } catch (Exception JavaDoc e) {
139             ErrorHandler.show(this, e);
140         }
141     }
142
143     /**
144      * clean the model list
145      */

146     private final void cleanModelList() {
147         ListModel lmodel = list.getModel();
148         if (lmodel != null) {
149             DefaultListModel model = (DefaultListModel)lmodel;
150             model.removeAllElements();
151         }
152     }
153
154     /**
155      * create the product tree using a new
156      * product matrix
157      * @param matrix an array of product info to display
158      * @return the tree's model of data
159      */

160     private final DefaultTreeModel createTreeModel(ArrayList matrix) {
161         DefaultMutableTreeNode root = new DefaultMutableTreeNode("Groups");
162         DefaultMutableTreeNode groupNode, lineNode;
163         ArrayList lines;
164         String JavaDoc line;
165         MatrixObject mo;
166         for (int i=0;i<matrix.size();i++) {
167             mo = (MatrixObject)matrix.get(i);
168             mo.print();
169             groupNode = new DefaultMutableTreeNode(mo.getGroup());
170             lines = mo.getLines();
171             for (int j=0;j<lines.size();j++) {
172                 line = (String JavaDoc)lines.get(j);
173                 lineNode = new DefaultMutableTreeNode(line);
174                 groupNode.add(lineNode);
175             }
176             root.add(groupNode);
177         }
178         DefaultTreeModel model = new DefaultTreeModel(root);
179         return model;
180     }
181
182     /**
183      * get a quote line from the screen
184      * @return the QuoteLine
185      */

186     public final QuoteLine getQuoteLineFromScreen() {
187         QuoteLine line = new QuoteLine();
188         String JavaDoc name = (String JavaDoc)list.getSelectedValue();
189         //retrieve the rest of the product info for the product selected
190
try {
191             Product product = wb.getProduct(currentGroup, currentLine, name);
192             line.setProduct(product);
193             line.setQuantity(new Integer JavaDoc(count.getText()));
194         } catch (AngError e) {
195             ErrorHandler.show(this, e);
196         }
197
198         return line;
199     }
200
201     /** This method is called from within the constructor to
202      * initialize the form.
203      */

204     private final void initComponents() {
205         buttonPanel = new JPanel();
206         addButton = new JButton();
207         cancelButton = new JButton();
208         mainPanel = new JPanel();
209         splitPane = new JSplitPane();
210         treeScrollPane = new JScrollPane();
211         tree = new JTree();
212         listScrollPane = new JScrollPane();
213         list = new JList(new DefaultListModel());
214         countPanel = new JPanel();
215         countLabel = new JLabel();
216         count = new JTextField("1");
217         
218         setTitle(wb.getLang().getString("prodDialog"));
219         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
220             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
221                 closeDialog(evt);
222             }
223         });
224         
225         buttonPanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.RIGHT));
226         
227         addButton.setText(wb.getLang().getString("ok"));
228         buttonPanel.add(addButton);
229         
230         cancelButton.setText(wb.getLang().getString("cancel"));
231         buttonPanel.add(cancelButton);
232         
233         getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);
234         
235         mainPanel.setLayout(new java.awt.BorderLayout JavaDoc());
236         
237         mainPanel.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
238         treeScrollPane.setViewportBorder(new javax.swing.border.EtchedBorder JavaDoc());
239         treeScrollPane.setViewportView(tree);
240         
241         splitPane.setLeftComponent(treeScrollPane);
242         
243         listScrollPane.setViewportBorder(new javax.swing.border.EtchedBorder JavaDoc());
244         listScrollPane.setViewportView(list);
245         
246         splitPane.setRightComponent(listScrollPane);
247         
248         mainPanel.add(splitPane, java.awt.BorderLayout.CENTER);
249         
250         countPanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT));
251         
252         countLabel.setText(wb.getLang().getString("quantity"));
253         countLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
254         countPanel.add(countLabel);
255         
256         count.setText("1");
257         count.setPreferredSize(new Dimension(80, Prefs.FIELD_HEIGHT));
258         count.setMinimumSize(new Dimension(80, Prefs.FIELD_HEIGHT));
259         countPanel.add(count);
260         
261         mainPanel.add(countPanel, java.awt.BorderLayout.SOUTH);
262         
263         getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
264         
265         pack();
266     }
267
268     /** Closes the dialog */
269     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {
270         setVisible(false);
271         dispose();
272     }
273
274     /**
275      * set the screen's language
276      */

277     public final void setLang() {
278         setTitle(wb.getLang().getString("prodDialog"));
279         countLabel.setText(wb.getLang().getString("quantity"));
280         addButton.setText(wb.getLang().getString("ok"));
281         cancelButton.setText(wb.getLang().getString("cancel"));
282         countLabel.setText(wb.getLang().getString("quantity"));
283     }
284
285     public void setFonts() {}
286     public void setColors() {}
287
288 }
289
Popular Tags