1 20 21 package org.apache.directory.ldapstudio.browser.common.dnd; 22 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.DataInputStream ; 27 import java.io.DataOutputStream ; 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 32 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 33 import org.apache.directory.ldapstudio.browser.core.ConnectionManager; 34 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 35 import org.eclipse.swt.dnd.ByteArrayTransfer; 36 import org.eclipse.swt.dnd.Transfer; 37 import org.eclipse.swt.dnd.TransferData; 38 39 40 49 public class ConnectionTransfer extends ByteArrayTransfer 50 { 51 52 53 private static final String TYPENAME = "org.apache.directory.ldapstudio.browser.connection"; 54 55 56 private static final int TYPEID = registerType( TYPENAME ); 57 58 59 private static ConnectionTransfer instance = new ConnectionTransfer(); 60 61 62 65 private ConnectionTransfer() 66 { 67 } 68 69 70 75 public static ConnectionTransfer getInstance() 76 { 77 return instance; 78 } 79 80 81 88 public void javaToNative( Object object, TransferData transferData ) 89 { 90 if ( object == null || !( object instanceof IConnection[] ) ) 91 { 92 return; 93 } 94 95 if ( isSupportedType( transferData ) ) 96 { 97 IConnection[] connections = ( IConnection[] ) object; 98 try 99 { 100 ByteArrayOutputStream out = new ByteArrayOutputStream (); 101 DataOutputStream writeOut = new DataOutputStream ( out ); 102 103 for ( int i = 0; i < connections.length; i++ ) 104 { 105 byte[] name = connections[i].getName().getBytes(); 106 writeOut.writeInt( name.length ); 107 writeOut.write( name ); 108 } 109 110 byte[] buffer = out.toByteArray(); 111 writeOut.close(); 112 113 super.javaToNative( buffer, transferData ); 114 115 } 116 catch ( IOException e ) 117 { 118 } 119 } 120 } 121 122 123 131 public Object nativeToJava( TransferData transferData ) 132 { 133 if ( isSupportedType( transferData ) ) 134 { 135 byte[] buffer = ( byte[] ) super.nativeToJava( transferData ); 136 if ( buffer == null ) 137 { 138 return null; 139 } 140 141 List <IConnection> connectionList = new ArrayList <IConnection>(); 142 try 143 { 144 ByteArrayInputStream in = new ByteArrayInputStream ( buffer ); 145 DataInputStream readIn = new DataInputStream ( in ); 146 147 do 148 { 149 if ( readIn.available() > 1 ) 150 { 151 int size = readIn.readInt(); 152 byte[] connectionName = new byte[size]; 153 readIn.read( connectionName ); 154 IConnection connection = BrowserCorePlugin.getDefault().getConnectionManager().getConnection( 155 new String ( connectionName ) ); 156 connectionList.add( connection ); 157 } 158 } 159 while ( readIn.available() > 1 ); 160 161 readIn.close(); 162 } 163 catch ( IOException ex ) 164 { 165 return null; 166 } 167 168 return connectionList.toArray( new IConnection[0] ); 169 } 170 171 return null; 172 } 173 174 175 178 protected String [] getTypeNames() 179 { 180 return new String [] 181 { TYPENAME }; 182 } 183 184 185 188 protected int[] getTypeIds() 189 { 190 return new int[] 191 { TYPEID }; 192 } 193 194 } | Popular Tags |