Project

General

Profile

Download (2.68 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
		if (object != null){
41
			byte[] bytes = toByteArray((T[]) object);
42
			if (bytes != null)
43
				super.javaToNative(bytes, transferData);
44
		}
45
	}
46
	
47
	/* (non-Javadoc)
48
	 * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(org.eclipse.swt.dnd.TransferData)
49
	 */
50
	@Override
51
	protected Object nativeToJava(TransferData transferData) {
52
		byte[] bytes = (byte[]) super.nativeToJava(transferData);
53
		if (bytes != null){
54
			return fromByteArray(bytes);
55
		}
56
		return null;
57
	}
58
		
59
	
60
	protected byte[] toByteArray(T[] elements) {
61
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
62
		DataOutputStream out = new DataOutputStream(byteOut);
63

    
64
		byte[] bytes = null;
65

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

    
83
	protected Object fromByteArray(byte[] bytes) {
84
		DataInputStream in = new DataInputStream(
85
				new ByteArrayInputStream(bytes));
86

    
87
		try {
88
			List<T> elements = new ArrayList<T>();
89
			
90
			try{
91
				while(true){
92
					UUID uuid = readElementUuid(in);
93
					elements.add(loadElement(uuid));
94
				}
95
			}catch(EOFException e){
96
				return elements.toArray();
97
			}
98
		} catch (IOException e) {
99
			return null;
100
		}
101
	}
102

    
103
	public abstract T loadElement(UUID uuid);
104
	
105
	/**
106
	 * @param in
107
	 * @return
108
	 * @throws IOException 
109
	 */
110
	public UUID readElementUuid(DataInputStream in) throws IOException{
111
		return UUID.fromString(in.readUTF());
112
	}
113
}
(5-5/38)