KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > PluginTransfer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.part;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.DataInputStream JavaDoc;
16 import java.io.DataOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18
19 import org.eclipse.swt.dnd.ByteArrayTransfer;
20 import org.eclipse.swt.dnd.TransferData;
21
22 /**
23  * This class can be used to transfer an instance of <code>PluginTransferData</code>
24  * between two parts in a workbench in a drop and drop operation.
25  * <p>
26  * In every drag and drop operation there is a <code>DragSource</code> and
27  * a <code>DropTarget</code>. When a drag occurs a <code>Transfer</code> is
28  * used to marshall the drag data from the source into a byte array. If a drop
29  * occurs another <code>Transfer</code> is used to marshall the byte array into
30  * drop data for the target.
31  * </p><p>
32  * A <code>PluginTransferData</code> contains the id of a drop action extension.
33  * If a drop occurs the extension is invoked to perform a drop action. As a benefit,
34  * the destination viewer doesn't need to have any knowledge of the items being
35  * dropped into it.
36  * </p><p>
37  * This class can be used for a <code>Viewer<code> or an SWT component directly.
38  * A singleton is provided which may be serially reused (see <code>getInstance</code>).
39  * It is not intended to be subclassed.
40  * </p>
41  *
42  * @see org.eclipse.jface.viewers.StructuredViewer
43  * @see org.eclipse.swt.dnd.DropTarget
44  * @see org.eclipse.swt.dnd.DragSource
45  */

46 public class PluginTransfer extends ByteArrayTransfer {
47
48     private static final String JavaDoc TYPE_NAME = "pluggable-transfer-format";//$NON-NLS-1$
49

50     private static final int TYPEID = registerType(TYPE_NAME);
51
52     /**
53      * Singleton instance.
54      */

55     private static PluginTransfer instance = new PluginTransfer();
56
57     /**
58      * Creates a new transfer object.
59      */

60     private PluginTransfer() {
61         super();
62     }
63
64     /**
65      * Returns the singleton instance.
66      *
67      * @return the singleton instance
68      */

69     public static PluginTransfer getInstance() {
70         return instance;
71     }
72
73     /* (non-Javadoc)
74      * Method declared on Transfer.
75      */

76     protected int[] getTypeIds() {
77         return new int[] { TYPEID };
78     }
79
80     /* (non-Javadoc)
81      * Returns the type names.
82      *
83      * @return the list of type names
84      */

85     protected String JavaDoc[] getTypeNames() {
86         return new String JavaDoc[] { TYPE_NAME };
87     }
88
89     /* (non-Javadoc)
90      * Method declared on Transfer.
91      */

92     protected void javaToNative(Object JavaDoc data, TransferData transferData) {
93         PluginTransferData realData = (PluginTransferData) data;
94         if (data == null) {
95             return;
96         }
97         try {
98             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
99             DataOutputStream JavaDoc dataOut = new DataOutputStream JavaDoc(out);
100             dataOut.writeUTF(realData.getExtensionId());
101             dataOut.writeInt(realData.getData().length);
102             dataOut.write(realData.getData());
103             dataOut.close();
104             super.javaToNative(out.toByteArray(), transferData);
105         } catch (IOException JavaDoc e) {
106             e.printStackTrace();
107         }
108     }
109
110     /* (non-Javadoc)
111      * Method declared on Transfer.
112      */

113     protected Object JavaDoc nativeToJava(TransferData transferData) {
114         try {
115             byte[] bytes = (byte[]) super.nativeToJava(transferData);
116             ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(bytes);
117             DataInputStream JavaDoc dataIn = new DataInputStream JavaDoc(in);
118             String JavaDoc extensionName = dataIn.readUTF();
119             int len = dataIn.readInt();
120             byte[] pluginData = new byte[len];
121             dataIn.readFully(pluginData);
122             return new PluginTransferData(extensionName, pluginData);
123         } catch (IOException JavaDoc e) {
124             e.printStackTrace();
125         }
126         //can't get here
127
return null;
128     }
129 }
130
Free Books   Free Magazines  
Popular Tags