KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 import org.eclipse.core.resources.IMarker;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IWorkspace;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.swt.dnd.ByteArrayTransfer;
26 import org.eclipse.swt.dnd.TransferData;
27
28 /**
29  * A <code>MarkerTransfer</code> is used to transfer an array of
30  * <code>IMarker</code>s from one part to another in a drag and drop
31  * operation.
32  * <p>
33  * In every drag and drop operation there is a <code>DragSource</code> and
34  * a <code>DropTarget</code>. When a drag occurs a <code>Transfer</code> is
35  * used to marshall the drag data from the source into a byte array. If a drop
36  * occurs another <code>Transfer</code> is used to marshall the byte array into
37  * drop data for the target.
38  * </p><p>
39  * This class can be used for a <code>Viewer<code> or an SWT component directly.
40  * A singleton is provided which may be serially reused (see <code>getInstance</code>).
41  * It is not intended to be subclassed.
42  * </p>
43  *
44  * @see org.eclipse.jface.viewers.StructuredViewer
45  * @see org.eclipse.swt.dnd.DropTarget
46  * @see org.eclipse.swt.dnd.DragSource
47  */

48 public class MarkerTransfer extends ByteArrayTransfer {
49
50     /**
51      * Singleton instance.
52      */

53     private static final MarkerTransfer instance = new MarkerTransfer();
54
55     // Create a unique ID to make sure that different Eclipse
56
// applications use different "types" of <code>MarkerTransfer</code>
57
private static final String JavaDoc TYPE_NAME = "marker-transfer-format" + System.currentTimeMillis() + ":" + instance.hashCode();//$NON-NLS-2$//$NON-NLS-1$
58

59     private static final int TYPEID = registerType(TYPE_NAME);
60
61     private IWorkspace workspace;
62
63     /**
64      * Creates a new transfer object.
65      */

66     private MarkerTransfer() {
67     }
68
69     /**
70      * Locates and returns the marker associated with the given attributes.
71      *
72      * @param pathString the resource path
73      * @param id the id of the marker to get (as per {@link IResource#getMarker
74      * IResource.getMarker})
75      * @return the specified marker
76      */

77     private IMarker findMarker(String JavaDoc pathString, long id) {
78         IPath path = new Path(pathString);
79         IResource resource = workspace.getRoot().findMember(path);
80         if (resource != null) {
81             return resource.getMarker(id);
82         }
83         return null;
84     }
85
86     /**
87      * Returns the singleton instance.
88      *
89      * @return the singleton instance
90      */

91     public static MarkerTransfer getInstance() {
92         return instance;
93     }
94
95     /* (non-Javadoc)
96      * Method declared on Transfer.
97      */

98     protected int[] getTypeIds() {
99         return new int[] { TYPEID };
100     }
101
102     /* (non-Javadoc)
103      * Returns the type names.
104      *
105      * @return the list of type names
106      */

107     protected String JavaDoc[] getTypeNames() {
108         return new String JavaDoc[] { TYPE_NAME };
109     }
110
111     /* (non-Javadoc)
112      * Method declared on Transfer.
113      * On a successful conversion, the transferData.result field will be set to
114      * OLE.S_OK. If this transfer agent is unable to perform the conversion, the
115      * transferData.result field will be set to the failure value of OLE.DV_E_TYMED.
116      */

117     protected void javaToNative(Object JavaDoc object, TransferData transferData) {
118         /**
119          * Transfer data is an array of markers. Serialized version is:
120          * (int) number of markers
121          * (Marker) marker 1
122          * (Marker) marker 2
123          * ... repeat last four for each subsequent marker
124          * see writeMarker for the (Marker) format.
125          */

126         Object JavaDoc[] markers = (Object JavaDoc[]) object;
127         lazyInit(markers);
128
129         ByteArrayOutputStream JavaDoc byteOut = new ByteArrayOutputStream JavaDoc();
130         DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(byteOut);
131
132         byte[] bytes = null;
133
134         try {
135             /* write number of markers */
136             out.writeInt(markers.length);
137
138             /* write markers */
139             for (int i = 0; i < markers.length; i++) {
140                 writeMarker((IMarker) markers[i], out);
141             }
142             out.close();
143             bytes = byteOut.toByteArray();
144         } catch (IOException JavaDoc e) {
145             //when in doubt send nothing
146
}
147
148         if (bytes != null) {
149             super.javaToNative(bytes, transferData);
150         }
151     }
152
153     /**
154      * Initializes the transfer mechanism if necessary.
155      */

156     private void lazyInit(Object JavaDoc[] markers) {
157         if (workspace == null) {
158             if (markers != null && markers.length > 0) {
159                 this.workspace = ((IMarker) markers[0]).getResource()
160                         .getWorkspace();
161             }
162         }
163     }
164
165     /* (non-Javadoc)
166      * Method declared on Transfer.
167      */

168     protected Object JavaDoc nativeToJava(TransferData transferData) {
169         byte[] bytes = (byte[]) super.nativeToJava(transferData);
170         DataInputStream JavaDoc in = new DataInputStream JavaDoc(
171                 new ByteArrayInputStream JavaDoc(bytes));
172
173         try {
174             /* read number of markers */
175             int n = in.readInt();
176
177             /* read markers */
178             IMarker[] markers = new IMarker[n];
179             for (int i = 0; i < n; i++) {
180                 IMarker marker = readMarker(in);
181                 if (marker == null) {
182                     return null;
183                 }
184                 markers[i] = marker;
185             }
186             return markers;
187         } catch (IOException JavaDoc e) {
188             return null;
189         }
190     }
191
192     /**
193      * Reads and returns a single marker from the given stream.
194      *
195      * @param dataIn the input stream
196      * @return the marker
197      * @exception IOException if there is a problem reading from the stream
198      */

199     private IMarker readMarker(DataInputStream JavaDoc dataIn) throws IOException JavaDoc {
200         /**
201          * Marker serialization format is as follows:
202          * (String) path of resource for marker
203          * (int) marker ID
204          */

205         String JavaDoc path = dataIn.readUTF();
206         long id = dataIn.readLong();
207         return findMarker(path, id);
208     }
209
210     /**
211      * Writes the given marker to the given stream.
212      *
213      * @param marker the marker
214      * @param dataOut the output stream
215      * @exception IOException if there is a problem writing to the stream
216      */

217     private void writeMarker(IMarker marker, DataOutputStream JavaDoc dataOut)
218             throws IOException JavaDoc {
219         /**
220          * Marker serialization format is as follows:
221          * (String) path of resource for marker
222          * (int) marker ID
223          */

224
225         dataOut.writeUTF(marker.getResource().getFullPath().toString());
226         dataOut.writeLong(marker.getId());
227     }
228 }
229
Popular Tags