001: /**
002: * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Gregory Lapouchnian
022: * Patrick Smith
023: * --------------------------------------------------------------------------
024: * $Id: OrderPanel.java 7027 2005-07-08 14:00:45Z glapouch $
025: * --------------------------------------------------------------------------
026: */package olstore.client;
027:
028: import java.awt.Dimension;
029: import java.awt.GridBagConstraints;
030: import java.awt.GridBagLayout;
031: import java.awt.Image;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.text.DecimalFormat;
035:
036: import javax.swing.ImageIcon;
037: import javax.swing.JButton;
038: import javax.swing.JLabel;
039: import javax.swing.JPanel;
040: import javax.swing.JTextField;
041:
042: /**
043: * A custom JPanel class used for displaying items
044: * that are in the shopping cart.
045: */
046: public class OrderPanel extends JPanel {
047:
048: /** The total price for the item and the quantity in the cart. */
049: private JLabel totalPrice;
050:
051: /** The quantity of the item in the cart. */
052: private JTextField quantity;
053:
054: /** The item represented by this panel. */
055: private OrderItem orderItem;
056:
057: /** The formatter used for displaying prices. */
058: private DecimalFormat formatter = new DecimalFormat("#.00");
059:
060: /**
061: * Creates a new OrderPanel given an OrderItem,
062: * and builds the panel to display the item information.
063: * @param orderItem The order item to add to the panel.
064: */
065: public OrderPanel(OrderItem orderItem) {
066: this .orderItem = orderItem;
067:
068: buildPanel();
069: }
070:
071: /**
072: * Creates the panel to display the item by creating
073: * components for each piece of information (picture, price, etc...)
074: * and putting them in a GridBagLayout.
075: */
076: public void buildPanel() {
077:
078: GridBagLayout gridbag = new GridBagLayout();
079: GridBagConstraints c = new GridBagConstraints();
080: setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
081:
082: setLayout(gridbag);
083: JButton button;
084: setLayout(new GridBagLayout());
085: c.fill = GridBagConstraints.HORIZONTAL;
086:
087: ImageIcon icon = new ImageIcon(orderItem.getImage()
088: .getScaledInstance(50, -1, Image.SCALE_SMOOTH));
089: JLabel image = new JLabel(icon);
090: c.ipadx = 5;
091: c.gridx = 0;
092: c.gridy = 0;
093: c.gridheight = 2;
094: add(image, c);
095:
096: JLabel name = new JLabel(orderItem.getTitle());
097: c.weightx = 1.0;
098: c.gridheight = 1;
099: c.gridwidth = 4;
100: c.gridx = 1;
101: c.gridy = 0;
102: add(name, c);
103:
104: quantity = new JTextField();
105: quantity.setText(String.valueOf(orderItem.getQuantity()));
106: quantity.addActionListener(new ActionListener() {
107:
108: public void actionPerformed(ActionEvent arg0) {
109: try {
110: int newQuantity = Integer.valueOf(
111: quantity.getText()).intValue();
112:
113: double oldTotal = orderItem.getTotal();
114: orderItem.setQuantity(newQuantity);
115: double newTotal = orderItem.getTotal();
116:
117: // update the labels that display the price
118: ((ShoppingCartPanel) getParent())
119: .updatePrice(newTotal - oldTotal);
120: setTotalPrice(orderItem.getTotal());
121: } catch (Exception e) {
122: }
123: }
124: });
125: c.weightx = 0;
126: c.gridwidth = 1;
127: c.ipadx = 40;
128: c.gridx = 3;
129: c.gridy = 1;
130: add(quantity, c);
131:
132: JLabel x = new JLabel("x");
133: c.gridx = 2;
134: c.gridy = 1;
135: c.ipadx = 0;
136: add(x, c);
137:
138: totalPrice = new JLabel();
139: c.gridx = 4;
140: c.gridy = 1;
141: c.ipadx = 10;
142: add(totalPrice, c);
143: // set the initial price
144: setTotalPrice(orderItem.getTotal());
145:
146: JLabel price = new JLabel(orderItem.getPrice());
147: c.weightx = 1.0;
148: c.ipadx = 0;
149: c.gridx = 1;
150: c.gridy = 1;
151: add(price, c);
152:
153: JButton remove = new JButton("X");
154: remove.setBackground(OlstoreSwingClient.buttonColor);
155: remove.addActionListener(new ActionListener() {
156: public void actionPerformed(ActionEvent arg0) {
157: ((ShoppingCartPanel) getParent())
158: .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: }
|