Mercurial > projects > dwt-mac
comparison dwt/dnd/HTMLTransfer.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.OS; | |
14 | |
15 /** | |
16 * The class <code>HTMLTransfer</code> provides a platform specific mechanism | |
17 * for converting text in HTML format represented as a java <code>String</code> | |
18 * to a platform specific representation of the data and vice versa. See | |
19 * <code>Transfer</code> for additional information. | |
20 * | |
21 * <p>An example of a java <code>String</code> containing HTML text is shown | |
22 * below:</p> | |
23 * | |
24 * <code><pre> | |
25 * String htmlData = "<p>This is a paragraph of text.</p>"; | |
26 * </code></pre> | |
27 */ | |
28 public class HTMLTransfer : ByteArrayTransfer { | |
29 | |
30 static HTMLTransfer _instance = new HTMLTransfer(); | |
31 static final String HTML = "HTML"; //$NON-NLS-1$ | |
32 static final int HTMLID = registerType(HTML); | |
33 | |
34 HTMLTransfer() {} | |
35 | |
36 /** | |
37 * Returns the singleton instance of the HTMLTransfer class. | |
38 * | |
39 * @return the singleton instance of the HTMLTransfer class | |
40 */ | |
41 public static HTMLTransfer getInstance () { | |
42 return _instance; | |
43 } | |
44 | |
45 /** | |
46 * This implementation of <code>javaToNative</code> converts HTML-formatted text | |
47 * represented by a java <code>String</code> to a platform specific representation. | |
48 * For additional information see <code>Transfer#javaToNative</code>. | |
49 * | |
50 * @param object a java <code>String</code> containing HTML text | |
51 * @param transferData an empty <code>TransferData</code> object; this | |
52 * object will be filled in on return with the platform specific format of the data | |
53 */ | |
54 public void javaToNative (Object object, TransferData transferData){ | |
55 if (!checkHTML(object) || !isSupportedType(transferData)) { | |
56 DND.error(DND.ERROR_INVALID_DATA); | |
57 } | |
58 String String = (String)object; | |
59 int count = String.length(); | |
60 char[] chars = new char[count]; | |
61 String.getChars(0, count, chars, 0); | |
62 byte[] buffer = new byte[chars.length * 2]; | |
63 OS.memmove(buffer, chars, buffer.length); | |
64 transferData.data = new byte[1][]; | |
65 transferData.data[0] = buffer; | |
66 transferData.result = OS.noErr; | |
67 } | |
68 | |
69 /** | |
70 * This implementation of <code>nativeToJava</code> converts a platform specific | |
71 * representation of HTML text to a java <code>String</code>. | |
72 * For additional information see <code>Transfer#nativeToJava</code>. | |
73 * | |
74 * @param transferData the platform specific representation of the data to be | |
75 * been converted | |
76 * @return a java <code>String</code> containing HTML text if the | |
77 * conversion was successful; otherwise null | |
78 */ | |
79 public Object nativeToJava(TransferData transferData){ | |
80 if (!isSupportedType(transferData) || transferData.data is null) return null; | |
81 if (transferData.data.length is 0 || transferData.data[0].length is 0) return null; | |
82 byte[] buffer = transferData.data[0]; | |
83 char[] chars = new char[(buffer.length + 1) / 2]; | |
84 OS.memmove(chars, buffer, buffer.length); | |
85 return new String(chars); | |
86 } | |
87 | |
88 protected int[] getTypeIds() { | |
89 return new int[] {HTMLID}; | |
90 } | |
91 | |
92 protected String[] getTypeNames() { | |
93 return new String[] {HTML}; | |
94 } | |
95 | |
96 bool checkHTML(Object object) { | |
97 return (object !is null && object instanceof String && ((String)object).length() > 0); | |
98 } | |
99 | |
100 protected bool validate(Object object) { | |
101 return checkHTML(object); | |
102 } | |
103 } |