annotate dwt/dnd/URLTransfer.d @ 122:2e671fa40eec

Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
author Jacob Carlborg <doob@me.com>
date Wed, 31 Dec 2008 21:01:13 +0100
parents d0d0b721dbcc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
122
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
1 /*******************************************************************************
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
2 * Copyright (c) 20007 IBM Corporation and others.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
3 * All rights reserved. This program and the accompanying materials
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
4 * are made available under the terms of the Eclipse Public License v1.0
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
5 * which accompanies this distribution, and is available at
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
6 * http://www.eclipse.org/legal/epl-v10.html
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
7 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
8 * Contributors:
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
9 * IBM Corporation - initial API and implementation
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
10 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
11 * Port to the D programming language:
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
12 * Jacob Carlborg <doob@me.com>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
13 *******************************************************************************/
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
14 module dwt.dnd.URLTransfer;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
15
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
16 import dwt.dwthelper.utils;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
17
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
18 import dwt.internal.cocoa.NSString;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
19 import dwt.internal.cocoa.NSURL;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
20 import dwt.internal.cocoa.OS;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
21
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
22 import dwt.dnd.ByteArrayTransfer;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
23 import dwt.dnd.DND;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
24 import dwt.dnd.TransferData;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
25
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
26 /**
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
27 * The class <code>URLTransfer</code> provides a platform specific mechanism
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
28 * for converting text in URL format represented as a java <code>String</code>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
29 * to a platform specific representation of the data and vice versa. The string
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
30 * must contain a fully specified url.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
31 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
32 * <p>An example of a java <code>String</code> containing a URL is shown below:</p>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
33 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
34 * <code><pre>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
35 * String url = "http://www.eclipse.org";
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
36 * </code></pre>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
37 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
38 * @see Transfer
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
39 */
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
40 public class URLTransfer : ByteArrayTransfer {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
41
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
42 static URLTransfer _instance;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
43 static const String URL;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
44 static const int URL_ID;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
45
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
46 static this ()
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
47 {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
48 _instance = new URLTransfer();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
49 URL = OS.NSURLPboardType.getString();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
50 URL_ID = registerType(URL);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
51 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
52
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
53 private this() {}
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
54
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
55 /**
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
56 * Returns the singleton instance of the URLTransfer class.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
57 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
58 * @return the singleton instance of the URLTransfer class
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
59 */
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
60 public static URLTransfer getInstance () {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
61 return _instance;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
62 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
63
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
64 /**
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
65 * This implementation of <code>javaToNative</code> converts a URL
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
66 * represented by a java <code>String</code> to a platform specific representation.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
67 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
68 * @param object a java <code>String</code> containing a URL
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
69 * @param transferData an empty <code>TransferData</code> object that will
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
70 * be filled in on return with the platform specific format of the data
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
71 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
72 * @see Transfer#nativeToJava
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
73 */
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
74 public void javaToNative (Object object, TransferData transferData){
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
75 if (!checkURL(object) || !isSupportedType(transferData)) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
76 DND.error(DND.ERROR_INVALID_DATA);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
77 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
78 String url = (cast(ArrayWrapperString)object).array;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
79 NSString nsString = NSString.stringWith(url);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
80 NSString escapedString = nsString.stringByAddingPercentEscapesUsingEncoding(OS.NSUTF8StringEncoding);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
81 transferData.data = NSURL.URLWithString(escapedString);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
82 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
83
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
84 /**
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
85 * This implementation of <code>nativeToJava</code> converts a platform
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
86 * specific representation of a URL to a java <code>String</code>.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
87 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
88 * @param transferData the platform specific representation of the data to be converted
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
89 * @return a java <code>String</code> containing a URL if the conversion was successful;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
90 * otherwise null
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
91 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
92 * @see Transfer#javaToNative
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
93 */
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
94 public Object nativeToJava(TransferData transferData){
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
95 if (!isSupportedType(transferData) || transferData.data is null) return null;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
96 NSURL nsUrl = cast(NSURL) transferData.data;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
97 NSString nsString = nsUrl.absoluteString();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
98 return new ArrayWrapperString(nsString.getString());
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
99 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
100
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
101 protected int[] getTypeIds(){
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
102 return [URL_ID];
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
103 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
104
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
105 protected String[] getTypeNames(){
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
106 return [URL];
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
107 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
108
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
109 bool checkURL(Object object) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
110 return object !is null && (cast(ArrayWrapperString) object) && (cast(ArrayWrapperString) object).array.length() > 0;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
111 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
112
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
113 protected bool validate(Object object) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
114 return checkURL(object);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
115 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents: 114
diff changeset
116 }