KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > dnd > LocalTransfer


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: LocalTransfer.java,v 1.2 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.dnd;
18
19
20 import org.eclipse.swt.dnd.ByteArrayTransfer;
21 import org.eclipse.swt.dnd.TransferData;
22
23
24 /**
25  * This derived implementation of a byte array transfer short circuits the transfer process
26  * so that a local transfer does not serialize the object
27  * and hence can and will return the original object, not just a clone.
28  * You only really need ever know about {@link #getInstance LocalTransfer.getInstance()},
29  * so that you can include it in when adding drag support to a viewer.
30  * See {@link EditingDomainViewerDropAdapter} and {@link ViewerDragAdapter} for more details.
31  * <p>
32  * As an addded guard, the time is recorded and serialized in javaToNative
33  * to that navive to java can ensure that it's returns the value that was really to have been transferred.
34  */

35 public class LocalTransfer extends ByteArrayTransfer
36 {
37   /**
38    * This is the register transfer type name.
39    */

40   protected static final String JavaDoc TYPE_NAME = "local-transfer-format";
41
42   /**
43    * This is the ID that is registered to the name.
44    */

45   protected static final int TYPE_ID = registerType(TYPE_NAME);
46
47   /**
48    * This is initialized and returned by {@link #getInstance}.
49    */

50   protected static LocalTransfer instance;
51
52   /**
53    * This returns the one instance of this transfer agent.
54    */

55   public static LocalTransfer getInstance()
56   {
57     if (instance == null)
58     {
59       instance = new LocalTransfer();
60     }
61
62     return instance;
63   }
64
65   /**
66    * This records the time at which the transfer data was recorded.
67    */

68   protected long startTime;
69
70   /**
71    * This records the data being transferred.
72    */

73   protected Object JavaDoc object;
74
75   /**
76    * This creates an instance; typically you get one from {@link #getInstance}.
77    */

78   protected LocalTransfer()
79   {
80   }
81
82   /**
83    * This returns the transfer ids that this agent supports.
84    */

85   protected int[] getTypeIds()
86   {
87     return new int[] { TYPE_ID };
88   }
89
90   /**
91    * This returns the transfer names that this agent supports.
92    */

93   public String JavaDoc[] getTypeNames()
94   {
95     return new String JavaDoc[] { TYPE_NAME };
96   }
97
98   /**
99    * This records the object and current time and encodes only the current time into the transfer data.
100    */

101   public void javaToNative(Object JavaDoc object, TransferData transferData)
102   {
103     startTime = System.currentTimeMillis();
104     this.object = object;
105     if (transferData != null)
106     {
107       super.javaToNative(String.valueOf(startTime).getBytes(), transferData);
108     }
109   }
110
111   /**
112    * This decodes the time of the transfer and returns the recorded the object if the recorded time and the decoded time match.
113    */

114   public Object JavaDoc nativeToJava(TransferData transferData)
115   {
116     byte[] bytes = (byte[])super.nativeToJava(transferData);
117     if (bytes == null) return null;
118     
119     try
120     {
121       long startTime = Long.valueOf(new String JavaDoc(bytes)).longValue();
122       return this.startTime == startTime ? object : null;
123     }
124     catch (NumberFormatException JavaDoc exception)
125     {
126      return null;
127     }
128   }
129 }
130
Popular Tags