Project

General

Profile

Download (2.67 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
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.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.model;
11

    
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;
21

    
22
import org.eclipse.swt.dnd.ByteArrayTransfer;
23
import org.eclipse.swt.dnd.TransferData;
24

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

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

    
63
		byte[] bytes = null;
64

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

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

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

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