KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > client > ItemPanelTransferHandler


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

27 package olstore.client;
28
29 import java.awt.datatransfer.DataFlavor;
30 import java.awt.datatransfer.Transferable;
31 import java.awt.datatransfer.UnsupportedFlavorException;
32 import java.io.IOException;
33 import java.util.Iterator;
34 import java.util.List;
35
36 import javax.swing.JComponent;
37 import javax.swing.TransferHandler;
38
39 /**
40  * The handler that allows ItemPanel objects to be dragged to the shopping cart.
41  */

42 class ItemPanelTransferHandler extends TransferHandler {
43
44     /** The flavor for the transfer handler */
45     private DataFlavor itemPanelFlavor;
46
47     /** The type of the object that will be transferred. */
48     private String itemPanelType = DataFlavor.javaJVMLocalObjectMimeType + ";class="
49             + ItemPanel.class.getName();
50
51     /**
52      * Transfer handler to handle drag and drop of item panels.
53      */

54     public ItemPanelTransferHandler() {
55         try {
56             itemPanelFlavor = new DataFlavor(itemPanelType);
57         } catch (ClassNotFoundException e) {
58             System.out.println("ItemPanelTransferHandler: unable to create data flavor");
59         }
60     }
61
62     /**
63      * Handle the drop part of the drag&drop.
64      * @param c the component on which the transferable is being dropped
65      * @param t the object being dragged
66      * @return true if the item can be imported into this component, false
67      * otherwise
68      */

69     public boolean importData(JComponent c, Transferable t) {
70         ItemPanel item;
71         try {
72             item = (ItemPanel) t.getTransferData(itemPanelFlavor);
73             addItemToCart(c, item.getItem());
74         } catch (UnsupportedFlavorException e) {
75             e.printStackTrace();
76             return false;
77         } catch (IOException e) {
78             e.printStackTrace();
79             return false;
80         }
81         c.validate();
82         c.repaint();
83         return true;
84     }
85
86     /**
87      * Create transferable out of the ItemPanel component.
88      * @see javax.swing.TransferHandler#createTransferable(javax.swing.JComponent)
89      */

90     protected Transferable createTransferable(JComponent c) {
91         ItemPanel panel = (ItemPanel) c;
92         return new ItemPanelTransferable(panel);
93     }
94
95     /**
96      * Get the supported actions for the drag and drop.
97      * @see javax.swing.TransferHandler#getSourceActions(javax.swing.JComponent)
98      */

99     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, int action) {
107     }
108
109     /**
110      * Determine whether the given component can accept the given data flavors.
111      * @param c the component that potentially takes an import
112      * @param flavors the list of flavors that describes the importable data
113      * @see javax.swing.TransferHandler#canImport(javax.swing.JComponent, java.awt.datatransfer.DataFlavor[])
114      * @return true if the component can import the given data flavor, false
115      * otherwise
116      */

117     public boolean canImport(JComponent c, DataFlavor[] flavors) {
118         // do not allow to drop on the ItemPanel area of the client (because it
119
// uses the same TransferHandler and will try to import data)
120
if (c instanceof ItemPanel) {
121             return false;
122         }
123
124         for (int i = 0; i < flavors.length; i++) {
125             if (itemPanelFlavor.equals(flavors[i])) {
126                 return true;
127             }
128         }
129         return false;
130     }
131
132     /**
133      * Add item to the shopping cart.
134      * @param c the shopping cart panel on which the item is being dropped.
135      * @param item the item that needs to be added to the cart.
136      */

137     private void addItemToCart(JComponent c, Item item) {
138         ShoppingCartPanel cart = (ShoppingCartPanel) c;
139
140         List items = cart.getItemsInCart();
141         Iterator iter = items.iterator();
142
143         OrderItem orderItem = new OrderItem(item);
144
145         // item of this type is already in the cart, increase the quantity
146
if (cart.isItemInCart(orderItem)) {
147             cart.increaseQuantity(orderItem);
148             return;
149         }
150
151         // this item was not found in the cart, so we add a new entry
152
cart.addNewItemToCart(orderItem);
153     }
154
155     /**
156      * A wrapper class for the panel that is being transferred from one component
157      * to another using drag and drop.
158      */

159     class ItemPanelTransferable implements Transferable {
160
161         /** The panel that is being dragged. */
162         private ItemPanel panel;
163
164         /**
165          * Wrapper for the ItemPanel for transfer.
166          * @param itemPanel the panel that is being wrapped for transfer
167          */

168         public ItemPanelTransferable(ItemPanel itemPanel) {
169             panel = itemPanel;
170         }
171
172         /**
173          * Get the data that is being transferred.
174          * @param flavor the flavor of the data requested
175          * @throws UnsupportedFlavorException if the flavor is not supported by
176          * this handler
177          * @return the ItemPanel cast as an Object
178          */

179         public Object getTransferData(DataFlavor flavor)
180                 throws UnsupportedFlavorException {
181             if (!isDataFlavorSupported(flavor)) {
182                 throw new UnsupportedFlavorException(flavor);
183             }
184             return panel;
185         }
186
187         /**
188          * Get the supported data flavors.
189          * @return the array of data flavors supported by this handler.
190          */

191         public DataFlavor[] getTransferDataFlavors() {
192             return new DataFlavor[] {itemPanelFlavor};
193         }
194
195         /**
196          * Is the given data flavor supported by this handler.
197          * @param flavor the data flavor to check
198          * @return true if the data flavor is supported, false otherwise
199          */

200         public boolean isDataFlavorSupported(DataFlavor flavor) {
201             return itemPanelFlavor.equals(flavor);
202         }
203     }
204 }
205
Popular Tags