p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / SynonymRelationshipTransfer.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.taxon.SynonymRelationship;
7 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
8
9 /**
10 * For drag and drop - a drag widget with this transfer type
11 * can only be dropped onto a drop widget with the same type.
12 *
13 * @author p.ciardelli
14 *
15 */
16 public class SynonymRelationshipTransfer extends ByteArrayTransfer {
17
18 private static final String RELATIONTYPENAME = "relation_type";
19 private static final int RELATIONTYPEID = registerType (RELATIONTYPENAME);
20 private static final SynonymRelationshipTransfer INSTANCE = new SynonymRelationshipTransfer();
21
22 /*
23 * The object associated with this transfer event
24 */
25 private SynonymRelationship synonymRelationship;
26
27 /*
28 * Returns the singleton
29 */
30 public static SynonymRelationshipTransfer getInstance() {
31 return INSTANCE;
32 }
33 /*
34 * Set transfer data for local use
35 */
36 public void setSynonymRelationship(SynonymRelationship synonymRelationship) {
37 this.synonymRelationship = synonymRelationship;
38 }
39 /*
40 * Returns the local transfer data
41 */
42 public SynonymRelationship getSynonymRelationship() {
43 return synonymRelationship;
44 }
45
46 /*
47 * The type ID is used to identify this transfer
48 */
49 @Override
50 protected int[] getTypeIds() {
51 return new int[] { RELATIONTYPEID };
52 }
53
54 @Override
55 protected String[] getTypeNames() {
56 return new String[] { RELATIONTYPENAME } ;
57 }
58
59 @Override
60 protected void javaToNative(Object object, TransferData transferData) {
61 // No encoding needed since this is a hardcoded string read and written
62 // in the same process.
63 // See nativeToJava below
64 byte[] check = RELATIONTYPENAME.getBytes();
65 super.javaToNative(check, transferData);
66 }
67
68 @Override
69 protected Object nativeToJava(TransferData transferData) {
70 Object result = super.nativeToJava(transferData);
71 if (isInvalidNativeType(result)) {
72 throw new RuntimeException(); //$NON-NLS-1$
73 }
74 return synonymRelationship;
75 }
76
77 private boolean isInvalidNativeType(Object result) {
78 // No encoding needed since this is a hardcoded string read and written
79 // in the same process.
80 // See javaToNative above
81 return !(result instanceof byte[])
82 || !RELATIONTYPENAME.equals(new String((byte[]) result));
83 }
84 }