updated code for kona libs, not quite working yet.
[feisty_meow.git] / kona / src / org / feistymeow / dragdrop / DragonTransferHandler.java
1 package org.feistymeow.dragdrop;
2
3 import java.awt.datatransfer.DataFlavor;
4 import java.awt.datatransfer.Transferable;
5 import java.util.List;
6
7 import javax.swing.JComponent;
8 import javax.swing.TransferHandler;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 /**
14  * A transfer handler that can be extended and used to inter-operate with DragonDropManager. This
15  * object is not strictly necessary to use, but it can help if one has not already implemented one's
16  * own transfer handler.
17  * 
18  * @author Chris Koeritz
19  * @copyright Copyright (c) 2012-$now By University of Virginia
20  * @license This file is free software; you can modify and redistribute it under the terms of the
21  *          Apache License v2.0: http://www.apache.org/licenses/LICENSE-2.0
22  */
23 @SuppressWarnings("serial")
24 public class DragonTransferHandler extends TransferHandler
25 {
26     static private Log logger = LogFactory.getLog(DragonTransferHandler.class);
27     IDragonDropDataProvider c_provider;
28
29     public DragonTransferHandler(IDragonDropDataProvider provider)
30     {
31         c_provider = provider;
32     }
33
34     @Override
35     public boolean canImport(TransferSupport support)
36     {
37         if (support == null) return false;
38         if (!support.isDrop())
39             return false; // we don't support cut&paste here.
40         logger.debug("canImport: base just saying okay.");
41         return true;
42     }
43
44     @Override
45     protected Transferable createTransferable(JComponent c)
46     {
47         logger.debug("createTransferable: at base, returning ListTransferable.");
48         return new ListTransferable(c_provider.provideDragList());
49     }
50
51     @Override
52     protected void exportDone(JComponent source, Transferable data, int action)
53     {
54         logger.debug("exportDone: base got event for component " + source.toString());
55     }
56
57     @Override
58     public int getSourceActions(JComponent c)
59     {
60         return COPY;
61     }
62
63     @Override
64     public boolean importData(TransferSupport support)
65     {
66         if (support == null) return false;
67         logger.debug("importData: at base...");
68
69         if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
70             logger.debug("importing data with java files flavor");
71             List<Object> files = ListTransferable.extractData(support.getTransferable());
72             if ((files != null) && (files.size() != 0)) {
73                 c_provider.consumeDropList(files, support.getDropLocation().getDropPoint());
74                 return true;
75             }
76         } else if (support.isDataFlavorSupported(ListTransferable.getURIListFlavor1())
77                 || support.isDataFlavorSupported(ListTransferable.getURIListFlavor2())) {
78             logger.debug("importing data with uri list flavor");
79             List<Object> files = ListTransferable.extractData(support.getTransferable());
80             if ((files != null) && (files.size() != 0)) {
81                 c_provider.consumeDropList(files, support.getDropLocation().getDropPoint());
82                 return true;
83             }
84         }
85         logger.warn("passing importData request to superclass, which will probably fail.");
86         return super.importData(support);
87     }
88 }