Source Code Cross Referenced for ItemPanelTransferHandler.java in  » J2EE » JOnAS-4.8.6 » olstore » client » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » olstore.client 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.