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
.model
;
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
.dnd
.ByteArrayTransfer
;
23 import org
.eclipse
.swt
.dnd
.TransferData
;
25 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
29 * @created Feb 8, 2011
32 public abstract class CdmObjectTransfer
<T
extends ICdmBase
> extends ByteArrayTransfer
{
35 * @see org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
38 protected void javaToNative(Object object
, TransferData transferData
) {
40 byte[] bytes
= toByteArray((T
[]) object
);
42 super.javaToNative(bytes
, transferData
);
47 * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(org.eclipse.swt.dnd.TransferData)
50 protected Object
nativeToJava(TransferData transferData
) {
51 byte[] bytes
= (byte[]) super.nativeToJava(transferData
);
53 return fromByteArray(bytes
);
59 protected byte[] toByteArray(T
[] elements
) {
60 ByteArrayOutputStream byteOut
= new ByteArrayOutputStream();
61 DataOutputStream out
= new DataOutputStream(byteOut
);
67 for(T element
: elements
){
68 writeElementUuid(element
, out
);
71 bytes
= byteOut
.toByteArray();
72 } catch (IOException e
) {
73 // when in doubt send nothing
78 private void writeElementUuid(T element
, DataOutputStream out
) throws IOException
{
79 out
.writeUTF(element
.getUuid().toString());
82 protected Object
fromByteArray(byte[] bytes
) {
83 DataInputStream in
= new DataInputStream(
84 new ByteArrayInputStream(bytes
));
87 List
<T
> elements
= new ArrayList
<T
>();
91 UUID uuid
= readElementUuid(in
);
92 elements
.add(loadElement(uuid
));
94 }catch(EOFException e
){
95 return elements
.toArray();
97 } catch (IOException e
) {
102 public abstract T
loadElement(UUID uuid
);
107 * @throws IOException
109 public UUID
readElementUuid(DataInputStream in
) throws IOException
{
110 return UUID
.fromString(in
.readUTF());