KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > reorg > JavaElementTransfer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.corext.refactoring.reorg;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.DataInputStream JavaDoc;
16 import java.io.DataOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.JavaCore;
21
22 import org.eclipse.swt.dnd.ByteArrayTransfer;
23 import org.eclipse.swt.dnd.TransferData;
24
25 public class JavaElementTransfer extends ByteArrayTransfer {
26
27     /**
28      * Singleton instance.
29      */

30     private static final JavaElementTransfer fInstance= new JavaElementTransfer();
31
32     // Create a unique ID to make sure that different Eclipse
33
// applications use different "types" of <code>JavaElementTransfer</code>
34
private static final String JavaDoc TYPE_NAME= "java-element-transfer-format:" + System.currentTimeMillis() + ":" + fInstance.hashCode(); //$NON-NLS-2$//$NON-NLS-1$
35

36     private static final int TYPEID= registerType(TYPE_NAME);
37
38     private JavaElementTransfer() {
39     }
40
41     /**
42      * Returns the singleton instance.
43      *
44      * @return the singleton instance
45      */

46     public static JavaElementTransfer getInstance() {
47         return fInstance;
48     }
49
50     /* (non-Javadoc)
51      * Method declared on Transfer.
52      */

53     protected int[] getTypeIds() {
54         return new int[] { TYPEID };
55     }
56     /* (non-Javadoc)
57      * Returns the type names.
58      *
59      * @return the list of type names
60      */

61     protected String JavaDoc[] getTypeNames() {
62         return new String JavaDoc[] { TYPE_NAME };
63     }
64
65     /* (non-Javadoc)
66      * @see org.eclipse.swt.dnd.Transfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
67      */

68     protected void javaToNative(Object JavaDoc data, TransferData transferData) {
69         if (!(data instanceof IJavaElement[]))
70             return;
71
72         IJavaElement[] javaElements= (IJavaElement[]) data;
73         /*
74          * The element serialization format is:
75          * (int) number of element
76          * Then, the following for each element:
77          * (String) handle identifier
78          */

79
80         try {
81             ByteArrayOutputStream JavaDoc out= new ByteArrayOutputStream JavaDoc();
82             DataOutputStream JavaDoc dataOut= new DataOutputStream JavaDoc(out);
83
84             //write the number of elements
85
dataOut.writeInt(javaElements.length);
86
87             //write each element
88
for (int i= 0; i < javaElements.length; i++) {
89                 writeJavaElement(dataOut, javaElements[i]);
90             }
91
92             //cleanup
93
dataOut.close();
94             out.close();
95             byte[] bytes= out.toByteArray();
96             super.javaToNative(bytes, transferData);
97         } catch (IOException JavaDoc e) {
98             //it's best to send nothing if there were problems
99
}
100     }
101
102     /* (non-Javadoc)
103      * @see org.eclipse.swt.dnd.Transfer#nativeToJava(org.eclipse.swt.dnd.TransferData)
104      */

105     protected Object JavaDoc nativeToJava(TransferData transferData) {
106         /*
107          * The element serialization format is:
108          * (int) number of element
109          * Then, the following for each element:
110          * (String) handle identifier
111          */

112
113         byte[] bytes= (byte[]) super.nativeToJava(transferData);
114         if (bytes == null)
115             return null;
116         DataInputStream JavaDoc in= new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
117         try {
118             int count= in.readInt();
119             IJavaElement[] results= new IJavaElement[count];
120             for (int i= 0; i < count; i++) {
121                 results[i]= readJavaElement(in);
122             }
123             return results;
124         } catch (IOException JavaDoc e) {
125             return null;
126         }
127     }
128
129     private IJavaElement readJavaElement(DataInputStream JavaDoc dataIn) throws IOException JavaDoc {
130         String JavaDoc handleIdentifier= dataIn.readUTF();
131         return JavaCore.create(handleIdentifier);
132     }
133
134     private static void writeJavaElement(DataOutputStream JavaDoc dataOut, IJavaElement element) throws IOException JavaDoc {
135         dataOut.writeUTF(element.getHandleIdentifier());
136     }
137 }
138
Popular Tags