Revision 8481f952
Added by Andreas Müller almost 4 years ago
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/CdmDataTransfer.java | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (C) 2007 EDIT |
3 |
* European Distributed Institute of Taxonomy
|
|
3 |
* European Distributed Institute of Taxonomy |
|
4 | 4 |
* http://www.e-taxonomy.eu |
5 |
*
|
|
5 |
* |
|
6 | 6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
7 | 7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
8 | 8 |
*/ |
... | ... | |
34 | 34 |
* |
35 | 35 |
* @author n.hoffmann |
36 | 36 |
* @created Jun 18, 2010 |
37 |
* @version 1.0 |
|
38 | 37 |
*/ |
39 | 38 |
public class CdmDataTransfer extends ByteArrayTransfer { |
40 |
private static final Logger logger = Logger |
|
41 |
.getLogger(CdmDataTransfer.class); |
|
39 |
|
|
40 |
@SuppressWarnings("unused") |
|
41 |
private static final Logger logger = Logger.getLogger(CdmDataTransfer.class); |
|
42 | 42 |
|
43 | 43 |
private static int SEPARATOR = -10; |
44 | 44 |
private static int EOF = -20; |
45 | 45 |
private static final String TYPE_NAME = "cdmdata-transfer-format"; //$NON-NLS-1$ |
46 |
private static final int TYPE_ID = registerType (TYPE_NAME);
|
|
46 |
private static final int TYPE_ID = registerType (TYPE_NAME); |
|
47 | 47 |
private static final CdmDataTransfer instance = new CdmDataTransfer(); |
48 |
|
|
48 |
|
|
49 | 49 |
/** |
50 | 50 |
* <p>Getter for the field <code>instance</code>.</p> |
51 | 51 |
* |
... | ... | |
56 | 56 |
} |
57 | 57 |
|
58 | 58 |
private ICdmBase cdmBase; |
59 |
|
|
60 |
/* (non-Javadoc) |
|
61 |
* @see org.eclipse.swt.dnd.Transfer#getTypeIds() |
|
62 |
*/ |
|
63 |
/** {@inheritDoc} */ |
|
59 |
|
|
64 | 60 |
@Override |
65 | 61 |
protected int[] getTypeIds() { |
66 |
return new int[] { TYPE_ID };
|
|
62 |
return new int[] { TYPE_ID }; |
|
67 | 63 |
} |
68 | 64 |
|
69 |
/* (non-Javadoc) |
|
70 |
* @see org.eclipse.swt.dnd.Transfer#getTypeNames() |
|
71 |
*/ |
|
72 |
/** {@inheritDoc} */ |
|
73 | 65 |
@Override |
74 | 66 |
protected String[] getTypeNames() { |
75 | 67 |
return new String[]{ TYPE_NAME }; |
76 | 68 |
} |
77 | 69 |
|
78 |
/** {@inheritDoc} */ |
|
79 | 70 |
@Override |
80 | 71 |
protected void javaToNative(Object object, TransferData transferData) { |
81 | 72 |
byte[] bytes = toByteArray((ICdmBase[]) object); |
82 |
if (bytes != null) |
|
83 |
super.javaToNative(bytes, transferData); |
|
73 |
if (bytes != null) { |
|
74 |
super.javaToNative(bytes, transferData); |
|
75 |
} |
|
84 | 76 |
} |
85 | 77 |
|
86 |
|
|
87 |
|
|
88 |
/** {@inheritDoc} */ |
|
89 | 78 |
@Override |
90 | 79 |
protected Object nativeToJava(TransferData transferData) { |
91 | 80 |
byte[] bytes = (byte[]) super.nativeToJava(transferData); |
... | ... | |
105 | 94 |
byte[] bytes = null; |
106 | 95 |
|
107 | 96 |
try { |
108 |
|
|
97 |
|
|
109 | 98 |
for(ICdmBase cdmBaseObject : cdmBaseObjects){ |
110 | 99 |
writeCdmBaseObject(cdmBaseObject, out); |
111 | 100 |
} |
... | ... | |
116 | 105 |
} |
117 | 106 |
return bytes; |
118 | 107 |
} |
119 |
|
|
120 |
|
|
108 |
|
|
109 |
|
|
121 | 110 |
private ICdmBase[] fromByteArray(byte[] bytes) { |
122 | 111 |
DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes)); |
123 |
|
|
112 |
|
|
124 | 113 |
try { |
125 | 114 |
List<ICdmBase> cdmBaseObjects = new ArrayList<ICdmBase>(); |
126 |
|
|
115 |
|
|
127 | 116 |
try{ |
128 | 117 |
while(true){ |
129 | 118 |
cdmBaseObjects.add(readCdmBaseObject(in)); |
... | ... | |
135 | 124 |
return null; |
136 | 125 |
} |
137 | 126 |
} |
138 |
|
|
127 |
|
|
139 | 128 |
private ICdmBase readCdmBaseObject(DataInputStream in) throws IOException{ |
140 | 129 |
String className = in.readUTF(); |
141 | 130 |
in.readInt(); |
142 | 131 |
String idString = in.readUTF(); |
143 | 132 |
in.readInt(); |
144 |
|
|
133 |
|
|
145 | 134 |
if(className.equals(Synonym.class.getName()) || className.equals(Taxon.class.getName())){ |
146 | 135 |
ICdmBase object = CdmStore.getService(ITaxonService.class).load(UUID.fromString(idString)); |
147 |
|
|
136 |
|
|
148 | 137 |
if(object == null){ |
149 | 138 |
return cdmBase; |
150 | 139 |
} |
151 |
|
|
140 |
|
|
152 | 141 |
return object; |
153 | 142 |
} |
154 |
|
|
143 |
|
|
155 | 144 |
return null; |
156 | 145 |
} |
157 |
|
|
146 |
|
|
158 | 147 |
private void writeCdmBaseObject(ICdmBase cdmBase, DataOutputStream out) throws IOException{ |
159 | 148 |
/** |
160 | 149 |
* CdmBase object serialization: |
161 |
*
|
|
150 |
* |
|
162 | 151 |
*/ |
163 | 152 |
out.writeUTF(cdmBase.getClass().getName()); |
164 | 153 |
out.writeInt(SEPARATOR); |
165 | 154 |
out.writeUTF(cdmBase.getUuid().toString()); |
166 | 155 |
out.writeInt(EOF); |
167 |
|
|
156 |
|
|
168 | 157 |
if(cdmBase.getId() == 0){ |
169 | 158 |
// fallback for unsaved instances |
170 | 159 |
this.cdmBase = cdmBase; |
Also available in: Unified diff
cleanup