KickJava   Java API By Example, From Geeks To Geeks.

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


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.eclipse.swt.dnd.ByteArrayTransfer;
36 import org.eclipse.swt.dnd.Transfer;
37 import org.eclipse.swt.dnd.TransferData;
38
39
40 /**
41  * A {@link Transfer} that could be used to transfer {@link IConnection} objects.
42  * Note that only the connection name is converted to a platform specific
43  * representation, not the complete object. To convert it back to an {@link IConnection}
44  * object the {@link ConnectionManager#getConnection(String)} method is invoked.
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

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

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

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

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

131     public Object JavaDoc 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 JavaDoc<IConnection> connectionList = new ArrayList JavaDoc<IConnection>();
142             try
143             {
144                 ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( buffer );
145                 DataInputStream JavaDoc readIn = new DataInputStream JavaDoc( 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 JavaDoc( connectionName ) );
156                         connectionList.add( connection );
157                     }
158                 }
159                 while ( readIn.available() > 1 );
160
161                 readIn.close();
162             }
163             catch ( IOException JavaDoc ex )
164             {
165                 return null;
166             }
167
168             return connectionList.toArray( new IConnection[0] );
169         }
170
171         return null;
172     }
173
174
175     /**
176      * {@inheritDoc}
177      */

178     protected String JavaDoc[] getTypeNames()
179     {
180         return new String JavaDoc[]
181             { TYPENAME };
182     }
183
184
185     /**
186      * {@inheritDoc}
187      */

188     protected int[] getTypeIds()
189     {
190         return new int[]
191             { TYPEID };
192     }
193
194 }
Popular Tags