My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* MicroEmulator and BlueCove
* Copyright (C) 2006-2009 Bartek Teodorczyk <barteo@barteo.net>
* Copyright (C) 2006-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package net.sf.bluecove.obex;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.JComponent;
import javax.swing.TransferHandler;

/**
*
*/
public class DropTransferHandler extends TransferHandler {

private static final long serialVersionUID = 1L;

private static DataFlavor uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String", null);

private static boolean debugImport = false;

private Main mainInstance;

public DropTransferHandler(Main main) {
mainInstance = main;
}

public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}

public boolean canImport(JComponent comp, DataFlavor transferFlavors[]) {
for (int i = 0; i < transferFlavors.length; i++) {
Class representationclass = transferFlavors[i].getRepresentationClass();
// URL from Explorer or Firefox, KDE
if ((representationclass != null) && URL.class.isAssignableFrom(representationclass)) {
if (debugImport) {
Logger.debug("acepted ", transferFlavors[i]);
}
return true;
}
// Drop from Windows Explorer
if (DataFlavor.javaFileListFlavor.equals(transferFlavors[i])) {
if (debugImport) {
Logger.debug("acepted ", transferFlavors[i]);
}
return true;
}
// Drop from GNOME
if (DataFlavor.stringFlavor.equals(transferFlavors[i])) {
if (debugImport) {
Logger.debug("acepted ", transferFlavors[i]);
}
return true;
}
if (uriListFlavor.equals(transferFlavors[i])) {
if (debugImport) {
Logger.debug("acepted ", transferFlavors[i]);
}
return true;
}
// String mimePrimaryType = transferFlavors[i].getPrimaryType();
// String mimeSubType = transferFlavors[i].getSubType();
// if ((mimePrimaryType != null) && (mimeSubType != null)) {
// if (mimePrimaryType.equals("text") &&
// mimeSubType.equals("uri-list")) {
// Logger.debug("acepted ", transferFlavors[i]);
// return true;
// }
// }
if (debugImport) {
Logger.debug(i + " unknown import ", transferFlavors[i]);
}
}
if (debugImport) {
Logger.debug("import rejected");
}
return false;
}

public boolean importData(JComponent comp, Transferable t) {
DataFlavor[] transferFlavors = t.getTransferDataFlavors();
for (int i = 0; i < transferFlavors.length; i++) {
// Drop from Windows Explorer
if (DataFlavor.javaFileListFlavor.equals(transferFlavors[i])) {
Logger.debug("importing", transferFlavors[i]);
try {
List fileList = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
if (fileList.get(0) instanceof File) {
File f = (File) fileList.get(0);
mainInstance.downloadFile(getCanonicalFileURL(f));
for (int fi = 1; fi < fileList.size(); fi++) {
f = (File) fileList.get(fi);
mainInstance.queueFile(getCanonicalFileURL(f));
}
} else {
Logger.debug("Unknown object in list ", fileList.get(0));
}
} catch (UnsupportedFlavorException e) {
Logger.debug(e);
} catch (IOException e) {
Logger.debug(e);
}
return true;
}

// Drop from GNOME Firefox
if (DataFlavor.stringFlavor.equals(transferFlavors[i])) {
Object data;
try {
data = t.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
continue;
} catch (IOException e) {
continue;
}
if (data instanceof String) {
Logger.debug("importing", transferFlavors[i]);
String path = getPathString((String) data);
mainInstance.downloadFile(path);
return true;
}
}
// Drop from GNOME Nautilus
if (uriListFlavor.equals(transferFlavors[i])) {
Object data;
try {
data = t.getTransferData(uriListFlavor);
} catch (UnsupportedFlavorException e) {
continue;
} catch (IOException e) {
continue;
}
if (data instanceof String) {
Logger.debug("importing", transferFlavors[i]);
String path = getPathString((String) data);
mainInstance.downloadFile(path);
return true;
}
}
if (debugImport) {
Logger.debug(i + " unknown importData ", transferFlavors[i]);
}
}
// This is the second best option since it works incorrectly on Max OS X
// making url like this [file://localhost/users/work/app.jad]
for (int i = 0; i < transferFlavors.length; i++) {
Class representationclass = transferFlavors[i].getRepresentationClass();
// URL from Explorer or Firefox, KDE
if ((representationclass != null) && URL.class.isAssignableFrom(representationclass)) {
Logger.debug("importing", transferFlavors[i]);
try {
URL jadUrl = (URL) t.getTransferData(transferFlavors[i]);
String urlString = jadUrl.toExternalForm();
mainInstance.downloadFile(urlString);
} catch (UnsupportedFlavorException e) {
Logger.debug(e);
} catch (IOException e) {
Logger.debug(e);
}
return true;
}
}
return false;
}

private String getPathString(String path) {
if (path == null) {
return null;
}
StringTokenizer st = new StringTokenizer(path.trim(), "\n\r");
if (st.hasMoreTokens()) {
return st.nextToken();
}
return path;
}

public static String getCanonicalFileURL(File file) {
String path = file.getAbsoluteFile().getPath();
if (File.separatorChar != '/') {
path = path.replace(File.separatorChar, '/');
}
// Not network path
if (!path.startsWith("//")) {
if (path.startsWith("/")) {
path = "//" + path;
} else {
path = "///" + path;
}
}
return "file:" + path;
}
}

Change log

r2916 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2471 by skarzhevskyy on Nov 30, 2008   Diff
Change license to Apache License,
Version 2.0, Update headers
r2408 by skarzhevskyy on Oct 9, 2008   Diff
organize product to modules
r1755 by skarzhevskyy on Feb 9, 2008   Diff
command line (no GUI)
All revisions of this file

File info

Size: 6727 bytes, 222 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting