KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
44  * A {@link Transfer} that could be used to transfer {@link IEntry} objects.
45  * Note that only the connection name and entry's DN is converted to a platform specific
46  * representation, not the complete object.
47  *
48  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
49  * @version $Rev$, $Date$
50  */

51 public class EntryTransfer extends ByteArrayTransfer
52 {
53
54     /** The Constant TYPENAME. */
55     private static final String JavaDoc TYPENAME = "org.apache.directory.ldapstudio.browser.entry";
56
57     /** The Constant TYPEID. */
58     private static final int TYPEID = registerType( TYPENAME );
59
60     /** The instance. */
61     private static EntryTransfer instance = new EntryTransfer();
62
63
64     /**
65      * Gets the instance.
66      *
67      * @return the instance
68      */

69     public static EntryTransfer getInstance()
70     {
71         return instance;
72     }
73
74
75     /**
76      * Creates a new instance of EntryTransfer.
77      */

78     private EntryTransfer()
79     {
80     }
81
82
83     /**
84      * {@inheritDoc}
85      *
86      * This implementation only accepts {@link IEntry} objects.
87      * It just converts the name of the connection and the entry's DN
88      * to the platform specific representation.
89      */

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

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

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

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