KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > dnd > FileTransfer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.dnd;
12
13 import org.eclipse.swt.internal.ole.win32.*;
14 import org.eclipse.swt.internal.win32.*;
15
16 /**
17  * The class <code>FileTransfer</code> provides a platform specific mechanism
18  * for converting a list of files represented as a java <code>String[]</code> to a
19  * platform specific representation of the data and vice versa.
20  * Each <code>String</code> in the array contains the absolute path for a single
21  * file or directory.
22  * See <code>Transfer</code> for additional information.
23  *
24  * <p>An example of a java <code>String[]</code> containing a list of files is shown
25  * below:</p>
26  *
27  * <code><pre>
28  * File file1 = new File("C:\temp\file1");
29  * File file2 = new File("C:\temp\file2");
30  * String[] fileData = new String[2];
31  * fileData[0] = file1.getAbsolutePath();
32  * fileData[1] = file2.getAbsolutePath();
33  * </code></pre>
34  */

35 public class FileTransfer extends ByteArrayTransfer {
36     
37     private static FileTransfer _instance = new FileTransfer();
38     private static final String JavaDoc CF_HDROP = "CF_HDROP "; //$NON-NLS-1$
39
private static final int CF_HDROPID = COM.CF_HDROP;
40     private static final String JavaDoc CF_HDROP_SEPARATOR = "\0"; //$NON-NLS-1$
41

42 private FileTransfer() {}
43
44 /**
45  * Returns the singleton instance of the FileTransfer class.
46  *
47  * @return the singleton instance of the FileTransfer class
48  */

49 public static FileTransfer getInstance () {
50     return _instance;
51 }
52
53 /**
54  * This implementation of <code>javaToNative</code> converts a list of file names
55  * represented by a java <code>String[]</code> to a platform specific representation.
56  * Each <code>String</code> in the array contains the absolute path for a single
57  * file or directory. For additional information see
58  * <code>Transfer#javaToNative</code>.
59  *
60  * @param object a java <code>String[]</code> containing the file names to be
61  * converted
62  * @param transferData an empty <code>TransferData</code> object; this
63  * object will be filled in on return with the platform specific format of the data
64  */

65 public void javaToNative(Object JavaDoc object, TransferData transferData) {
66     if (!checkFile(object) || !isSupportedType(transferData)) {
67         DND.error(DND.ERROR_INVALID_DATA);
68     }
69     String JavaDoc[] fileNames = (String JavaDoc[]) object;
70     StringBuffer JavaDoc allFiles = new StringBuffer JavaDoc();
71     for (int i = 0; i < fileNames.length; i++) {
72         allFiles.append(fileNames[i]);
73         allFiles.append(CF_HDROP_SEPARATOR); // each name is null terminated
74
}
75     TCHAR buffer = new TCHAR(0, allFiles.toString(), true); // there is an extra null terminator at the very end
76
DROPFILES dropfiles = new DROPFILES();
77     dropfiles.pFiles = DROPFILES.sizeof;
78     dropfiles.pt_x = dropfiles.pt_y = 0;
79     dropfiles.fNC = 0;
80     dropfiles.fWide = OS.IsUnicode ? 1 : 0;
81     // Allocate the memory because the caller (DropTarget) has not handed it in
82
// The caller of this method must release the data when it is done with it.
83
int byteCount = buffer.length() * TCHAR.sizeof;
84     int newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, DROPFILES.sizeof + byteCount);
85     OS.MoveMemory(newPtr, dropfiles, DROPFILES.sizeof);
86     OS.MoveMemory(newPtr + DROPFILES.sizeof, buffer, byteCount);
87     transferData.stgmedium = new STGMEDIUM();
88     transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
89     transferData.stgmedium.unionField = newPtr;
90     transferData.stgmedium.pUnkForRelease = 0;
91     transferData.result = COM.S_OK;
92 }
93
94 /**
95  * This implementation of <code>nativeToJava</code> converts a platform specific
96  * representation of a list of file names to a java <code>String[]</code>.
97  * Each String in the array contains the absolute path for a single file or directory.
98  * For additional information see <code>Transfer#nativeToJava</code>.
99  *
100  * @param transferData the platform specific representation of the data to be
101  * been converted
102  * @return a java <code>String[]</code> containing a list of file names if the
103  * conversion was successful; otherwise null
104  */

105 public Object JavaDoc nativeToJava(TransferData transferData) {
106     if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null;
107     
108     // get file names from IDataObject
109
IDataObject dataObject = new IDataObject(transferData.pIDataObject);
110     dataObject.AddRef();
111     FORMATETC formatetc = new FORMATETC();
112     formatetc.cfFormat = COM.CF_HDROP;
113     formatetc.ptd = 0;
114     formatetc.dwAspect = COM.DVASPECT_CONTENT;
115     formatetc.lindex = -1;
116     formatetc.tymed = COM.TYMED_HGLOBAL;
117     STGMEDIUM stgmedium = new STGMEDIUM();
118     stgmedium.tymed = COM.TYMED_HGLOBAL;
119     transferData.result = dataObject.GetData(formatetc, stgmedium);
120     dataObject.Release();
121     if (transferData.result != COM.S_OK) return null;
122     // How many files are there?
123
int count = OS.DragQueryFile(stgmedium.unionField, 0xFFFFFFFF, null, 0);
124     String JavaDoc[] fileNames = new String JavaDoc[count];
125     for (int i = 0; i < count; i++) {
126         // How long is the name ?
127
int size = OS.DragQueryFile(stgmedium.unionField, i, null, 0) + 1;
128         TCHAR lpszFile = new TCHAR(0, size);
129         // Get file name and append it to string
130
OS.DragQueryFile(stgmedium.unionField, i, lpszFile, size);
131         fileNames[i] = lpszFile.toString(0, lpszFile.strlen());
132     }
133     OS.DragFinish(stgmedium.unionField); // frees data associated with HDROP data
134
return fileNames;
135 }
136
137 protected int[] getTypeIds(){
138     return new int[] {CF_HDROPID};
139 }
140
141 protected String JavaDoc[] getTypeNames(){
142     return new String JavaDoc[] {CF_HDROP};
143 }
144 boolean checkFile(Object JavaDoc object) {
145     if (object == null || !(object instanceof String JavaDoc[]) || ((String JavaDoc[])object).length == 0) return false;
146     String JavaDoc[] strings = (String JavaDoc[])object;
147     for (int i = 0; i < strings.length; i++) {
148         if (strings[i] == null || strings[i].length() == 0) return false;
149     }
150     return true;
151 }
152
153 protected boolean validate(Object JavaDoc object) {
154     return checkFile(object);
155 }
156 }
157
Popular Tags