1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.featuretree;
|
12
|
|
13
|
import java.io.ByteArrayInputStream;
|
14
|
import java.io.ByteArrayOutputStream;
|
15
|
import java.io.DataInputStream;
|
16
|
import java.io.DataOutputStream;
|
17
|
import java.io.EOFException;
|
18
|
import java.io.IOException;
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.List;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import org.eclipse.swt.dnd.ByteArrayTransfer;
|
24
|
import org.eclipse.swt.dnd.TransferData;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.description.FeatureNode;
|
27
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
28
|
|
29
|
/**
|
30
|
* <p>FeatureNodeTransfer class.</p>
|
31
|
*
|
32
|
* @author n.hoffmann
|
33
|
* @created Aug 5, 2010
|
34
|
* @version 1.0
|
35
|
*/
|
36
|
public class FeatureNodeTransfer extends ByteArrayTransfer {
|
37
|
|
38
|
private static FeatureNodeTransfer instance = new FeatureNodeTransfer();
|
39
|
private static final String TYPE_NAME = "featureNode-transfer-format";
|
40
|
private static final int TYPEID = registerType(TYPE_NAME);
|
41
|
|
42
|
/**
|
43
|
* <p>Getter for the field <code>instance</code>.</p>
|
44
|
*
|
45
|
* @return a {@link eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer} object.
|
46
|
*/
|
47
|
public static synchronized FeatureNodeTransfer getInstance() {
|
48
|
return instance;
|
49
|
}
|
50
|
|
51
|
private FeatureNodeTransfer() {
|
52
|
|
53
|
}
|
54
|
|
55
|
/** {@inheritDoc} */
|
56
|
@Override
|
57
|
protected int[] getTypeIds() {
|
58
|
return new int[] { TYPEID };
|
59
|
}
|
60
|
|
61
|
/** {@inheritDoc} */
|
62
|
@Override
|
63
|
protected String[] getTypeNames() {
|
64
|
return new String[] { TYPE_NAME };
|
65
|
}
|
66
|
|
67
|
/*
|
68
|
* Method declared on Transfer.
|
69
|
*/
|
70
|
/** {@inheritDoc} */
|
71
|
protected void javaToNative(Object object, TransferData transferData) {
|
72
|
byte[] bytes = toByteArray((FeatureNode[]) object);
|
73
|
if (bytes != null)
|
74
|
super.javaToNative(bytes, transferData);
|
75
|
}
|
76
|
|
77
|
/*
|
78
|
* Method declared on Transfer.
|
79
|
*/
|
80
|
/** {@inheritDoc} */
|
81
|
protected Object nativeToJava(TransferData transferData) {
|
82
|
byte[] bytes = (byte[]) super.nativeToJava(transferData);
|
83
|
return fromByteArray(bytes);
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* <p>toByteArray</p>
|
88
|
*
|
89
|
* @param featureNodes an array of {@link eu.etaxonomy.cdm.model.description.FeatureNode} objects.
|
90
|
* @return an array of byte.
|
91
|
*/
|
92
|
protected byte[] toByteArray(FeatureNode[] featureNodes) {
|
93
|
/**
|
94
|
* Transfer data is an array of gadgets. Serialized version is: (int)
|
95
|
* number of gadgets (Gadget) gadget 1 (Gadget) gadget 2 ... repeat for
|
96
|
* each subsequent gadget see writeGadget for the (Gadget) format.
|
97
|
*/
|
98
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
99
|
DataOutputStream out = new DataOutputStream(byteOut);
|
100
|
|
101
|
byte[] bytes = null;
|
102
|
|
103
|
try {
|
104
|
|
105
|
for(FeatureNode featureNode : featureNodes){
|
106
|
writeFeatureNode(featureNode, out);
|
107
|
}
|
108
|
out.close();
|
109
|
bytes = byteOut.toByteArray();
|
110
|
} catch (IOException e) {
|
111
|
// when in doubt send nothing
|
112
|
}
|
113
|
return bytes;
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* <p>fromByteArray</p>
|
118
|
*
|
119
|
* @param bytes an array of byte.
|
120
|
* @return an array of {@link eu.etaxonomy.cdm.model.description.FeatureNode} objects.
|
121
|
*/
|
122
|
protected FeatureNode[] fromByteArray(byte[] bytes) {
|
123
|
DataInputStream in = new DataInputStream(
|
124
|
new ByteArrayInputStream(bytes));
|
125
|
|
126
|
try {
|
127
|
List<FeatureNode> featureNodes = new ArrayList<FeatureNode>();
|
128
|
|
129
|
try{
|
130
|
while(true){
|
131
|
featureNodes.add(readFeatureNode(in));
|
132
|
}
|
133
|
}catch(EOFException e){
|
134
|
return featureNodes.toArray(new FeatureNode[]{});
|
135
|
}
|
136
|
} catch (IOException e) {
|
137
|
return null;
|
138
|
}
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Reads and returns a single featureNode from the given stream.
|
143
|
*/
|
144
|
private FeatureNode readFeatureNode(DataInputStream dataIn) throws IOException {
|
145
|
|
146
|
UUID uuid = UUID.fromString(dataIn.readUTF());
|
147
|
|
148
|
FeatureNode featureNode = CdmStore.getFeatureNodeService().load(uuid);
|
149
|
|
150
|
return featureNode;
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Writes the given featureNode to the stream.
|
155
|
*/
|
156
|
private void writeFeatureNode(FeatureNode featureNode,
|
157
|
DataOutputStream dataOut) throws IOException {
|
158
|
dataOut.writeUTF(featureNode.getUuid().toString());
|
159
|
}
|
160
|
}
|