KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > dnd > SearchTransfer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.dnd;
22
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.DataInputStream JavaDoc;
27 import java.io.DataOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
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 /**
42  * A {@link Transfer} that could be used to transfer {@link ISearch} objects.
43  * Note that only the connection name and search name is converted to a platform specific
44  * representation, not the complete object.
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

49 public class SearchTransfer extends ByteArrayTransfer
50 {
51
52     /** The Constant TYPENAME. */
53     private static final String JavaDoc TYPENAME = "org.apache.directory.ldapstudio.browser.search";
54
55     /** The Constant TYPEID. */
56     private static final int TYPEID = registerType( TYPENAME );
57
58     /** The instance. */
59     private static SearchTransfer instance = new SearchTransfer();
60
61
62     /**
63      * Creates a new instance of SearchTransfer.
64      */

65     private SearchTransfer()
66     {
67     }
68
69
70     /**
71      * Gets the instance.
72      *
73      * @return the instance
74      */

75     public static SearchTransfer getInstance()
76     {
77         return instance;
78     }
79
80
81     /**
82      * {@inheritDoc}
83      *
84      * This implementation only accepts {@link ISearch} objects.
85      * It just converts the name of the connection and the name of the search
86      * to the platform specific representation.
87      */

88     public void javaToNative( Object JavaDoc 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 JavaDoc out = new ByteArrayOutputStream JavaDoc();
101                 DataOutputStream JavaDoc writeOut = new DataOutputStream JavaDoc( 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 JavaDoc e )
120             {
121             }
122         }
123     }
124
125
126     /**
127      * {@inheritDoc}
128      *
129      * This implementation just converts the platform specific representation
130      * to the connection name and search name and invokes
131      * {@link ConnectionManager#getConnection(String)} to get the
132      * {@link IConnection} object and {@link IConnection#getSearchManager()}
133      * to get the {@link ISearch} object.
134      */

135     public Object JavaDoc 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 JavaDoc<ISearch> searchList = new ArrayList JavaDoc<ISearch>();
148                 try
149                 {
150                     IConnection connection = null;
151                     ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( buffer );
152                     DataInputStream JavaDoc readIn = new DataInputStream JavaDoc( 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 JavaDoc( 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 JavaDoc( 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 JavaDoc ex )
188                 {
189                     return null;
190                 }
191
192                 return searchList.isEmpty() ? null : searchList.toArray( new ISearch[0] );
193             }
194
195         }
196         catch ( Exception JavaDoc e )
197         {
198             e.printStackTrace();
199         }
200
201         return null;
202
203     }
204
205
206     /**
207      * {@inheritDoc}
208      */

209     protected String JavaDoc[] getTypeNames()
210     {
211         return new String JavaDoc[]
212             { TYPENAME };
213     }
214
215
216     /**
217      * {@inheritDoc}
218      */

219     protected int[] getTypeIds()
220     {
221         return new int[]
222             { TYPEID };
223     }
224
225 }
Popular Tags