Avoid double rendering of concept view
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / CdmDataTransfer.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.editor;
12
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.DataInputStream;
16 import java.io.DataOutputStream;
17 import java.io.EOFException;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.UUID;
22
23 import org.apache.log4j.Logger;
24 import org.eclipse.swt.dnd.ByteArrayTransfer;
25 import org.eclipse.swt.dnd.TransferData;
26
27 import eu.etaxonomy.cdm.api.service.ITaxonService;
28 import eu.etaxonomy.cdm.model.common.ICdmBase;
29 import eu.etaxonomy.cdm.model.taxon.Synonym;
30 import eu.etaxonomy.cdm.model.taxon.Taxon;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32
33 /**
34 * <p>CdmDataTransfer class.</p>
35 *
36 * @author n.hoffmann
37 * @created Jun 18, 2010
38 * @version 1.0
39 */
40 public class CdmDataTransfer extends ByteArrayTransfer {
41 private static final Logger logger = Logger
42 .getLogger(CdmDataTransfer.class);
43
44 private static int SEPARATOR = -10;
45 private static int EOF = -20;
46 private static final String TYPE_NAME = "cdmdata-transfer-format";
47 private static final int TYPE_ID = registerType (TYPE_NAME);
48 private static final CdmDataTransfer instance = new CdmDataTransfer();
49
50 /**
51 * <p>Getter for the field <code>instance</code>.</p>
52 *
53 * @return a {@link eu.etaxonomy.taxeditor.editor.CdmDataTransfer} object.
54 */
55 public static synchronized CdmDataTransfer getInstance() {
56 return instance;
57 }
58
59 private ICdmBase cdmBase;
60
61 /* (non-Javadoc)
62 * @see org.eclipse.swt.dnd.Transfer#getTypeIds()
63 */
64 /** {@inheritDoc} */
65 @Override
66 protected int[] getTypeIds() {
67 return new int[] { TYPE_ID };
68 }
69
70 /* (non-Javadoc)
71 * @see org.eclipse.swt.dnd.Transfer#getTypeNames()
72 */
73 /** {@inheritDoc} */
74 @Override
75 protected String[] getTypeNames() {
76 return new String[]{ TYPE_NAME };
77 }
78
79 /** {@inheritDoc} */
80 @Override
81 protected void javaToNative(Object object, TransferData transferData) {
82 byte[] bytes = toByteArray((ICdmBase[]) object);
83 if (bytes != null)
84 super.javaToNative(bytes, transferData);
85 }
86
87
88
89 /** {@inheritDoc} */
90 @Override
91 protected Object nativeToJava(TransferData transferData) {
92 byte[] bytes = (byte[]) super.nativeToJava(transferData);
93 return fromByteArray(bytes);
94 }
95
96
97 private byte[] toByteArray(ICdmBase[] cdmBaseObjects) {
98 /**
99 * Transfer data is an array of gadgets. Serialized version is: (int)
100 * number of gadgets (Gadget) gadget 1 (Gadget) gadget 2 ... repeat for
101 * each subsequent gadget see writeGadget for the (Gadget) format.
102 */
103 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
104 DataOutputStream out = new DataOutputStream(byteOut);
105
106 byte[] bytes = null;
107
108 try {
109
110 for(ICdmBase cdmBaseObject : cdmBaseObjects){
111 writeCdmBaseObject(cdmBaseObject, out);
112 }
113 out.close();
114 bytes = byteOut.toByteArray();
115 } catch (IOException e) {
116 // when in doubt send nothing
117 }
118 return bytes;
119 }
120
121
122 private ICdmBase[] fromByteArray(byte[] bytes) {
123 DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
124
125 try {
126 List<ICdmBase> cdmBaseObjects = new ArrayList<ICdmBase>();
127
128 try{
129 while(true){
130 cdmBaseObjects.add(readCdmBaseObject(in));
131 }
132 }catch(EOFException e){
133 return cdmBaseObjects.toArray(new ICdmBase[]{});
134 }
135 } catch (IOException e) {
136 return null;
137 }
138 }
139
140 private ICdmBase readCdmBaseObject(DataInputStream in) throws IOException{
141 String className = in.readUTF();
142 in.readInt();
143 String idString = in.readUTF();
144 in.readInt();
145
146 if(className.equals(Synonym.class.getName()) || className.equals(Taxon.class.getName())){
147 ICdmBase object = CdmStore.getService(ITaxonService.class).load(UUID.fromString(idString));
148
149 if(object == null){
150 return cdmBase;
151 }
152
153 return object;
154 }
155
156 return null;
157 }
158
159 private void writeCdmBaseObject(ICdmBase cdmBase, DataOutputStream out) throws IOException{
160 /**
161 * CdmBase object serialization:
162 *
163 */
164 out.writeUTF(cdmBase.getClass().getName());
165 out.writeInt(SEPARATOR);
166 out.writeUTF(cdmBase.getUuid().toString());
167 out.writeInt(EOF);
168
169 if(cdmBase.getId() == 0){
170 // fallback for unsaved instances
171 this.cdmBase = cdmBase;
172 }
173 }
174
175 }