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