]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/FileDropHandler.java
2c76385fc676963b8419319f31c8c1cb10de138a
[GpsPrune.git] / tim / prune / FileDropHandler.java
1 package tim.prune;
2
3 import java.awt.datatransfer.DataFlavor;
4 import java.io.File;
5 import java.net.URI;
6 import java.util.ArrayList;
7
8 import javax.swing.TransferHandler;
9
10 /**
11  * Class to listen for drag/drop events and react to dropping files on to the application
12  */
13 public class FileDropHandler extends TransferHandler
14 {
15         /** App object for passing results back to */
16         private App _app = null;
17
18         /** Fixed flavour in case the java file list flavour isn't available */
19         private static DataFlavor _uriListFlavour = null;
20
21         /** Static block to initialise the list flavour */
22         static
23         {
24                 try {_uriListFlavour = new DataFlavor("text/uri-list;class=java.lang.String");
25                 } catch (ClassNotFoundException nfe) {}
26         }
27
28
29         /**
30          * Constructor
31          * @param inApp App object to pass results of drop back to
32          */
33         public FileDropHandler(App inApp)
34         {
35                 _app = inApp;
36         }
37
38         /**
39          * Check if the object being dragged can be accepted
40          * @param inSupport object to check
41          */
42         public boolean canImport(TransferSupport inSupport)
43         {
44                 boolean retval = inSupport.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
45                         || inSupport.isDataFlavorSupported(_uriListFlavour);
46                 // Modify icon to show a copy, not a move (+ icon on cursor)
47                 if (retval) {
48                         inSupport.setDropAction(COPY);
49                 }
50                 return retval;
51         }
52
53         /**
54          * Accept the incoming data and pass it on to the App
55          * @param inSupport contents of drop
56          */
57         public boolean importData(TransferSupport inSupport)
58         {
59                 if (!canImport(inSupport)) {return false;} // not allowed
60
61                 boolean success = false;
62                 ArrayList<File> dataFiles = new ArrayList<File>();
63
64                 // Try a java file list flavour first
65                 try
66                 {
67                         @SuppressWarnings("unchecked")
68                         java.util.List<File> fileList = (java.util.List<File>)
69                         inSupport.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
70                         success = true;
71
72                         for (File f : fileList)
73                         {
74                                 if (f != null && f.exists())
75                                 {
76                                         if (f.isDirectory()) {
77                                                 addDirectoryToList(f, dataFiles);
78                                         }
79                                         else if (f.isFile()) {
80                                                 dataFiles.add(f);
81                                         }
82                                 }
83                         }
84                 } catch (Exception e) {}  // exception caught, probably missing a javafilelist flavour - just continue
85
86                 // If that didn't work, try a list of strings instead
87                 if (!success)
88                 {
89                         try
90                         {
91                                 String pathList = inSupport.getTransferable().getTransferData(_uriListFlavour).toString();
92                                 success = true;
93
94                                 for (String s : pathList.split("[\n\r]+"))
95                                 {
96                                         if (s != null && !s.equals(""))
97                                         {
98                                                 File f = new File(new URI(s));
99                                                 if (f.exists())
100                                                 {
101                                                         if (f.isDirectory()) {
102                                                                 addDirectoryToList(f, dataFiles);
103                                                         }
104                                                         else if (f.isFile()) {
105                                                                 dataFiles.add(f);
106                                                         }
107                                                 }
108                                         }
109                                 }
110                         } catch (Exception e) {
111                                 System.err.println("exception: " + e.getClass().getName() + " - " + e.getMessage());
112                                 return false;
113                         }
114                 }
115
116                 // Pass files back to app
117                 if (!dataFiles.isEmpty()) {
118                         _app.loadDataFiles(dataFiles);
119                 }
120                 return true;
121         }
122
123
124         /**
125          * Recursively-called method to add files from the given directory (and its subdirectories)
126          * to the given list of files
127          * @param inDir directory to add
128          * @param inList file list to append to
129          */
130         private void addDirectoryToList(File inDir, ArrayList<File> inList)
131         {
132                 if (inDir != null && inDir.exists() && inDir.canRead() && inDir.isDirectory() && inList != null)
133                 {
134                         for (String path : inDir.list())
135                         {
136                                 if (path != null)
137                                 {
138                                         File f = new File(inDir, path);
139                                         if (f.exists() && f.canRead())
140                                         {
141                                                 if (f.isFile())
142                                                 {
143                                                         // Add the found file to the list (if it's not in there already)
144                                                         if (!inList.contains(f)) {
145                                                                 inList.add(f);
146                                                         }
147                                                 }
148                                                 else if (f.isDirectory())
149                                                 {
150                                                         // Recursively add the files from subdirectories
151                                                         addDirectoryToList(f, inList);
152                                                 }
153                                         }
154                                 }
155                         }
156                 }
157         }
158 }