1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* 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.editor;
|
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.apache.log4j.Logger;
|
23
|
import org.eclipse.swt.dnd.ByteArrayTransfer;
|
24
|
import org.eclipse.swt.dnd.TransferData;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
27
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
28
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
30
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
31
|
|
32
|
/**
|
33
|
* <p>CdmDataTransfer class.</p>
|
34
|
*
|
35
|
* @author n.hoffmann
|
36
|
* @created Jun 18, 2010
|
37
|
* @version 1.0
|
38
|
*/
|
39
|
public class CdmDataTransfer extends ByteArrayTransfer {
|
40
|
private static final Logger logger = Logger
|
41
|
.getLogger(CdmDataTransfer.class);
|
42
|
|
43
|
private static int SEPARATOR = -10;
|
44
|
private static int EOF = -20;
|
45
|
private static final String TYPE_NAME = "cdmdata-transfer-format"; //$NON-NLS-1$
|
46
|
private static final int TYPE_ID = registerType (TYPE_NAME);
|
47
|
private static final CdmDataTransfer instance = new CdmDataTransfer();
|
48
|
|
49
|
/**
|
50
|
* <p>Getter for the field <code>instance</code>.</p>
|
51
|
*
|
52
|
* @return a {@link eu.etaxonomy.taxeditor.editor.CdmDataTransfer} object.
|
53
|
*/
|
54
|
public static synchronized CdmDataTransfer getInstance() {
|
55
|
return instance;
|
56
|
}
|
57
|
|
58
|
private ICdmBase cdmBase;
|
59
|
|
60
|
/* (non-Javadoc)
|
61
|
* @see org.eclipse.swt.dnd.Transfer#getTypeIds()
|
62
|
*/
|
63
|
/** {@inheritDoc} */
|
64
|
@Override
|
65
|
protected int[] getTypeIds() {
|
66
|
return new int[] { TYPE_ID };
|
67
|
}
|
68
|
|
69
|
/* (non-Javadoc)
|
70
|
* @see org.eclipse.swt.dnd.Transfer#getTypeNames()
|
71
|
*/
|
72
|
/** {@inheritDoc} */
|
73
|
@Override
|
74
|
protected String[] getTypeNames() {
|
75
|
return new String[]{ TYPE_NAME };
|
76
|
}
|
77
|
|
78
|
/** {@inheritDoc} */
|
79
|
@Override
|
80
|
protected void javaToNative(Object object, TransferData transferData) {
|
81
|
byte[] bytes = toByteArray((ICdmBase[]) object);
|
82
|
if (bytes != null)
|
83
|
super.javaToNative(bytes, transferData);
|
84
|
}
|
85
|
|
86
|
|
87
|
|
88
|
/** {@inheritDoc} */
|
89
|
@Override
|
90
|
protected Object nativeToJava(TransferData transferData) {
|
91
|
byte[] bytes = (byte[]) super.nativeToJava(transferData);
|
92
|
return fromByteArray(bytes);
|
93
|
}
|
94
|
|
95
|
|
96
|
private byte[] toByteArray(ICdmBase[] cdmBaseObjects) {
|
97
|
/**
|
98
|
* Transfer data is an array of gadgets. Serialized version is: (int)
|
99
|
* number of gadgets (Gadget) gadget 1 (Gadget) gadget 2 ... repeat for
|
100
|
* each subsequent gadget see writeGadget for the (Gadget) format.
|
101
|
*/
|
102
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
103
|
DataOutputStream out = new DataOutputStream(byteOut);
|
104
|
|
105
|
byte[] bytes = null;
|
106
|
|
107
|
try {
|
108
|
|
109
|
for(ICdmBase cdmBaseObject : cdmBaseObjects){
|
110
|
writeCdmBaseObject(cdmBaseObject, out);
|
111
|
}
|
112
|
out.close();
|
113
|
bytes = byteOut.toByteArray();
|
114
|
} catch (IOException e) {
|
115
|
// when in doubt send nothing
|
116
|
}
|
117
|
return bytes;
|
118
|
}
|
119
|
|
120
|
|
121
|
private ICdmBase[] fromByteArray(byte[] bytes) {
|
122
|
DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
|
123
|
|
124
|
try {
|
125
|
List<ICdmBase> cdmBaseObjects = new ArrayList<ICdmBase>();
|
126
|
|
127
|
try{
|
128
|
while(true){
|
129
|
cdmBaseObjects.add(readCdmBaseObject(in));
|
130
|
}
|
131
|
}catch(EOFException e){
|
132
|
return cdmBaseObjects.toArray(new ICdmBase[]{});
|
133
|
}
|
134
|
} catch (IOException e) {
|
135
|
return null;
|
136
|
}
|
137
|
}
|
138
|
|
139
|
private ICdmBase readCdmBaseObject(DataInputStream in) throws IOException{
|
140
|
String className = in.readUTF();
|
141
|
in.readInt();
|
142
|
String idString = in.readUTF();
|
143
|
in.readInt();
|
144
|
|
145
|
if(className.equals(Synonym.class.getName()) || className.equals(Taxon.class.getName())){
|
146
|
ICdmBase object = CdmStore.getService(ITaxonService.class).load(UUID.fromString(idString));
|
147
|
|
148
|
if(object == null){
|
149
|
return cdmBase;
|
150
|
}
|
151
|
|
152
|
return object;
|
153
|
}
|
154
|
|
155
|
return null;
|
156
|
}
|
157
|
|
158
|
private void writeCdmBaseObject(ICdmBase cdmBase, DataOutputStream out) throws IOException{
|
159
|
/**
|
160
|
* CdmBase object serialization:
|
161
|
*
|
162
|
*/
|
163
|
out.writeUTF(cdmBase.getClass().getName());
|
164
|
out.writeInt(SEPARATOR);
|
165
|
out.writeUTF(cdmBase.getUuid().toString());
|
166
|
out.writeInt(EOF);
|
167
|
|
168
|
if(cdmBase.getId() == 0){
|
169
|
// fallback for unsaved instances
|
170
|
this.cdmBase = cdmBase;
|
171
|
}
|
172
|
}
|
173
|
|
174
|
}
|