annotate dwt/dnd/ByteArrayTransfer.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
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:
diff changeset
1 /*******************************************************************************
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
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:
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:
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:
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:
diff changeset
7 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
8 * Contributors:
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
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:
diff changeset
10 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
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:
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:
diff changeset
13 *******************************************************************************/
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
14 module dwt.dnd.ByteArrayTransfer;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
15
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
16 import dwt.dwthelper.utils;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
17
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
18 import dwt.internal.cocoa.NSData;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
19
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
20 import dwt.dnd.DND;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
21 import dwt.dnd.Transfer;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
22 import dwt.dnd.TransferData;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
23
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
24
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
25 /**
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
26 * The class <code>ByteArrayTransfer</code> provides a platform specific
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
27 * mechanism for converting a java <code>byte[]</code> to a platform
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
28 * specific representation of the byte array and vice versa.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
29 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
30 * <p><code>ByteArrayTransfer</code> is never used directly but is sub-classed
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
31 * by transfer agents that convert between data in a java format such as a
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
32 * <code>String</code> and a platform specific byte array.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
33 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
34 * <p>If the data you are converting <b>does not</b> map to a
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
35 * <code>byte[]</code>, you should sub-class <code>Transfer</code> directly
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
36 * and do your own mapping to a platform data type.</p>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
37 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
38 * <p>The following snippet shows a subclass of ByteArrayTransfer that transfers
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
39 * data defined by the class <code>MyType</code>.</p>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
40 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
41 * <pre><code>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
42 * public class MyType {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
43 * public String fileName;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
44 * public long fileLength;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
45 * public long lastModified;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
46 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
47 * </code></pre>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
48 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
49 * <pre><code>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
50 * public class MyTypeTransfer extends ByteArrayTransfer {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
51 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
52 * private static final String MYTYPENAME = "my_type_name";
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
53 * private static final int MYTYPEID = registerType(MYTYPENAME);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
54 * private static MyTypeTransfer _instance = new MyTypeTransfer();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
55 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
56 * private MyTypeTransfer() {}
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
57 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
58 * public static MyTypeTransfer getInstance () {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
59 * return _instance;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
60 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
61 * public void javaToNative (Object object, TransferData transferData) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
62 * if (object is null || !(object instanceof MyType[])) return;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
63 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
64 * if (isSupportedType(transferData)) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
65 * MyType[] myTypes = (MyType[]) object;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
66 * try {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
67 * // write data to a byte array and then ask super to convert to pMedium
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
68 * ByteArrayOutputStream out = new ByteArrayOutputStream();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
69 * DataOutputStream writeOut = new DataOutputStream(out);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
70 * for (int i = 0, length = myTypes.length; i &lt; length; i++){
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
71 * byte[] buffer = myTypes[i].fileName.getBytes();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
72 * writeOut.writeInt(buffer.length);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
73 * writeOut.write(buffer);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
74 * writeOut.writeLong(myTypes[i].fileLength);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
75 * writeOut.writeLong(myTypes[i].lastModified);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
76 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
77 * byte[] buffer = out.toByteArray();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
78 * writeOut.close();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
79 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
80 * super.javaToNative(buffer, transferData);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
81 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
82 * } catch (IOException e) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
83 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
84 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
85 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
86 * public Object nativeToJava(TransferData transferData){
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
87 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
88 * if (isSupportedType(transferData)) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
89 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
90 * byte[] buffer = (byte[])super.nativeToJava(transferData);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
91 * if (buffer is null) return null;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
92 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
93 * MyType[] myData = new MyType[0];
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
94 * try {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
95 * ByteArrayInputStream in = new ByteArrayInputStream(buffer);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
96 * DataInputStream readIn = new DataInputStream(in);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
97 * while(readIn.available() > 20) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
98 * MyType datum = new MyType();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
99 * int size = readIn.readInt();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
100 * byte[] name = new byte[size];
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
101 * readIn.read(name);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
102 * datum.fileName = new String(name);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
103 * datum.fileLength = readIn.readLong();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
104 * datum.lastModified = readIn.readLong();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
105 * MyType[] newMyData = new MyType[myData.length + 1];
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
106 * System.arraycopy(myData, 0, newMyData, 0, myData.length);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
107 * newMyData[myData.length] = datum;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
108 * myData = newMyData;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
109 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
110 * readIn.close();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
111 * } catch (IOException ex) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
112 * return null;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
113 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
114 * return myData;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
115 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
116 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
117 * return null;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
118 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
119 * protected String[] getTypeNames(){
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
120 * return new String[]{MYTYPENAME};
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
121 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
122 * protected int[] getTypeIds(){
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
123 * return new int[] {MYTYPEID};
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
124 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
125 * }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
126 * </code></pre>
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
127 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
128 * @see Transfer
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
129 */
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
130 public abstract class ByteArrayTransfer : Transfer {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
131
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
132 public TransferData[] getSupportedTypes() {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
133 int[] types = getTypeIds();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
134 TransferData[] data = new TransferData[types.length];
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
135 for (int i = 0; i < types.length; i++) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
136 data[i] = new TransferData();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
137 data[i].type = types[i];
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
138 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
139 return data;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
140 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
141
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
142 public bool isSupportedType(TransferData transferData){
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
143 if (transferData is null) return false;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
144 int[] types = getTypeIds();
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
145 for (int i = 0; i < types.length; i++) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
146 if (transferData.type is types[i]) return true;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
147 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
148 return false;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
149 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
150
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
151 /**
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
152 * This implementation of <code>javaToNative</code> converts a java
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
153 * <code>byte[]</code> to a platform specific representation.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
154 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
155 * @param object a java <code>byte[]</code> containing the data to be converted
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
156 * @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:
diff changeset
157 * 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:
diff changeset
158 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
159 * @see Transfer#nativeToJava
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
160 */
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
161 protected void javaToNative (Object object, TransferData transferData) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
162 if (!checkByteArray(object) && !isSupportedType(transferData)) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
163 DND.error(DND.ERROR_INVALID_DATA);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
164 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
165 byte[] orig = (cast(ArrayWrapperByte)object).array;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
166 NSData data = NSData.dataWithBytes(orig.ptr, orig.length);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
167 transferData.data = data;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
168 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
169
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
170 /**
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
171 * This implementation of <code>nativeToJava</code> converts a platform specific
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
172 * representation of a byte array to a java <code>byte[]</code>.
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
173 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
174 * @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:
diff changeset
175 * @return a java <code>byte[]</code> containing the converted data if the conversion was
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
176 * successful; otherwise null
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
177 *
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
178 * @see Transfer#javaToNative
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
179 */
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
180 protected Object nativeToJava(TransferData transferData) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
181 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:
diff changeset
182 if (transferData.data is null) return null;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
183 NSData data = cast(NSData) transferData.data;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
184 if (data.length() is 0) return null;
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
185 byte[] bytes = new byte[data.length()];
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
186 data.getBytes(bytes.ptr);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
187 return new ArrayWrapperByte(bytes);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
188 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
189 bool checkByteArray(Object object) {
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
190 return (object !is null && cast(ArrayWrapperByte)object && (cast(ArrayWrapperByte)object).array.length > 0);
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
191 }
2e671fa40eec Ported dwt.dnd, dwt.opengl, dwt.printing and dwt.program
Jacob Carlborg <doob@me.com>
parents:
diff changeset
192 }