1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.gef.dnd;
|
12
|
|
13
|
import org.eclipse.swt.dnd.ByteArrayTransfer;
|
14
|
import org.eclipse.swt.dnd.TransferData;
|
15
|
|
16
|
/**
|
17
|
* Holds on to the object being transferred in a field so that
|
18
|
* DropTargetListeners can know what's being dragged before the drop occurs. The
|
19
|
* object isn't converted to bytes, so this Transfer will only work when
|
20
|
* dragging within the same instance of Eclipse. Subclasses should maintain a
|
21
|
* single instance of their Transfer and provide a static method to obtain that
|
22
|
* instance.
|
23
|
*/
|
24
|
public abstract class SimpleObjectTransfer extends ByteArrayTransfer {
|
25
|
|
26
|
private Object object;
|
27
|
private long startTime;
|
28
|
|
29
|
/**
|
30
|
* Returns the Object.
|
31
|
*
|
32
|
* @return The Object
|
33
|
*/
|
34
|
public Object getObject() {
|
35
|
return object;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* The data object is not converted to bytes. It is held onto in a field.
|
40
|
* Instead, a checksum is written out to prevent unwanted drags across
|
41
|
* mulitple running copies of Eclipse.
|
42
|
*
|
43
|
* @see org.eclipse.swt.dnd.Transfer#javaToNative(Object, TransferData)
|
44
|
*/
|
45
|
public void javaToNative(Object object, TransferData transferData) {
|
46
|
setObject(object);
|
47
|
startTime = System.currentTimeMillis();
|
48
|
if (transferData != null)
|
49
|
super.javaToNative(String.valueOf(startTime).getBytes(),
|
50
|
transferData);
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* The data object is not converted to bytes. It is held onto in a field.
|
55
|
* Instead, a checksum is written out to prevent unwanted drags across
|
56
|
* mulitple running. copies of Eclipse.
|
57
|
*
|
58
|
* @see org.eclipse.swt.dnd.Transfer#nativeToJava(TransferData)
|
59
|
*/
|
60
|
public Object nativeToJava(TransferData transferData) {
|
61
|
byte bytes[] = (byte[]) super.nativeToJava(transferData);
|
62
|
if (bytes == null) {
|
63
|
return null;
|
64
|
}
|
65
|
long startTime = Long.parseLong(new String(bytes));
|
66
|
return (this.startTime == startTime) ? getObject() : null;
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Sets the Object.
|
71
|
*
|
72
|
* @param obj
|
73
|
* The Object
|
74
|
*/
|
75
|
public void setObject(Object obj) {
|
76
|
object = obj;
|
77
|
}
|
78
|
|
79
|
}
|