001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ui.part;
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.swt.dnd.ByteArrayTransfer;
019: import org.eclipse.swt.dnd.TransferData;
020:
021: /**
022: * This class can be used to transfer an instance of <code>PluginTransferData</code>
023: * between two parts in a workbench in a drag and drop operation.
024: * <p>
025: * In every drag and drop operation there is a <code>DragSource</code> and
026: * a <code>DropTarget</code>. When a drag occurs a <code>Transfer</code> is
027: * used to marshall the drag data from the source into a byte array. If a drop
028: * occurs another <code>Transfer</code> is used to marshall the byte array into
029: * drop data for the target.
030: * </p><p>
031: * A <code>PluginTransferData</code> contains the id of a drop action extension.
032: * If a drop occurs the extension is invoked to perform a drop action. As a benefit,
033: * the destination viewer doesn't need to have any knowledge of the items being
034: * dropped into it.
035: * </p><p>
036: * This class can be used for a <code>Viewer<code> or an SWT component directly.
037: * A singleton is provided which may be serially reused (see <code>getInstance</code>).
038: * It is not intended to be subclassed.
039: * </p>
040: *
041: * @see org.eclipse.jface.viewers.StructuredViewer
042: * @see org.eclipse.swt.dnd.DropTarget
043: * @see org.eclipse.swt.dnd.DragSource
044: */
045: public class PluginTransfer extends ByteArrayTransfer {
046:
047: private static final String TYPE_NAME = "pluggable-transfer-format";//$NON-NLS-1$
048:
049: private static final int TYPEID = registerType(TYPE_NAME);
050:
051: /**
052: * Singleton instance.
053: */
054: private static PluginTransfer instance = new PluginTransfer();
055:
056: /**
057: * Creates a new transfer object.
058: */
059: private PluginTransfer() {
060: super ();
061: }
062:
063: /**
064: * Returns the singleton instance.
065: *
066: * @return the singleton instance
067: */
068: public static PluginTransfer getInstance() {
069: return instance;
070: }
071:
072: /* (non-Javadoc)
073: * Method declared on Transfer.
074: */
075: protected int[] getTypeIds() {
076: return new int[] { TYPEID };
077: }
078:
079: /* (non-Javadoc)
080: * Returns the type names.
081: *
082: * @return the list of type names
083: */
084: protected String[] getTypeNames() {
085: return new String[] { TYPE_NAME };
086: }
087:
088: /* (non-Javadoc)
089: * Method declared on Transfer.
090: */
091: protected void javaToNative(Object data, TransferData transferData) {
092: PluginTransferData realData = (PluginTransferData) data;
093: if (data == null) {
094: return;
095: }
096: try {
097: ByteArrayOutputStream out = new ByteArrayOutputStream();
098: DataOutputStream dataOut = new DataOutputStream(out);
099: dataOut.writeUTF(realData.getExtensionId());
100: dataOut.writeInt(realData.getData().length);
101: dataOut.write(realData.getData());
102: dataOut.close();
103: super .javaToNative(out.toByteArray(), transferData);
104: } catch (IOException e) {
105: e.printStackTrace();
106: }
107: }
108:
109: /* (non-Javadoc)
110: * Method declared on Transfer.
111: */
112: protected Object nativeToJava(TransferData transferData) {
113: try {
114: byte[] bytes = (byte[]) super .nativeToJava(transferData);
115: ByteArrayInputStream in = new ByteArrayInputStream(bytes);
116: DataInputStream dataIn = new DataInputStream(in);
117: String extensionName = dataIn.readUTF();
118: int len = dataIn.readInt();
119: byte[] pluginData = new byte[len];
120: dataIn.readFully(pluginData);
121: return new PluginTransferData(extensionName, pluginData);
122: } catch (IOException e) {
123: e.printStackTrace();
124: }
125: //can't get here
126: return null;
127: }
128: }
|