1 11 package org.eclipse.swt.dnd; 12 13 import org.eclipse.swt.internal.ole.win32.*; 14 import org.eclipse.swt.internal.win32.*; 15 16 35 public class FileTransfer extends ByteArrayTransfer { 36 37 private static FileTransfer _instance = new FileTransfer(); 38 private static final String CF_HDROP = "CF_HDROP "; private static final int CF_HDROPID = COM.CF_HDROP; 40 private static final String CF_HDROP_SEPARATOR = "\0"; 42 private FileTransfer() {} 43 44 49 public static FileTransfer getInstance () { 50 return _instance; 51 } 52 53 65 public void javaToNative(Object object, TransferData transferData) { 66 if (!checkFile(object) || !isSupportedType(transferData)) { 67 DND.error(DND.ERROR_INVALID_DATA); 68 } 69 String [] fileNames = (String []) object; 70 StringBuffer allFiles = new StringBuffer (); 71 for (int i = 0; i < fileNames.length; i++) { 72 allFiles.append(fileNames[i]); 73 allFiles.append(CF_HDROP_SEPARATOR); } 75 TCHAR buffer = new TCHAR(0, allFiles.toString(), true); 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 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 105 public Object nativeToJava(TransferData transferData) { 106 if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null; 107 108 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 int count = OS.DragQueryFile(stgmedium.unionField, 0xFFFFFFFF, null, 0); 124 String [] fileNames = new String [count]; 125 for (int i = 0; i < count; i++) { 126 int size = OS.DragQueryFile(stgmedium.unionField, i, null, 0) + 1; 128 TCHAR lpszFile = new TCHAR(0, size); 129 OS.DragQueryFile(stgmedium.unionField, i, lpszFile, size); 131 fileNames[i] = lpszFile.toString(0, lpszFile.strlen()); 132 } 133 OS.DragFinish(stgmedium.unionField); return fileNames; 135 } 136 137 protected int[] getTypeIds(){ 138 return new int[] {CF_HDROPID}; 139 } 140 141 protected String [] getTypeNames(){ 142 return new String [] {CF_HDROP}; 143 } 144 boolean checkFile(Object object) { 145 if (object == null || !(object instanceof String []) || ((String [])object).length == 0) return false; 146 String [] strings = (String [])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 object) { 154 return checkFile(object); 155 } 156 } 157 | Popular Tags |