1
|
package eu.etaxonomy.taxeditor.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
|
* @version $Id: $
|
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
|
/**
|
30
|
* <p>getInstance</p>
|
31
|
*
|
32
|
* @return a {@link eu.etaxonomy.taxeditor.model.TaxonTransfer} object.
|
33
|
*/
|
34
|
public static TaxonTransfer getInstance() {
|
35
|
return INSTANCE;
|
36
|
}
|
37
|
/*
|
38
|
* Set transfer data for local use
|
39
|
*/
|
40
|
/**
|
41
|
* <p>setTaxon</p>
|
42
|
*
|
43
|
* @param taxon a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
|
44
|
*/
|
45
|
public void setTaxon(TaxonBase<?> taxon) {
|
46
|
this.taxonBase = taxon;
|
47
|
}
|
48
|
/*
|
49
|
* Returns the local transfer data
|
50
|
*/
|
51
|
/**
|
52
|
* <p>getTaxon</p>
|
53
|
*
|
54
|
* @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
|
55
|
*/
|
56
|
public TaxonBase<?> getTaxon() {
|
57
|
return taxonBase;
|
58
|
}
|
59
|
|
60
|
/*
|
61
|
* The type ID is used to identify this transfer
|
62
|
*/
|
63
|
/** {@inheritDoc} */
|
64
|
@Override
|
65
|
protected int[] getTypeIds() {
|
66
|
return new int[] { TAXONTYPEID };
|
67
|
}
|
68
|
|
69
|
/** {@inheritDoc} */
|
70
|
@Override
|
71
|
protected String[] getTypeNames() {
|
72
|
return new String[] { TAXONTYPENAME } ;
|
73
|
}
|
74
|
|
75
|
/** {@inheritDoc} */
|
76
|
@Override
|
77
|
protected void javaToNative(Object object, TransferData transferData) {
|
78
|
// No encoding needed since this is a hardcoded string read and written
|
79
|
// in the same process.
|
80
|
// See nativeToJava below
|
81
|
byte[] check = TAXONTYPENAME.getBytes();
|
82
|
super.javaToNative(check, transferData);
|
83
|
}
|
84
|
|
85
|
/** {@inheritDoc} */
|
86
|
@Override
|
87
|
protected Object nativeToJava(TransferData transferData) {
|
88
|
Object result = super.nativeToJava(transferData);
|
89
|
if (isInvalidNativeType(result)) {
|
90
|
throw new RuntimeException(); //$NON-NLS-1$
|
91
|
}
|
92
|
return taxonBase;
|
93
|
}
|
94
|
|
95
|
private boolean isInvalidNativeType(Object result) {
|
96
|
// No encoding needed since this is a hardcoded string read and written
|
97
|
// in the same process.
|
98
|
// See javaToNative above
|
99
|
return !(result instanceof byte[])
|
100
|
|| !TAXONTYPENAME.equals(new String((byte[]) result));
|
101
|
}
|
102
|
}
|