URLTransferpublic class URLTransfer extends org.eclipse.swt.dnd.ByteArrayTransfer URL Transfer type for Drag and Drop of URLs
Windows IDs are already functional.
Please use Win32TransferTypes to determine the IDs for other OSes! |
Fields Summary |
---|
private boolean | bCheckingStringWe are in the process of checking a string to see if it's a valid URL | private static boolean | DEBUG | private static URLTransfer | _instance | private static final String[] | supportedTypes | private static final int[] | supportedTypeIds |
Methods Summary |
---|
public org.gudy.azureus2.ui.swt.URLTransfer$URLType | bytebufferToJava(byte[] buffer)
if (buffer == null) {
if (DEBUG) System.out.println("buffer null");
return null;
}
URLType myData = null;
try {
String data;
if (buffer.length > 1) {
if (DEBUG) {
for (int i = 0; i < buffer.length; i++) {
if (buffer[i] >= 32)
System.out.print(((char) buffer[i]));
else
System.out.print("#");
}
System.out.println();
}
boolean bFirst0 = buffer[0] == 0;
boolean bSecond0 = buffer[1] == 0;
if (bFirst0 && bSecond0)
// This is probably UTF-32 Big Endian.
// Let's hope default constructor can handle it (It can't)
data = new String(buffer);
else if (bFirst0)
data = new String(buffer, "UTF-16BE");
else if (bSecond0)
data = new String(buffer, "UTF-16LE");
else if (buffer[0] == (byte) 0xEF && buffer[1] == (byte) 0xBB
&& buffer.length > 3 && buffer[2] == (byte) 0xBF)
data = new String(buffer, 3, buffer.length - 3, "UTF-8");
else if (buffer[0] == (byte) 0xFF || buffer[0] == (byte) 0xFE)
data = new String(buffer, "UTF-16");
else {
data = new String(buffer);
}
} else {
// Older Code:
// Remove 0 values from byte array, messing up any Unicode strings
byte[] text = new byte[buffer.length];
int j = 0;
for (int i = 0; i < buffer.length; i++) {
if (buffer[i] != 0)
text[j++] = buffer[i];
}
data = new String(text, 0, j);
}
if (data == null) {
if (DEBUG) System.out.println("data null");
return null;
}
int iPos = data.indexOf("\nURL=");
if (iPos > 0) {
int iEndPos = data.indexOf("\r", iPos);
if (iEndPos < 0) {
iEndPos = data.length();
}
myData = new URLType();
myData.linkURL = data.substring(iPos + 5, iEndPos);
myData.linkText = "";
} else {
String[] split = data.split("[\r\n]+", 2);
myData = new URLType();
myData.linkURL = (split.length > 0) ? split[0] : "";
myData.linkText = (split.length > 1) ? split[1] : "";
}
} catch (Exception ex) {
ex.printStackTrace();
}
return myData;
| public static org.gudy.azureus2.ui.swt.URLTransfer | getInstance()
return _instance;
| protected int[] | getTypeIds()
return supportedTypeIds;
| protected java.lang.String[] | getTypeNames()
return supportedTypes;
| public boolean | isSupportedType(org.eclipse.swt.dnd.TransferData transferData)
if (bCheckingString)
return true;
if (transferData == null)
return false;
// TODO: Check if it's a string list of URLs
// String -- Check if URL, skip to next if not
URLType url = null;
if (DEBUG) System.out.println("Checking if type #" + transferData.type + " is URL");
bCheckingString = true;
try {
byte[] buffer = (byte[]) super.nativeToJava(transferData);
url = bytebufferToJava(buffer);
} catch (Exception e) {
Debug.out(e);
} finally {
bCheckingString = false;
}
if (url == null) {
if (DEBUG) System.out.println("no, Null URL for type #" + transferData.type);
return false;
}
if (UrlUtils.isURL(url.linkURL, false)) {
if (DEBUG) System.out.println("Yes, " + url.linkURL + " of type #" + transferData.type);
return true;
}
if (DEBUG) System.out.println("no, " + url.linkURL + " not URL for type #" + transferData.type);
return false;
| public void | javaToNative(java.lang.Object object, org.eclipse.swt.dnd.TransferData transferData)
if (DEBUG)
System.out.println("javaToNative called");
if (object == null || !(object instanceof URLType[]))
return;
if (isSupportedType(transferData)) {
URLType[] myTypes = (URLType[]) object;
try {
// write data to a byte array and then ask super to convert to pMedium
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream writeOut = new DataOutputStream(out);
for (int i = 0, length = myTypes.length; i < length; i++) {
writeOut.writeBytes(myTypes[i].linkURL);
writeOut.writeBytes("\n");
writeOut.writeBytes(myTypes[i].linkText);
}
byte[] buffer = out.toByteArray();
writeOut.close();
super.javaToNative(buffer, transferData);
} catch (IOException e) {
}
}
| public static void | main(java.lang.String[] args)Test for varioud UTF Strings
BOM information from http://www.unicode.org/faq/utf_bom.html
Map map = new LinkedHashMap();
map.put("UTF-8", new byte[] { (byte) 0xEF, (byte) 0xbb, (byte) 0xbf, 'H",
'i" });
map.put("UTF-32 BE BOM", new byte[] { 0, 0, (byte) 0xFE, (byte) 0xFF, 'H",
0, 0, 0, 'i", 0, 0, 0 });
map.put("UTF-16 LE BOM", new byte[] { (byte) 0xFF, (byte) 0xFE, 'H", 0,
'i", 0 });
map.put("UTF-16 BE BOM", new byte[] { (byte) 0xFE, (byte) 0xFF, 0, 'H", 0,
'i" });
map.put("UTF-16 LE", new byte[] { 'H", 0, 'i", 0 });
map.put("UTF-16 BE", new byte[] { 0, 'H", 0, 'i" });
for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
String element = (String) iterator.next();
System.out.println(element + ":");
byte[] buffer = (byte[]) map.get(element);
boolean bFirst0 = buffer[0] == 0;
boolean bSecond0 = buffer[1] == 0;
String data = "";
try {
if (bFirst0 && bSecond0)
// This is probably UTF-32 Big Endian.
// Let's hope default constructor can handle it (It can't)
data = new String(buffer);
else if (bFirst0)
data = new String(buffer, "UTF-16BE");
else if (bSecond0)
data = new String(buffer, "UTF-16LE");
else if (buffer[0] == (byte) 0xEF && buffer[1] == (byte) 0xBB
&& buffer.length > 3 && buffer[2] == (byte) 0xBF)
data = new String(buffer, 3, buffer.length - 3, "UTF-8");
else if (buffer[0] == (byte) 0xFF || buffer[0] == (byte) 0xFE)
data = new String(buffer, "UTF-16");
else {
data = new String(buffer);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(data);
}
| public java.lang.Object | nativeToJava(org.eclipse.swt.dnd.TransferData transferData)
if (DEBUG) System.out.println("nativeToJava called");
try {
if (isSupportedType(transferData)) {
byte [] buffer = (byte[]) super.nativeToJava(transferData);
return bytebufferToJava(buffer);
}
} catch (Exception e) {
Debug.out(e);
}
return null;
| public static org.eclipse.swt.dnd.TransferData | pickBestType(org.eclipse.swt.dnd.TransferData[] dataTypes, org.eclipse.swt.dnd.TransferData def)Sometimes, CF_Text will be in currentDataType even though CF_UNICODETEXT
is present. This is a workaround until its fixed properly.
Place it in dropAccept
if (event.data instanceof URLTransfer.URLType)
event.currentDataType = URLTransfer.pickBestType(event.dataTypes, event.currentDataType);
for (int i = 0; i < supportedTypeIds.length; i++) {
int supportedTypeID = supportedTypeIds[i];
for (int j = 0; j < dataTypes.length; j++) {
try {
TransferData data = dataTypes[j];
if (supportedTypeID == data.type)
return data;
} catch (Throwable t) {
Debug.out("Picking Best Type", t);
}
}
}
return def;
|
|