Mercurial > projects > dwt-mac
comparison dwt/dnd/URLTransfer.d @ 114:d0d0b721dbcc
Ported dwt.dnd.URLTransfer
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Wed, 31 Dec 2008 15:17:35 +0100 |
parents | d8635bb48c7c |
children | 2e671fa40eec |
comparison
equal
deleted
inserted
replaced
113:2d6116ea306e | 114:d0d0b721dbcc |
---|---|
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * | |
11 * Port to the D programming language: | |
12 * Jacob Carlborg <doob@me.com> | |
10 *******************************************************************************/ | 13 *******************************************************************************/ |
11 module dwt.dnd; | 14 module dwt.dnd.URLTransfer; |
15 | |
16 import dwt.dwthelper.utils; | |
12 | 17 |
13 import dwt.internal.cocoa.NSString; | 18 import dwt.internal.cocoa.NSString; |
14 import dwt.internal.cocoa.NSURL; | 19 import dwt.internal.cocoa.NSURL; |
15 import dwt.internal.cocoa.OS.URLTransfer; | 20 import dwt.internal.cocoa.OS; |
21 | |
22 import dwt.dnd.ByteArrayTransfer; | |
23 import dwt.dnd.DND; | |
24 import dwt.dnd.TransferData; | |
16 | 25 |
17 /** | 26 /** |
18 | |
19 import dwt.dwthelper.utils; | |
20 * The class <code>URLTransfer</code> provides a platform specific mechanism | 27 * The class <code>URLTransfer</code> provides a platform specific mechanism |
21 * for converting text in URL format represented as a java <code>String</code> | 28 * for converting text in URL format represented as a java <code>String</code> |
22 * to a platform specific representation of the data and vice versa. The string | 29 * to a platform specific representation of the data and vice versa. The string |
23 * must contain a fully specified url. | 30 * must contain a fully specified url. |
24 * | 31 * |
28 * String url = "http://www.eclipse.org"; | 35 * String url = "http://www.eclipse.org"; |
29 * </code></pre> | 36 * </code></pre> |
30 * | 37 * |
31 * @see Transfer | 38 * @see Transfer |
32 */ | 39 */ |
33 public class URLTransfer extends ByteArrayTransfer { | 40 public class URLTransfer : ByteArrayTransfer { |
34 | 41 |
35 static URLTransfer _instance = new URLTransfer(); | 42 static URLTransfer _instance; |
36 static final String URL = OS.NSURLPboardType.getString(); | 43 static final String URL; |
37 static final int URL_ID = registerType(URL); | 44 static final int URL_ID; |
45 | |
46 static this () | |
47 { | |
48 _instance = new URLTransfer(); | |
49 URL = OS.NSURLPboardType.getString(); | |
50 URL_ID = registerType(URL); | |
51 } | |
38 | 52 |
39 private URLTransfer() {} | 53 private this() {} |
40 | 54 |
41 /** | 55 /** |
42 * Returns the singleton instance of the URLTransfer class. | 56 * Returns the singleton instance of the URLTransfer class. |
43 * | 57 * |
44 * @return the singleton instance of the URLTransfer class | 58 * @return the singleton instance of the URLTransfer class |
45 */ | 59 */ |
46 public static URLTransfer getInstance () { | 60 public static URLTransfer getInstance () { |
47 return _instance; | 61 return _instance; |
48 } | 62 } |
49 | 63 |
50 /** | 64 /** |
51 * This implementation of <code>javaToNative</code> converts a URL | 65 * This implementation of <code>javaToNative</code> converts a URL |
52 * represented by a java <code>String</code> to a platform specific representation. | 66 * represented by a java <code>String</code> to a platform specific representation. |
56 * be filled in on return with the platform specific format of the data | 70 * be filled in on return with the platform specific format of the data |
57 * | 71 * |
58 * @see Transfer#nativeToJava | 72 * @see Transfer#nativeToJava |
59 */ | 73 */ |
60 public void javaToNative (Object object, TransferData transferData){ | 74 public void javaToNative (Object object, TransferData transferData){ |
61 if (!checkURL(object) || !isSupportedType(transferData)) { | 75 if (!checkURL(object) || !isSupportedType(transferData)) { |
62 DND.error(DND.ERROR_INVALID_DATA); | 76 DND.error(DND.ERROR_INVALID_DATA); |
63 } | 77 } |
64 String url = (String)object; | 78 String url = (cast(ArrayWrapperString)object).array; |
65 NSString nsString = NSString.stringWith(url); | 79 NSString nsString = NSString.stringWith(url); |
66 NSString escapedString = nsString.stringByAddingPercentEscapesUsingEncoding(OS.NSUTF8StringEncoding); | 80 NSString escapedString = nsString.stringByAddingPercentEscapesUsingEncoding(OS.NSUTF8StringEncoding); |
67 transferData.data = NSURL.URLWithString(escapedString); | 81 transferData.data = NSURL.URLWithString(escapedString); |
68 } | 82 } |
69 | 83 |
70 /** | 84 /** |
71 * This implementation of <code>nativeToJava</code> converts a platform | 85 * This implementation of <code>nativeToJava</code> converts a platform |
72 * specific representation of a URL to a java <code>String</code>. | 86 * specific representation of a URL to a java <code>String</code>. |
76 * otherwise null | 90 * otherwise null |
77 * | 91 * |
78 * @see Transfer#javaToNative | 92 * @see Transfer#javaToNative |
79 */ | 93 */ |
80 public Object nativeToJava(TransferData transferData){ | 94 public Object nativeToJava(TransferData transferData){ |
81 if (!isSupportedType(transferData) || transferData.data is null) return null; | 95 if (!isSupportedType(transferData) || transferData.data is null) return null; |
82 NSURL nsUrl = (NSURL) transferData.data; | 96 NSURL nsUrl = cast(NSURL) transferData.data; |
83 NSString nsString = nsUrl.absoluteString(); | 97 NSString nsString = nsUrl.absoluteString(); |
84 return nsString.getString(); | 98 return new ArrayWrapperString(nsString.getString()); |
85 } | 99 } |
86 | 100 |
87 protected int[] getTypeIds(){ | 101 protected int[] getTypeIds(){ |
88 return new int[] {URL_ID}; | 102 return [URL_ID]; |
89 } | 103 } |
90 | 104 |
91 protected String[] getTypeNames(){ | 105 protected String[] getTypeNames(){ |
92 return new String[] {URL}; | 106 return [URL]; |
93 } | 107 } |
94 | 108 |
95 bool checkURL(Object object) { | 109 bool checkURL(Object object) { |
96 return object !is null && (object instanceof String) && ((String)object).length() > 0; | 110 return object !is null && (cast(ArrayWrapperString) object) && (cast(ArrayWrapperString) object).array.length() > 0; |
97 } | 111 } |
98 | 112 |
99 protected bool validate(Object object) { | 113 protected bool validate(Object object) { |
100 return checkURL(object); | 114 return checkURL(object); |
101 } | 115 } |
102 } | 116 } |