]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/reorg/TypedSourceTransfer.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui refactoring / org / eclipse / jdt / internal / ui / refactoring / reorg / TypedSourceTransfer.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
14 import java.io.ByteArrayOutputStream;
15 import java.io.DataInputStream;
16 import java.io.DataOutputStream;
17 import java.io.IOException;
18
19 import org.eclipse.swt.dnd.ByteArrayTransfer;
20 import org.eclipse.swt.dnd.TransferData;
21
22 import org.eclipse.core.runtime.Assert;
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 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         @Override
55         protected int[] getTypeIds() {
56                 return new int[] {TYPEID};
57         }
58
59         /* (non-Javadoc)
60          * Returns the type names.
61          *
62          * @return the list of type names
63          */
64         @Override
65         protected String[] getTypeNames() {
66                 return new String[] {TYPE_NAME};
67         }
68
69         /* (non-Javadoc)
70          * Method declared on Transfer.
71          */
72         @Override
73         protected void javaToNative(Object data, TransferData transferData) {
74                 if (! (data instanceof TypedSource[]))
75                         return;
76                 TypedSource[] sources = (TypedSource[]) data;
77
78                 /*
79                  * The serialization format is:
80                  *  (int) number of elements
81                  * Then, the following for each element:
82                  *  (int) type (see <code>IJavaElement</code>)
83                  *  (String) source of the element
84                  */
85
86                 try {
87                         ByteArrayOutputStream out = new ByteArrayOutputStream();
88                         DataOutputStream dataOut = new DataOutputStream(out);
89
90                         dataOut.writeInt(sources.length);
91
92                         for (int i = 0; i < sources.length; i++) {
93                                 writeJavaElement(dataOut, sources[i]);
94                         }
95
96                         dataOut.close();
97                         out.close();
98
99                         super.javaToNative(out.toByteArray(), transferData);
100                 } catch (IOException e) {
101                         //it's best to send nothing if there were problems
102                 }
103         }
104
105         /* (non-Javadoc)
106          * Method declared on Transfer.
107          */
108         @Override
109         protected Object nativeToJava(TransferData transferData) {
110
111                 byte[] bytes = (byte[]) super.nativeToJava(transferData);
112                 if (bytes == null)
113                         return null;
114                 DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
115                 try {
116                         int count = in.readInt();
117                         TypedSource[] results = new TypedSource[count];
118                         for (int i = 0; i < count; i++) {
119                                 results[i] = readJavaElement(in);
120                                 Assert.isNotNull(results[i]);
121                         }
122                         in.close();
123                         return results;
124                 } catch (IOException e) {
125                         return null;
126                 }
127         }
128
129         private static TypedSource readJavaElement(DataInputStream dataIn) throws IOException {
130                 int type= dataIn.readInt();
131                 String source= dataIn.readUTF();
132                 return TypedSource.create(source, type);
133         }
134
135         private static void writeJavaElement(DataOutputStream dataOut, TypedSource sourceReference) throws IOException {
136                 dataOut.writeInt(sourceReference.getType());
137                 dataOut.writeUTF(sourceReference.getSource());
138         }
139 }
140