blob: 7719e64abda0ea7d5fa507326bfa8f37dac739be [file] [log] [blame]
package org.eclipse.ui.part;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
import org.eclipse.swt.dnd.ByteArrayTransfer;
import org.eclipse.swt.dnd.TransferData;
import java.io.*;
/**
* This class can be used to transfer an instance of <code>PluginTransferData</code>
* between two parts in a workbench in a drop and drop operation.
* <p>
* In every drag and drop operation there is a <code>DragSource</code> and
* a <code>DropTarget</code>. When a drag occurs a <code>Transfer</code> is
* used to marshall the drag data from the source into a byte array. If a drop
* occurs another <code>Transfer</code> is used to marshall the byte array into
* drop data for the target.
* </p><p>
* A <code>PluginTransferData</code> contains the id of a drop action extension.
* If a drop occurs the extension is invoked to perform a drop action. As a benefit,
* the destination viewer doesn't need to have any knowledge of the items being
* dropped into it.
* </p><p>
* This class can be used for a <code>Viewer<code> or an SWT component directly.
* A singleton is provided which may be serially reused (see <code>getInstance</code>).
* It is not intended to be subclassed.
* </p>
*
* @see StructuredViewer
* @see DropTarget
* @see DragSource
*/
public class PluginTransfer extends ByteArrayTransfer {
private static final String TYPE_NAME = "pluggable-transfer-format";//$NON-NLS-1$
private static final int TYPEID = registerType(TYPE_NAME);
/**
* Singleton instance.
*/
private static PluginTransfer instance = new PluginTransfer();
/**
* Creates a new transfer object.
*/
private PluginTransfer() {
super();
}
/**
* Returns the singleton instance.
*
* @return the singleton instance
*/
public static PluginTransfer getInstance () {
return instance;
}
/* (non-Javadoc)
* Method declared on Transfer.
*/
protected int[] getTypeIds() {
return new int[] {TYPEID};
}
/* (non-Javadoc)
* Returns the type names.
*
* @return the list of type names
*/
protected String[] getTypeNames() {
return new String[] {TYPE_NAME};
}
/* (non-Javadoc)
* Method declared on Transfer.
*/
protected void javaToNative (Object data, TransferData transferData){
PluginTransferData realData = (PluginTransferData)data;
if (data == null) return;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(out);
dataOut.writeUTF(realData.getExtensionId());
dataOut.writeInt(realData.getData().length);
dataOut.write(realData.getData());
dataOut.close();
super.javaToNative(out.toByteArray(), transferData);
} catch (IOException e) {
e.printStackTrace();
}
}
/* (non-Javadoc)
* Method declared on Transfer.
*/
protected Object nativeToJava(TransferData transferData) {
try {
byte[] bytes = (byte[]) super.nativeToJava(transferData);
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
DataInputStream dataIn = new DataInputStream(in);
String extensionName = dataIn.readUTF();
int len = dataIn.readInt();
byte[] pluginData = new byte[len];
dataIn.readFully(pluginData);
return new PluginTransferData(extensionName, pluginData);
} catch (IOException e) {
e.printStackTrace();
}
//can't get here
return null;
}
}