KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspace;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.swt.dnd.ByteArrayTransfer;
24 import org.eclipse.swt.dnd.TransferData;
25
26 /**
27  * The <code>ResourceTransfer</code> class is used to transfer an
28  * array of <code>IResources</code>s from one part to another in a
29  * drag and drop operation or a cut, copy, paste action.
30  * <p>
31  * In every drag and drop operation there is a <code>DragSource</code> and
32  * a <code>DropTarget</code>. When a drag occurs a <code>Transfer</code> is
33  * used to marshall the drag data from the source into a byte array. If a drop
34  * occurs another <code>Transfer</code> is used to marshall the byte array into
35  * drop data for the target.
36  * </p>
37  * <p>
38  * When a <code>CutAction</code> or a <code>CopyAction</code> is performed,
39  * this transfer is used to place references to the selected resources
40  * on the <code>Clipboard</code>. When a <code>PasteAction</code> is performed, the
41  * references on the clipboard are used to move or copy the resources
42  * to the selected destination.
43  * </p>
44  * <p>
45  * This class can be used for a <code>Viewer<code> or an SWT component directly.
46  * A singleton is provided which may be serially reused (see <code>getInstance</code>).
47  * It is not intended to be subclassed.
48  * </p>
49  *
50  * @see org.eclipse.jface.viewers.StructuredViewer
51  * @see org.eclipse.swt.dnd.DropTarget
52  * @see org.eclipse.swt.dnd.DragSource
53  */

54 public class ResourceTransfer extends ByteArrayTransfer {
55
56     /**
57      * Singleton instance.
58      */

59     private static final ResourceTransfer instance = new ResourceTransfer();
60
61     // Create a unique ID to make sure that different Eclipse
62
// applications use different "types" of <code>ResourceTransfer</code>
63
private static final String JavaDoc TYPE_NAME = "resource-transfer-format:" + System.currentTimeMillis() + ":" + instance.hashCode();//$NON-NLS-2$//$NON-NLS-1$
64

65     private static final int TYPEID = registerType(TYPE_NAME);
66
67     private IWorkspace workspace = ResourcesPlugin.getWorkspace();
68
69     /**
70      * Creates a new transfer object.
71      */

72     private ResourceTransfer() {
73     }
74
75     /**
76      * Returns the singleton instance.
77      *
78      * @return the singleton instance
79      */

80     public static ResourceTransfer getInstance() {
81         return instance;
82     }
83
84     /* (non-Javadoc)
85      * Method declared on Transfer.
86      */

87     protected int[] getTypeIds() {
88         return new int[] { TYPEID };
89     }
90
91     /* (non-Javadoc)
92      * Returns the type names.
93      *
94      * @return the list of type names
95      */

96     protected String JavaDoc[] getTypeNames() {
97         return new String JavaDoc[] { TYPE_NAME };
98     }
99
100     /* (non-Javadoc)
101      * Method declared on Transfer.
102      */

103     protected void javaToNative(Object JavaDoc data, TransferData transferData) {
104         if (!(data instanceof IResource[])) {
105             return;
106         }
107
108         IResource[] resources = (IResource[]) data;
109         /**
110          * The resource serialization format is:
111          * (int) number of resources
112          * Then, the following for each resource:
113          * (int) resource type
114          * (String) path of resource
115          */

116
117         int resourceCount = resources.length;
118
119         try {
120             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
121             DataOutputStream JavaDoc dataOut = new DataOutputStream JavaDoc(out);
122
123             //write the number of resources
124
dataOut.writeInt(resourceCount);
125
126             //write each resource
127
for (int i = 0; i < resources.length; i++) {
128                 writeResource(dataOut, resources[i]);
129             }
130
131             //cleanup
132
dataOut.close();
133             out.close();
134             byte[] bytes = out.toByteArray();
135             super.javaToNative(bytes, transferData);
136         } catch (IOException JavaDoc e) {
137             //it's best to send nothing if there were problems
138
}
139     }
140
141     /* (non-Javadoc)
142      * Method declared on Transfer.
143      */

144     protected Object JavaDoc nativeToJava(TransferData transferData) {
145         /**
146          * The resource serialization format is:
147          * (int) number of resources
148          * Then, the following for each resource:
149          * (int) resource type
150          * (String) path of resource
151          */

152
153         byte[] bytes = (byte[]) super.nativeToJava(transferData);
154         if (bytes == null) {
155             return null;
156         }
157         DataInputStream JavaDoc in = new DataInputStream JavaDoc(
158                 new ByteArrayInputStream JavaDoc(bytes));
159         try {
160             int count = in.readInt();
161             IResource[] results = new IResource[count];
162             for (int i = 0; i < count; i++) {
163                 results[i] = readResource(in);
164             }
165             return results;
166         } catch (IOException JavaDoc e) {
167             return null;
168         }
169     }
170
171     /**
172      * Reads a resource from the given stream.
173      *
174      * @param dataIn the input stream
175      * @return the resource
176      * @exception IOException if there is a problem reading from the stream
177      */

178     private IResource readResource(DataInputStream JavaDoc dataIn) throws IOException JavaDoc {
179         int type = dataIn.readInt();
180         String JavaDoc path = dataIn.readUTF();
181         switch (type) {
182         case IResource.FOLDER:
183             return workspace.getRoot().getFolder(new Path(path));
184         case IResource.FILE:
185             return workspace.getRoot().getFile(new Path(path));
186         case IResource.PROJECT:
187             return workspace.getRoot().getProject(path);
188         }
189         throw new IllegalArgumentException JavaDoc(
190                 "Unknown resource type in ResourceTransfer.readResource"); //$NON-NLS-1$
191
}
192
193     /**
194      * Writes the given resource to the given stream.
195      *
196      * @param dataOut the output stream
197      * @param resource the resource
198      * @exception IOException if there is a problem writing to the stream
199      */

200     private void writeResource(DataOutputStream JavaDoc dataOut, IResource resource)
201             throws IOException JavaDoc {
202         dataOut.writeInt(resource.getType());
203         dataOut.writeUTF(resource.getFullPath().toString());
204     }
205 }
206
Popular Tags