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
.editor
;
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
.apache
.log4j
.Logger
;
23 import org
.eclipse
.swt
.dnd
.ByteArrayTransfer
;
24 import org
.eclipse
.swt
.dnd
.TransferData
;
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
;
33 * <p>CdmDataTransfer class.</p>
36 * @created Jun 18, 2010
39 public class CdmDataTransfer
extends ByteArrayTransfer
{
40 private static final Logger logger
= Logger
41 .getLogger(CdmDataTransfer
.class);
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();
50 * <p>Getter for the field <code>instance</code>.</p>
52 * @return a {@link eu.etaxonomy.taxeditor.editor.CdmDataTransfer} object.
54 public static synchronized CdmDataTransfer
getInstance() {
58 private ICdmBase cdmBase
;
61 * @see org.eclipse.swt.dnd.Transfer#getTypeIds()
65 protected int[] getTypeIds() {
66 return new int[] { TYPE_ID
};
70 * @see org.eclipse.swt.dnd.Transfer#getTypeNames()
74 protected String
[] getTypeNames() {
75 return new String
[]{ TYPE_NAME
};
80 protected void javaToNative(Object object
, TransferData transferData
) {
81 byte[] bytes
= toByteArray((ICdmBase
[]) object
);
83 super.javaToNative(bytes
, transferData
);
90 protected Object
nativeToJava(TransferData transferData
) {
91 byte[] bytes
= (byte[]) super.nativeToJava(transferData
);
92 return fromByteArray(bytes
);
96 private byte[] toByteArray(ICdmBase
[] cdmBaseObjects
) {
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.
102 ByteArrayOutputStream byteOut
= new ByteArrayOutputStream();
103 DataOutputStream out
= new DataOutputStream(byteOut
);
109 for(ICdmBase cdmBaseObject
: cdmBaseObjects
){
110 writeCdmBaseObject(cdmBaseObject
, out
);
113 bytes
= byteOut
.toByteArray();
114 } catch (IOException e
) {
115 // when in doubt send nothing
121 private ICdmBase
[] fromByteArray(byte[] bytes
) {
122 DataInputStream in
= new DataInputStream(new ByteArrayInputStream(bytes
));
125 List
<ICdmBase
> cdmBaseObjects
= new ArrayList
<ICdmBase
>();
129 cdmBaseObjects
.add(readCdmBaseObject(in
));
131 }catch(EOFException e
){
132 return cdmBaseObjects
.toArray(new ICdmBase
[]{});
134 } catch (IOException e
) {
139 private ICdmBase
readCdmBaseObject(DataInputStream in
) throws IOException
{
140 String className
= in
.readUTF();
142 String idString
= in
.readUTF();
145 if(className
.equals(Synonym
.class.getName()) || className
.equals(Taxon
.class.getName())){
146 ICdmBase object
= CdmStore
.getService(ITaxonService
.class).load(UUID
.fromString(idString
));
158 private void writeCdmBaseObject(ICdmBase cdmBase
, DataOutputStream out
) throws IOException
{
160 * CdmBase object serialization:
163 out
.writeUTF(cdmBase
.getClass().getName());
164 out
.writeInt(SEPARATOR
);
165 out
.writeUTF(cdmBase
.getUuid().toString());
168 if(cdmBase
.getId() == 0){
169 // fallback for unsaved instances
170 this.cdmBase
= cdmBase
;