KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > actions > SelectionUtils


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.actions;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedHashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserCategory;
33 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserEntryPage;
34 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserSearchResultPage;
35 import org.apache.directory.ldapstudio.browser.core.internal.model.Search;
36 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
37 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
38 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
39 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
40 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
41 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
42 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
43 import org.apache.directory.ldapstudio.browser.core.model.IValue;
44 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeTypeDescription;
45 import org.apache.directory.ldapstudio.browser.core.utils.LdapFilterUtils;
46 import org.eclipse.jface.viewers.ISelection;
47 import org.eclipse.jface.viewers.IStructuredSelection;
48 import org.eclipse.jface.viewers.StructuredSelection;
49
50
51 /**
52  * The SelectionUtils are used to extract specific beans from the current
53  * selection (org.eclipse.jface.viewers.ISelection).
54  *
55  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
56  * @version $Rev$, $Date$
57  */

58 public abstract class SelectionUtils
59 {
60
61     /**
62      * This method creates a prototype search from the given selection.
63      *
64      * Dependend on the selected element it determines the best connection,
65      * search base and filter:
66      * <ul>
67      * <li>ISearch: all parameters are copied to the prototype search (clone)
68      * <li>IEntry or ISearchResult or IBookmark: DN is used as search base
69      * <li>IAttribute or IValue: the entry's DN is used as search base,
70      * the filter is built using the name-value-pairs (query by example).
71      * </ul>
72      *
73      *
74      * @param selection the current selection
75      * @return a prototype search
76      */

77     public static ISearch getExampleSearch( ISelection selection )
78     {
79
80         ISearch exampleSearch = new Search();
81         String JavaDoc oldName = exampleSearch.getSearchParameter().getName();
82         exampleSearch.getSearchParameter().setName( null );
83         exampleSearch.setScope( ISearch.SCOPE_SUBTREE );
84
85         if ( selection != null && !selection.isEmpty() && selection instanceof StructuredSelection )
86         {
87
88             Object JavaDoc[] objects = ( ( IStructuredSelection ) selection ).toArray();
89             Comparator JavaDoc<Object JavaDoc> comparator = new Comparator JavaDoc<Object JavaDoc>()
90             {
91                 public int compare( Object JavaDoc o1, Object JavaDoc o2 )
92                 {
93                     if ( ( o1 instanceof IValue ) && !( o2 instanceof IValue ) )
94                     {
95                         return -1;
96                     }
97                     else if ( !( o1 instanceof IValue ) && ( o2 instanceof IValue ) )
98                     {
99                         return 1;
100                     }
101                     else if ( ( o1 instanceof IAttribute ) && !( o2 instanceof IAttribute ) )
102                     {
103                         return -1;
104                     }
105                     else if ( !( o1 instanceof IAttribute ) && ( o2 instanceof IAttribute ) )
106                     {
107                         return 1;
108                     }
109                     else if ( ( o1 instanceof AttributeHierarchy ) && !( o2 instanceof AttributeHierarchy ) )
110                     {
111                         return -1;
112                     }
113                     else if ( !( o1 instanceof AttributeHierarchy ) && ( o2 instanceof AttributeHierarchy ) )
114                     {
115                         return 1;
116                     }
117                     return 0;
118                 }
119             };
120             Arrays.sort( objects, comparator );
121             Object JavaDoc obj = objects[0];
122
123             if ( obj instanceof ISearch )
124             {
125                 ISearch search = ( ISearch ) obj;
126                 exampleSearch = ( ISearch ) search.clone();
127                 exampleSearch.setName( null );
128             }
129             else if ( obj instanceof IEntry )
130             {
131                 IEntry entry = ( IEntry ) obj;
132                 exampleSearch.setConnection( entry.getConnection() );
133                 exampleSearch.setSearchBase( entry.getDn() );
134             }
135             else if ( obj instanceof ISearchResult )
136             {
137                 ISearchResult searchResult = ( ISearchResult ) obj;
138                 exampleSearch.setConnection( searchResult.getEntry().getConnection() );
139                 exampleSearch.setSearchBase( searchResult.getEntry().getDn() );
140             }
141             else if ( obj instanceof IBookmark )
142             {
143                 IBookmark bookmark = ( IBookmark ) obj;
144                 exampleSearch.setConnection( bookmark.getConnection() );
145                 exampleSearch.setSearchBase( bookmark.getDn() );
146             }
147
148             else if ( obj instanceof AttributeHierarchy || obj instanceof IAttribute || obj instanceof IValue )
149             {
150
151                 IEntry entry = null;
152                 Set JavaDoc<String JavaDoc> filterSet = new LinkedHashSet JavaDoc<String JavaDoc>();
153                 for ( int i = 0; i < objects.length; i++ )
154                 {
155                     Object JavaDoc object = objects[i];
156                     if ( object instanceof AttributeHierarchy )
157                     {
158                         AttributeHierarchy ah = ( AttributeHierarchy ) object;
159                         for ( Iterator JavaDoc it = ah.iterator(); it.hasNext(); )
160                         {
161                             IAttribute attribute = ( IAttribute ) it.next();
162                             entry = attribute.getEntry();
163                             IValue[] values = attribute.getValues();
164                             for ( int v = 0; v < values.length; v++ )
165                             {
166                                 filterSet.add( LdapFilterUtils.getFilter( values[v] ) );
167                             }
168                         }
169                     }
170                     else if ( object instanceof IAttribute )
171                     {
172                         IAttribute attribute = ( IAttribute ) object;
173                         entry = attribute.getEntry();
174                         IValue[] values = attribute.getValues();
175                         for ( int v = 0; v < values.length; v++ )
176                         {
177                             filterSet.add( LdapFilterUtils.getFilter( values[v] ) );
178                         }
179                     }
180                     else if ( object instanceof IValue )
181                     {
182                         IValue value = ( IValue ) object;
183                         entry = value.getAttribute().getEntry();
184                         filterSet.add( LdapFilterUtils.getFilter( value ) );
185                     }
186                 }
187
188                 exampleSearch.setConnection( entry.getConnection() );
189                 exampleSearch.setSearchBase( entry.getDn() );
190                 StringBuffer JavaDoc filter = new StringBuffer JavaDoc();
191                 if ( filterSet.size() > 1 )
192                 {
193                     filter.append( "(&" );
194                     for ( Iterator JavaDoc<String JavaDoc> filterIterator = filterSet.iterator(); filterIterator.hasNext(); )
195                     {
196                         filter.append( filterIterator.next() );
197                     }
198                     filter.append( ")" );
199                 }
200                 else if ( filterSet.size() == 1 )
201                 {
202                     filter.append( filterSet.toArray()[0] );
203                 }
204                 else
205                 {
206                     filter.append( ISearch.FILTER_TRUE );
207                 }
208                 exampleSearch.setFilter( filter.toString() );
209             }
210
211             else if ( obj instanceof IConnection )
212             {
213                 IConnection connection = ( IConnection ) obj;
214                 exampleSearch.setConnection( connection );
215                 if ( connection.getRootDSE().getChildrenCount() > 0 )
216                 {
217                     exampleSearch.setSearchBase( connection.getRootDSE().getChildren()[0].getDn() );
218                 }
219                 else
220                 {
221                     exampleSearch.setSearchBase( connection.getRootDSE().getDn() );
222                 }
223             }
224             else if ( obj instanceof BrowserCategory )
225             {
226                 BrowserCategory cat = ( BrowserCategory ) obj;
227                 exampleSearch.setConnection( cat.getParent() );
228                 if ( cat.getParent().getRootDSE().getChildrenCount() > 0 )
229                 {
230                     exampleSearch.setSearchBase( cat.getParent().getRootDSE().getChildren()[0].getDn() );
231                 }
232                 else
233                 {
234                     exampleSearch.setSearchBase( cat.getParent().getRootDSE().getDn() );
235                 }
236             }
237
238         }
239
240         exampleSearch.getSearchParameter().setName( oldName );
241         return exampleSearch;
242     }
243
244
245     /**
246      * Gets the BrowserCategory beans contained in the given selection.
247      *
248      * @param selection the selection
249      * @return an array with BrowserCategory beans, may be empty.
250      */

251     public static BrowserCategory[] getBrowserViewCategories( ISelection selection )
252     {
253         List JavaDoc<Object JavaDoc> list = getTypes( selection, BrowserCategory.class );
254         return list.toArray( new BrowserCategory[list.size()] );
255     }
256
257
258     /**
259      * Gets the IValue beans contained in the given selection.
260      *
261      * @param selection the selection
262      * @return an array with IValue beans, may be empty.
263      */

264     public static IValue[] getValues( ISelection selection )
265     {
266         List JavaDoc<Object JavaDoc> list = getTypes( selection, IValue.class );
267         return list.toArray( new IValue[list.size()] );
268     }
269
270
271     /**
272      * Gets the IAttribute beans contained in the given selection.
273      *
274      * @param selection the selection
275      * @return an array with IAttribute beans, may be empty.
276      */

277     public static IAttribute[] getAttributes( ISelection selection )
278     {
279         List JavaDoc<Object JavaDoc> list = getTypes( selection, IAttribute.class );
280         return list.toArray( new IAttribute[list.size()] );
281     }
282
283
284     /**
285      * Gets the AttributeHierarchy beans contained in the given selection.
286      *
287      * @param selection the selection
288      * @return an array with AttributeHierarchy beans, may be empty.
289      */

290     public static AttributeHierarchy[] getAttributeHierarchie( ISelection selection )
291     {
292         List JavaDoc<Object JavaDoc> list = getTypes( selection, AttributeHierarchy.class );
293         return list.toArray( new AttributeHierarchy[list.size()] );
294     }
295
296
297     /**
298      * Gets the Strings contained in the given selection.
299      *
300      * @param selection the selection
301      * @return an array with Strings, may be empty.
302      */

303     public static String JavaDoc[] getProperties( ISelection selection )
304     {
305         List JavaDoc<Object JavaDoc> list = getTypes( selection, String JavaDoc.class );
306         return list.toArray( new String JavaDoc[list.size()] );
307     }
308
309
310     /**
311      * Gets the AttributeTypeDescription beans contained in the given selection.
312      *
313      * @param selection the selection
314      * @return an array with AttributeTypeDescription beans, may be empty.
315      */

316     public static AttributeTypeDescription[] getAttributeTypeDescription( ISelection selection )
317     {
318         List JavaDoc<Object JavaDoc> list = getTypes( selection, AttributeTypeDescription.class );
319         return list.toArray( new AttributeTypeDescription[list.size()] );
320     }
321
322
323     /**
324      * Gets the IEntry beans contained in the given selection.
325      *
326      * @param selection the selection
327      * @return an array with IEntry beans, may be empty.
328      */

329     public static IEntry[] getEntries( ISelection selection )
330     {
331         List JavaDoc<Object JavaDoc> list = getTypes( selection, IEntry.class );
332         return list.toArray( new IEntry[list.size()] );
333     }
334
335
336     /**
337      * Gets the IBookmark beans contained in the given selection.
338      *
339      * @param selection the selection
340      * @return an array with IBookmark beans, may be empty.
341      */

342     public static IBookmark[] getBookmarks( ISelection selection )
343     {
344         List JavaDoc<Object JavaDoc> list = getTypes( selection, IBookmark.class );
345         return list.toArray( new IBookmark[list.size()] );
346     }
347
348
349     /**
350      * Gets the ISearchResult beans contained in the given selection.
351      *
352      * @param selection the selection
353      * @return an array with ISearchResult beans, may be empty.
354      */

355     public static ISearchResult[] getSearchResults( ISelection selection )
356     {
357         List JavaDoc<Object JavaDoc> list = getTypes( selection, ISearchResult.class );
358         return list.toArray( new ISearchResult[list.size()] );
359     }
360
361
362     /**
363      * Gets all beans of the requested type contained in the given selection.
364      *
365      * @param selection the selection
366      * @param type the requested type
367      * @return a list containg beans of the requesten type
368      */

369     private static List JavaDoc<Object JavaDoc> getTypes( ISelection selection, Class JavaDoc type )
370     {
371         List JavaDoc<Object JavaDoc> list = new ArrayList JavaDoc<Object JavaDoc>();
372         if ( selection instanceof IStructuredSelection )
373         {
374             IStructuredSelection structuredSelection = ( IStructuredSelection ) selection;
375             Iterator JavaDoc it = structuredSelection.iterator();
376             while ( it.hasNext() )
377             {
378                 Object JavaDoc o = it.next();
379                 if ( type.isInstance( o ) )
380                 {
381                     list.add( o );
382                 }
383             }
384         }
385         return list;
386     }
387
388
389     /**
390      * Gets the ISearch beans contained in the given selection.
391      *
392      * @param selection the selection
393      * @return an array with ISearch beans, may be empty.
394      */

395     public static ISearch[] getSearches( ISelection selection )
396     {
397         List JavaDoc<Object JavaDoc> list = getTypes( selection, ISearch.class );
398         return list.toArray( new ISearch[list.size()] );
399     }
400
401
402     /**
403      * Gets the IConnection beans contained in the given selection.
404      *
405      * @param selection the selection
406      * @return an array with IConnection beans, may be empty.
407      */

408     public static IConnection[] getConnections( ISelection selection )
409     {
410         List JavaDoc<Object JavaDoc> list = getTypes( selection, IConnection.class );
411         return list.toArray( new IConnection[list.size()] );
412     }
413
414
415     /**
416      * Gets the BrowserEntryPage beans contained in the given selection.
417      *
418      * @param selection the selection
419      * @return an array with BrowserEntryPage beans, may be empty.
420      */

421     public static BrowserEntryPage[] getBrowserEntryPages( ISelection selection )
422     {
423         List JavaDoc<Object JavaDoc> list = getTypes( selection, BrowserEntryPage.class );
424         return list.toArray( new BrowserEntryPage[list.size()] );
425     }
426
427
428     /**
429      * Gets the BrowserSearchResultPage beans contained in the given selection.
430      *
431      * @param selection the selection
432      * @return an array with BrowserSearchResultPage beans, may be empty.
433      */

434     public static BrowserSearchResultPage[] getBrowserSearchResultPages( ISelection selection )
435     {
436         List JavaDoc<Object JavaDoc> list = getTypes( selection, BrowserSearchResultPage.class );
437         return list.toArray( new BrowserSearchResultPage[list.size()] );
438     }
439
440     
441     /**
442      * Gets the objects contained in the given selection.
443      *
444      * @param selection the selection
445      * @return an array with object, may be empty.
446      */

447     public static Object JavaDoc[] getObjects( ISelection selection )
448     {
449         List JavaDoc<Object JavaDoc> list = getTypes( selection, Object JavaDoc.class );
450         return list.toArray( new Object JavaDoc[list.size()] );
451     }
452 }
453
Popular Tags