Project

General

Profile

Download (2.61 KB) Statistics
| Branch: | Tag: | Revision:
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.model;
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.eclipse.swt.dnd.ByteArrayTransfer;
24
import org.eclipse.swt.dnd.TransferData;
25

    
26
import eu.etaxonomy.cdm.model.common.ICdmBase;
27

    
28
/**
29
 * @author n.hoffmann
30
 * @created Feb 8, 2011
31
 * @version 1.0
32
 */
33
public abstract class CdmObjectTransfer<T extends ICdmBase> extends ByteArrayTransfer {
34
	
35
	/* (non-Javadoc)
36
	 * @see org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
37
	 */
38
	@Override
39
	protected void javaToNative(Object object, TransferData transferData) {
40
		byte[] bytes = toByteArray((T[]) object);
41
		if (bytes != null)
42
			super.javaToNative(bytes, transferData);
43
	}
44
	
45
	/* (non-Javadoc)
46
	 * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(org.eclipse.swt.dnd.TransferData)
47
	 */
48
	@Override
49
	protected Object nativeToJava(TransferData transferData) {
50
		byte[] bytes = (byte[]) super.nativeToJava(transferData);
51
		return fromByteArray(bytes);
52
	}
53
	
54
	protected byte[] toByteArray(T[] elements) {
55
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
56
		DataOutputStream out = new DataOutputStream(byteOut);
57

    
58
		byte[] bytes = null;
59

    
60
		try {
61
			
62
			for(T element : elements){
63
				writeElementUuid(element, out);
64
			}
65
			out.close();
66
			bytes = byteOut.toByteArray();
67
		} catch (IOException e) {
68
			// when in doubt send nothing
69
		}
70
		return bytes;
71
	}
72
	
73
	private void writeElementUuid(T element, DataOutputStream out) throws IOException {
74
		out.writeUTF(element.getUuid().toString());		
75
	}
76

    
77
	protected Object fromByteArray(byte[] bytes) {
78
		DataInputStream in = new DataInputStream(
79
				new ByteArrayInputStream(bytes));
80

    
81
		try {
82
			List<T> elements = new ArrayList<T>();
83
			
84
			try{
85
				while(true){
86
					UUID uuid = readElementUuid(in);
87
					elements.add(loadElement(uuid));
88
				}
89
			}catch(EOFException e){
90
				return elements.toArray();
91
			}
92
		} catch (IOException e) {
93
			return null;
94
		}
95
	}
96

    
97
	public abstract T loadElement(UUID uuid);
98
	
99
	/**
100
	 * @param in
101
	 * @return
102
	 * @throws IOException 
103
	 */
104
	public UUID readElementUuid(DataInputStream in) throws IOException{
105
		return UUID.fromString(in.readUTF());
106
	}
107
}
(4-4/29)