2 * Copyright (C) 2020 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.featuretree
;
11 import java
.io
.ByteArrayInputStream
;
12 import java
.io
.ByteArrayOutputStream
;
13 import java
.io
.DataInputStream
;
14 import java
.io
.DataOutputStream
;
15 import java
.io
.EOFException
;
16 import java
.io
.IOException
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import org
.eclipse
.swt
.SWTException
;
22 import org
.eclipse
.swt
.dnd
.ByteArrayTransfer
;
23 import org
.eclipse
.swt
.dnd
.TransferData
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITermNodeService
;
26 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
27 import eu
.etaxonomy
.cdm
.model
.term
.TermNode
;
28 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermNodeDto
;
29 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
30 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
36 public class FeatureNodeDtoTransfer
extends ByteArrayTransfer
{
38 private static FeatureNodeDtoTransfer instance
= new FeatureNodeDtoTransfer();
39 private static final String TYPE_NAME
= "featureNodeDto-transfer-format";
40 private static final int TYPEID
= registerType(TYPE_NAME
);
43 * <p>Getter for the field <code>instance</code>.</p>
45 * @return a {@link eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer} object.
47 public static synchronized FeatureNodeDtoTransfer
getInstance() {
51 private FeatureNodeDtoTransfer() {
56 protected int[] getTypeIds() {
57 return new int[] { TYPEID
};
62 protected String
[] getTypeNames() {
63 return new String
[] { TYPE_NAME
};
66 protected void javaToNative(Object object
, TransferData transferData
) {
68 byte[] bytes
= toByteArray((TermNodeDto
[]) object
);
70 super.javaToNative(bytes
, transferData
);
76 protected Object
nativeToJava(TransferData transferData
) {
78 byte[] bytes
= (byte[]) super.nativeToJava(transferData
);
80 return fromByteArray(bytes
);
82 }catch (SWTException e
){
83 MessagingUtils
.warningDialog("The new imported node needs to be saved first", this, "Newly created nodes can not be moved without saving");
90 protected byte[] toByteArray(TermNodeDto
[] elements
) {
91 ByteArrayOutputStream byteOut
= new ByteArrayOutputStream();
92 DataOutputStream out
= new DataOutputStream(byteOut
);
97 for(TermNodeDto element
: elements
){
98 if (element
.getUuid() == null){
99 // MessagingUtils.warningDialog("The new imported node needs to be saved first", this, "Newly created nodes can not be moved without saving");
102 writeElementUuid(element
, out
);
106 bytes
= byteOut
.toByteArray();
107 } catch (IOException e
) {
109 // when in doubt send nothing
114 protected Object
[] fromByteArray(byte[] bytes
) {
115 DataInputStream in
= new DataInputStream(
116 new ByteArrayInputStream(bytes
));
119 List
<TermNodeDto
> elements
= new ArrayList
<TermNodeDto
>();
123 UUID uuid
= readElementUuid(in
);
124 elements
.add(loadElement(uuid
));
126 }catch(EOFException e
){
127 return elements
.toArray();
129 } catch (IOException e
) {
134 public TermNodeDto
loadElement(UUID uuid
){
135 TermNode
<Feature
> node
= CdmStore
.getService(ITermNodeService
.class).load(uuid
);
136 return TermNodeDto
.fromNode(node
, null);
139 public UUID
readElementUuid(DataInputStream in
) throws IOException
{
140 return UUID
.fromString(in
.readUTF());
143 private void writeElementUuid(TermNodeDto element
, DataOutputStream out
) throws IOException
{
144 out
.writeUTF(element
.getUuid().toString());