KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > dnd > ByteArrayTransfer


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  *******************************************************************************/

11 package org.eclipse.swt.dnd;
12
13 import org.eclipse.swt.internal.ole.win32.*;
14 import org.eclipse.swt.internal.win32.*;
15
16 /**
17  * The class <code>ByteArrayTransfer</code> provides a platform specific
18  * mechanism for converting a java <code>byte[]</code> to a platform
19  * specific representation of the byte array and vice versa. See
20  * <code>Transfer</code> for additional information.
21  *
22  * <p><code>ByteArrayTransfer</code> is never used directly but is sub-classed
23  * by transfer agents that convert between data in a java format such as a
24  * <code>String</code> and a platform specific byte array.
25  *
26  * <p>If the data you are converting <b>does not</b> map to a
27  * <code>byte[]</code>, you should sub-class <code>Transfer</code> directly
28  * and do your own mapping to a platform data type.</p>
29  *
30  * <p>The following snippet shows a subclass of ByteArrayTransfer that transfers
31  * data defined by the class <code>MyType</code>.</p>
32  *
33  * <pre><code>
34  * public class MyType {
35  * public String fileName;
36  * public long fileLength;
37  * public long lastModified;
38  * }
39  * </code></pre>
40  *
41  * <pre><code>
42  * public class MyTypeTransfer extends ByteArrayTransfer {
43  *
44  * private static final String MYTYPENAME = "my_type_name";
45  * private static final int MYTYPEID = registerType(MYTYPENAME);
46  * private static MyTypeTransfer _instance = new MyTypeTransfer();
47  *
48  * private MyTypeTransfer() {}
49  *
50  * public static MyTypeTransfer getInstance () {
51  * return _instance;
52  * }
53  * public void javaToNative (Object object, TransferData transferData) {
54  * if (object == null || !(object instanceof MyType[])) return;
55  *
56  * if (isSupportedType(transferData)) {
57  * MyType[] myTypes = (MyType[]) object;
58  * try {
59  * // write data to a byte array and then ask super to convert to pMedium
60  * ByteArrayOutputStream out = new ByteArrayOutputStream();
61  * DataOutputStream writeOut = new DataOutputStream(out);
62  * for (int i = 0, length = myTypes.length; i &lt; length; i++){
63  * byte[] buffer = myTypes[i].fileName.getBytes();
64  * writeOut.writeInt(buffer.length);
65  * writeOut.write(buffer);
66  * writeOut.writeLong(myTypes[i].fileLength);
67  * writeOut.writeLong(myTypes[i].lastModified);
68  * }
69  * byte[] buffer = out.toByteArray();
70  * writeOut.close();
71  *
72  * super.javaToNative(buffer, transferData);
73  *
74  * } catch (IOException e) {
75  * }
76  * }
77  * }
78  * public Object nativeToJava(TransferData transferData){
79  *
80  * if (isSupportedType(transferData)) {
81  *
82  * byte[] buffer = (byte[])super.nativeToJava(transferData);
83  * if (buffer == null) return null;
84  *
85  * MyType[] myData = new MyType[0];
86  * try {
87  * ByteArrayInputStream in = new ByteArrayInputStream(buffer);
88  * DataInputStream readIn = new DataInputStream(in);
89  * while(readIn.available() > 20) {
90  * MyType datum = new MyType();
91  * int size = readIn.readInt();
92  * byte[] name = new byte[size];
93  * readIn.read(name);
94  * datum.fileName = new String(name);
95  * datum.fileLength = readIn.readLong();
96  * datum.lastModified = readIn.readLong();
97  * MyType[] newMyData = new MyType[myData.length + 1];
98  * System.arraycopy(myData, 0, newMyData, 0, myData.length);
99  * newMyData[myData.length] = datum;
100  * myData = newMyData;
101  * }
102  * readIn.close();
103  * } catch (IOException ex) {
104  * return null;
105  * }
106  * return myData;
107  * }
108  *
109  * return null;
110  * }
111  * protected String[] getTypeNames(){
112  * return new String[]{MYTYPENAME};
113  * }
114  * protected int[] getTypeIds(){
115  * return new int[] {MYTYPEID};
116  * }
117  * }
118  * </code></pre>
119  */

120 public abstract class ByteArrayTransfer extends Transfer {
121
122 public TransferData[] getSupportedTypes() {
123     int[] types = getTypeIds();
124     TransferData[] data = new TransferData[types.length];
125     for (int i = 0; i < types.length; i++) {
126         data[i] = new TransferData();
127         data[i].type = types[i];
128         data[i].formatetc = new FORMATETC();
129         data[i].formatetc.cfFormat = types[i];
130         data[i].formatetc.dwAspect = COM.DVASPECT_CONTENT;
131         data[i].formatetc.lindex = -1;
132         data[i].formatetc.tymed = COM.TYMED_HGLOBAL;
133     }
134     return data;
135 }
136
137 public boolean isSupportedType(TransferData transferData){
138     if (transferData == null) return false;
139     int[] types = getTypeIds();
140     for (int i = 0; i < types.length; i++) {
141         FORMATETC format = transferData.formatetc;
142         if (format.cfFormat == types[i] &&
143             (format.dwAspect & COM.DVASPECT_CONTENT) == COM.DVASPECT_CONTENT &&
144             (format.tymed & COM.TYMED_HGLOBAL) == COM.TYMED_HGLOBAL )
145             return true;
146     }
147     return false;
148 }
149
150 /**
151  * This implementation of <code>javaToNative</code> converts a java
152  * <code>byte[]</code> to a platform specific representation. For additional
153  * information see <code>Transfer#javaToNative</code>.
154  *
155  * @see Transfer#javaToNative
156  *
157  * @param object a java <code>byte[]</code> containing the data to be converted
158  * @param transferData an empty <code>TransferData</code> object; this
159  * object will be filled in on return with the platform specific format of the data
160  */

161 protected void javaToNative (Object JavaDoc object, TransferData transferData) {
162     if (!checkByteArray(object) || !isSupportedType(transferData)) {
163         DND.error(DND.ERROR_INVALID_DATA);
164     }
165     // Allocate the memory because the caller (DropTarget) has not handed it in
166
// The caller of this method must release the data when it is done with it.
167
byte[] data = (byte[])object;
168     int size = data.length;
169     int newPtr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, size);
170     OS.MoveMemory(newPtr, data, size);
171     transferData.stgmedium = new STGMEDIUM();
172     transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
173     transferData.stgmedium.unionField = newPtr;
174     transferData.stgmedium.pUnkForRelease = 0;
175     transferData.result = COM.S_OK;
176 }
177
178 /**
179  * This implementation of <code>nativeToJava</code> converts a platform specific
180  * representation of a byte array to a java <code>byte[]</code>.
181  * For additional information see <code>Transfer#nativeToJava</code>.
182  *
183  * @see Transfer#nativeToJava
184  *
185  * @param transferData the platform specific representation of the data to be
186  * been converted
187  * @return a java <code>byte[]</code> containing the converted data if the
188  * conversion was successful; otherwise null
189  */

190 protected Object JavaDoc nativeToJava(TransferData transferData) {
191     if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null;
192     
193     IDataObject data = new IDataObject(transferData.pIDataObject);
194     data.AddRef();
195     FORMATETC formatetc = transferData.formatetc;
196     STGMEDIUM stgmedium = new STGMEDIUM();
197     stgmedium.tymed = COM.TYMED_HGLOBAL;
198     transferData.result = data.GetData(formatetc, stgmedium);
199     data.Release();
200     if (transferData.result != COM.S_OK) return null;
201     int hMem = stgmedium.unionField;
202     int size = OS.GlobalSize(hMem);
203     byte[] buffer = new byte[size];
204     int ptr = OS.GlobalLock(hMem);
205     OS.MoveMemory(buffer, ptr, size);
206     OS.GlobalUnlock(hMem);
207     OS.GlobalFree(hMem);
208     return buffer;
209 }
210
211 boolean checkByteArray(Object JavaDoc object) {
212     return (object != null && object instanceof byte[] && ((byte[])object).length > 0);
213 }
214 }
215
Popular Tags