remove deprecated preference page for taxon search
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / TaxonTransfer.java
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 */
14 public class TaxonTransfer extends ByteArrayTransfer {
15
16 private static final String TAXONTYPENAME = "taxon_type";
17 private static final int TAXONTYPEID = registerType (TAXONTYPENAME);
18 private static final TaxonTransfer INSTANCE = new TaxonTransfer();
19
20 /*
21 * The object associated with this transfer event
22 */
23 private TaxonBase<?> taxonBase;
24
25 /*
26 * Returns the singleton
27 */
28 /**
29 * <p>getInstance</p>
30 *
31 * @return a {@link eu.etaxonomy.taxeditor.model.TaxonTransfer} object.
32 */
33 public static TaxonTransfer getInstance() {
34 return INSTANCE;
35 }
36 /*
37 * Set transfer data for local use
38 */
39 /**
40 * <p>setTaxon</p>
41 *
42 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
43 */
44 public void setTaxon(TaxonBase<?> taxon) {
45 this.taxonBase = taxon;
46 }
47 /*
48 * Returns the local transfer data
49 */
50 /**
51 * <p>getTaxon</p>
52 *
53 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
54 */
55 public TaxonBase<?> getTaxon() {
56 return taxonBase;
57 }
58
59 /*
60 * The type ID is used to identify this transfer
61 */
62 @Override
63 protected int[] getTypeIds() {
64 return new int[] { TAXONTYPEID };
65 }
66
67 @Override
68 protected String[] getTypeNames() {
69 return new String[] { TAXONTYPENAME } ;
70 }
71
72 @Override
73 protected void javaToNative(Object object, TransferData transferData) {
74 // No encoding needed since this is a hardcoded string read and written
75 // in the same process.
76 // See nativeToJava below
77 byte[] check = TAXONTYPENAME.getBytes();
78 super.javaToNative(check, transferData);
79 }
80
81 @Override
82 protected Object nativeToJava(TransferData transferData) {
83 Object result = super.nativeToJava(transferData);
84 if (isInvalidNativeType(result)) {
85 throw new RuntimeException(); //$NON-NLS-1$
86 }
87 return taxonBase;
88 }
89
90 private boolean isInvalidNativeType(Object result) {
91 // No encoding needed since this is a hardcoded string read and written
92 // in the same process.
93 // See javaToNative above
94 return !(result instanceof byte[])
95 || !TAXONTYPENAME.equals(new String((byte[]) result));
96 }
97 }