comparison dwt/dnd/TextTransfer.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.dnd;
12
13 import dwt.internal.carbon.CFRange;
14 import dwt.internal.carbon.OS;
15
16 /**
17 * The class <code>TextTransfer</code> provides a platform specific mechanism
18 * for converting plain text represented as a java <code>String</code>
19 * to a platform specific representation of the data and vice versa.
20 *
21 * <p>An example of a java <code>String</code> containing plain text is shown
22 * below:</p>
23 *
24 * <code><pre>
25 * String textData = "Hello World";
26 * </code></pre>
27 *
28 * @see Transfer
29 */
30 public class TextTransfer : ByteArrayTransfer {
31
32 static TextTransfer _instance = new TextTransfer();
33 static final String TEXT = "TEXT"; //$NON-NLS-1$
34 static final String UTEXT = "utxt"; //$NON-NLS-1$
35 static final int TEXTID = OS.kScrapFlavorTypeText;
36 static final int UTEXTID = OS.kScrapFlavorTypeUnicode;
37
38 TextTransfer() {}
39
40 /**
41 * Returns the singleton instance of the TextTransfer class.
42 *
43 * @return the singleton instance of the TextTransfer class
44 */
45 public static TextTransfer getInstance () {
46 return _instance;
47 }
48
49 /**
50 * This implementation of <code>javaToNative</code> converts plain text
51 * represented by a java <code>String</code> to a platform specific representation.
52 *
53 * @param object a java <code>String</code> containing text
54 * @param transferData an empty <code>TransferData</code> object; this object
55 * will be filled in on return with the platform specific format of the data
56 *
57 * @see Transfer#javaToNative
58 */
59 public void javaToNative (Object object, TransferData transferData) {
60 if (!checkText(object) || !isSupportedType(transferData)) {
61 DND.error(DND.ERROR_INVALID_DATA);
62 }
63 String String = (String)object;
64 char[] chars = new char[String.length()];
65 String.getChars (0, chars.length, chars, 0);
66 transferData.result = -1;
67 switch (transferData.type) {
68 case TEXTID: {
69 int cfString = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, chars, chars.length);
70 if (cfString is 0) return;
71 byte[] buffer = null;
72 try {
73 CFRange range = new CFRange();
74 range.length = chars.length;
75 int encoding = OS.CFStringGetSystemEncoding();
76 int[] size = new int[1];
77 int numChars = OS.CFStringGetBytes(cfString, range, encoding, (byte)'?', true, null, 0, size);
78 if (numChars is 0) return;
79 buffer = new byte[size[0]];
80 numChars = OS.CFStringGetBytes(cfString, range, encoding, (byte)'?', true, buffer, size [0], size);
81 if (numChars is 0) return;
82 } finally {
83 OS.CFRelease(cfString);
84 }
85 transferData.data = new byte[1][];
86 transferData.data[0] = buffer;
87 transferData.result = OS.noErr;
88 break;
89 }
90 case UTEXTID: {
91 byte[] buffer = new byte[chars.length * 2];
92 OS.memmove(buffer, chars, buffer.length);
93 transferData.data = new byte[1][];
94 transferData.data[0] = buffer;
95 transferData.result = OS.noErr;
96 break;
97 }
98 }
99 }
100
101 /**
102 * This implementation of <code>nativeToJava</code> converts a platform specific
103 * representation of plain text to a java <code>String</code>.
104 *
105 * @param transferData the platform specific representation of the data to be converted
106 * @return a java <code>String</code> containing text if the conversion was successful; otherwise null
107 *
108 * @see Transfer#nativeToJava
109 */
110 public Object nativeToJava(TransferData transferData){
111 if (!isSupportedType(transferData) || transferData.data is null) return null;
112 if (transferData.data.length is 0 || transferData.data[0].length is 0) return null;
113 byte[] buffer = transferData.data[0];
114 switch (transferData.type) {
115 case TEXTID: {
116 int encoding = OS.CFStringGetSystemEncoding();
117 int cfString = OS.CFStringCreateWithBytes(OS.kCFAllocatorDefault, buffer, buffer.length, encoding, true);
118 if (cfString is 0) return null;
119 try {
120 int length = OS.CFStringGetLength(cfString);
121 if (length is 0) return null;
122 char[] chars = new char[length];
123 CFRange range = new CFRange();
124 range.length = length;
125 OS.CFStringGetCharacters(cfString, range, chars);
126 return new String(chars);
127 } finally {
128 OS.CFRelease(cfString);
129 }
130 }
131 case UTEXTID: {
132 char[] chars = new char[(buffer.length + 1) / 2];
133 OS.memmove(chars, buffer, buffer.length);
134 return new String(chars);
135 }
136 }
137 return null;
138 }
139
140 protected int[] getTypeIds() {
141 return new int[] {UTEXTID, TEXTID};
142 }
143
144 protected String[] getTypeNames() {
145 return new String[] {UTEXT, TEXT};
146 }
147
148 bool checkText(Object object) {
149 return (object !is null && object instanceof String && ((String)object).length() > 0);
150 }
151 protected bool validate(Object object) {
152 return checkText(object);
153 }
154 }