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 120 public abstract class ByteArrayTransfer extends Transfer { 121 122 public TransferData[] getSupportedTypes() { 123 int[] types = getTypeIds(); 124 TransferData[] data = new TransferData[types.length]; 125 for (int i = 0; i < types.length; i++) { 126 data[i] = new TransferData(); 127 data[i].type = types[i]; 128 data[i].formatetc = new FORMATETC(); 129 data[i].formatetc.cfFormat = types[i]; 130 data[i].formatetc.dwAspect = COM.DVASPECT_CONTENT; 131 data[i].formatetc.lindex = -1; 132 data[i].formatetc.tymed = COM.TYMED_HGLOBAL; 133 } 134 return data; 135 } 136 137 public boolean isSupportedType(TransferData transferData){ 138 if (transferData == null) return false; 139 int[] types = getTypeIds(); 140 for (int i = 0; i < types.length; i++) { 141 FORMATETC format = transferData.formatetc; 142 if (format.cfFormat == types[i] && 143 (format.dwAspect & COM.DVASPECT_CONTENT) == COM.DVASPECT_CONTENT && 144 (format.tymed & COM.TYMED_HGLOBAL) == COM.TYMED_HGLOBAL ) 145 return true; 146 } 147 return false; 148 } 149 150 161 protected void javaToNative (Object object, TransferData transferData) { 162 if (!checkByteArray(object) || !isSupportedType(transferData)) { 163 DND.error(DND.ERROR_INVALID_DATA); 164 } 165 byte[] data = (byte[])object; 168 int size = data.length; 169 int newPtr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, size); 170 OS.MoveMemory(newPtr, data, size); 171 transferData.stgmedium = new STGMEDIUM(); 172 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; 173 transferData.stgmedium.unionField = newPtr; 174 transferData.stgmedium.pUnkForRelease = 0; 175 transferData.result = COM.S_OK; 176 } 177 178 190 protected Object nativeToJava(TransferData transferData) { 191 if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null; 192 193 IDataObject data = new IDataObject(transferData.pIDataObject); 194 data.AddRef(); 195 FORMATETC formatetc = transferData.formatetc; 196 STGMEDIUM stgmedium = new STGMEDIUM(); 197 stgmedium.tymed = COM.TYMED_HGLOBAL; 198 transferData.result = data.GetData(formatetc, stgmedium); 199 data.Release(); 200 if (transferData.result != COM.S_OK) return null; 201 int hMem = stgmedium.unionField; 202 int size = OS.GlobalSize(hMem); 203 byte[] buffer = new byte[size]; 204 int ptr = OS.GlobalLock(hMem); 205 OS.MoveMemory(buffer, ptr, size); 206 OS.GlobalUnlock(hMem); 207 OS.GlobalFree(hMem); 208 return buffer; 209 } 210 211 boolean checkByteArray(Object object) { 212 return (object != null && object instanceof byte[] && ((byte[])object).length > 0); 213 } 214 } 215 | Popular Tags |