KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.DN;
34 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
35 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
36 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
37 import org.apache.directory.ldapstudio.browser.core.model.IValue;
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 IValue} objects.
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

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

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

76     private ValuesTransfer()
77     {
78     }
79
80
81     /**
82      * {@inheritDoc}
83      *
84      * This implementation only accepts {@link IValue} objects.
85      * It converts the name of the connection, the entry's DN, the
86      * attribute description and the value to the platform specific
87      * representation.
88      */

89     public void javaToNative( Object JavaDoc object, TransferData transferData )
90     {
91         if ( object == null || !( object instanceof IValue[] ) )
92         {
93             return;
94         }
95
96         if ( isSupportedType( transferData ) )
97         {
98             IValue[] values = ( IValue[] ) object;
99             try
100             {
101                 ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
102                 DataOutputStream JavaDoc writeOut = new DataOutputStream JavaDoc( out );
103
104                 for ( int i = 0; i < values.length; i++ )
105                 {
106                     byte[] connectionName = values[i].getAttribute().getEntry().getConnection().getName().getBytes();
107                     writeOut.writeInt( connectionName.length );
108                     writeOut.write( connectionName );
109                     byte[] dn = values[i].getAttribute().getEntry().getDn().toString().getBytes();
110                     writeOut.writeInt( dn.length );
111                     writeOut.write( dn );
112                     byte[] attributeName = values[i].getAttribute().getDescription().getBytes();
113                     writeOut.writeInt( attributeName.length );
114                     writeOut.write( attributeName );
115                     if ( values[i].isString() )
116                     {
117                         byte[] value = values[i].getStringValue().getBytes();
118                         writeOut.writeBoolean( true );
119                         writeOut.writeInt( value.length );
120                         writeOut.write( value );
121                     }
122                     else if ( values[i].isBinary() )
123                     {
124                         byte[] value = values[i].getBinaryValue();
125                         writeOut.writeBoolean( false );
126                         writeOut.writeInt( value.length );
127                         writeOut.write( value );
128                     }
129                 }
130
131                 byte[] buffer = out.toByteArray();
132                 writeOut.close();
133
134                 super.javaToNative( buffer, transferData );
135
136             }
137             catch ( IOException JavaDoc e )
138             {
139             }
140         }
141     }
142
143
144     /**
145      * {@inheritDoc}
146      *
147      * This implementation converts the platform specific representation
148      * to the connection name, entry DN, attribute description and value and
149      * restores the {@link IValue} object.
150      */

151     public Object JavaDoc nativeToJava( TransferData transferData )
152     {
153         try
154         {
155             if ( isSupportedType( transferData ) )
156             {
157                 byte[] buffer = ( byte[] ) super.nativeToJava( transferData );
158                 if ( buffer == null )
159                 {
160                     return null;
161                 }
162
163                 List JavaDoc<IValue> valueList = new ArrayList JavaDoc<IValue>();
164                 try
165                 {
166                     ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( buffer );
167                     DataInputStream JavaDoc readIn = new DataInputStream JavaDoc( in );
168
169                     do
170                     {
171                         IConnection connection = null;
172                         if ( readIn.available() > 1 )
173                         {
174                             int size = readIn.readInt();
175                             byte[] connectionName = new byte[size];
176                             readIn.read( connectionName );
177                             connection = BrowserCorePlugin.getDefault().getConnectionManager().getConnection(
178                                 new String JavaDoc( connectionName ) );
179                         }
180
181                         IEntry entry = null;
182                         if ( readIn.available() > 1 && connection != null )
183                         {
184                             int size = readIn.readInt();
185                             byte[] dn = new byte[size];
186                             readIn.read( dn );
187                             entry = connection.getEntryFromCache( new DN( new String JavaDoc( dn ) ) );
188                         }
189                         else
190                         {
191                             return null;
192                         }
193
194                         IAttribute attribute = null;
195                         if ( readIn.available() > 1 && entry != null )
196                         {
197                             int size = readIn.readInt();
198                             byte[] attributeName = new byte[size];
199                             readIn.read( attributeName );
200                             attribute = entry.getAttribute( new String JavaDoc( attributeName ) );
201                         }
202                         else
203                         {
204                             return null;
205                         }
206
207                         IValue value = null;
208                         if ( readIn.available() > 1 && attribute != null )
209                         {
210                             boolean isString = readIn.readBoolean();
211                             int size = readIn.readInt();
212                             byte[] val = new byte[size];
213                             readIn.read( val );
214                             String JavaDoc test = new String JavaDoc( val );
215
216                             IValue[] values = attribute.getValues();
217                             for ( int i = 0; i < values.length; i++ )
218                             {
219                                 if ( isString && values[i].isString() && test.equals( values[i].getStringValue() ) )
220                                 {
221                                     value = values[i];
222                                     break;
223                                 }
224                                 else if ( !isString && values[i].isBinary()
225                                     && test.equals( new String JavaDoc( values[i].getBinaryValue() ) ) )
226                                 {
227                                     value = values[i];
228                                     break;
229                                 }
230                             }
231                         }
232                         else
233                         {
234                             return null;
235                         }
236
237                         if ( value != null )
238                         {
239                             valueList.add( value );
240                         }
241                     }
242                     while ( readIn.available() > 1 );
243
244                     readIn.close();
245                 }
246                 catch ( IOException JavaDoc ex )
247                 {
248                     return null;
249                 }
250
251                 return valueList.isEmpty() ? null : valueList.toArray( new IValue[valueList.size()] );
252             }
253
254         }
255         catch ( Exception JavaDoc e )
256         {
257             e.printStackTrace();
258         }
259
260         return null;
261
262     }
263
264
265     /**
266      * {@inheritDoc}
267      */

268     protected String JavaDoc[] getTypeNames()
269     {
270         return new String JavaDoc[]
271             { TYPENAME };
272     }
273
274
275     /**
276      * {@inheritDoc}
277      */

278     protected int[] getTypeIds()
279     {
280         return new int[]
281             { TYPEID };
282     }
283
284 }
Popular Tags