1 |
840d4d5d
|
n.hoffmann
|
/**
|
2 |
|
|
* Copyright (C) 2007 EDIT
|
3 |
baafe6d6
|
Patrick Plitzner
|
* European Distributed Institute of Taxonomy
|
4 |
840d4d5d
|
n.hoffmann
|
* http://www.e-taxonomy.eu
|
5 |
baafe6d6
|
Patrick Plitzner
|
*
|
6 |
840d4d5d
|
n.hoffmann
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7 |
|
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
|
|
package eu.etaxonomy.taxeditor.model;
|
11 |
|
|
|
12 |
|
|
import java.io.ByteArrayInputStream;
|
13 |
|
|
import java.io.ByteArrayOutputStream;
|
14 |
|
|
import java.io.DataInputStream;
|
15 |
|
|
import java.io.DataOutputStream;
|
16 |
|
|
import java.io.EOFException;
|
17 |
|
|
import java.io.IOException;
|
18 |
|
|
import java.util.ArrayList;
|
19 |
|
|
import java.util.List;
|
20 |
|
|
import java.util.UUID;
|
21 |
|
|
|
22 |
|
|
import org.eclipse.swt.dnd.ByteArrayTransfer;
|
23 |
|
|
import org.eclipse.swt.dnd.TransferData;
|
24 |
|
|
|
25 |
|
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
26 |
|
|
|
27 |
|
|
/**
|
28 |
|
|
* @author n.hoffmann
|
29 |
|
|
* @created Feb 8, 2011
|
30 |
|
|
* @version 1.0
|
31 |
|
|
*/
|
32 |
|
|
public abstract class CdmObjectTransfer<T extends ICdmBase> extends ByteArrayTransfer {
|
33 |
baafe6d6
|
Patrick Plitzner
|
|
34 |
840d4d5d
|
n.hoffmann
|
@Override
|
35 |
|
|
protected void javaToNative(Object object, TransferData transferData) {
|
36 |
07655893
|
Katja Luther
|
if (object != null){
|
37 |
|
|
byte[] bytes = toByteArray((T[]) object);
|
38 |
baafe6d6
|
Patrick Plitzner
|
if (bytes != null) {
|
39 |
|
|
super.javaToNative(bytes, transferData);
|
40 |
|
|
}
|
41 |
07655893
|
Katja Luther
|
}
|
42 |
840d4d5d
|
n.hoffmann
|
}
|
43 |
baafe6d6
|
Patrick Plitzner
|
|
44 |
840d4d5d
|
n.hoffmann
|
@Override
|
45 |
|
|
protected Object nativeToJava(TransferData transferData) {
|
46 |
|
|
byte[] bytes = (byte[]) super.nativeToJava(transferData);
|
47 |
07655893
|
Katja Luther
|
if (bytes != null){
|
48 |
|
|
return fromByteArray(bytes);
|
49 |
|
|
}
|
50 |
|
|
return null;
|
51 |
840d4d5d
|
n.hoffmann
|
}
|
52 |
baafe6d6
|
Patrick Plitzner
|
|
53 |
|
|
|
54 |
840d4d5d
|
n.hoffmann
|
protected byte[] toByteArray(T[] elements) {
|
55 |
|
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
56 |
|
|
DataOutputStream out = new DataOutputStream(byteOut);
|
57 |
|
|
|
58 |
|
|
byte[] bytes = null;
|
59 |
|
|
|
60 |
|
|
try {
|
61 |
baafe6d6
|
Patrick Plitzner
|
|
62 |
840d4d5d
|
n.hoffmann
|
for(T element : elements){
|
63 |
|
|
writeElementUuid(element, out);
|
64 |
|
|
}
|
65 |
|
|
out.close();
|
66 |
|
|
bytes = byteOut.toByteArray();
|
67 |
|
|
} catch (IOException e) {
|
68 |
|
|
// when in doubt send nothing
|
69 |
|
|
}
|
70 |
|
|
return bytes;
|
71 |
|
|
}
|
72 |
baafe6d6
|
Patrick Plitzner
|
|
73 |
840d4d5d
|
n.hoffmann
|
private void writeElementUuid(T element, DataOutputStream out) throws IOException {
|
74 |
baafe6d6
|
Patrick Plitzner
|
out.writeUTF(element.getUuid().toString());
|
75 |
840d4d5d
|
n.hoffmann
|
}
|
76 |
|
|
|
77 |
|
|
protected Object fromByteArray(byte[] bytes) {
|
78 |
|
|
DataInputStream in = new DataInputStream(
|
79 |
|
|
new ByteArrayInputStream(bytes));
|
80 |
|
|
|
81 |
|
|
try {
|
82 |
|
|
List<T> elements = new ArrayList<T>();
|
83 |
baafe6d6
|
Patrick Plitzner
|
|
84 |
840d4d5d
|
n.hoffmann
|
try{
|
85 |
|
|
while(true){
|
86 |
|
|
UUID uuid = readElementUuid(in);
|
87 |
|
|
elements.add(loadElement(uuid));
|
88 |
|
|
}
|
89 |
|
|
}catch(EOFException e){
|
90 |
|
|
return elements.toArray();
|
91 |
|
|
}
|
92 |
|
|
} catch (IOException e) {
|
93 |
|
|
return null;
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
public abstract T loadElement(UUID uuid);
|
98 |
baafe6d6
|
Patrick Plitzner
|
|
99 |
840d4d5d
|
n.hoffmann
|
public UUID readElementUuid(DataInputStream in) throws IOException{
|
100 |
|
|
return UUID.fromString(in.readUTF());
|
101 |
|
|
}
|
102 |
|
|
}
|