001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.reorg;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.DataInputStream;
015: import java.io.DataOutputStream;
016: import java.io.IOException;
017:
018: import org.eclipse.jdt.core.IJavaElement;
019: import org.eclipse.jdt.core.JavaCore;
020:
021: import org.eclipse.swt.dnd.ByteArrayTransfer;
022: import org.eclipse.swt.dnd.TransferData;
023:
024: public class JavaElementTransfer extends ByteArrayTransfer {
025:
026: /**
027: * Singleton instance.
028: */
029: private static final JavaElementTransfer fInstance = new JavaElementTransfer();
030:
031: // Create a unique ID to make sure that different Eclipse
032: // applications use different "types" of <code>JavaElementTransfer</code>
033: private static final String TYPE_NAME = "java-element-transfer-format:" + System.currentTimeMillis() + ":" + fInstance.hashCode(); //$NON-NLS-2$//$NON-NLS-1$
034:
035: private static final int TYPEID = registerType(TYPE_NAME);
036:
037: private JavaElementTransfer() {
038: }
039:
040: /**
041: * Returns the singleton instance.
042: *
043: * @return the singleton instance
044: */
045: public static JavaElementTransfer getInstance() {
046: return fInstance;
047: }
048:
049: /* (non-Javadoc)
050: * Method declared on Transfer.
051: */
052: protected int[] getTypeIds() {
053: return new int[] { TYPEID };
054: }
055:
056: /* (non-Javadoc)
057: * Returns the type names.
058: *
059: * @return the list of type names
060: */
061: protected String[] getTypeNames() {
062: return new String[] { TYPE_NAME };
063: }
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.swt.dnd.Transfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
067: */
068: protected void javaToNative(Object data, TransferData transferData) {
069: if (!(data instanceof IJavaElement[]))
070: return;
071:
072: IJavaElement[] javaElements = (IJavaElement[]) data;
073: /*
074: * The element serialization format is:
075: * (int) number of element
076: * Then, the following for each element:
077: * (String) handle identifier
078: */
079:
080: try {
081: ByteArrayOutputStream out = new ByteArrayOutputStream();
082: DataOutputStream dataOut = new DataOutputStream(out);
083:
084: //write the number of elements
085: dataOut.writeInt(javaElements.length);
086:
087: //write each element
088: for (int i = 0; i < javaElements.length; i++) {
089: writeJavaElement(dataOut, javaElements[i]);
090: }
091:
092: //cleanup
093: dataOut.close();
094: out.close();
095: byte[] bytes = out.toByteArray();
096: super .javaToNative(bytes, transferData);
097: } catch (IOException e) {
098: //it's best to send nothing if there were problems
099: }
100: }
101:
102: /* (non-Javadoc)
103: * @see org.eclipse.swt.dnd.Transfer#nativeToJava(org.eclipse.swt.dnd.TransferData)
104: */
105: protected Object 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 in = new DataInputStream(
117: new ByteArrayInputStream(bytes));
118: try {
119: int count = in.readInt();
120: IJavaElement[] results = new IJavaElement[count];
121: for (int i = 0; i < count; i++) {
122: results[i] = readJavaElement(in);
123: }
124: return results;
125: } catch (IOException e) {
126: return null;
127: }
128: }
129:
130: private IJavaElement readJavaElement(DataInputStream dataIn)
131: throws IOException {
132: String handleIdentifier = dataIn.readUTF();
133: return JavaCore.create(handleIdentifier);
134: }
135:
136: private static void writeJavaElement(DataOutputStream dataOut,
137: IJavaElement element) throws IOException {
138: dataOut.writeUTF(element.getHandleIdentifier());
139: }
140: }
|