2 * Copyright (C) 2007 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.
10 package eu
.etaxonomy
.taxeditor
.featuretree
;
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
;
22 import org
.eclipse
.swt
.SWTException
;
23 import org
.eclipse
.swt
.dnd
.ByteArrayTransfer
;
24 import org
.eclipse
.swt
.dnd
.TransferData
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
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
;
33 * <p>FeatureNodeTransfer class.</p>
36 * @created Aug 5, 2010
38 public class FeatureNodeTransfer
extends ByteArrayTransfer
{
40 private static FeatureNodeTransfer instance
= new FeatureNodeTransfer();
41 private static final String TYPE_NAME
= "featureNode-transfer-format";
42 private static final int TYPEID
= registerType(TYPE_NAME
);
45 * <p>Getter for the field <code>instance</code>.</p>
47 * @return a {@link eu.etaxonomy.taxeditor.featuretree.FeatureNodeTransfer} object.
49 public static synchronized FeatureNodeTransfer
getInstance() {
53 private FeatureNodeTransfer() {
58 protected int[] getTypeIds() {
59 return new int[] { TYPEID
};
64 protected String
[] getTypeNames() {
65 return new String
[] { TYPE_NAME
};
68 protected void javaToNative(Object object
, TransferData transferData
) {
70 byte[] bytes
= toByteArray((TermNode
[]) object
);
72 super.javaToNative(bytes
, transferData
);
78 protected Object
nativeToJava(TransferData transferData
) {
80 byte[] bytes
= (byte[]) super.nativeToJava(transferData
);
82 return fromByteArray(bytes
);
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");
92 protected byte[] toByteArray(TermNode
[] elements
) {
93 ByteArrayOutputStream byteOut
= new ByteArrayOutputStream();
94 DataOutputStream out
= new DataOutputStream(byteOut
);
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");
104 writeElementUuid(element
, out
);
108 bytes
= byteOut
.toByteArray();
109 } catch (IOException e
) {
111 // when in doubt send nothing
116 protected Object
[] fromByteArray(byte[] bytes
) {
117 DataInputStream in
= new DataInputStream(
118 new ByteArrayInputStream(bytes
));
121 List
<TermNode
> elements
= new ArrayList
<TermNode
>();
125 UUID uuid
= readElementUuid(in
);
126 elements
.add(loadElement(uuid
));
128 }catch(EOFException e
){
129 return elements
.toArray();
131 } catch (IOException e
) {
136 public TermNode
loadElement(UUID uuid
){
137 TermNode
<Feature
> node
= CdmStore
.getService(IFeatureNodeService
.class).load(uuid
);
141 public UUID
readElementUuid(DataInputStream in
) throws IOException
{
142 return UUID
.fromString(in
.readUTF());
145 private void writeElementUuid(Object element
, DataOutputStream out
) throws IOException
{
146 out
.writeUTF(((TermNode
)element
).getUuid().toString());