Debian Codegraph @

plugins/org.eclipse.emf.edit.ui/src/org/eclipse/emf/edit/ui/dnd/LocalTransfer.java

raw
1 /**
2 * Copyright (c) 2002-2006 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 v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.edit.ui.dnd;
12
13
14 import org.eclipse.swt.dnd.ByteArrayTransfer;
15 import org.eclipse.swt.dnd.TransferData;
16
17
18 /**
19 * This derived implementation of a byte array transfer short circuits the transfer process
20 * so that a local transfer does not serialize the object
21 * and hence can and will return the original object, not just a clone.
22 * You only really need ever know about {@link #getInstance LocalTransfer.getInstance()},
23 * so that you can include it in when adding drag support to a viewer.
24 * See {@link EditingDomainViewerDropAdapter} and {@link ViewerDragAdapter} for more details.
25 * <p>
26 * As an added guard, the time is recorded and serialized in javaToNative
27 * to that native to java can ensure that it's returns the value that was really to have been transferred.
28 */
29 public class LocalTransfer extends ByteArrayTransfer
30 {
31 /**
32 * This is the register transfer type name.
33 */
34 protected static final String TYPE_NAME = "local-transfer-format";
35
36 /**
37 * This is the ID that is registered to the name.
38 */
39 protected static final int TYPE_ID = registerType(TYPE_NAME);
40
41 /**
42 * This is initialized and returned by {@link #getInstance}.
43 */
44 protected static LocalTransfer instance;
45
46 /**
47 * This returns the one instance of this transfer agent.
48 */
49 public static LocalTransfer getInstance()
50 {
51 if (instance == null)
52 {
53 instance = new LocalTransfer();
54 }
55
56 return instance;
57 }
58
59 /**
60 * This records the time at which the transfer data was recorded.
61 */
62 protected long startTime;
63
64 /**
65 * This records the data being transferred.
66 */
67 protected Object object;
68
69 /**
70 * This creates an instance; typically you get one from {@link #getInstance}.
71 */
72 protected LocalTransfer()
73 {
74 super();
75 }
76
77 /**
78 * This returns the transfer IDs that this agent supports.
79 */
80 @Override
81 protected int[] getTypeIds()
82 {
83 return new int[] { TYPE_ID };
84 }
85
86 /**
87 * This returns the transfer names that this agent supports.
88 */
89 @Override
90 public String[] getTypeNames()
91 {
92 return new String[] { TYPE_NAME };
93 }
94
95 /**
96 * This records the object and current time and encodes only the current time into the transfer data.
97 */
98 @Override
99 public void javaToNative(Object object, TransferData transferData)
100 {
101 startTime = System.currentTimeMillis();
102 this.object = object;
103 if (transferData != null)
104 {
105 super.javaToNative(String.valueOf(startTime).getBytes(), transferData);
106 }
107 }
108
109 /**
110 * This decodes the time of the transfer and returns the recorded the object if the recorded time and the decoded time match.
111 */
112 @Override
113 public Object nativeToJava(TransferData transferData)
114 {
115 byte[] bytes = (byte[])super.nativeToJava(transferData);
116 if (bytes == null) return null;
117
118 try
119 {
120 long startTime = Long.parseLong(new String(bytes));
121 return this.startTime == startTime ? object : null;
122 }
123 catch (NumberFormatException exception)
124 {
125 return null;
126 }
127 }
128 }