comparison dwt/dnd/URLTransfer.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents a9ab4c738ed8
children d0d0b721dbcc
comparison
equal deleted inserted replaced
44:ca5e494f2bbf 45:d8635bb48c7c
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/ 10 *******************************************************************************/
11 module dwt.dnd; 11 module dwt.dnd;
12 12
13 import dwt.internal.carbon.*; 13 import dwt.internal.cocoa.NSString;
14 import dwt.internal.cocoa.NSURL;
15 import dwt.internal.cocoa.OS.URLTransfer;
14 16
15 /** 17 /**
18
19 import dwt.dwthelper.utils;
16 * The class <code>URLTransfer</code> provides a platform specific mechanism 20 * The class <code>URLTransfer</code> provides a platform specific mechanism
17 * for converting text in URL format represented as a java <code>String</code> 21 * for converting text in URL format represented as a java <code>String</code>
18 * to a platform specific representation of the data and vice versa. See 22 * to a platform specific representation of the data and vice versa. The string
19 * <code>Transfer</code> for additional information. The String 23 * must contain a fully specified url.
20 * must contain the fully specified url.
21 * 24 *
22 * <p>An example of a java <code>String[]</code> containing a URL is shown 25 * <p>An example of a java <code>String</code> containing a URL is shown below:</p>
23 * below:</p>
24 * 26 *
25 * <code><pre> 27 * <code><pre>
26 * String url = "http://www.eclipse.org"; 28 * String url = "http://www.eclipse.org";
27 * </code></pre> 29 * </code></pre>
30 *
31 * @see Transfer
28 */ 32 */
29 public class URLTransfer : ByteArrayTransfer { 33 public class URLTransfer extends ByteArrayTransfer {
30 34
31 static URLTransfer _instance = new URLTransfer(); 35 static URLTransfer _instance = new URLTransfer();
32 static final String URL = "url "; //$NON-NLS-1$ 36 static final String URL = OS.NSURLPboardType.getString();
33 static final int URL_ID = registerType(URL); 37 static final int URL_ID = registerType(URL);
34 static final String URLN = "urln"; //$NON-NLS-1$
35 static final int URLN_ID = registerType(URLN);
36 38
37 private URLTransfer() {} 39 private URLTransfer() {}
38 40
39 /** 41 /**
40 * Returns the singleton instance of the URLTransfer class. 42 * Returns the singleton instance of the URLTransfer class.
46 } 48 }
47 49
48 /** 50 /**
49 * This implementation of <code>javaToNative</code> converts a URL 51 * This implementation of <code>javaToNative</code> converts a URL
50 * represented by a java <code>String</code> to a platform specific representation. 52 * represented by a java <code>String</code> to a platform specific representation.
51 * For additional information see <code>Transfer#javaToNative</code>.
52 * 53 *
53 * @param object a java <code>String[]</code> containing a URL 54 * @param object a java <code>String</code> containing a URL
54 * @param transferData an empty <code>TransferData</code> object; this 55 * @param transferData an empty <code>TransferData</code> object that will
55 * object will be filled in on return with the platform specific format of the data 56 * be filled in on return with the platform specific format of the data
57 *
58 * @see Transfer#nativeToJava
56 */ 59 */
57 public void javaToNative (Object object, TransferData transferData){ 60 public void javaToNative (Object object, TransferData transferData){
58 if (!checkURL(object) || !isSupportedType(transferData)) { 61 if (!checkURL(object) || !isSupportedType(transferData)) {
59 DND.error(DND.ERROR_INVALID_DATA); 62 DND.error(DND.ERROR_INVALID_DATA);
60 } 63 }
61 transferData.result = -1; 64 String url = (String)object;
62 String url = cast(String)object; 65 NSString nsString = NSString.stringWith(url);
63 int count = url.length(); 66 NSString escapedString = nsString.stringByAddingPercentEscapesUsingEncoding(OS.NSUTF8StringEncoding);
64 char[] chars = new char[count]; 67 transferData.data = NSURL.URLWithString(escapedString);
65 url.getChars(0, count, chars, 0);
66 int cfString = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, chars, count);
67 if (cfString is 0) return;
68 try {
69 CFRange range = new CFRange();
70 range.length = chars.length;
71 int encoding = OS.CFStringGetSystemEncoding();
72 int[] size = new int[1];
73 int numChars = OS.CFStringGetBytes(cfString, range, encoding, cast(byte)'?', true, null, 0, size);
74 if (numChars is 0 || size[0] is 0) return;
75 byte[] buffer = new byte[size[0]];
76 numChars = OS.CFStringGetBytes(cfString, range, encoding, cast(byte)'?', true, buffer, size [0], size);
77 if (numChars is 0) return;
78 transferData.data = new byte[][] {buffer};
79 transferData.result = 0;
80 } finally {
81 OS.CFRelease(cfString);
82 }
83 } 68 }
84 69
85 /** 70 /**
86 * This implementation of <code>nativeToJava</code> converts a platform specific 71 * This implementation of <code>nativeToJava</code> converts a platform
87 * representation of a URL to a java <code>String</code>. 72 * specific representation of a URL to a java <code>String</code>.
88 * For additional information see <code>Transfer#nativeToJava</code>.
89 * 73 *
90 * @param transferData the platform specific representation of the data to be 74 * @param transferData the platform specific representation of the data to be converted
91 * converted 75 * @return a java <code>String</code> containing a URL if the conversion was successful;
92 * @return a java <code>String[]</code> containing a URL if the 76 * otherwise null
93 * conversion was successful; otherwise null 77 *
78 * @see Transfer#javaToNative
94 */ 79 */
95 public Object nativeToJava(TransferData transferData){ 80 public Object nativeToJava(TransferData transferData){
96 if (!isSupportedType(transferData) || transferData.data is null) return null; 81 if (!isSupportedType(transferData) || transferData.data is null) return null;
97 if (transferData.data.length is 0) return null; 82 NSURL nsUrl = (NSURL) transferData.data;
98 byte[] buffer = transferData.data[0]; 83 NSString nsString = nsUrl.absoluteString();
99 int encoding = OS.CFStringGetSystemEncoding(); 84 return nsString.getString();
100 int cfString = OS.CFStringCreateWithBytes(OS.kCFAllocatorDefault, buffer, buffer.length, encoding, true);
101 if (cfString is 0) return null;
102 try {
103 int length = OS.CFStringGetLength(cfString);
104 if (length is 0) return null;
105 char[] chars = new char[length];
106 CFRange range = new CFRange();
107 range.length = length;
108 OS.CFStringGetCharacters(cfString, range, chars);
109 return new String(chars);
110 } finally {
111 OS.CFRelease(cfString);
112 }
113 } 85 }
114 86
115 protected int[] getTypeIds(){ 87 protected int[] getTypeIds(){
116 return new int[] {URL_ID, URLN_ID}; 88 return new int[] {URL_ID};
117 } 89 }
118 90
119 protected String[] getTypeNames(){ 91 protected String[] getTypeNames(){
120 return new String[] {URL, URLN}; 92 return new String[] {URL};
121 } 93 }
122 94
123 bool checkURL(Object object) { 95 bool checkURL(Object object) {
124 return object !is null && ( null !is cast(String)object ) && (cast(String)object).length() > 0; 96 return object !is null && (object instanceof String) && ((String)object).length() > 0;
125 } 97 }
126 98
127 protected bool validate(Object object) { 99 protected bool validate(Object object) {
128 return checkURL(object); 100 return checkURL(object);
129 } 101 }