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: ItemPanelTransferHandler.java 7027 2005-07-08 14:00:45Z glapouch $
025: * --------------------------------------------------------------------------
026: */package olstore.client;
027:
028: import java.awt.datatransfer.DataFlavor;
029: import java.awt.datatransfer.Transferable;
030: import java.awt.datatransfer.UnsupportedFlavorException;
031: import java.io.IOException;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import javax.swing.JComponent;
036: import javax.swing.TransferHandler;
037:
038: /**
039: * The handler that allows ItemPanel objects to be dragged to the shopping cart.
040: */
041: class ItemPanelTransferHandler extends TransferHandler {
042:
043: /** The flavor for the transfer handler */
044: private DataFlavor itemPanelFlavor;
045:
046: /** The type of the object that will be transferred. */
047: private String itemPanelType = DataFlavor.javaJVMLocalObjectMimeType
048: + ";class=" + ItemPanel.class.getName();
049:
050: /**
051: * Transfer handler to handle drag and drop of item panels.
052: */
053: public ItemPanelTransferHandler() {
054: try {
055: itemPanelFlavor = new DataFlavor(itemPanelType);
056: } catch (ClassNotFoundException e) {
057: System.out
058: .println("ItemPanelTransferHandler: unable to create data flavor");
059: }
060: }
061:
062: /**
063: * Handle the drop part of the drag&drop.
064: * @param c the component on which the transferable is being dropped
065: * @param t the object being dragged
066: * @return true if the item can be imported into this component, false
067: * otherwise
068: */
069: public boolean importData(JComponent c, Transferable t) {
070: ItemPanel item;
071: try {
072: item = (ItemPanel) t.getTransferData(itemPanelFlavor);
073: addItemToCart(c, item.getItem());
074: } catch (UnsupportedFlavorException e) {
075: e.printStackTrace();
076: return false;
077: } catch (IOException e) {
078: e.printStackTrace();
079: return false;
080: }
081: c.validate();
082: c.repaint();
083: return true;
084: }
085:
086: /**
087: * Create transferable out of the ItemPanel component.
088: * @see javax.swing.TransferHandler#createTransferable(javax.swing.JComponent)
089: */
090: protected Transferable createTransferable(JComponent c) {
091: ItemPanel panel = (ItemPanel) c;
092: return new ItemPanelTransferable(panel);
093: }
094:
095: /**
096: * Get the supported actions for the drag and drop.
097: * @see javax.swing.TransferHandler#getSourceActions(javax.swing.JComponent)
098: */
099: public int getSourceActions(JComponent c) {
100: return COPY_OR_MOVE;
101: }
102:
103: /**
104: * @see javax.swing.TransferHandler#exportDone(javax.swing.JComponent, java.awt.datatransfer.Transferable, int)
105: */
106: protected void exportDone(JComponent c, Transferable data,
107: int action) {
108: }
109:
110: /**
111: * Determine whether the given component can accept the given data flavors.
112: * @param c the component that potentially takes an import
113: * @param flavors the list of flavors that describes the importable data
114: * @see javax.swing.TransferHandler#canImport(javax.swing.JComponent, java.awt.datatransfer.DataFlavor[])
115: * @return true if the component can import the given data flavor, false
116: * otherwise
117: */
118: public boolean canImport(JComponent c, DataFlavor[] flavors) {
119: // do not allow to drop on the ItemPanel area of the client (because it
120: // uses the same TransferHandler and will try to import data)
121: if (c instanceof ItemPanel) {
122: return false;
123: }
124:
125: for (int i = 0; i < flavors.length; i++) {
126: if (itemPanelFlavor.equals(flavors[i])) {
127: return true;
128: }
129: }
130: return false;
131: }
132:
133: /**
134: * Add item to the shopping cart.
135: * @param c the shopping cart panel on which the item is being dropped.
136: * @param item the item that needs to be added to the cart.
137: */
138: private void addItemToCart(JComponent c, Item item) {
139: ShoppingCartPanel cart = (ShoppingCartPanel) c;
140:
141: List items = cart.getItemsInCart();
142: Iterator iter = items.iterator();
143:
144: OrderItem orderItem = new OrderItem(item);
145:
146: // item of this type is already in the cart, increase the quantity
147: if (cart.isItemInCart(orderItem)) {
148: cart.increaseQuantity(orderItem);
149: return;
150: }
151:
152: // this item was not found in the cart, so we add a new entry
153: cart.addNewItemToCart(orderItem);
154: }
155:
156: /**
157: * A wrapper class for the panel that is being transferred from one component
158: * to another using drag and drop.
159: */
160: class ItemPanelTransferable implements Transferable {
161:
162: /** The panel that is being dragged. */
163: private ItemPanel panel;
164:
165: /**
166: * Wrapper for the ItemPanel for transfer.
167: * @param itemPanel the panel that is being wrapped for transfer
168: */
169: public ItemPanelTransferable(ItemPanel itemPanel) {
170: panel = itemPanel;
171: }
172:
173: /**
174: * Get the data that is being transferred.
175: * @param flavor the flavor of the data requested
176: * @throws UnsupportedFlavorException if the flavor is not supported by
177: * this handler
178: * @return the ItemPanel cast as an Object
179: */
180: public Object getTransferData(DataFlavor flavor)
181: throws UnsupportedFlavorException {
182: if (!isDataFlavorSupported(flavor)) {
183: throw new UnsupportedFlavorException(flavor);
184: }
185: return panel;
186: }
187:
188: /**
189: * Get the supported data flavors.
190: * @return the array of data flavors supported by this handler.
191: */
192: public DataFlavor[] getTransferDataFlavors() {
193: return new DataFlavor[] { itemPanelFlavor };
194: }
195:
196: /**
197: * Is the given data flavor supported by this handler.
198: * @param flavor the data flavor to check
199: * @return true if the data flavor is supported, false otherwise
200: */
201: public boolean isDataFlavorSupported(DataFlavor flavor) {
202: return itemPanelFlavor.equals(flavor);
203: }
204: }
205: }
|