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

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents e76aa0b07480
children 6a1ed74f02e8
comparison
equal deleted inserted replaced
44:ca5e494f2bbf 45:d8635bb48c7c
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
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 *******************************************************************************/ 10 *******************************************************************************/
11 module dwt.dnd; 11 module dwt.dnd.HTMLTransfer;
12
13 import dwt.dwthelper.utils;
12 14
13 import dwt.internal.carbon.OS; 15 import dwt.internal.cocoa.NSString;
16 import dwt.internal.cocoa.OS;
14 17
15 /** 18 /**
16 * The class <code>HTMLTransfer</code> provides a platform specific mechanism 19 * The class <code>HTMLTransfer</code> provides a platform specific mechanism
17 * for converting text in HTML format represented as a java <code>String</code> 20 * 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 21 * to a platform specific representation of the data and vice versa.
19 * <code>Transfer</code> for additional information.
20 * 22 *
21 * <p>An example of a java <code>String</code> containing HTML text is shown 23 * <p>An example of a java <code>String</code> containing HTML text is shown
22 * below:</p> 24 * below:</p>
23 * 25 *
24 * <code><pre> 26 * <code><pre>
25 * String htmlData = "<p>This is a paragraph of text.</p>"; 27 * String htmlData = "<p>This is a paragraph of text.</p>";
26 * </code></pre> 28 * </code></pre>
29 *
30 * @see Transfer
27 */ 31 */
28 public class HTMLTransfer : ByteArrayTransfer { 32 public class HTMLTransfer extends ByteArrayTransfer {
29 33
30 static HTMLTransfer _instance = new HTMLTransfer(); 34 static HTMLTransfer _instance = new HTMLTransfer();
31 static final String HTML = "HTML"; //$NON-NLS-1$ 35 static final String HTML = OS.NSHTMLPboardType.getString();
32 static final int HTMLID = registerType(HTML); 36 static final int HTMLID = registerType(HTML);
33 37
34 this() {} 38 HTMLTransfer() {}
35 39
36 /** 40 /**
37 * Returns the singleton instance of the HTMLTransfer class. 41 * Returns the singleton instance of the HTMLTransfer class.
38 * 42 *
39 * @return the singleton instance of the HTMLTransfer class 43 * @return the singleton instance of the HTMLTransfer class
43 } 47 }
44 48
45 /** 49 /**
46 * This implementation of <code>javaToNative</code> converts HTML-formatted text 50 * This implementation of <code>javaToNative</code> converts HTML-formatted text
47 * represented by a java <code>String</code> to a platform specific representation. 51 * represented by a java <code>String</code> to a platform specific representation.
48 * For additional information see <code>Transfer#javaToNative</code>.
49 * 52 *
50 * @param object a java <code>String</code> containing HTML text 53 * @param object a java <code>String</code> containing HTML text
51 * @param transferData an empty <code>TransferData</code> object; this 54 * @param transferData an empty <code>TransferData</code> object that will
52 * object will be filled in on return with the platform specific format of the data 55 * be filled in on return with the platform specific format of the data
56 *
57 * @see Transfer#nativeToJava
53 */ 58 */
54 public void javaToNative (Object object, TransferData transferData){ 59 public void javaToNative (Object object, TransferData transferData){
55 if (!checkHTML(object) || !isSupportedType(transferData)) { 60 if (!checkHTML(object) || !isSupportedType(transferData)) {
56 DND.error(DND.ERROR_INVALID_DATA); 61 DND.error(DND.ERROR_INVALID_DATA);
57 } 62 }
58 String String = cast(String)object; 63 transferData.data = NSString.stringWith((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 } 64 }
68 65
69 /** 66 /**
70 * This implementation of <code>nativeToJava</code> converts a platform specific 67 * This implementation of <code>nativeToJava</code> converts a platform specific
71 * representation of HTML text to a java <code>String</code>. 68 * representation of HTML text to a java <code>String</code>.
72 * For additional information see <code>Transfer#nativeToJava</code>.
73 * 69 *
74 * @param transferData the platform specific representation of the data to be 70 * @param transferData the platform specific representation of the data to be converted
75 * been converted 71 * @return a java <code>String</code> containing HTML text if the conversion was successful;
76 * @return a java <code>String</code> containing HTML text if the 72 * otherwise null
77 * conversion was successful; otherwise null 73 *
74 * @see Transfer#javaToNative
78 */ 75 */
79 public Object nativeToJava(TransferData transferData){ 76 public Object nativeToJava(TransferData transferData){
80 if (!isSupportedType(transferData) || transferData.data is null) return null; 77 if (!isSupportedType(transferData) || transferData.data is null) return null;
81 if (transferData.data.length is 0 || transferData.data[0].length is 0) return null; 78 NSString string = (NSString) transferData.data;
82 byte[] buffer = transferData.data[0]; 79 return string.getString();
83 char[] chars = new char[(buffer.length + 1) / 2];
84 OS.memmove(chars, buffer, buffer.length);
85 return new String(chars);
86 } 80 }
87 81
88 protected int[] getTypeIds() { 82 protected int[] getTypeIds() {
89 return new int[] {HTMLID}; 83 return new int[] {HTMLID};
90 } 84 }
92 protected String[] getTypeNames() { 86 protected String[] getTypeNames() {
93 return new String[] {HTML}; 87 return new String[] {HTML};
94 } 88 }
95 89
96 bool checkHTML(Object object) { 90 bool checkHTML(Object object) {
97 return (object !is null && null !is cast(String)object && (cast(String)object).length() > 0); 91 return (object !is null && object instanceof String && ((String)object).length() > 0);
98 } 92 }
99 93
100 protected bool validate(Object object) { 94 protected bool validate(Object object) {
101 return checkHTML(object); 95 return checkHTML(object);
102 } 96 }