blob: 8b70854312db035130c8e0b5c3626b481192d164 (
plain) (
blame)
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
|
package org.eclipse.swt.dnd;
/*
* Licensed Materials - Property of IBM,
* (c) Copyright IBM Corp. 1998, 2000 All Rights Reserved
*/
public class FileTransfer extends ByteArrayTransfer {
private static FileTransfer _instance = new FileTransfer();
private static final String TYPENAME = "text/uri-list\0";
private static final int TYPEID = registerType(TYPENAME);
private FileTransfer() {}
public static FileTransfer getInstance () {
return _instance;
}
public void javaToNative(Object object, TransferData transferData) {
if (object == null || !(object instanceof String[])) return;
// build a byte array from data
String[] files = (String[])object;
// create a string separated by "new lines" to represent list of files
String nativeFormat = "file:";
for (int i = 0, length = files.length; i < length; i++){
nativeFormat += files[i]+"\r";
}
nativeFormat += "\0";
// pass byte array on to super to convert to native
super.javaToNative(nativeFormat.getBytes(), transferData);
}
public Object nativeToJava(TransferData transferData) {
byte[] data = (byte[])super.nativeToJava(transferData);
if (data == null) return null;
String string = new String(data);
// parse data and convert string to array of files
int start = string.indexOf("file:");
if (start == -1) return null;
start += 5;
String[] fileNames = new String[0];
while (start < string.length()) {
int end = string.indexOf("\r", start);
if (end == -1) end = string.length() - 1;
String fileName = string.substring(start, end);
String[] newFileNames = new String[fileNames.length + 1];
System.arraycopy(fileNames, 0, newFileNames, 0, fileNames.length);
newFileNames[fileNames.length] = fileName;
fileNames = newFileNames;
start = string.indexOf("file:", end);
if (start == -1) break;
start += 5;
}
return fileNames;
}
protected String[] getTypeNames(){
return new String[]{TYPENAME};
}
protected int[] getTypeIds(){
return new int[]{TYPEID};
}
}
|