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.DN; 35 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 36 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 37 38 import org.eclipse.swt.dnd.ByteArrayTransfer; 39 import org.eclipse.swt.dnd.Transfer; 40 import org.eclipse.swt.dnd.TransferData; 41 42 43 51 public class EntryTransfer extends ByteArrayTransfer 52 { 53 54 55 private static final String TYPENAME = "org.apache.directory.ldapstudio.browser.entry"; 56 57 58 private static final int TYPEID = registerType( TYPENAME ); 59 60 61 private static EntryTransfer instance = new EntryTransfer(); 62 63 64 69 public static EntryTransfer getInstance() 70 { 71 return instance; 72 } 73 74 75 78 private EntryTransfer() 79 { 80 } 81 82 83 90 public void javaToNative( Object object, TransferData transferData ) 91 { 92 if ( object == null || !( object instanceof IEntry[] ) ) 93 { 94 return; 95 } 96 97 if ( isSupportedType( transferData ) ) 98 { 99 IEntry[] entries = ( IEntry[] ) object; 100 try 101 { 102 ByteArrayOutputStream out = new ByteArrayOutputStream (); 103 DataOutputStream writeOut = new DataOutputStream ( out ); 104 105 for ( int i = 0; i < entries.length; i++ ) 106 { 107 byte[] connectionName = entries[i].getConnection().getName().getBytes(); 108 writeOut.writeInt( connectionName.length ); 109 writeOut.write( connectionName ); 110 byte[] dn = entries[i].getDn().toString().getBytes(); 111 writeOut.writeInt( dn.length ); 112 writeOut.write( dn ); 113 } 114 115 byte[] buffer = out.toByteArray(); 116 writeOut.close(); 117 118 super.javaToNative( buffer, transferData ); 119 120 } 121 catch ( IOException e ) 122 { 123 } 124 } 125 } 126 127 128 137 public Object nativeToJava( TransferData transferData ) 138 { 139 try 140 { 141 if ( isSupportedType( transferData ) ) 142 { 143 byte[] buffer = ( byte[] ) super.nativeToJava( transferData ); 144 if ( buffer == null ) 145 { 146 return null; 147 } 148 149 List <IEntry> entryList = new ArrayList <IEntry>(); 150 try 151 { 152 IConnection connection = null; 153 ByteArrayInputStream in = new ByteArrayInputStream ( buffer ); 154 DataInputStream readIn = new DataInputStream ( in ); 155 156 do 157 { 158 if ( readIn.available() > 1 ) 159 { 160 int size = readIn.readInt(); 161 byte[] connectionName = new byte[size]; 162 readIn.read( connectionName ); 163 connection = BrowserCorePlugin.getDefault().getConnectionManager().getConnection( 164 new String ( connectionName ) ); 165 } 166 167 IEntry entry = null; 168 if ( readIn.available() > 1 && connection != null ) 169 { 170 int size = readIn.readInt(); 171 byte[] dn = new byte[size]; 172 readIn.read( dn ); 173 entry = connection.getEntryFromCache( new DN( new String ( dn ) ) ); 174 } 175 else 176 { 177 return null; 178 } 179 180 if ( entry != null ) 181 { 182 entryList.add( entry ); 183 } 184 } 185 while ( readIn.available() > 1 ); 186 187 readIn.close(); 188 } 189 catch ( IOException ex ) 190 { 191 return null; 192 } 193 194 return entryList.isEmpty() ? null : entryList.toArray( new IEntry[0] ); 195 } 196 197 } 198 catch ( Exception e ) 199 { 200 e.printStackTrace(); 201 } 202 203 return null; 204 205 } 206 207 208 211 protected String [] getTypeNames() 212 { 213 return new String [] 214 { TYPENAME }; 215 } 216 217 218 221 protected int[] getTypeIds() 222 { 223 return new int[] 224 { TYPEID }; 225 } 226 227 } | Popular Tags |