1 27 package olstore.client; 28 29 import java.awt.Color; 30 import java.awt.Component; 31 import java.awt.Dimension; 32 import java.awt.GridBagConstraints; 33 import java.awt.GridBagLayout; 34 import java.awt.GridLayout; 35 import java.awt.event.MouseEvent; 36 import java.awt.event.MouseListener; 37 import java.awt.event.MouseMotionListener; 38 import java.awt.image.BufferedImage; 39 40 import javax.swing.BorderFactory; 41 import javax.swing.Box; 42 import javax.swing.Icon; 43 import javax.swing.ImageIcon; 44 import javax.swing.JComponent; 45 import javax.swing.JLabel; 46 import javax.swing.JPanel; 47 import javax.swing.JTextArea; 48 import javax.swing.TransferHandler; 49 import javax.swing.border.TitledBorder; 50 51 54 public class ItemPanel extends JPanel implements MouseListener, 55 MouseMotionListener { 56 57 58 private MouseEvent firstMouseEvent = null; 59 60 61 private Item item; 62 63 64 private Color background; 65 66 67 private static final int PIXEL_DISTANCE = 5; 68 69 79 public ItemPanel(String title, String description, String price, 80 String category, String id, BufferedImage image, Color bgColor) { 81 82 background = bgColor; 83 addMouseMotionListener(this); 84 addMouseListener(this); 85 86 item = new Item(title, description, price, category, id, image); 87 88 buildPanel(); 89 } 90 91 94 public void buildPanel() { 95 Icon icon = new ImageIcon(item.getImage()); 96 97 GridBagLayout gbl = new GridBagLayout(); 98 GridBagConstraints c = new GridBagConstraints(); 99 c.fill = GridBagConstraints.HORIZONTAL; 100 gbl.setConstraints(this, c); 101 setLayout(gbl); 102 103 Component spacer = Box.createRigidArea(new Dimension(20, 75)); 104 105 JLabel imageLabel = new JLabel(icon); 106 107 add(imageLabel); 108 add(spacer); 109 110 JPanel descrip = new JPanel(); 111 descrip.setLayout(new GridLayout(3, 1)); 112 113 descrip.add(new JLabel(item.getTitle())); 114 JTextArea descriptionLabel = new JTextArea(item.getDescription()); 115 116 descriptionLabel.setSize(350, 250); 117 descriptionLabel.setOpaque(false); 118 descriptionLabel.setEditable(false); 119 descriptionLabel.setFocusable(false); 120 descriptionLabel.setEnabled(false); 121 descriptionLabel.setDisabledTextColor(Color.BLACK); 122 descriptionLabel.setBorder(null); 123 descriptionLabel.setLineWrap(true); 124 descriptionLabel.setWrapStyleWord(true); 125 126 descrip.add(descriptionLabel); 127 descrip.setBackground(background); 128 descrip.add(new JLabel("Price: $" + item.getPrice())); 129 add(descrip); 130 131 TitledBorder border = BorderFactory.createTitledBorder(item 132 .getCategory()); 133 border.setTitleJustification(TitledBorder.CENTER); 134 setBorder(border); 135 } 136 137 140 public void mousePressed(MouseEvent e) { 141 firstMouseEvent = e; 142 e.consume(); 143 } 144 145 150 public void mouseDragged(MouseEvent e) { 151 if (firstMouseEvent != null) { 152 e.consume(); 153 154 int dx = Math.abs(e.getX() - firstMouseEvent.getX()); 155 int dy = Math.abs(e.getY() - firstMouseEvent.getY()); 156 if (dx > PIXEL_DISTANCE || dy > PIXEL_DISTANCE) { 159 JComponent c = (JComponent) e.getSource(); 161 TransferHandler handler = c.getTransferHandler(); 162 handler.exportAsDrag(c, firstMouseEvent, TransferHandler.COPY); 164 firstMouseEvent = null; 165 } 166 } 167 } 168 169 172 public void mouseReleased(MouseEvent e) { 173 firstMouseEvent = null; 174 } 175 176 179 public void mouseMoved(MouseEvent e) { 180 } 181 182 185 public void mouseClicked(MouseEvent arg0) { 186 } 187 188 191 public void mouseEntered(MouseEvent arg0) { 192 } 193 194 197 public void mouseExited(MouseEvent arg0) { 198 } 199 200 204 public Item getItem() { 205 return item; 206 } 207 208 212 public void setItem(Item item) { 213 this.item = item; 214 } 215 } 216 | Popular Tags |