Added a bunch of functionality that will be needed when opening the editor with an...
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / WidgetTransfer.java
1 package eu.etaxonomy.taxeditor.editor;
2
3 import org.eclipse.swt.dnd.ByteArrayTransfer;
4 import org.eclipse.swt.dnd.TransferData;
5 import org.eclipse.swt.widgets.Widget;
6
7 /**
8 * For drag and drop - a drag widget with this transfer type can only be dropped onto
9 * a drop widget with the same type.
10 *
11 * @author p.ciardelli
12 *
13 */
14 public class WidgetTransfer extends ByteArrayTransfer {
15
16 private static final String WIDGETTYPENAME = "widget_type";
17 private static final int WIDGETTYPEID = registerType (WIDGETTYPENAME);
18 private static final WidgetTransfer INSTANCE = new WidgetTransfer();
19
20 /*
21 * The object associated with this transfer event
22 */
23 private Widget widget;
24
25 /*
26 * Returns the singleton
27 */
28 public static WidgetTransfer getInstance() {
29 return INSTANCE;
30 }
31 /*
32 * Set transfer data for local use
33 */
34 public void setWidget(Widget widget) {
35 this.widget = widget;
36 }
37 /*
38 * Returns the local transfer data
39 */
40 public Widget getWidget() {
41 return widget;
42 }
43
44 /*
45 * The type ID is used to identify this transfer
46 */
47 @Override
48 protected int[] getTypeIds() {
49 return new int[] { WIDGETTYPEID };
50 }
51
52 @Override
53 protected String[] getTypeNames() {
54 return new String[] { WIDGETTYPENAME } ;
55 }
56
57 @Override
58 protected void javaToNative(Object object, TransferData transferData) {
59 // No encoding needed since this is a hardcoded string read and written
60 // in the same process.
61 // See nativeToJava below
62 byte[] check = WIDGETTYPENAME.getBytes();
63 super.javaToNative(check, transferData);
64 }
65
66 @Override
67 protected Object nativeToJava(TransferData transferData) {
68 Object result = super.nativeToJava(transferData);
69 if (isInvalidNativeType(result)) {
70 throw new RuntimeException(); //$NON-NLS-1$
71 }
72 return widget;
73 }
74
75 private boolean isInvalidNativeType(Object result) {
76 // No encoding needed since this is a hardcoded string read and written
77 // in the same process.
78 // See javaToNative above
79 return !(result instanceof byte[])
80 || !WIDGETTYPENAME.equals(new String((byte[]) result));
81 }
82 }