Project

General

Profile

Download (2.1 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.store.model;
2

    
3
import org.eclipse.swt.dnd.ByteArrayTransfer;
4
import org.eclipse.swt.dnd.TransferData;
5

    
6
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
7

    
8
/**
9
 * For drag and drop - a drag widget with this transfer type can only be dropped onto
10
 *  a drop widget with the same type.
11
 *    
12
 * @author p.ciardelli
13
 *
14
 */
15
public class TaxonTransfer extends ByteArrayTransfer {
16

    
17
	private static final String TAXONTYPENAME = "taxon_type";
18
	private static final int TAXONTYPEID = registerType (TAXONTYPENAME);		
19
	private static final TaxonTransfer INSTANCE = new TaxonTransfer();
20
	
21
	/*
22
	 * The object associated with this transfer event
23
	 */
24
	private TaxonBase taxonBase;
25
		
26
	/*
27
	 * Returns the singleton
28
	 */
29
	public static TaxonTransfer getInstance() {
30
		return INSTANCE;
31
	}
32
	/*
33
	 * Set transfer data for local use
34
	 */
35
	public void setTaxon(TaxonBase taxon) {
36
		this.taxonBase = taxon;
37
	}
38
	/*
39
	 * Returns the local transfer data
40
	 */
41
	public TaxonBase getTaxon() {
42
		return taxonBase;
43
	}
44
	
45
	/* 
46
	 * The type ID is used to identify this transfer
47
	 */
48
	@Override
49
	protected int[] getTypeIds() {
50
		return new int[] { TAXONTYPEID };
51
	}
52

    
53
	@Override
54
	protected String[] getTypeNames() {
55
		return new String[] { TAXONTYPENAME } ;
56
	}
57

    
58
	@Override
59
	protected void javaToNative(Object object, TransferData transferData) {
60
		// No encoding needed since this is a hardcoded string read and written
61
		// in the same process.
62
		// See nativeToJava below
63
		byte[] check = TAXONTYPENAME.getBytes();
64
		super.javaToNative(check, transferData);
65
	}
66

    
67
	@Override
68
	protected Object nativeToJava(TransferData transferData) {
69
		Object result = super.nativeToJava(transferData);
70
		if (isInvalidNativeType(result)) {
71
			throw new RuntimeException(); //$NON-NLS-1$
72
		}
73
		return taxonBase;
74
	}
75
	
76
	private boolean isInvalidNativeType(Object result) {
77
		// No encoding needed since this is a hardcoded string read and written
78
		// in the same process.
79
		// See javaToNative above
80
		return !(result instanceof byte[])
81
				|| !TAXONTYPENAME.equals(new String((byte[]) result));
82
	}
83
}
(15-15/18)