comparison dwt/dnd/URLTransfer.d @ 240:ce446666f5a2

Update to SWT 3.4M7
author Frank Benoit <benoit@tionex.de>
date Mon, 12 May 2008 19:13:01 +0200
parents
children 5a30aa9820f3
comparison
equal deleted inserted replaced
239:06a1f6829310 240:ce446666f5a2
1 /*******************************************************************************
2 * Copyright (c) 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.URLTransfer;
12
13 import dwt.internal.gtk.OS;
14
15 import dwt.dnd.ByteArrayTransfer;
16 import dwt.dnd.TransferData;
17 import dwt.dnd.DND;
18
19 import dwt.dwthelper.utils;
20
21 /**
22 * The class <code>URLTransfer</code> provides a platform specific mechanism
23 * for converting text in URL format represented as a java <code>String</code>
24 * to a platform specific representation of the data and vice versa. See
25 * <code>Transfer</code> for additional information. The string
26 * must be a fully specified url.
27 *
28 * <p>An example of a java <code>String[]</code> containing a URL is shown
29 * below:</p>
30 *
31 * <code><pre>
32 * String urlData = "http://www.eclipse.org";
33 * </code></pre>
34 */
35 public class URLTransfer : ByteArrayTransfer {
36
37 static URLTransfer _instance;
38 private static const String TEXT_UNICODE = "text/unicode"; //$NON-NLS-1$
39 private static const String TEXT_XMOZURL = "text/x-moz-url"; //$NON-NLS-1$
40 private static int TEXT_UNICODE_ID;
41 private static int TEXT_XMOZURL_ID;
42
43 static this(){
44 TEXT_UNICODE_ID = registerType(TEXT_UNICODE);
45 TEXT_XMOZURL_ID = registerType(TEXT_XMOZURL);
46 _instance = new URLTransfer();
47 }
48
49 private this() {}
50
51 /**
52 * Returns the singleton instance of the URLTransfer class.
53 *
54 * @return the singleton instance of the URLTransfer class
55 */
56 public static URLTransfer getInstance () {
57 return _instance;
58 }
59
60 /**
61 * This implementation of <code>javaToNative</code> converts a URL
62 * represented by a java <code>String</code> to a platform specific representation.
63 * For additional information see <code>Transfer#javaToNative</code>.
64 *
65 * @param object a java <code>String</code> containing a URL
66 * @param transferData an empty <code>TransferData</code> object; this
67 * object will be filled in on return with the platform specific format of the data
68 */
69 public void javaToNative (Object object, TransferData transferData){
70 transferData.result = 0;
71 if (!checkURL(object) || !isSupportedType(transferData)) {
72 DND.error(DND.ERROR_INVALID_DATA);
73 }
74 String string = (cast(ArrayWrapperString)object).array;
75 char* pValue = cast(char*)OS.g_malloc(string.length+1);
76 if (pValue is null) return;
77 pValue[ 0 .. string.length ] = string[];
78 pValue[ string.length ] = '\0';
79 transferData.length = string.length;
80 transferData.format = 8;
81 transferData.pValue = pValue;
82 transferData.result = 1;
83 }
84
85 /**
86 * This implementation of <code>nativeToJava</code> converts a platform specific
87 * representation of a URL <code>String</code>.
88 * For additional information see <code>Transfer#nativeToJava</code>.
89 *
90 * @param transferData the platform specific representation of the data to be
91 * converted
92 * @return a java <code>String</code> containing a URL if the
93 * conversion was successful; otherwise null
94 */
95 public Object nativeToJava(TransferData transferData){
96 if (!isSupportedType(transferData) || transferData.pValue is null) return null;
97 int size = (transferData.format * transferData.length / 8);
98 if (size <= 0) return null;
99 String string = tango.stdc.stringz.fromStringz(cast(char*)transferData.pValue).dup;
100 int end = string.indexOf('\0');
101 return new ArrayWrapperString((end is -1) ? string : string.substring(0, end));
102 }
103
104 protected int[] getTypeIds(){
105 return [TEXT_XMOZURL_ID, TEXT_UNICODE_ID];
106 }
107
108 protected String[] getTypeNames(){
109 return [TEXT_XMOZURL, TEXT_UNICODE];
110 }
111
112 bool checkURL(Object object) {
113 return object !is null && (null !is cast(ArrayWrapperString)object) && (cast(ArrayWrapperString)object).array.length > 0;
114 }
115
116 protected bool validate(Object object) {
117 return checkURL(object);
118 }
119 }