1 11 package org.eclipse.swt.dnd; 12 13 import java.net.*; 14 15 import org.eclipse.swt.internal.ole.win32.*; 16 import org.eclipse.swt.internal.win32.*; 17 18 34 class URLTransfer extends ByteArrayTransfer { 35 36 static URLTransfer _instance = new URLTransfer(); 37 static final String CFSTR_INETURL = "UniformResourceLocator"; static final int CFSTR_INETURLID = registerType(CFSTR_INETURL); 39 40 private URLTransfer() {} 41 42 47 public static URLTransfer getInstance () { 48 return _instance; 49 } 50 51 60 public void javaToNative (Object object, TransferData transferData){ 61 if (!checkURL(object) || !isSupportedType(transferData)) { 62 DND.error(DND.ERROR_INVALID_DATA); 63 } 64 transferData.result = COM.E_FAIL; 65 String url = ((String [])object)[0]; 67 int count = url.length(); 68 char[] chars = new char[count + 1]; 69 url.getChars(0, count, chars, 0); 70 int codePage = OS.GetACP(); 71 int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null); 72 if (cchMultiByte == 0) { 73 transferData.stgmedium = new STGMEDIUM(); 74 transferData.result = COM.DV_E_STGMEDIUM; 75 return; 76 } 77 int lpMultiByteStr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, cchMultiByte); 78 OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null); 79 transferData.stgmedium = new STGMEDIUM(); 80 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; 81 transferData.stgmedium.unionField = lpMultiByteStr; 82 transferData.stgmedium.pUnkForRelease = 0; 83 transferData.result = COM.S_OK; 84 return; 85 } 86 87 97 public Object nativeToJava(TransferData transferData){ 98 if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null; 99 IDataObject data = new IDataObject(transferData.pIDataObject); 100 data.AddRef(); 101 STGMEDIUM stgmedium = new STGMEDIUM(); 102 FORMATETC formatetc = transferData.formatetc; 103 stgmedium.tymed = COM.TYMED_HGLOBAL; 104 transferData.result = data.GetData(formatetc, stgmedium); 105 data.Release(); 106 if (transferData.result != COM.S_OK) return null; 107 int hMem = stgmedium.unionField; 108 try { 109 int lpMultiByteStr = OS.GlobalLock(hMem); 110 if (lpMultiByteStr == 0) return null; 111 try { 112 int codePage = OS.GetACP(); 113 int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0); 114 if (cchWideChar == 0) return null; 115 char[] lpWideCharStr = new char [cchWideChar - 1]; 116 OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length); 117 return new String []{new String (lpWideCharStr)}; 118 } finally { 119 OS.GlobalUnlock(hMem); 120 } 121 } finally { 122 OS.GlobalFree(hMem); 123 } 124 } 125 126 protected int[] getTypeIds(){ 127 return new int[] {CFSTR_INETURLID}; 128 } 129 130 protected String [] getTypeNames(){ 131 return new String [] {CFSTR_INETURL}; 132 } 133 134 boolean checkURL(Object object) { 135 if (object == null || !(object instanceof String []) || ((String [])object).length == 0) return false; 136 String [] strings = (String [])object; 137 if (strings[0] == null || strings[0].length() == 0) return false; 138 try { 139 new URL(strings[0]); 140 } catch (java.net.MalformedURLException e) { 141 return false; 142 } 143 return true; 144 } 145 146 protected boolean validate(Object object) { 147 return checkURL(object); 148 } 149 } 150 | Popular Tags |