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.termtree;
|
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.SWTException;
|
23
|
import org.eclipse.swt.dnd.ByteArrayTransfer;
|
24
|
import org.eclipse.swt.dnd.TransferData;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.service.ITermNodeService;
|
27
|
import eu.etaxonomy.cdm.model.description.Feature;
|
28
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
29
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
30
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
31
|
|
32
|
/**
|
33
|
* <p>FeatureNodeTransfer class.</p>
|
34
|
*
|
35
|
* @author n.hoffmann
|
36
|
* @created Aug 5, 2010
|
37
|
*/
|
38
|
public class TermNodeTransfer extends ByteArrayTransfer {
|
39
|
|
40
|
private static TermNodeTransfer instance = new TermNodeTransfer();
|
41
|
private static final String TYPE_NAME = "featureNode-transfer-format";
|
42
|
private static final int TYPEID = registerType(TYPE_NAME);
|
43
|
|
44
|
/**
|
45
|
* <p>Getter for the field <code>instance</code>.</p>
|
46
|
*
|
47
|
* @return a {@link eu.etaxonomy.taxeditor.termtree.FeatureNodeTransfer} object.
|
48
|
*/
|
49
|
public static synchronized TermNodeTransfer getInstance() {
|
50
|
return instance;
|
51
|
}
|
52
|
|
53
|
private TermNodeTransfer() {
|
54
|
}
|
55
|
|
56
|
/** {@inheritDoc} */
|
57
|
@Override
|
58
|
protected int[] getTypeIds() {
|
59
|
return new int[] { TYPEID };
|
60
|
}
|
61
|
|
62
|
/** {@inheritDoc} */
|
63
|
@Override
|
64
|
protected String[] getTypeNames() {
|
65
|
return new String[] { TYPE_NAME };
|
66
|
}
|
67
|
@Override
|
68
|
protected void javaToNative(Object object, TransferData transferData) {
|
69
|
if (object != null){
|
70
|
byte[] bytes = toByteArray((TermNode[]) object);
|
71
|
if (bytes != null) {
|
72
|
super.javaToNative(bytes, transferData);
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
protected Object nativeToJava(TransferData transferData) {
|
79
|
try{
|
80
|
byte[] bytes = (byte[]) super.nativeToJava(transferData);
|
81
|
if (bytes != null){
|
82
|
return fromByteArray(bytes);
|
83
|
}
|
84
|
}catch (SWTException e){
|
85
|
MessagingUtils.warningDialog("The new imported node needs to be saved first", this, "Newly created nodes can not be moved without saving");
|
86
|
}
|
87
|
|
88
|
return null;
|
89
|
}
|
90
|
|
91
|
|
92
|
protected byte[] toByteArray(TermNode[] elements) {
|
93
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
94
|
DataOutputStream out = new DataOutputStream(byteOut);
|
95
|
|
96
|
byte[] bytes = null;
|
97
|
|
98
|
try {
|
99
|
for(TermNode element : elements){
|
100
|
if (element.getUuid() == null){
|
101
|
// MessagingUtils.warningDialog("The new imported node needs to be saved first", this, "Newly created nodes can not be moved without saving");
|
102
|
|
103
|
}else{
|
104
|
writeElementUuid(element, out);
|
105
|
}
|
106
|
}
|
107
|
out.close();
|
108
|
bytes = byteOut.toByteArray();
|
109
|
} catch (IOException e) {
|
110
|
|
111
|
// when in doubt send nothing
|
112
|
}
|
113
|
return bytes;
|
114
|
}
|
115
|
|
116
|
protected Object[] fromByteArray(byte[] bytes) {
|
117
|
DataInputStream in = new DataInputStream(
|
118
|
new ByteArrayInputStream(bytes));
|
119
|
|
120
|
try {
|
121
|
List<TermNode> elements = new ArrayList<TermNode>();
|
122
|
|
123
|
try{
|
124
|
while(true){
|
125
|
UUID uuid = readElementUuid(in);
|
126
|
elements.add(loadElement(uuid));
|
127
|
}
|
128
|
}catch(EOFException e){
|
129
|
return elements.toArray();
|
130
|
}
|
131
|
} catch (IOException e) {
|
132
|
return null;
|
133
|
}
|
134
|
}
|
135
|
|
136
|
public TermNode loadElement(UUID uuid){
|
137
|
TermNode<Feature> node = CdmStore.getService(ITermNodeService.class).load(uuid);
|
138
|
return node;
|
139
|
}
|
140
|
|
141
|
public UUID readElementUuid(DataInputStream in) throws IOException{
|
142
|
return UUID.fromString(in.readUTF());
|
143
|
}
|
144
|
|
145
|
private void writeElementUuid(Object element, DataOutputStream out) throws IOException {
|
146
|
out.writeUTF(((TermNode)element).getUuid().toString());
|
147
|
}
|
148
|
|
149
|
}
|