KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > dnd > FileDropListener


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.swing.dnd;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.awt.dnd.DnDConstants JavaDoc;
25 import java.awt.dnd.DropTargetDragEvent JavaDoc;
26 import java.awt.dnd.DropTargetDropEvent JavaDoc;
27 import java.awt.dnd.DropTargetEvent JavaDoc;
28 import java.awt.dnd.DropTargetListener JavaDoc;
29 import java.io.BufferedReader JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 public abstract class FileDropListener implements DropTargetListener JavaDoc {
37
38     public FileDropListener() {
39     }
40     
41     public abstract void fileDropped(File JavaDoc[] files);
42
43     public void drop(DropTargetDropEvent JavaDoc dtde) {
44         try {
45             doDrop(dtde);
46         } catch (Exception JavaDoc e) {
47             e.printStackTrace();
48         }
49     }
50
51     void doDrop(DropTargetDropEvent JavaDoc dtde) throws Exception JavaDoc {
52         // System.out.println("doDrop(" + dtde + ")");
53
DataFlavor JavaDoc[] flavors = dtde.getCurrentDataFlavors();
54         if (canImport(flavors)) {
55             // System.out.println("can import");
56
for (int i = 0; i < flavors.length; i++) {
57                 // System.out.println("flavor " + flavors[i]);
58
// Drop from Windows
59
if (DataFlavor.javaFileListFlavor.equals(flavors[i])) {
60                     dtde.acceptDrop(DnDConstants.ACTION_COPY);
61                     Transferable JavaDoc t = dtde.getTransferable();
62                     if (t.getTransferData(DataFlavor.javaFileListFlavor) instanceof java.util.List JavaDoc) {
63                         java.util.List JavaDoc fileList = (java.util.List JavaDoc) t.getTransferData(DataFlavor.javaFileListFlavor);
64                         if (fileList.get(0) instanceof File JavaDoc) {
65                             File JavaDoc[] f = new File JavaDoc[fileList.size()];
66                             fileList.toArray(f);
67                             fileDropped(f);
68                         }
69                     }
70                     return;
71                 }
72                 // Drop from GNOME
73
else if (flavors[i].getMimeType().startsWith("text/uri-list") && flavors[i].getRepresentationClass()
74                                 .equals(Reader JavaDoc.class)) {
75                     dtde.acceptDrop(DnDConstants.ACTION_COPY);
76                     Transferable JavaDoc t = dtde.getTransferable();
77                     BufferedReader JavaDoc r = new BufferedReader JavaDoc((Reader JavaDoc) t.getTransferData(flavors[i]));
78                     String JavaDoc s = null;
79                     List JavaDoc l = new ArrayList JavaDoc();
80                     while ((s = r.readLine()) != null) {
81                         if (s.startsWith("file://")) {
82                             s = s.substring(7);
83                         }
84                         l.add(new File JavaDoc(s));
85                     }
86                     File JavaDoc[] f = new File JavaDoc[l.size()];
87                     l.toArray(f);
88                     fileDropped(f);
89                     return;
90                 }
91                 // Drop from GNOME
92
else if (DataFlavor.stringFlavor.equals(flavors[i])) {
93                     dtde.acceptDrop(DnDConstants.ACTION_COPY);
94                     Transferable JavaDoc t = dtde.getTransferable();
95                     Object JavaDoc o = t.getTransferData(DataFlavor.stringFlavor);
96                     if (o instanceof String JavaDoc) {
97                         // System.out.println("string uri list = " + o);
98
List JavaDoc l = new ArrayList JavaDoc();
99                         StringTokenizer JavaDoc tz = new StringTokenizer JavaDoc(o.toString(), "\n\r");
100                         while (tz.hasMoreTokens()) {
101                             String JavaDoc path = (String JavaDoc) tz.nextToken();
102                             if (path.startsWith("file://")) {
103                                 path = path.substring(7);
104                             }
105                             l.add(new File JavaDoc(path));
106                         }
107                         File JavaDoc[] f = new File JavaDoc[l.size()];
108                         l.toArray(f);
109                         fileDropped(f);
110                     }
111                     return;
112                 }
113             }
114         }
115         dtde.rejectDrop();
116     }
117
118     boolean canImport(DataFlavor JavaDoc[] flavors) {
119         // System.out.println("Testing if can import");
120
for (int i = 0; i < flavors.length; i++) {
121             if (DataFlavor.javaFileListFlavor.equals(flavors[i])) {
122                 // System.out.println("Yes, " + flavors[i] + " is
123
// importable");
124
return true;
125             } else if (DataFlavor.stringFlavor.equals(flavors[i])) {
126                 // System.out.println("Yes, " + flavors[i] + " is
127
// importable");
128
return true;
129             } else {
130                 if (flavors[i].getMimeType().startsWith("text/uri-list")) {
131                     // System.out.println("Yes, " + flavors[i] + " is
132
// importable");
133
return true;
134                 }
135                 // System.out.println("Mime = " + flavors[i].getMimeType());
136
// System.out.println("Class = " +
137
// flavors[i].getRepresentationClass());
138
//
139
// System.out.println("No, " + flavors[i] + " is not
140
// importable");
141
}
142         }
143         return false;
144     }
145
146     public void dragEnter(DropTargetDragEvent JavaDoc dtde) {
147         System.out.println("dragEnter(" + dtde + ")");
148         dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see java.awt.dnd.DropTargetListener#dragOver(java.awt.dnd.DropTargetDragEvent)
155      */

156     public void dragOver(DropTargetDragEvent JavaDoc dtde) {
157     }
158
159     /*
160      * (non-Javadoc)
161      *
162      * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
163      */

164     public void dropActionChanged(DropTargetDragEvent JavaDoc dtde) {
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see java.awt.dnd.DropTargetListener#dragExit(java.awt.dnd.DropTargetEvent)
171      */

172     public void dragExit(DropTargetEvent JavaDoc dte) {
173     }
174
175
176 }
177
Popular Tags