About to add serious annotation functionality - backup first.
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / NameTransfer.java
1 package eu.etaxonomy.taxeditor.actions;
2
3 import org.eclipse.swt.dnd.ByteArrayTransfer;
4 import org.eclipse.swt.dnd.TransferData;
5
6 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
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 NameTransfer extends ByteArrayTransfer {
16
17 private static final String TYPENAME = "name_type";
18 private static final int NAMETYPEID = registerType (TYPENAME);
19 private static final NameTransfer INSTANCE = new NameTransfer();
20
21 /*
22 * The object associated with this transfer event
23 */
24 private TaxonNameBase name;
25
26 /*
27 * Returns the singleton
28 */
29 public static NameTransfer getInstance() {
30 return INSTANCE;
31 }
32 /*
33 * Set transfer data for local use
34 */
35 public void setName(TaxonNameBase name) {
36 this.name = name;
37 }
38 /*
39 * Returns the local transfer data
40 */
41 public TaxonNameBase getName() {
42 return name;
43 }
44
45 /*
46 * The type ID is used to identify this transfer
47 */
48 @Override
49 protected int[] getTypeIds() {
50 return new int[] { NAMETYPEID };
51 }
52
53 @Override
54 protected String[] getTypeNames() {
55 return new String[] { TYPENAME } ;
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 = TYPENAME.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 name;
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 || !TYPENAME.equals(new String((byte[]) result));
82 }
83 }