KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > client > OrderPanel


1 /**
2  * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Gregory Lapouchnian
22  * Patrick Smith
23  * --------------------------------------------------------------------------
24  * $Id: OrderPanel.java,v 1.2 2005/07/08 14:00:45 glapouch Exp $
25  * --------------------------------------------------------------------------
26  */

27 package olstore.client;
28
29 import java.awt.Dimension;
30 import java.awt.GridBagConstraints;
31 import java.awt.GridBagLayout;
32 import java.awt.Image;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.text.DecimalFormat;
36
37 import javax.swing.ImageIcon;
38 import javax.swing.JButton;
39 import javax.swing.JLabel;
40 import javax.swing.JPanel;
41 import javax.swing.JTextField;
42
43 /**
44  * A custom JPanel class used for displaying items
45  * that are in the shopping cart.
46  */

47 public class OrderPanel extends JPanel {
48
49     /** The total price for the item and the quantity in the cart. */
50     private JLabel totalPrice;
51
52     /** The quantity of the item in the cart. */
53     private JTextField quantity;
54
55     /** The item represented by this panel. */
56     private OrderItem orderItem;
57
58     /** The formatter used for displaying prices. */
59     private DecimalFormat formatter = new DecimalFormat("#.00");
60
61     /**
62      * Creates a new OrderPanel given an OrderItem,
63      * and builds the panel to display the item information.
64      * @param orderItem The order item to add to the panel.
65      */

66     public OrderPanel(OrderItem orderItem) {
67         this.orderItem = orderItem;
68
69         buildPanel();
70     }
71
72     /**
73      * Creates the panel to display the item by creating
74      * components for each piece of information (picture, price, etc...)
75      * and putting them in a GridBagLayout.
76      */

77     public void buildPanel() {
78
79         GridBagLayout gridbag = new GridBagLayout();
80         GridBagConstraints c = new GridBagConstraints();
81         setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
82
83         setLayout(gridbag);
84         JButton button;
85         setLayout(new GridBagLayout());
86         c.fill = GridBagConstraints.HORIZONTAL;
87
88         ImageIcon icon = new ImageIcon(orderItem.getImage().getScaledInstance(
89                 50, -1, Image.SCALE_SMOOTH));
90         JLabel image = new JLabel(icon);
91         c.ipadx = 5;
92         c.gridx = 0;
93         c.gridy = 0;
94         c.gridheight = 2;
95         add(image, c);
96
97         JLabel name = new JLabel(orderItem.getTitle());
98         c.weightx = 1.0;
99         c.gridheight = 1;
100         c.gridwidth = 4;
101         c.gridx = 1;
102         c.gridy = 0;
103         add(name, c);
104
105         quantity = new JTextField();
106         quantity.setText(String.valueOf(orderItem.getQuantity()));
107         quantity.addActionListener(new ActionListener() {
108
109             public void actionPerformed(ActionEvent arg0) {
110                 try {
111                     int newQuantity = Integer.valueOf(quantity.getText())
112                             .intValue();
113
114                     double oldTotal = orderItem.getTotal();
115                     orderItem.setQuantity(newQuantity);
116                     double newTotal = orderItem.getTotal();
117
118                     // update the labels that display the price
119
((ShoppingCartPanel) getParent()).updatePrice(newTotal
120                             - oldTotal);
121                     setTotalPrice(orderItem.getTotal());
122                 } catch (Exception e) {
123                 }
124             }
125         });
126         c.weightx = 0;
127         c.gridwidth = 1;
128         c.ipadx = 40;
129         c.gridx = 3;
130         c.gridy = 1;
131         add(quantity, c);
132
133         JLabel x = new JLabel("x");
134         c.gridx = 2;
135         c.gridy = 1;
136         c.ipadx = 0;
137         add(x, c);
138
139         totalPrice = new JLabel();
140         c.gridx = 4;
141         c.gridy = 1;
142         c.ipadx = 10;
143         add(totalPrice, c);
144         // set the initial price
145
setTotalPrice(orderItem.getTotal());
146
147         JLabel price = new JLabel(orderItem.getPrice());
148         c.weightx = 1.0;
149         c.ipadx = 0;
150         c.gridx = 1;
151         c.gridy = 1;
152         add(price, c);
153
154         JButton remove = new JButton("X");
155         remove.setBackground(OlstoreSwingClient.buttonColor);
156         remove.addActionListener(new ActionListener() {
157             public void actionPerformed(ActionEvent arg0) {
158                 ((ShoppingCartPanel) getParent()).removeFromCart(orderItem);
159             }
160         });
161         c.weightx = 0;
162         c.ipadx = 0;
163         c.gridx = 5;
164         c.gridheight = 2;
165         c.gridy = 0;
166         add(remove, c);
167
168         setBackground(OlstoreSwingClient.itemColor1);
169     }
170
171     /**
172      * Sets the total price for this panel.
173      * @param price The price to display.
174      */

175     private void setTotalPrice(double price) {
176         DecimalFormat formatter = new DecimalFormat("#.00");
177         totalPrice.setText(formatter.format(price));
178     }
179
180     /**
181      * Returns the OrderItem represented by this panel.
182      * @return The orderItem represented by this panel.
183      */

184     public OrderItem getOrderItem() {
185         return orderItem;
186     }
187
188     /**
189      * Sets the order item represented by this panel
190      * to the given order item.
191      * @param orderItem
192      * The orderItem to set.
193      */

194     public void setOrderItem(OrderItem orderItem) {
195         this.orderItem = orderItem;
196     }
197
198     /**
199      * Updates the quantity for this item.
200      */

201     public void updateQuantity() {
202         quantity.setText(String.valueOf(orderItem.getQuantity()));
203         setTotalPrice(orderItem.getTotal());
204     }
205
206 }
207
Popular Tags