KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > client > ItemPanel


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: ItemPanel.java,v 1.2 2005/07/08 14:00:45 glapouch Exp $
25  * --------------------------------------------------------------------------
26  */

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 /**
52  * A panel to display all the available information about an item to the user.
53  */

54 public class ItemPanel extends JPanel implements MouseListener,
55         MouseMotionListener {
56
57     /** Used to determine whether the mouse motion is considered to be a drag */
58     private MouseEvent firstMouseEvent = null;
59
60     /** The product item that this panel represents. */
61     private Item item;
62
63     /** The alternating background color. */
64     private Color background;
65
66     /** The number of pixels that define a mouse drag */
67     private static final int PIXEL_DISTANCE = 5;
68
69     /**
70      * Panel to display information about an item.
71      * @param title the name of the product
72      * @param description the description of the item
73      * @param price the item's price
74      * @param category the item's product category
75      * @param id the item's unique ID
76      * @param image the image showing the item
77      * @param bgColor which of the alternating colors to use for this panel
78      */

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     /**
92      * Build up the components that make up this panel.
93      */

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     /**
138      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
139      */

140     public void mousePressed(MouseEvent e) {
141         firstMouseEvent = e;
142         e.consume();
143     }
144
145     /**
146      * Determine whether the mouse drag is long enough to initiate drag and drop
147      * operation.
148      * @param e the mouse event
149      */

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             //Arbitrarily define a 5-pixel shift as the
157
//official beginning of a drag.
158
if (dx > PIXEL_DISTANCE || dy > PIXEL_DISTANCE) {
159                 //This is a drag, not a click.
160
JComponent c = (JComponent) e.getSource();
161                 TransferHandler handler = c.getTransferHandler();
162                 //Tell the transfer handler to initiate the drag.
163
handler.exportAsDrag(c, firstMouseEvent, TransferHandler.COPY);
164                 firstMouseEvent = null;
165             }
166         }
167     }
168
169     /**
170      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
171      */

172     public void mouseReleased(MouseEvent e) {
173         firstMouseEvent = null;
174     }
175
176     /**
177      * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
178      */

179     public void mouseMoved(MouseEvent e) {
180     }
181
182     /**
183      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
184      */

185     public void mouseClicked(MouseEvent arg0) {
186     }
187
188     /**
189      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
190      */

191     public void mouseEntered(MouseEvent arg0) {
192     }
193
194     /**
195      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
196      */

197     public void mouseExited(MouseEvent arg0) {
198     }
199
200     /**
201      * Get the item from this panel.
202      * @return Returns the item.
203      */

204     public Item getItem() {
205         return item;
206     }
207
208     /**
209      * Set the item for this panel.
210      * @param item The item to set.
211      */

212     public void setItem(Item item) {
213         this.item = item;
214     }
215 }
216
Popular Tags