25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.dnd.HTMLTransfer;
|
|
14
|
|
15 import org.eclipse.swt.internal.gtk.OS;
|
|
16 import org.eclipse.swt.dnd.ByteArrayTransfer;
|
|
17 import org.eclipse.swt.dnd.TransferData;
|
|
18 import org.eclipse.swt.dnd.DND;
|
|
19 import java.lang.all;
|
|
20
|
48
|
21 version(Tango){
|
25
|
22 static import tango.text.Util;
|
48
|
23 } else { // Phobos
|
|
24 }
|
25
|
25
|
|
26 /**
|
|
27 * The class <code>HTMLTransfer</code> provides a platform specific mechanism
|
|
28 * for converting text in HTML format represented as a java <code>String</code>
|
|
29 * to a platform specific representation of the data and vice versa.
|
|
30 *
|
|
31 * <p>An example of a java <code>String</code> containing HTML text is shown
|
|
32 * below:</p>
|
|
33 *
|
|
34 * <code><pre>
|
|
35 * String htmlData = "<p>This is a paragraph of text.</p>";
|
|
36 * </code></pre>
|
|
37 *
|
|
38 * @see Transfer
|
|
39 */
|
|
40 public class HTMLTransfer : ByteArrayTransfer {
|
|
41
|
|
42 private static HTMLTransfer _instance;
|
|
43 private static const String TEXT_HTML = "text/html"; //$NON-NLS-1$
|
|
44 private static const int TEXT_HTML_ID;
|
|
45 private static const String TEXT_HTML2 = "TEXT/HTML"; //$NON-NLS-1$
|
|
46 private static const int TEXT_HTML2_ID;
|
|
47
|
|
48 static this(){
|
|
49 _instance = new HTMLTransfer();
|
|
50 TEXT_HTML_ID = registerType(TEXT_HTML);
|
|
51 TEXT_HTML2_ID = registerType(TEXT_HTML2);
|
|
52 }
|
|
53
|
|
54 private this() {}
|
|
55
|
|
56 /**
|
|
57 * Returns the singleton instance of the HTMLTransfer class.
|
|
58 *
|
|
59 * @return the singleton instance of the HTMLTransfer class
|
|
60 */
|
|
61 public static HTMLTransfer getInstance () {
|
|
62 return _instance;
|
|
63 }
|
|
64
|
|
65 /**
|
|
66 * This implementation of <code>javaToNative</code> converts HTML-formatted text
|
|
67 * represented by a java <code>String</code> to a platform specific representation.
|
|
68 *
|
|
69 * @param object a java <code>String</code> containing HTML text
|
|
70 * @param transferData an empty <code>TransferData</code> object that will
|
|
71 * be filled in on return with the platform specific format of the data
|
|
72 *
|
|
73 * @see Transfer#nativeToJava
|
|
74 */
|
|
75 public override void javaToNative (Object object, TransferData transferData){
|
|
76 transferData.result = 0;
|
|
77 if (!checkHTML(object) || !isSupportedType(transferData)) {
|
|
78 DND.error(DND.ERROR_INVALID_DATA);
|
|
79 }
|
|
80 String string = (cast(ArrayWrapperString)object).array;
|
|
81 char* pValue = cast(char*)OS.g_malloc(string.length);
|
|
82 if (pValue is null) return;
|
|
83 pValue[0 .. string.length ] = string;
|
|
84 transferData.length = string.length;
|
|
85 transferData.format = 8;
|
|
86 transferData.pValue = pValue;
|
|
87 transferData.result = 1;
|
|
88 }
|
|
89
|
|
90 /**
|
|
91 * This implementation of <code>nativeToJava</code> converts a platform specific
|
|
92 * representation of HTML text to a java <code>String</code>.
|
|
93 *
|
|
94 * @param transferData the platform specific representation of the data to be converted
|
|
95 * @return a java <code>String</code> containing HTML text if the conversion was successful;
|
|
96 * otherwise null
|
|
97 *
|
|
98 * @see Transfer#javaToNative
|
|
99 */
|
|
100 public override Object nativeToJava(TransferData transferData){
|
|
101 if ( !isSupportedType(transferData) || transferData.pValue is null ) return null;
|
|
102 /* Ensure byteCount is a multiple of 2 bytes */
|
|
103 int size = (transferData.format * transferData.length / 8) / 2 * 2;
|
|
104 if (size <= 0) return null;
|
|
105 String chars = transferData.pValue[ 0 .. size ].dup;
|
|
106 return new ArrayWrapperString( chars[ 0 .. tango.text.Util.locate( chars, '\0' ) ] );
|
|
107 }
|
|
108 protected override int[] getTypeIds() {
|
|
109 return [TEXT_HTML_ID, TEXT_HTML2_ID];
|
|
110 }
|
|
111
|
|
112 protected override String[] getTypeNames() {
|
|
113 return [TEXT_HTML, TEXT_HTML2];
|
|
114 }
|
|
115
|
|
116 bool checkHTML(Object object) {
|
|
117 if( object is null ) return false;
|
|
118 auto arr = cast(ArrayWrapperString)object;
|
|
119 if( arr is null ) return false;
|
|
120 if( arr.array.length is 0 ) return false;
|
|
121 return true;
|
|
122 }
|
|
123
|
|
124 protected override bool validate(Object object) {
|
|
125 return checkHTML(object);
|
|
126 }
|
|
127 }
|