KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > browser > BrowserContentProvider


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.widgets.browser;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
32 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
33 import org.apache.directory.ldapstudio.browser.core.internal.model.DirectoryMetadataEntry;
34 import org.apache.directory.ldapstudio.browser.core.jobs.InitializeChildrenJob;
35 import org.apache.directory.ldapstudio.browser.core.jobs.OpenConnectionsJob;
36 import org.apache.directory.ldapstudio.browser.core.jobs.SearchJob;
37 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
38 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
39 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
40 import org.apache.directory.ldapstudio.browser.core.model.IRootDSE;
41 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
42 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
43 import org.eclipse.jface.viewers.ITreeContentProvider;
44 import org.eclipse.jface.viewers.Viewer;
45
46
47 /**
48  * The BrowserContentProvider implements the content provider for
49  * the browser widget. It accepts an IConnection as input.
50  *
51  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
52  * @version $Rev$, $Date$
53  */

54 public class BrowserContentProvider implements ITreeContentProvider
55 {
56
57     /** The prefernces */
58     protected BrowserPreferences preferences;
59
60     /** The sorter */
61     protected BrowserSorter sorter;
62
63     /** This map contains the pages for entries with many children (if folding is activated) */
64     private Map JavaDoc<IEntry, BrowserEntryPage[]> entryToEntryPagesMap;
65
66     /** This map contains the pages for searches with many results (if folding is activated) */
67     private Map JavaDoc<ISearch, BrowserSearchResultPage[]> searchToSearchResultPagesMap;
68
69     /** This map contains the top-level categories for each connection */
70     private Map JavaDoc<IConnection, BrowserCategory[]> connectionToCategoriesMap;
71
72
73     /**
74      * Creates a new instance of BrowserContentProvider.
75      *
76      * @param preferences the preferences
77      * @param sorter the sorter
78      */

79     public BrowserContentProvider( BrowserPreferences preferences, BrowserSorter sorter )
80     {
81         this.preferences = preferences;
82         this.sorter = sorter;
83         this.entryToEntryPagesMap = new HashMap JavaDoc<IEntry, BrowserEntryPage[]>();
84         this.searchToSearchResultPagesMap = new HashMap JavaDoc<ISearch, BrowserSearchResultPage[]>();
85         this.connectionToCategoriesMap = new HashMap JavaDoc<IConnection, BrowserCategory[]>();
86     }
87
88
89     /**
90      * {@inheritDoc}
91      */

92     public void inputChanged( Viewer v, Object JavaDoc oldInput, Object JavaDoc newInput )
93     {
94     }
95
96
97     /**
98      * {@inheritDoc}
99      */

100     public void dispose()
101     {
102         if ( entryToEntryPagesMap != null )
103         {
104             entryToEntryPagesMap.clear();
105             entryToEntryPagesMap = null;
106         }
107         if ( searchToSearchResultPagesMap != null )
108         {
109             searchToSearchResultPagesMap.clear();
110             searchToSearchResultPagesMap = null;
111         }
112         if ( connectionToCategoriesMap != null )
113         {
114             connectionToCategoriesMap.clear();
115             connectionToCategoriesMap = null;
116         }
117     }
118
119
120     /**
121      * {@inheritDoc}
122      */

123     public Object JavaDoc[] getElements( Object JavaDoc parent )
124     {
125         if ( parent instanceof IConnection )
126         {
127             IConnection connection = ( IConnection ) parent;
128             if ( !connectionToCategoriesMap.containsKey( connection ) )
129             {
130                 BrowserCategory[] categories = new BrowserCategory[3];
131                 categories[0] = new BrowserCategory( BrowserCategory.TYPE_DIT, connection );
132                 categories[1] = new BrowserCategory( BrowserCategory.TYPE_SEARCHES, connection );
133                 categories[2] = new BrowserCategory( BrowserCategory.TYPE_BOOKMARKS, connection );
134                 connectionToCategoriesMap.put( connection, categories );
135             }
136
137             BrowserCategory[] categories = connectionToCategoriesMap.get( connection );
138
139             List JavaDoc<BrowserCategory> catList = new ArrayList JavaDoc<BrowserCategory>( 3 );
140             if ( preferences.isShowDIT() )
141             {
142                 catList.add( categories[0] );
143             }
144             if ( preferences.isShowSearches() )
145             {
146                 catList.add( categories[1] );
147             }
148             if ( preferences.isShowBookmarks() )
149             {
150                 catList.add( categories[2] );
151             }
152
153             return catList.toArray( new BrowserCategory[0] );
154         }
155         else if ( parent instanceof IEntry[] )
156         {
157             return ( IEntry[] ) parent;
158         }
159         else
160         {
161             return getChildren( parent );
162         }
163     }
164
165
166     /**
167      * {@inheritDoc}
168      */

169     public Object JavaDoc getParent( final Object JavaDoc child )
170     {
171         if ( child instanceof BrowserCategory )
172         {
173             return ( ( BrowserCategory ) child ).getParent();
174         }
175         else if ( child instanceof BrowserEntryPage )
176         {
177             return ( ( BrowserEntryPage ) child ).getParent();
178         }
179         else if ( child instanceof IEntry )
180         {
181             IEntry parentEntry = ( ( IEntry ) child ).getParententry();
182             if ( parentEntry == null )
183             {
184                 if ( connectionToCategoriesMap.get( ( ( IEntry ) child ).getConnection() ) != null )
185                 {
186                     return connectionToCategoriesMap.get( ( ( IEntry ) child ).getConnection() )[0];
187                 }
188                 else
189                 {
190                     return null;
191                 }
192             }
193             else if ( entryToEntryPagesMap.containsKey( parentEntry ) )
194             {
195                 BrowserEntryPage[] entryPages = entryToEntryPagesMap.get( parentEntry );
196                 BrowserEntryPage ep = null;
197                 for ( int i = 0; i < entryPages.length && ep == null; i++ )
198                 {
199                     ep = entryPages[i].getParentOf( ( IEntry ) child );
200                 }
201                 return ep;
202             }
203             else
204             {
205                 return parentEntry;
206             }
207         }
208         else if ( child instanceof BrowserSearchResultPage )
209         {
210             return ( ( BrowserSearchResultPage ) child ).getParent();
211         }
212         else if ( child instanceof ISearch )
213         {
214             ISearch search = ( ( ISearch ) child );
215             return connectionToCategoriesMap.get( search.getConnection() )[1];
216         }
217         else if ( child instanceof ISearchResult )
218         {
219             ISearch parentSearch = ( ( ISearchResult ) child ).getSearch();
220             if ( parentSearch != null && searchToSearchResultPagesMap.containsKey( parentSearch ) )
221             {
222                 BrowserSearchResultPage[] srPages = searchToSearchResultPagesMap.get( parentSearch );
223                 BrowserSearchResultPage srp = null;
224                 for ( int i = 0; i < srPages.length && srp == null; i++ )
225                 {
226                     srp = srPages[i].getParentOf( ( ISearchResult ) child );
227                 }
228                 return srp;
229             }
230             else
231             {
232                 return parentSearch;
233             }
234         }
235         else if ( child instanceof IBookmark )
236         {
237             IBookmark bookmark = ( ( IBookmark ) child );
238             return connectionToCategoriesMap.get( bookmark.getConnection() )[2];
239         }
240         else
241         {
242             return null;
243         }
244     }
245
246
247     /**
248      * {@inheritDoc}
249      */

250     public Object JavaDoc[] getChildren( Object JavaDoc parent )
251     {
252         if ( parent instanceof BrowserEntryPage )
253         {
254             BrowserEntryPage entryPage = ( BrowserEntryPage ) parent;
255             Object JavaDoc[] objects = entryPage.getChildren();
256             if ( objects == null )
257             {
258                 return new String JavaDoc[]
259                     { "Fetching Entries..." };
260             }
261             else if ( objects instanceof IEntry[] )
262             {
263                 IEntry[] entries = ( IEntry[] ) objects;
264                 return entries;
265             }
266             else
267             {
268                 return objects;
269             }
270         }
271         else if ( parent instanceof IRootDSE )
272         {
273             final IRootDSE rootDSE = ( IRootDSE ) parent;
274
275             if ( !rootDSE.isChildrenInitialized() && rootDSE.isDirectoryEntry() )
276             {
277                 new InitializeChildrenJob( new IEntry[]
278                     { rootDSE } ).execute();
279                 return new String JavaDoc[]
280                     { "Fetching Entries..." };
281             }
282
283             // get base entries
284
List JavaDoc<IEntry> entryList = new ArrayList JavaDoc<IEntry>();
285             entryList.addAll( Arrays.asList( rootDSE.getChildren() ) );
286
287             // remove non-visible entries
288
for ( Iterator JavaDoc<IEntry> it = entryList.iterator(); it.hasNext(); )
289             {
290                 Object JavaDoc o = it.next();
291                 if ( !preferences.isShowDirectoryMetaEntries() && ( o instanceof DirectoryMetadataEntry ) )
292                 {
293                     it.remove();
294                 }
295             }
296
297             return entryList.toArray();
298         }
299         else if ( parent instanceof IEntry )
300             {
301                 final IEntry parentEntry = ( IEntry ) parent;
302
303             if ( !parentEntry.isChildrenInitialized() && parentEntry.isDirectoryEntry() )
304             {
305                 new InitializeChildrenJob( new IEntry[]
306                     { parentEntry } ).execute();
307                 return new String JavaDoc[]
308                     { "Fetching Entries..." };
309             }
310
311             int childrenCount = parentEntry.getChildrenCount();
312             if ( childrenCount <= preferences.getFoldingSize() || !preferences.isUseFolding() )
313             {
314                 if ( entryToEntryPagesMap.containsKey( parentEntry ) )
315                 {
316                     entryToEntryPagesMap.remove( parentEntry );
317                 }
318
319                 IEntry[] entries = parentEntry.getChildren();
320                 if ( entries == null )
321                 {
322                     return new String JavaDoc[]
323                         { "Fetching Entries..." };
324                 }
325                 else
326                 {
327                     return entries;
328                 }
329             }
330             else
331             {
332                 BrowserEntryPage[] entryPages = null;
333                 if ( !entryToEntryPagesMap.containsKey( parentEntry ) )
334                 {
335                     entryPages = getEntryPages( parentEntry, 0, childrenCount - 1 );
336                     entryToEntryPagesMap.put( parentEntry, entryPages );
337                 }
338                 else
339                 {
340                     entryPages = entryToEntryPagesMap.get( parentEntry );
341                     if ( childrenCount - 1 != entryPages[entryPages.length - 1].getLast() )
342                     {
343                         entryPages = getEntryPages( parentEntry, 0, childrenCount - 1 );
344                         entryToEntryPagesMap.put( parentEntry, entryPages );
345                     }
346                 }
347                 return entryPages;
348             }
349         }
350         else if ( parent instanceof BrowserSearchResultPage )
351         {
352             BrowserSearchResultPage srPage = ( BrowserSearchResultPage ) parent;
353             Object JavaDoc[] objects = srPage.getChildren();
354             if ( objects == null )
355             {
356                 return new String JavaDoc[]
357                     { "Fetching Search Results..." };
358             }
359             else if ( objects instanceof ISearchResult[] )
360             {
361                 ISearchResult[] srs = ( ISearchResult[] ) objects;
362                 return srs;
363             }
364             else
365             {
366                 return objects;
367             }
368         }
369         else if ( parent instanceof ISearch )
370         {
371             ISearch search = ( ISearch ) parent;
372             if ( search.getSearchResults() == null )
373             {
374                 new SearchJob( new ISearch[]
375                     { search } ).execute();
376                 return new String JavaDoc[]
377                     { "Performing Search..." };
378             }
379             else if ( search.getSearchResults().length == 0 )
380             {
381                 return new String JavaDoc[]
382                     { "No Results" };
383             }
384             else if ( search.getSearchResults().length <= preferences.getFoldingSize() || !preferences.isUseFolding() )
385             {
386                 ISearchResult[] results = search.getSearchResults();
387                 return results;
388             }
389             else
390             {
391                 BrowserSearchResultPage[] srPages = null;
392                 if ( !searchToSearchResultPagesMap.containsKey( search ) )
393                 {
394                     srPages = getSearchResultPages( search, 0, search.getSearchResults().length - 1 );
395                     searchToSearchResultPagesMap.put( search, srPages );
396                 }
397                 else
398                 {
399                     srPages = searchToSearchResultPagesMap.get( search );
400                     if ( search.getSearchResults().length - 1 != srPages[srPages.length - 1].getLast() )
401                     {
402                         srPages = getSearchResultPages( search, 0, search.getSearchResults().length - 1 );
403                         searchToSearchResultPagesMap.put( search, srPages );
404                     }
405                 }
406                 return srPages;
407             }
408         }
409         else if ( parent instanceof BrowserCategory )
410         {
411             BrowserCategory category = ( BrowserCategory ) parent;
412             IConnection connection = category.getParent();
413
414             switch ( category.getType() )
415             {
416                 case BrowserCategory.TYPE_DIT:
417                 {
418                     // open connection when expanding DIT
419
if ( !connection.isOpened() )
420                     {
421                         new OpenConnectionsJob( connection ).execute();
422                         return new String JavaDoc[]
423                             { "Opening Connection..." };
424                     }
425
426                     return new Object JavaDoc[]
427                         { connection.getRootDSE() };
428                 }
429
430                 case BrowserCategory.TYPE_SEARCHES:
431                 {
432                     return connection.getSearchManager().getSearches();
433                 }
434
435                 case BrowserCategory.TYPE_BOOKMARKS:
436                 {
437                     return connection.getBookmarkManager().getBookmarks();
438                 }
439             }
440
441             return new Object JavaDoc[0];
442         }
443         else
444         {
445             return new Object JavaDoc[0];
446         }
447     }
448
449
450     /**
451      * {@inheritDoc}
452      */

453     public boolean hasChildren( Object JavaDoc parent )
454     {
455         if ( parent instanceof IEntry )
456         {
457             IEntry parentEntry = ( IEntry ) parent;
458             return parentEntry.hasChildren()
459                 || ( BrowserCorePlugin.getDefault().getPluginPreferences().getBoolean(
460                     BrowserCoreConstants.PREFERENCE_SHOW_ALIAS_AND_REFERRAL_OBJECTS ) && ( parentEntry.isAlias() || parentEntry
461                     .isReferral() ) );
462         }
463         else if ( parent instanceof BrowserEntryPage )
464         {
465             return true;
466         }
467         else if ( parent instanceof BrowserSearchResultPage )
468         {
469             return true;
470         }
471         else if ( parent instanceof ISearchResult )
472         {
473             return false;
474         }
475         else if ( parent instanceof ISearch )
476         {
477             return true;
478         }
479         else if ( parent instanceof BrowserCategory )
480         {
481             return true;
482         }
483         else
484         {
485             return false;
486         }
487     }
488
489
490     /**
491      * Creates and returns the entry pages for the given entry. The number of pages
492      * depends on the number of entries and the paging size.
493      *
494      * @param entry the parent entry
495      * @param first the index of the first child entry
496      * @param last the index of the last child entry
497      * @return the created entry pages
498      */

499     private BrowserEntryPage[] getEntryPages( IEntry entry, int first, int last )
500     {
501         int pagingSize = preferences.getFoldingSize();
502
503         int diff = last - first;
504         int factor = diff > 0 ? ( int ) ( Math.log( diff ) / Math.log( pagingSize ) ) : 0;
505
506         int groupFirst = first;
507         int groupLast = first;
508         BrowserEntryPage[] pages = new BrowserEntryPage[( int ) ( diff / Math.pow( pagingSize, factor ) ) + 1];
509         for ( int i = 0; i < pages.length; i++ )
510         {
511             groupFirst = ( int ) ( i * Math.pow( pagingSize, factor ) ) + first;
512             groupLast = ( int ) ( ( i + 1 ) * Math.pow( pagingSize, factor ) ) + first - 1;
513             groupLast = groupLast > last ? last : groupLast;
514             BrowserEntryPage[] subpages = ( factor > 1 ) ? getEntryPages( entry, groupFirst, groupLast ) : null;
515             pages[i] = new BrowserEntryPage( entry, groupFirst, groupLast, subpages, sorter );
516         }
517
518         return pages;
519     }
520
521
522     /**
523      * Creates and returns the search result pages for the given search. The number of pages
524      * depends on the number of search results and the paging size.
525      *
526      * @param search the parent search
527      * @param first the index of the first search result
528      * @param last the index of the last child search result
529      * @return the created search result pages
530      */

531     private BrowserSearchResultPage[] getSearchResultPages( ISearch search, int first, int last )
532     {
533         int pagingSize = preferences.getFoldingSize();
534
535         int diff = last - first;
536         int factor = diff > 0 ? ( int ) ( Math.log( diff ) / Math.log( pagingSize ) ) : 0;
537
538         int groupFirst = first;
539         int groupLast = first;
540         BrowserSearchResultPage[] pages = new BrowserSearchResultPage[( int ) ( diff / Math.pow( pagingSize, factor ) ) + 1];
541         for ( int i = 0; i < pages.length; i++ )
542         {
543             groupFirst = ( int ) ( i * Math.pow( pagingSize, factor ) ) + first;
544             groupLast = ( int ) ( ( i + 1 ) * Math.pow( pagingSize, factor ) ) + first - 1;
545             groupLast = groupLast > last ? last : groupLast;
546             BrowserSearchResultPage[] subpages = ( factor > 1 ) ? getSearchResultPages( search, groupFirst, groupLast )
547                 : null;
548             pages[i] = new BrowserSearchResultPage( search, groupFirst, groupLast, subpages, sorter );
549         }
550
551         return pages;
552     }
553
554 }
555
Popular Tags