KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigInteger JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
27 import org.apache.directory.ldapstudio.browser.core.internal.model.DirectoryMetadataEntry;
28 import org.apache.directory.ldapstudio.browser.core.internal.model.RootDSE;
29 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
30 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
31 import org.apache.directory.ldapstudio.browser.core.model.RDN;
32 import org.eclipse.jface.viewers.TreeViewer;
33 import org.eclipse.jface.viewers.Viewer;
34 import org.eclipse.jface.viewers.ViewerSorter;
35
36
37 /**
38  * The BrowserSorter implements the sorter for the browser widget.
39  *
40  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
41  * @version $Rev$, $Date$
42  */

43 public class BrowserSorter extends ViewerSorter
44 {
45
46     /** The browser preferences, used to get the sort settings */
47     private BrowserPreferences preferences;
48
49
50     /**
51      * Creates a new instance of BrowserSorter.
52      *
53      * @param preferences the browser preferences, used to get the sort settings
54      */

55     public BrowserSorter( BrowserPreferences preferences )
56     {
57         this.preferences = preferences;
58     }
59
60
61     /**
62      * Connects the tree viewer to this sorter.
63      *
64      * @param viewer the tree viewer
65      */

66     public void connect( TreeViewer viewer )
67     {
68         viewer.setSorter( this );
69     }
70
71
72     /**
73      * Disposes this sorter.
74      */

75     public void dispose()
76     {
77     }
78
79
80     /**
81      * {@inheritDoc}
82      *
83      * For performance reasons this implemention first checks if sorting is enabled
84      * and if the number of elements is less than the sort limit.
85      */

86     public void sort( final Viewer viewer, final Object JavaDoc[] elements )
87     {
88         if ( elements != null && ( preferences.getSortLimit() <= 0 || elements.length < preferences.getSortLimit() )
89             && ( preferences.getSortBy() != BrowserCoreConstants.SORT_BY_NONE || preferences.isLeafEntriesFirst() ) )
90         {
91             BrowserSorter.super.sort( viewer, elements );
92         }
93     }
94
95
96     /**
97      * {@inheritDoc}
98      *
99      * This method is used to categorize leaf entries, meta entries and normal entries.
100      */

101     public int category( Object JavaDoc element )
102     {
103         if ( preferences.isLeafEntriesFirst() || preferences.isMetaEntriesLast() )
104         {
105             if ( element instanceof IEntry )
106             {
107                 IEntry entry = ( IEntry ) element;
108                 if ( ( entry instanceof DirectoryMetadataEntry || entry instanceof RootDSE || entry.isAlias() || entry
109                     .isReferral() )
110                     && preferences.isMetaEntriesLast() )
111                 {
112                     return 3;
113                 }
114                 else if ( entry.isSubentry() && preferences.isLeafEntriesFirst() )
115                 {
116                     return 0;
117                 }
118                 else if ( !entry.hasChildren() && preferences.isLeafEntriesFirst() )
119                 {
120                     return 1;
121                 }
122                 else
123                 {
124                     return 2;
125                 }
126             }
127             else
128             {
129                 return 4;
130             }
131         }
132         else
133         {
134             return 0;
135         }
136     }
137
138
139     /**
140      * {@inheritDoc}
141      *
142      * This implementation compares IEntry or ISearchResult objects. Depending on
143      * the sort settings it delegates comparation to {@link #compareRdns(IEntry, IEntry)}
144      * or {@link #compareRdnValues(IEntry, IEntry)}.
145      */

146     public int compare( Viewer viewer, Object JavaDoc o1, Object JavaDoc o2 )
147     {
148         if ( o1 == null && o2 == null )
149         {
150             return equal();
151         }
152         else if ( o1 == null && o2 != null )
153         {
154             return lessThan();
155         }
156         else if ( o1 != null && o2 == null )
157         {
158             return greaterThan();
159         }
160         else if ( o1 instanceof IEntry || o2 instanceof IEntry )
161         {
162             if ( !( o1 instanceof IEntry ) && !( o2 instanceof IEntry ) )
163             {
164                 return equal();
165             }
166             else if ( !( o1 instanceof IEntry ) && ( o2 instanceof IEntry ) )
167             {
168                 return lessThan();
169             }
170             else if ( ( o1 instanceof IEntry ) && !( o2 instanceof IEntry ) )
171             {
172                 return greaterThan();
173             }
174             else
175             {
176                 IEntry entry1 = ( IEntry ) o1;
177                 IEntry entry2 = ( IEntry ) o2;
178
179                 int cat1 = category( entry1 );
180                 int cat2 = category( entry2 );
181                 if ( cat1 != cat2 )
182                 {
183                     return cat1 - cat2;
184                 }
185                 else if ( preferences.getSortBy() == BrowserCoreConstants.SORT_BY_NONE )
186                 {
187                     return equal();
188                 }
189                 else if ( preferences.getSortBy() == BrowserCoreConstants.SORT_BY_RDN )
190                 {
191                     return compareRdns( entry1, entry2 );
192                 }
193                 else if ( preferences.getSortBy() == BrowserCoreConstants.SORT_BY_RDN_VALUE )
194                 {
195                     return compareRdnValues( entry1, entry2 );
196                 }
197                 else
198                 {
199                     return equal();
200                 }
201             }
202         }
203         else if ( o1 instanceof ISearchResult || o2 instanceof ISearchResult )
204         {
205             if ( !( o1 instanceof ISearchResult ) && !( o2 instanceof ISearchResult ) )
206             {
207                 return equal();
208             }
209             else if ( !( o1 instanceof ISearchResult ) && ( o2 instanceof ISearchResult ) )
210             {
211                 return lessThan();
212             }
213             else if ( ( o1 instanceof ISearchResult ) && !( o2 instanceof ISearchResult ) )
214             {
215                 return greaterThan();
216             }
217             else
218             {
219                 ISearchResult sr1 = ( ISearchResult ) o1;
220                 ISearchResult sr2 = ( ISearchResult ) o2;
221
222                 int cat1 = category( sr1 );
223                 int cat2 = category( sr2 );
224                 if ( cat1 != cat2 )
225                 {
226                     return cat1 - cat2;
227                 }
228                 else if ( preferences.getSortBy() == BrowserCoreConstants.SORT_BY_NONE )
229                 {
230                     return equal();
231                 }
232                 else if ( preferences.getSortBy() == BrowserCoreConstants.SORT_BY_RDN )
233                 {
234                     return compareRdns( sr1.getEntry(), sr2.getEntry() );
235                 }
236                 else if ( preferences.getSortBy() == BrowserCoreConstants.SORT_BY_RDN_VALUE )
237                 {
238                     return compareRdnValues( sr1.getEntry(), sr2.getEntry() );
239                 }
240                 else
241                 {
242                     return equal();
243                 }
244             }
245         }
246         else
247         {
248             return equal();
249         }
250     }
251
252
253     /**
254      * Compares the string representation of the RDNs of two IEntry objects.
255      *
256      * @param entry1 the first entry
257      * @param entry2 the second entry
258      * @return a negative integer, zero, or a positive integer
259      */

260     private int compareRdns( IEntry entry1, IEntry entry2 )
261     {
262         RDN rdn1 = entry1.getRdn();
263         RDN rdn2 = entry2.getRdn();
264
265         if ( rdn1 == null && rdn2 == null )
266         {
267             return equal();
268         }
269         else if ( rdn1 == null && rdn2 != null )
270         {
271             return greaterThan();
272         }
273         else if ( rdn1 != null && rdn2 == null )
274         {
275             return lessThan();
276         }
277         else
278         {
279             return compare( rdn1.toString(), rdn2.toString() );
280         }
281     }
282
283
284     /**
285      * Compares the RDN values of two IEntry objects.
286      * Numeric values are compared as numeric.
287      *
288      * @param entry1 the first entry
289      * @param entry2 the second entry
290      * @return a negative integer, zero, or a positive integer
291      */

292     private int compareRdnValues( IEntry entry1, IEntry entry2 )
293     {
294
295         RDN rdn1 = entry1.getRdn();
296         RDN rdn2 = entry2.getRdn();
297
298         if ( ( rdn1 == null || rdn1.getValue() == null || "".equals( rdn1.getValue() ) )
299             && ( rdn2 == null || rdn2.getValue() == null || "".equals( rdn2.getValue() ) ) )
300         {
301             return equal();
302         }
303         else if ( ( rdn1 == null || rdn1.getValue() == null || "".equals( rdn1.getValue() ) )
304             && !( rdn2 == null || rdn2.getValue() == null || "".equals( rdn2.getValue() ) ) )
305         {
306             return greaterThan();
307         }
308         else if ( !( rdn1 == null || rdn1.getValue() == null || "".equals( rdn1.getValue() ) )
309             && ( rdn2 == null || rdn2.getValue() == null || "".equals( rdn2.getValue() ) ) )
310         {
311             return lessThan();
312         }
313
314         else if ( rdn1.getValue().matches( "\\d*" ) && !rdn2.getValue().matches( "\\d*" ) )
315         {
316             // return lessThan();
317
return compare( rdn1.getValue(), rdn2.getValue() );
318         }
319         else if ( !rdn1.getValue().matches( "\\d*" ) && rdn2.getValue().matches( "\\d*" ) )
320         {
321             // return greaterThan();
322
return compare( rdn1.getValue(), rdn2.getValue() );
323         }
324         else if ( rdn1.getValue().matches( "\\d*" ) && rdn2.getValue().matches( "\\d*" ) )
325         {
326             BigInteger JavaDoc bi1 = new BigInteger JavaDoc( rdn1.getValue() );
327             BigInteger JavaDoc bi2 = new BigInteger JavaDoc( rdn2.getValue() );
328             return compare( bi1, bi2 );
329             // return Integer.parseInt(rdn1.getValue()) -
330
// Integer.parseInt(rdn2.getValue());
331
}
332         else
333         {
334             return compare( rdn1.getValue(), rdn2.getValue() );
335         }
336     }
337
338
339     /**
340      * Returns +1 or -1, depending on the sort order.
341      *
342      * @return +1 or -1, depending on the sort order
343      */

344     private int lessThan()
345     {
346         return preferences.getSortOrder() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? -1 : 1;
347     }
348
349
350     /**
351      * Returns 0.
352      *
353      * @return 0
354      */

355     private int equal()
356     {
357         return 0;
358     }
359
360
361     /**
362      * Returns +1 or -1, depending on the sort order.
363      *
364      * @return +1 or -1, depending on the sort order
365      */

366     private int greaterThan()
367     {
368         return preferences.getSortOrder() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? 1 : -1;
369     }
370
371
372     /**
373      * Compares the two strings using the Strings's compareToIgnoreCase method,
374      * pays attention for the sort order.
375      *
376      * @param s1 the first string to compare
377      * @param s2 the second string to compare
378      * @return a negative integer, zero, or a positive integer
379      * @see java.lang.String#compareToIgnoreCase(String)
380      */

381     private int compare( String JavaDoc s1, String JavaDoc s2 )
382     {
383         return preferences.getSortOrder() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? s1.compareToIgnoreCase( s2 )
384             : s2.compareToIgnoreCase( s1 );
385     }
386
387
388     /**
389      * Compares the two numbers using the BigInteger compareTo method,
390      * pays attention for the sort order.
391      *
392      * @param bi1 the first number to compare
393      * @param bi1 the second number to compare
394      * @return -1, 0 or 1 as this BigInteger is numerically less than, equal
395      * to, or greater than
396      * @see java.math.BigInteger#compareTo(BigInteger)
397      */

398     private int compare( BigInteger JavaDoc bi1, BigInteger JavaDoc bi2 )
399     {
400         return preferences.getSortOrder() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? bi1.compareTo( bi2 ) : bi2
401             .compareTo( bi1 );
402     }
403
404 }
405
Popular Tags