Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c98e5ad7e66cab9fd2ef0e6263b10469ad8d21b (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
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
package org.eclipse.swt.dnd;

/*
 * Licensed Materials - Property of IBM,
 * WebSphere Studio Workbench
 * (c) Copyright IBM Corp 2000
 */
import org.eclipse.swt.internal.ole.win32.*;

/**
 *
 * The <code>FileTransfer</code> class is used to transfer files in a drag and drop operation.
 *
 */
public class FileTransfer extends ByteArrayTransfer {
	
	private static FileTransfer _instance = new FileTransfer();
	
private FileTransfer() {}
/**
 *
 * Returns the singleton instance of the FileTransfer class.
 *
 * @return the singleton instance of the FileTransfer class
 *
 */
public static FileTransfer getInstance () {
	return _instance;
}
/**
 *
 * Converts a list of file names to a platform specific representation of these file names. 
 *
 * <p>On a successful conversion, the transferData.result field will be set to COM.S_OK.
 * If this transfer agent is unable to perform the conversion, the transferData.result field 
 * will be set to the failure value of COM.DV_E_TYMED.</p>
 *
 * @param object a list of file names
 *
 * @param transferData an empty TransferData object; this object will be filled in on return
 *						with the platform specific format of the data
 *
 */
public void javaToNative(Object object, TransferData transferData) {

	if (object == null || !(object instanceof String[])) {
		transferData.result = COM.E_FAIL;
		return;
	}
		
	// build a byte array from data
	String[] fileNames = (String[]) object;
	int fileNameSize = 0;
	byte[][] files = new byte[fileNames.length][];
	for (int i = 0; i < fileNames.length; i++) {
		files[i] = (fileNames[i]+'\0').getBytes(); // each name is null terminated
		fileNameSize += files[i].length;
	}
	byte[] buffer = new byte[DROPFILES.sizeof + fileNameSize + 1]; // there is an extra null terminator at the very end
	DROPFILES dropfiles = new DROPFILES();
	dropfiles.pFiles = DROPFILES.sizeof;
	dropfiles.pt_x = dropfiles.pt_y = 0;
	dropfiles.fNC = 0;
	dropfiles.fWide = 0;	
	COM.MoveMemory(buffer, dropfiles, DROPFILES.sizeof);
	
	int offset = DROPFILES.sizeof;
	for (int i = 0; i < fileNames.length; i++) {
		System.arraycopy(files[i], 0, buffer, offset, files[i].length);
		offset += files[i].length;
	}

	// pass byte array on to super to convert to native
	super.javaToNative(buffer, transferData);
}
/**
 *
 * Converts a platform specific representation of a list of file names to a Java array of String.
 *
 * @param transferData the platform specific representation of the data that has been transferred
 *
 * @return a Java array of String containing a list of file names if the conversion was successful; otherwise null
 *
 */
public Object nativeToJava(TransferData transferData) {
	
	if (!isSupportedType(transferData) || transferData.pIDataObject == 0) {
		transferData.result = COM.E_FAIL;
		return null;
	}
	
	// get file names from IDataObject
	IDataObject dataObject = new IDataObject(transferData.pIDataObject);
	dataObject.AddRef();
	
	FORMATETC formatetc = new FORMATETC();
	formatetc.cfFormat = COM.CF_HDROP;
	formatetc.ptd = 0;
	formatetc.dwAspect = COM.DVASPECT_CONTENT;
	formatetc.lindex = -1;
	formatetc.tymed = COM.TYMED_HGLOBAL;

	STGMEDIUM stgmedium = new STGMEDIUM();
	stgmedium.tymed = COM.TYMED_HGLOBAL;
			
	transferData.result = dataObject.GetData(formatetc, stgmedium);
	dataObject.Release();
	if (transferData.result != COM.S_OK) {
		return null;
	}
	
	// How many files are there?
	int count = COM.DragQueryFile(stgmedium.unionField, 0xFFFFFFFF, null, 0);
	String[] fileNames = new String[count];
	for (int i = 0; i < count; i++){
		// How long is the name ?
		int size = COM.DragQueryFile(stgmedium.unionField, i, null, 0) + 1;
		byte[] lpszFile = new byte[size];	
		// Get file name and append it to string
		COM.DragQueryFile(stgmedium.unionField, i, lpszFile, size);
		String fileName = new String(lpszFile);
		// remove terminating '\0'
		int index = fileName.indexOf("\0");
		fileName = fileName.substring(0, index);
		fileNames[i] = fileName;
	}
	COM.DragFinish(stgmedium.unionField); // frees data associated with HDROP data
	return fileNames;
}
protected int[] getTypeIds(){
	return new int[] {COM.CF_HDROP};
}
protected String[] getTypeNames(){
	return new String[] {"CF_HDROP"};
}
}

Back to the top