1 11 package org.eclipse.pde.internal.ui.editor; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.IOException ; 16 import java.io.ObjectInputStream ; 17 import java.io.ObjectOutputStream ; 18 19 import org.eclipse.swt.dnd.ByteArrayTransfer; 20 import org.eclipse.swt.dnd.TransferData; 21 22 public class ModelDataTransfer extends ByteArrayTransfer { 23 26 private static final ModelDataTransfer instance = 27 new ModelDataTransfer(); 28 31 public static final String TYPE_PREFIX = "pde-model-transfer-format"; private static final String TYPE_NAME = 33 TYPE_PREFIX + ":" + System.currentTimeMillis() 35 + ":" + instance.hashCode(); 37 38 private static final int TYPEID = registerType(TYPE_NAME); 39 40 public static ModelDataTransfer getInstance() { 41 return instance; 42 } 43 44 47 public ModelDataTransfer() { 48 super(); 49 } 50 51 54 protected int[] getTypeIds() { 55 return new int[] { TYPEID }; 56 } 57 62 protected String [] getTypeNames() { 63 return new String [] { TYPE_NAME }; 64 } 65 68 protected void javaToNative(Object data, TransferData transferData) { 69 if (!(data instanceof Object [])) { 70 return; 71 } 72 Object [] objects = (Object []) data; 73 int count = objects.length; 74 75 try { 76 ByteArrayOutputStream out = new ByteArrayOutputStream (); 77 ObjectOutputStream objectOut = new ObjectOutputStream (out); 78 79 objectOut.writeInt(count); 81 82 for (int i = 0; i < objects.length; i++) { 84 objectOut.writeObject(objects[i]); 85 } 86 87 objectOut.close(); 89 out.close(); 90 byte[] bytes = out.toByteArray(); 91 super.javaToNative(bytes, transferData); 92 } catch (IOException e) { 93 System.out.println(e); 95 } 96 97 } 98 101 protected Object nativeToJava(TransferData transferData) { 102 byte[] bytes = (byte[]) super.nativeToJava(transferData); 103 if (bytes == null) 104 return null; 105 try { 106 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (bytes)); 107 108 int count = in.readInt(); 109 Object [] objects = new Object [count]; 110 for (int i = 0; i < count; i++) { 111 objects[i] = in.readObject(); 112 } 113 in.close(); 114 return objects; 115 } catch (ClassNotFoundException e) { 116 return null; 117 } catch (IOException e) { 118 return null; 119 } 120 } 121 } 122 | Popular Tags |