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: ItemPanel.java 7027 2005-07-08 14:00:45Z glapouch $
025: * --------------------------------------------------------------------------
026: */package olstore.client;
027:
028: import java.awt.Color;
029: import java.awt.Component;
030: import java.awt.Dimension;
031: import java.awt.GridBagConstraints;
032: import java.awt.GridBagLayout;
033: import java.awt.GridLayout;
034: import java.awt.event.MouseEvent;
035: import java.awt.event.MouseListener;
036: import java.awt.event.MouseMotionListener;
037: import java.awt.image.BufferedImage;
038:
039: import javax.swing.BorderFactory;
040: import javax.swing.Box;
041: import javax.swing.Icon;
042: import javax.swing.ImageIcon;
043: import javax.swing.JComponent;
044: import javax.swing.JLabel;
045: import javax.swing.JPanel;
046: import javax.swing.JTextArea;
047: import javax.swing.TransferHandler;
048: import javax.swing.border.TitledBorder;
049:
050: /**
051: * A panel to display all the available information about an item to the user.
052: */
053: public class ItemPanel extends JPanel implements MouseListener,
054: MouseMotionListener {
055:
056: /** Used to determine whether the mouse motion is considered to be a drag */
057: private MouseEvent firstMouseEvent = null;
058:
059: /** The product item that this panel represents. */
060: private Item item;
061:
062: /** The alternating background color. */
063: private Color background;
064:
065: /** The number of pixels that define a mouse drag */
066: private static final int PIXEL_DISTANCE = 5;
067:
068: /**
069: * Panel to display information about an item.
070: * @param title the name of the product
071: * @param description the description of the item
072: * @param price the item's price
073: * @param category the item's product category
074: * @param id the item's unique ID
075: * @param image the image showing the item
076: * @param bgColor which of the alternating colors to use for this panel
077: */
078: public ItemPanel(String title, String description, String price,
079: String category, String id, BufferedImage image,
080: Color bgColor) {
081:
082: background = bgColor;
083: addMouseMotionListener(this );
084: addMouseListener(this );
085:
086: item = new Item(title, description, price, category, id, image);
087:
088: buildPanel();
089: }
090:
091: /**
092: * Build up the components that make up this panel.
093: */
094: public void buildPanel() {
095: Icon icon = new ImageIcon(item.getImage());
096:
097: GridBagLayout gbl = new GridBagLayout();
098: GridBagConstraints c = new GridBagConstraints();
099: 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
115: .getDescription());
116:
117: descriptionLabel.setSize(350, 250);
118: descriptionLabel.setOpaque(false);
119: descriptionLabel.setEditable(false);
120: descriptionLabel.setFocusable(false);
121: descriptionLabel.setEnabled(false);
122: descriptionLabel.setDisabledTextColor(Color.BLACK);
123: descriptionLabel.setBorder(null);
124: descriptionLabel.setLineWrap(true);
125: descriptionLabel.setWrapStyleWord(true);
126:
127: descrip.add(descriptionLabel);
128: descrip.setBackground(background);
129: descrip.add(new JLabel("Price: $" + item.getPrice()));
130: add(descrip);
131:
132: TitledBorder border = BorderFactory.createTitledBorder(item
133: .getCategory());
134: border.setTitleJustification(TitledBorder.CENTER);
135: setBorder(border);
136: }
137:
138: /**
139: * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
140: */
141: public void mousePressed(MouseEvent e) {
142: firstMouseEvent = e;
143: e.consume();
144: }
145:
146: /**
147: * Determine whether the mouse drag is long enough to initiate drag and drop
148: * operation.
149: * @param e the mouse event
150: */
151: public void mouseDragged(MouseEvent e) {
152: if (firstMouseEvent != null) {
153: e.consume();
154:
155: int dx = Math.abs(e.getX() - firstMouseEvent.getX());
156: int dy = Math.abs(e.getY() - firstMouseEvent.getY());
157: //Arbitrarily define a 5-pixel shift as the
158: //official beginning of a drag.
159: if (dx > PIXEL_DISTANCE || dy > PIXEL_DISTANCE) {
160: //This is a drag, not a click.
161: JComponent c = (JComponent) e.getSource();
162: TransferHandler handler = c.getTransferHandler();
163: //Tell the transfer handler to initiate the drag.
164: handler.exportAsDrag(c, firstMouseEvent,
165: TransferHandler.COPY);
166: firstMouseEvent = null;
167: }
168: }
169: }
170:
171: /**
172: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
173: */
174: public void mouseReleased(MouseEvent e) {
175: firstMouseEvent = null;
176: }
177:
178: /**
179: * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
180: */
181: public void mouseMoved(MouseEvent e) {
182: }
183:
184: /**
185: * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
186: */
187: public void mouseClicked(MouseEvent arg0) {
188: }
189:
190: /**
191: * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
192: */
193: public void mouseEntered(MouseEvent arg0) {
194: }
195:
196: /**
197: * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
198: */
199: public void mouseExited(MouseEvent arg0) {
200: }
201:
202: /**
203: * Get the item from this panel.
204: * @return Returns the item.
205: */
206: public Item getItem() {
207: return item;
208: }
209:
210: /**
211: * Set the item for this panel.
212: * @param item The item to set.
213: */
214: public void setItem(Item item) {
215: this.item = item;
216: }
217: }
|