comparison dwt/dnd/URLTransfer.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 e831403a80a9
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 20007 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.*;
14
15 /**
16 * The class <code>URLTransfer</code> provides a platform specific mechanism
17 * for converting text in URL 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. The String
20 * must contain the fully specified url.
21 *
22 * <p>An example of a java <code>String[]</code> containing a URL is shown
23 * below:</p>
24 *
25 * <code><pre>
26 * String url = "http://www.eclipse.org";
27 * </code></pre>
28 */
29 public class URLTransfer : ByteArrayTransfer {
30
31 static URLTransfer _instance = new URLTransfer();
32 static final String URL = "url "; //$NON-NLS-1$
33 static final int URL_ID = registerType(URL);
34 static final String URLN = "urln"; //$NON-NLS-1$
35 static final int URLN_ID = registerType(URLN);
36
37 private URLTransfer() {}
38
39 /**
40 * Returns the singleton instance of the URLTransfer class.
41 *
42 * @return the singleton instance of the URLTransfer class
43 */
44 public static URLTransfer getInstance () {
45 return _instance;
46 }
47
48 /**
49 * This implementation of <code>javaToNative</code> converts a URL
50 * represented by a java <code>String</code> to a platform specific representation.
51 * For additional information see <code>Transfer#javaToNative</code>.
52 *
53 * @param object a java <code>String[]</code> containing a URL
54 * @param transferData an empty <code>TransferData</code> object; this
55 * object will be filled in on return with the platform specific format of the data
56 */
57 public void javaToNative (Object object, TransferData transferData){
58 if (!checkURL(object) || !isSupportedType(transferData)) {
59 DND.error(DND.ERROR_INVALID_DATA);
60 }
61 transferData.result = -1;
62 String url = (String)object;
63 int count = url.length();
64 char[] chars = new char[count];
65 url.getChars(0, count, chars, 0);
66 int cfString = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, chars, count);
67 if (cfString is 0) return;
68 try {
69 CFRange range = new CFRange();
70 range.length = chars.length;
71 int encoding = OS.CFStringGetSystemEncoding();
72 int[] size = new int[1];
73 int numChars = OS.CFStringGetBytes(cfString, range, encoding, (byte)'?', true, null, 0, size);
74 if (numChars is 0 || size[0] is 0) return;
75 byte[] buffer = new byte[size[0]];
76 numChars = OS.CFStringGetBytes(cfString, range, encoding, (byte)'?', true, buffer, size [0], size);
77 if (numChars is 0) return;
78 transferData.data = new byte[][] {buffer};
79 transferData.result = 0;
80 } finally {
81 OS.CFRelease(cfString);
82 }
83 }
84
85 /**
86 * This implementation of <code>nativeToJava</code> converts a platform specific
87 * representation of a URL to a java <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.data is null) return null;
97 if (transferData.data.length is 0) return null;
98 byte[] buffer = transferData.data[0];
99 int encoding = OS.CFStringGetSystemEncoding();
100 int cfString = OS.CFStringCreateWithBytes(OS.kCFAllocatorDefault, buffer, buffer.length, encoding, true);
101 if (cfString is 0) return null;
102 try {
103 int length = OS.CFStringGetLength(cfString);
104 if (length is 0) return null;
105 char[] chars = new char[length];
106 CFRange range = new CFRange();
107 range.length = length;
108 OS.CFStringGetCharacters(cfString, range, chars);
109 return new String(chars);
110 } finally {
111 OS.CFRelease(cfString);
112 }
113 }
114
115 protected int[] getTypeIds(){
116 return new int[] {URL_ID, URLN_ID};
117 }
118
119 protected String[] getTypeNames(){
120 return new String[] {URL, URLN};
121 }
122
123 bool checkURL(Object object) {
124 return object !is null && (object instanceof String) && ((String)object).length() > 0;
125 }
126
127 protected bool validate(Object object) {
128 return checkURL(object);
129 }
130 }