1 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 47 public class OrderPanel extends JPanel { 48 49 50 private JLabel totalPrice; 51 52 53 private JTextField quantity; 54 55 56 private OrderItem orderItem; 57 58 59 private DecimalFormat formatter = new DecimalFormat("#.00"); 60 61 66 public OrderPanel(OrderItem orderItem) { 67 this.orderItem = orderItem; 68 69 buildPanel(); 70 } 71 72 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 ((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 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 175 private void setTotalPrice(double price) { 176 DecimalFormat formatter = new DecimalFormat("#.00"); 177 totalPrice.setText(formatter.format(price)); 178 } 179 180 184 public OrderItem getOrderItem() { 185 return orderItem; 186 } 187 188 194 public void setOrderItem(OrderItem orderItem) { 195 this.orderItem = orderItem; 196 } 197 198 201 public void updateQuantity() { 202 quantity.setText(String.valueOf(orderItem.getQuantity())); 203 setTotalPrice(orderItem.getTotal()); 204 } 205 206 } 207 | Popular Tags |