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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring.reorg;
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;
19 import org.eclipse.swt.dnd.ByteArrayTransfer;
20 import org.eclipse.swt.dnd.TransferData;
22 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.jdt.internal.corext.refactoring.TypedSource;
26 public class TypedSourceTransfer extends ByteArrayTransfer {
31 private static final TypedSourceTransfer fgInstance = new TypedSourceTransfer();
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$
37 private static final int TYPEID = registerType(TYPE_NAME);
39 private TypedSourceTransfer() {
43 * Returns the singleton instance.
45 * @return the singleton instance
47 public static TypedSourceTransfer getInstance() {
52 * Method declared on Transfer.
55 protected int[] getTypeIds() {
56 return new int[] {TYPEID};
60 * Returns the type names.
62 * @return the list of type names
65 protected String[] getTypeNames() {
66 return new String[] {TYPE_NAME};
70 * Method declared on Transfer.
73 protected void javaToNative(Object data, TransferData transferData) {
74 if (! (data instanceof TypedSource[]))
76 TypedSource[] sources = (TypedSource[]) data;
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
87 ByteArrayOutputStream out = new ByteArrayOutputStream();
88 DataOutputStream dataOut = new DataOutputStream(out);
90 dataOut.writeInt(sources.length);
92 for (int i = 0; i < sources.length; i++) {
93 writeJavaElement(dataOut, sources[i]);
99 super.javaToNative(out.toByteArray(), transferData);
100 } catch (IOException e) {
101 //it's best to send nothing if there were problems
106 * Method declared on Transfer.
109 protected Object nativeToJava(TransferData transferData) {
111 byte[] bytes = (byte[]) super.nativeToJava(transferData);
114 DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
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]);
124 } catch (IOException e) {
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);
135 private static void writeJavaElement(DataOutputStream dataOut, TypedSource sourceReference) throws IOException {
136 dataOut.writeInt(sourceReference.getType());
137 dataOut.writeUTF(sourceReference.getSource());