KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > TypedSourceTransfer


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 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.ui.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.core.runtime.Assert;
20
21 import org.eclipse.swt.dnd.ByteArrayTransfer;
22 import org.eclipse.swt.dnd.TransferData;
23
24 import org.eclipse.jdt.internal.corext.refactoring.TypedSource;
25
26 public class TypedSourceTransfer extends ByteArrayTransfer {
27
28     /**
29      * Singleton instance.
30      */

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

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

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

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

63     protected String JavaDoc[] getTypeNames() {
64         return new String JavaDoc[] {TYPE_NAME};
65     }
66
67     /* (non-Javadoc)
68      * Method declared on Transfer.
69      */

70     protected void javaToNative(Object JavaDoc data, TransferData transferData) {
71         if (! (data instanceof TypedSource[]))
72             return;
73         TypedSource[] sources = (TypedSource[]) data;
74
75         /*
76          * The serialization format is:
77          * (int) number of elements
78          * Then, the following for each element:
79          * (int) type (see <code>IJavaElement</code>)
80          * (String) source of the element
81          */

82         
83         try {
84             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
85             DataOutputStream JavaDoc dataOut = new DataOutputStream JavaDoc(out);
86
87             dataOut.writeInt(sources.length);
88
89             for (int i = 0; i < sources.length; i++) {
90                 writeJavaElement(dataOut, sources[i]);
91             }
92
93             dataOut.close();
94             out.close();
95
96             super.javaToNative(out.toByteArray(), transferData);
97         } catch (IOException JavaDoc e) {
98             //it's best to send nothing if there were problems
99
}
100     }
101
102     /* (non-Javadoc)
103      * Method declared on Transfer.
104      */

105     protected Object JavaDoc nativeToJava(TransferData transferData) {
106     
107         byte[] bytes = (byte[]) super.nativeToJava(transferData);
108         if (bytes == null)
109             return null;
110         DataInputStream JavaDoc in = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
111         try {
112             int count = in.readInt();
113             TypedSource[] results = new TypedSource[count];
114             for (int i = 0; i < count; i++) {
115                 results[i] = readJavaElement(in);
116                 Assert.isNotNull(results[i]);
117             }
118             in.close();
119             return results;
120         } catch (IOException JavaDoc e) {
121             return null;
122         }
123     }
124
125     private static TypedSource readJavaElement(DataInputStream JavaDoc dataIn) throws IOException JavaDoc {
126         int type= dataIn.readInt();
127         String JavaDoc source= dataIn.readUTF();
128         return TypedSource.create(source, type);
129     }
130
131     private static void writeJavaElement(DataOutputStream JavaDoc dataOut, TypedSource sourceReference) throws IOException JavaDoc {
132         dataOut.writeInt(sourceReference.getType());
133         dataOut.writeUTF(sourceReference.getSource());
134     }
135 }
136
137
Popular Tags