comparison dwt/dnd/RTFTransfer.d @ 107:6b8fcfda8b57

Ported dwt.dnd.RTFTransfer
author Jacob Carlborg <doob@me.com>
date Wed, 31 Dec 2008 14:48:00 +0100
parents d8635bb48c7c
children a2d82e6fd054
comparison
equal deleted inserted replaced
106:ed7dd292bfb3 107:6b8fcfda8b57
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.RTFTransfer; 14 module dwt.dnd.RTFTransfer;
12 15
13 import dwt.dwthelper.utils; 16 import dwt.dwthelper.utils;
14 17
15 import dwt.internal.cocoa.NSString; 18 import dwt.internal.cocoa.NSString;
16 import dwt.internal.cocoa.OS; 19 import dwt.internal.cocoa.OS;
20
21 import dwt.dnd.ByteArrayTransfer;
22 import dwt.dnd.DND;
23 import dwt.dnd.TransferData;
17 24
18 /** 25 /**
19 * The class <code>RTFTransfer</code> provides a platform specific mechanism 26 * The class <code>RTFTransfer</code> provides a platform specific mechanism
20 * for converting text in RTF format represented as a java <code>String</code> 27 * for converting text in RTF format represented as a java <code>String</code>
21 * to a platform specific representation of the data and vice versa. 28 * to a platform specific representation of the data and vice versa.
27 * String rtfData = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i Hello World}"; 34 * String rtfData = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i Hello World}";
28 * </code></pre> 35 * </code></pre>
29 * 36 *
30 * @see Transfer 37 * @see Transfer
31 */ 38 */
32 public class RTFTransfer extends ByteArrayTransfer { 39 public class RTFTransfer : ByteArrayTransfer {
33 40
34 static RTFTransfer _instance = new RTFTransfer(); 41 static RTFTransfer _instance;
35 static final String RTF = OS.NSRTFPboardType.getString(); 42 static const String RTF;
36 static final int RTFID = registerType(RTF); 43 static const int RTFID;
44
45 static this ()
46 {
47 _instance = new RTFTransfer();
48 RTF = OS.NSRTFPboardType.getString();
49 RTFID = registerType(RTF);
50 }
37 51
38 RTFTransfer() {} 52 this() {}
39 53
40 /** 54 /**
41 * Returns the singleton instance of the RTFTransfer class. 55 * Returns the singleton instance of the RTFTransfer class.
42 * 56 *
43 * @return the singleton instance of the RTFTransfer class 57 * @return the singleton instance of the RTFTransfer class
58 */ 72 */
59 public void javaToNative (Object object, TransferData transferData){ 73 public void javaToNative (Object object, TransferData transferData){
60 if (!checkRTF(object) || !isSupportedType(transferData)) { 74 if (!checkRTF(object) || !isSupportedType(transferData)) {
61 DND.error(DND.ERROR_INVALID_DATA); 75 DND.error(DND.ERROR_INVALID_DATA);
62 } 76 }
63 transferData.data = NSString.stringWith((String) object); 77 transferData.data = NSString.stringWith((cast(ArrayWrapperString) object).array);
64 } 78 }
65 79
66 /** 80 /**
67 * This implementation of <code>nativeToJava</code> converts a platform specific 81 * This implementation of <code>nativeToJava</code> converts a platform specific
68 * representation of RTF text to a java <code>String</code>. 82 * representation of RTF text to a java <code>String</code>.
73 * 87 *
74 * @see Transfer#javaToNative 88 * @see Transfer#javaToNative
75 */ 89 */
76 public Object nativeToJava(TransferData transferData){ 90 public Object nativeToJava(TransferData transferData){
77 if (!isSupportedType(transferData) || transferData.data is null) return null; 91 if (!isSupportedType(transferData) || transferData.data is null) return null;
78 NSString string = (NSString) transferData.data; 92 NSString string = cast(NSString) transferData.data;
79 return string.getString(); 93 return string.getString();
80 } 94 }
81 95
82 protected int[] getTypeIds() { 96 protected int[] getTypeIds() {
83 return new int[] {RTFID}; 97 return [RTFID];
84 } 98 }
85 99
86 protected String[] getTypeNames() { 100 protected String[] getTypeNames() {
87 return new String[] {RTF}; 101 return [RTF];
88 } 102 }
89 103
90 bool checkRTF(Object object) { 104 bool checkRTF(Object object) {
91 return (object !is null && object instanceof String && ((String)object).length() > 0); 105 return (object !is null && cast(ArrayWrapperString) object && (cast(ArrayWrapperString) object).array.length() > 0);
92 } 106 }
93 107
94 protected bool validate(Object object) { 108 protected bool validate(Object object) {
95 return checkRTF(object); 109 return checkRTF(object);
96 } 110 }