comparison dwt/dnd/ByteArrayTransfer.d @ 135:242e33c0e383

Added dnd source, ByteArrayTransfer,Clipboard completed
author Frank Benoit <benoit@tionex.de>
date Wed, 13 Feb 2008 04:51:22 +0100
parents
children fd9c62a2998e
comparison
equal deleted inserted replaced
134:fa7d7d66b9ed 135:242e33c0e383
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 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 dwt.dnd.ByteArrayTransfer;
14
15 import dwt.internal.ole.win32.COM;
16 import dwt.internal.ole.win32.OBJIDL;
17 import dwt.internal.win32.OS;
18
19 import dwt.dnd.DND;
20 import dwt.dnd.Transfer;
21 import dwt.dnd.TransferData;
22
23 import dwt.dwthelper.utils;
24
25 /**
26 * The class <code>ByteArrayTransfer</code> provides a platform specific
27 * mechanism for converting a java <code>byte[]</code> to a platform
28 * specific representation of the byte array and vice versa. See
29 * <code>Transfer</code> for additional information.
30 *
31 * <p><code>ByteArrayTransfer</code> is never used directly but is sub-classed
32 * by transfer agents that convert between data in a java format such as a
33 * <code>String</code> and a platform specific byte array.
34 *
35 * <p>If the data you are converting <b>does not</b> map to a
36 * <code>byte[]</code>, you should sub-class <code>Transfer</code> directly
37 * and do your own mapping to a platform data type.</p>
38 *
39 * <p>The following snippet shows a subclass of ByteArrayTransfer that transfers
40 * data defined by the class <code>MyType</code>.</p>
41 *
42 * <pre><code>
43 * public class MyType {
44 * public String fileName;
45 * public long fileLength;
46 * public long lastModified;
47 * }
48 * </code></pre>
49 *
50 * <pre><code>
51 * public class MyTypeTransfer extends ByteArrayTransfer {
52 *
53 * private static final String MYTYPENAME = "my_type_name";
54 * private static final int MYTYPEID = registerType(MYTYPENAME);
55 * private static MyTypeTransfer _instance = new MyTypeTransfer();
56 *
57 * private MyTypeTransfer() {}
58 *
59 * public static MyTypeTransfer getInstance () {
60 * return _instance;
61 * }
62 * public void javaToNative (Object object, TransferData transferData) {
63 * if (object is null || !(object instanceof MyType[])) return;
64 *
65 * if (isSupportedType(transferData)) {
66 * MyType[] myTypes = (MyType[]) object;
67 * try {
68 * // write data to a byte array and then ask super to convert to pMedium
69 * ByteArrayOutputStream out = new ByteArrayOutputStream();
70 * DataOutputStream writeOut = new DataOutputStream(out);
71 * for (int i = 0, length = myTypes.length; i &lt; length; i++){
72 * byte[] buffer = myTypes[i].fileName.getBytes();
73 * writeOut.writeInt(buffer.length);
74 * writeOut.write(buffer);
75 * writeOut.writeLong(myTypes[i].fileLength);
76 * writeOut.writeLong(myTypes[i].lastModified);
77 * }
78 * byte[] buffer = out.toByteArray();
79 * writeOut.close();
80 *
81 * super.javaToNative(buffer, transferData);
82 *
83 * } catch (IOException e) {
84 * }
85 * }
86 * }
87 * public Object nativeToJava(TransferData transferData){
88 *
89 * if (isSupportedType(transferData)) {
90 *
91 * byte[] buffer = (byte[])super.nativeToJava(transferData);
92 * if (buffer is null) return null;
93 *
94 * MyType[] myData = new MyType[0];
95 * try {
96 * ByteArrayInputStream in = new ByteArrayInputStream(buffer);
97 * DataInputStream readIn = new DataInputStream(in);
98 * while(readIn.available() > 20) {
99 * MyType datum = new MyType();
100 * int size = readIn.readInt();
101 * byte[] name = new byte[size];
102 * readIn.read(name);
103 * datum.fileName = new String(name);
104 * datum.fileLength = readIn.readLong();
105 * datum.lastModified = readIn.readLong();
106 * MyType[] newMyData = new MyType[myData.length + 1];
107 * System.arraycopy(myData, 0, newMyData, 0, myData.length);
108 * newMyData[myData.length] = datum;
109 * myData = newMyData;
110 * }
111 * readIn.close();
112 * } catch (IOException ex) {
113 * return null;
114 * }
115 * return myData;
116 * }
117 *
118 * return null;
119 * }
120 * protected String[] getTypeNames(){
121 * return new String[]{MYTYPENAME};
122 * }
123 * protected int[] getTypeIds(){
124 * return new int[] {MYTYPEID};
125 * }
126 * }
127 * </code></pre>
128 */
129 public abstract class ByteArrayTransfer : Transfer {
130
131 public TransferData[] getSupportedTypes() {
132 int[] types = getTypeIds();
133 TransferData[] data = new TransferData[types.length];
134 for (int i = 0; i < types.length; i++) {
135 data[i] = new TransferData();
136 data[i].type = types[i];
137 data[i].formatetc = new FORMATETC();
138 data[i].formatetc.cfFormat = types[i];
139 data[i].formatetc.dwAspect = COM.DVASPECT_CONTENT;
140 data[i].formatetc.lindex = -1;
141 data[i].formatetc.tymed = COM.TYMED_HGLOBAL;
142 }
143 return data;
144 }
145
146 public bool isSupportedType(TransferData transferData){
147 if (transferData is null) return false;
148 int[] types = getTypeIds();
149 for (int i = 0; i < types.length; i++) {
150 FORMATETC* format = transferData.formatetc;
151 if (format.cfFormat is types[i] &&
152 (format.dwAspect & COM.DVASPECT_CONTENT) is COM.DVASPECT_CONTENT &&
153 (format.tymed & COM.TYMED_HGLOBAL) is COM.TYMED_HGLOBAL )
154 return true;
155 }
156 return false;
157 }
158
159 /**
160 * This implementation of <code>javaToNative</code> converts a java
161 * <code>byte[]</code> to a platform specific representation. For additional
162 * information see <code>Transfer#javaToNative</code>.
163 *
164 * @see Transfer#javaToNative
165 *
166 * @param object a java <code>byte[]</code> containing the data to be converted
167 * @param transferData an empty <code>TransferData</code> object; this
168 * object will be filled in on return with the platform specific format of the data
169 */
170 protected void javaToNative (Object object, TransferData transferData) {
171 if (!checkByteArray(object) || !isSupportedType(transferData)) {
172 DND.error(DND.ERROR_INVALID_DATA);
173 }
174 // Allocate the memory because the caller (DropTarget) has not handed it in
175 // The caller of this method must release the data when it is done with it.
176 byte[] data = (cast(ArrayWrapperByte)object).array;
177 int size = data.length;
178 auto newPtr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, size);
179 OS.MoveMemory(newPtr, data.ptr, size);
180 transferData.stgmedium = new STGMEDIUM();
181 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
182 transferData.stgmedium.unionField = newPtr;
183 transferData.stgmedium.pUnkForRelease = null;
184 transferData.result = COM.S_OK;
185 }
186
187 /**
188 * This implementation of <code>nativeToJava</code> converts a platform specific
189 * representation of a byte array to a java <code>byte[]</code>.
190 * For additional information see <code>Transfer#nativeToJava</code>.
191 *
192 * @see Transfer#nativeToJava
193 *
194 * @param transferData the platform specific representation of the data to be
195 * been converted
196 * @return a java <code>byte[]</code> containing the converted data if the
197 * conversion was successful; otherwise null
198 */
199 protected Object nativeToJava(TransferData transferData) {
200 if (!isSupportedType(transferData) || transferData.pIDataObject is null) return null;
201
202 IDataObject data = transferData.pIDataObject;
203 data.AddRef();
204 FORMATETC* formatetc = transferData.formatetc;
205 STGMEDIUM* stgmedium = new STGMEDIUM();
206 stgmedium.tymed = COM.TYMED_HGLOBAL;
207 transferData.result = data.GetData(formatetc, stgmedium);
208 data.Release();
209 if (transferData.result !is COM.S_OK) return null;
210 auto hMem = stgmedium.unionField;
211 int size = OS.GlobalSize(hMem);
212 byte[] buffer = new byte[size];
213 auto ptr = OS.GlobalLock(hMem);
214 OS.MoveMemory(buffer.ptr, ptr, size);
215 OS.GlobalUnlock(hMem);
216 OS.GlobalFree(hMem);
217 return new ArrayWrapperByte(buffer);
218 }
219
220 bool checkByteArray(Object object) {
221 return (object !is null && ( null !is cast(ArrayWrapperByte)object) && (cast(ArrayWrapperByte)object).array.length > 0);
222 }
223 }