KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.ui.part;
13
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.DataInputStream JavaDoc;
17 import java.io.DataOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.StringReader JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.swt.dnd.ByteArrayTransfer;
24 import org.eclipse.swt.dnd.TransferData;
25 import org.eclipse.ui.IEditorInput;
26 import org.eclipse.ui.IElementFactory;
27 import org.eclipse.ui.IPersistableElement;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.WorkbenchException;
30 import org.eclipse.ui.XMLMemento;
31
32 /**
33  * The <code>EditorInputTransfer</code> class is used to transfer an
34  * <code>IEditorInput</code> and corresponding editorId from one part to another
35  * in a drag and drop operation. Only opening of internal editors is supported.
36  * <p>
37  * In every drag and drop operation there is a <code>DragSource</code> and a
38  * <code>DropTarget</code>. When a drag occurs a <code>Transfer</code> is used
39  * to marshall the drag data from the source into a byte array. If a drop
40  * occurs another <code>Transfer</code> is used to marshall the byte array into
41  * drop data for the target.
42  * </p>
43  * <p>
44  * This class can be used for a <code>Viewer</code> or an SWT component directly.
45  * A singleton is provided which may be serially reused (see <code>getInstance</code>).
46  * For an implementor of <code>IEditorInput</code> to be supported by
47  * <code>EditorInputTransfer</code>, it must provide a proper implementation of
48  * <code>IEditorInput</code>.<code>getPersistable</code>. For further details,
49  * consult the <code>org.eclipse.ui.elementFactories</code> extension point.
50  * </p>
51  * <p>
52  * The data for a transfer is represented by the <code>EditorInputData</code>
53  * class, and a convenience method <code>createEditorInputData</code> is
54  * provided. A <code>DragSource</code>.<code>dragSetData</code> implementation
55  * should set the data to an array of <code>EditorInputData</code>. In this
56  * way, the dragging of multiple editor inputs is supported.
57  * </p>
58  * <p>
59  * Below is an example of how to set the data for dragging a single editor
60  * input using a <code>EditorInputTransfer</code>.
61  * </p>
62  * <p>
63  * <pre>
64  * public void dragSetData(DragSourceEvent event) {
65  * if (EditorInputTransfer.getInstance().isSupportedType(event.dataType)) {
66  *
67  * EditorInputTransfer.EditorInputData data =
68  * EditorInputTransfer.
69  * createEditorInputData(EDITOR_ID, getEditorInput());
70  * event.data = new EditorInputTransfer.EditorInputData [] {data};
71  * }
72  * }
73  * </pre>
74  * </p>
75  *
76  * @see org.eclipse.jface.viewers.StructuredViewer
77  * @see org.eclipse.swt.dnd.DropTarget
78  * @see org.eclipse.swt.dnd.DragSource
79  * @see org.eclipse.ui.IEditorInput
80  * @see org.eclipse.ui.IPersistableElement
81  * @see org.eclipse.ui.IElementFactory
82  */

83 public class EditorInputTransfer extends ByteArrayTransfer {
84
85     /**
86      * Singleton instance.
87      */

88     private static final EditorInputTransfer instance = new EditorInputTransfer();
89
90     // Create a unique ID to make sure that different Eclipse
91
// applications use different "types" of <code>EditorInputTransfer</code>
92
private static final String JavaDoc TYPE_NAME = "editor-input-transfer-format:" + System.currentTimeMillis() + ":" + instance.hashCode(); //$NON-NLS-2$//$NON-NLS-1$
93

94     private static final int TYPEID = registerType(TYPE_NAME);
95
96     public static class EditorInputData {
97
98         public String JavaDoc editorId;
99
100         public IEditorInput input;
101
102         private EditorInputData(String JavaDoc editorId, IEditorInput input) {
103             this.editorId = editorId;
104             this.input = input;
105         }
106     }
107
108     /**
109      * Creates a new transfer object.
110      */

111     private EditorInputTransfer() {
112     }
113
114     /**
115      * Returns the singleton instance.
116      *
117      * @return the singleton instance
118      */

119     public static EditorInputTransfer getInstance() {
120         return instance;
121     }
122
123     /* (non-Javadoc)
124      * Method declared on Transfer.
125      */

126     protected int[] getTypeIds() {
127         return new int[] { TYPEID };
128     }
129
130     /* (non-Javadoc)
131      * Returns the type names.
132      *
133      * @return the list of type names
134      */

135     protected String JavaDoc[] getTypeNames() {
136         return new String JavaDoc[] { TYPE_NAME };
137     }
138
139     /* (non-Javadoc)
140      * Method declared on Transfer.
141      */

142     public void javaToNative(Object JavaDoc data, TransferData transferData) {
143
144         if (!(data instanceof EditorInputData[])) {
145             return;
146         }
147
148         EditorInputData[] editorInputs = (EditorInputData[]) data;
149         /**
150          * The editor input serialization format is:
151          * (int) number of editor inputs
152          * Then, the following for each editor input:
153          * (String) editorId
154          * (String) factoryId
155          * (String) data used to recreate the IEditorInput
156          */

157
158         int editorInputCount = editorInputs.length;
159
160         try {
161             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
162             DataOutputStream JavaDoc dataOut = new DataOutputStream JavaDoc(out);
163
164             //write the number of resources
165
dataOut.writeInt(editorInputCount);
166
167             //write each resource
168
for (int i = 0; i < editorInputs.length; i++) {
169                 writeEditorInput(dataOut, editorInputs[i]);
170             }
171
172             //cleanup
173
dataOut.close();
174             out.close();
175             byte[] bytes = out.toByteArray();
176             super.javaToNative(bytes, transferData);
177         } catch (IOException JavaDoc e) {
178         }
179     }
180
181     /* (non-Javadoc)
182      * Method declared on Transfer.
183      */

184     public Object JavaDoc nativeToJava(TransferData transferData) {
185
186         byte[] bytes = (byte[]) super.nativeToJava(transferData);
187         if (bytes == null) {
188             return null;
189         }
190         DataInputStream JavaDoc in = new DataInputStream JavaDoc(
191                 new ByteArrayInputStream JavaDoc(bytes));
192         try {
193             int count = in.readInt();
194             EditorInputData[] results = new EditorInputData[count];
195             for (int i = 0; i < count; i++) {
196                 results[i] = readEditorInput(in);
197             }
198             return results;
199         } catch (IOException JavaDoc e) {
200             return null;
201         } catch (WorkbenchException e) {
202             return null;
203         }
204
205     }
206
207     /**
208      * Method readEditorInput.
209      * @param in
210      * @return EditorInputData
211      */

212     private EditorInputData readEditorInput(DataInputStream JavaDoc dataIn)
213             throws IOException JavaDoc, WorkbenchException {
214
215         String JavaDoc editorId = dataIn.readUTF();
216         String JavaDoc factoryId = dataIn.readUTF();
217         String JavaDoc xmlString = dataIn.readUTF();
218
219         if (xmlString == null || xmlString.length() == 0) {
220             return null;
221         }
222
223         StringReader JavaDoc reader = new StringReader JavaDoc(xmlString);
224
225         // Restore the editor input
226
XMLMemento memento = XMLMemento.createReadRoot(reader);
227
228         IElementFactory factory = PlatformUI.getWorkbench().getElementFactory(
229                 factoryId);
230
231         if (factory != null) {
232             IAdaptable adaptable = factory.createElement(memento);
233             if (adaptable != null && (adaptable instanceof IEditorInput)) {
234                 return new EditorInputData(editorId, (IEditorInput) adaptable);
235             }
236         }
237
238         return null;
239     }
240
241     /**
242      * Method writeEditorInput.
243      * @param dataOut
244      * @param editorInputData
245      */

246     private void writeEditorInput(DataOutputStream JavaDoc dataOut,
247             EditorInputData editorInputData) throws IOException JavaDoc {
248         //write the id of the editor
249
dataOut.writeUTF(editorInputData.editorId);
250
251         //write the information needed to recreate the editor input
252
if (editorInputData.input != null) {
253             // Capture the editor information
254
XMLMemento memento = XMLMemento.createWriteRoot("IEditorInput");//$NON-NLS-1$
255

256             IPersistableElement element = editorInputData.input
257                     .getPersistable();
258             if (element != null) {
259                 //get the IEditorInput to save its state
260
element.saveState(memento);
261
262                 //convert memento to String
263
StringWriter JavaDoc writer = new StringWriter JavaDoc();
264                 memento.save(writer);
265                 writer.close();
266
267                 //write the factor ID and state information
268
dataOut.writeUTF(element.getFactoryId());
269                 dataOut.writeUTF(writer.toString());
270             }
271         }
272     }
273
274     public static EditorInputData createEditorInputData(String JavaDoc editorId,
275             IEditorInput input) {
276         return new EditorInputData(editorId, input);
277     }
278
279 }
280
Popular Tags