KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > ModelDataTransfer


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.pde.internal.ui.editor;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.ObjectOutputStream JavaDoc;
18
19 import org.eclipse.swt.dnd.ByteArrayTransfer;
20 import org.eclipse.swt.dnd.TransferData;
21
22 public class ModelDataTransfer extends ByteArrayTransfer {
23     /**
24      * Singleton instance.
25      */

26     private static final ModelDataTransfer instance =
27         new ModelDataTransfer();
28     // Create a unique ID to make sure that different Eclipse
29
// applications use different "types" of <code>ModelDataTransfer</code>
30

31     public static final String JavaDoc TYPE_PREFIX = "pde-model-transfer-format"; //$NON-NLS-1$
32
private static final String JavaDoc TYPE_NAME =
33         TYPE_PREFIX + ":" //$NON-NLS-1$
34
+ System.currentTimeMillis()
35             + ":" //$NON-NLS-1$
36
+ instance.hashCode();
37
38     private static final int TYPEID = registerType(TYPE_NAME);
39     
40     public static ModelDataTransfer getInstance() {
41         return instance;
42     }
43
44     /**
45      * Constructor for ModelDataTransfer.
46      */

47     public ModelDataTransfer() {
48         super();
49     }
50
51     /* (non-Javadoc)
52      * Method declared on Transfer.
53      */

54     protected int[] getTypeIds() {
55         return new int[] { TYPEID };
56     }
57     /* (non-Javadoc)
58      * Returns the type names.
59      *
60      * @return the list of type names
61      */

62     protected String JavaDoc[] getTypeNames() {
63         return new String JavaDoc[] { TYPE_NAME };
64     }
65     /* (non-Javadoc)
66         * Method declared on Transfer.
67         */

68     protected void javaToNative(Object JavaDoc data, TransferData transferData) {
69         if (!(data instanceof Object JavaDoc[])) {
70             return;
71         }
72         Object JavaDoc[] objects = (Object JavaDoc[]) data;
73         int count = objects.length;
74         
75         try {
76             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
77             ObjectOutputStream JavaDoc objectOut = new ObjectOutputStream JavaDoc(out);
78
79             //write the number of resources
80
objectOut.writeInt(count);
81
82             //write each object
83
for (int i = 0; i < objects.length; i++) {
84                 objectOut.writeObject(objects[i]);
85             }
86
87             //cleanup
88
objectOut.close();
89             out.close();
90             byte[] bytes = out.toByteArray();
91             super.javaToNative(bytes, transferData);
92         } catch (IOException JavaDoc e) {
93             //it's best to send nothing if there were problems
94
System.out.println(e);
95         }
96             
97     }
98     /* (non-Javadoc)
99      * Method declared on Transfer.
100      */

101     protected Object JavaDoc nativeToJava(TransferData transferData) {
102         byte[] bytes = (byte[]) super.nativeToJava(transferData);
103         if (bytes == null)
104             return null;
105         try {
106             ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
107
108             int count = in.readInt();
109             Object JavaDoc[] objects = new Object JavaDoc[count];
110             for (int i = 0; i < count; i++) {
111                 objects[i] = in.readObject();
112             }
113             in.close();
114             return objects;
115         } catch (ClassNotFoundException JavaDoc e) {
116             return null;
117         } catch (IOException JavaDoc e) {
118             return null;
119         }
120     }
121 }
122
Popular Tags