1 26 package org.objectweb.util.explorer.swt.lib; 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.IOException ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutputStream ; 33 34 import org.eclipse.swt.dnd.ByteArrayTransfer; 35 import org.eclipse.swt.dnd.TransferData; 36 import org.objectweb.util.explorer.api.Entry; 37 import org.objectweb.util.trace.TraceSystem; 38 39 47 public class EntryTransfer 48 extends ByteArrayTransfer 49 { 50 51 57 private static final String MYTYPENAME = "EntryType"; 58 private static final int MYTYPEID = registerType(MYTYPENAME); 59 private static EntryTransfer instance_ = new EntryTransfer(); 60 61 67 73 76 protected int[] getTypeIds() { 77 return new int[] {MYTYPEID}; 78 } 79 80 83 protected String [] getTypeNames() { 84 return new String []{MYTYPENAME}; 85 } 86 87 93 public static EntryTransfer getInstance () { 94 return instance_; 95 } 96 97 public void javaToNative (Object object, TransferData transferData) { 98 if (object == null || !(object instanceof Entry)) return; 99 if (isSupportedType(transferData)) { 100 Entry entry = (Entry) object; 101 try { 102 ByteArrayOutputStream out = new ByteArrayOutputStream (); 103 ObjectOutputStream writeOut = new ObjectOutputStream (out); 104 writeOut.writeObject(entry.getName()); 106 byte[] buffer = out.toByteArray(); 107 writeOut.close(); 108 109 super.javaToNative(buffer, transferData); 110 111 } catch (IOException e) { 112 TraceSystem.get("explorer").debug("[" + getClass().getName() + "] IOException: " + e.getMessage()); 113 e.printStackTrace(); 114 } 115 } 116 } 117 118 public Object nativeToJava(TransferData transferData){ 119 if (isSupportedType(transferData)) { 120 121 byte[] buffer = (byte[])super.nativeToJava(transferData); 122 if (buffer == null) return null; 123 124 Entry[] myData = new Entry[0]; 125 try { 126 ByteArrayInputStream in = new ByteArrayInputStream (buffer); 127 ObjectInputStream readIn = new ObjectInputStream (in); 128 String name = (String ) readIn.readObject(); 129 readIn.close(); 130 } catch (IOException ex) { 131 return null; 132 } catch (ClassNotFoundException e) { 133 e.printStackTrace(); 134 return null; 135 } 136 return myData; 137 } 138 139 return null; 140 } 141 } 142 | Popular Tags |