1 20 21 package org.apache.directory.ldapstudio.browser.common.widgets.browser; 22 23 24 import java.math.BigInteger ; 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 43 public class BrowserSorter extends ViewerSorter 44 { 45 46 47 private BrowserPreferences preferences; 48 49 50 55 public BrowserSorter( BrowserPreferences preferences ) 56 { 57 this.preferences = preferences; 58 } 59 60 61 66 public void connect( TreeViewer viewer ) 67 { 68 viewer.setSorter( this ); 69 } 70 71 72 75 public void dispose() 76 { 77 } 78 79 80 86 public void sort( final Viewer viewer, final Object [] 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 101 public int category( Object 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 146 public int compare( Viewer viewer, Object o1, Object 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 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 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 compare( rdn1.getValue(), rdn2.getValue() ); 318 } 319 else if ( !rdn1.getValue().matches( "\\d*" ) && rdn2.getValue().matches( "\\d*" ) ) 320 { 321 return compare( rdn1.getValue(), rdn2.getValue() ); 323 } 324 else if ( rdn1.getValue().matches( "\\d*" ) && rdn2.getValue().matches( "\\d*" ) ) 325 { 326 BigInteger bi1 = new BigInteger ( rdn1.getValue() ); 327 BigInteger bi2 = new BigInteger ( rdn2.getValue() ); 328 return compare( bi1, bi2 ); 329 } 332 else 333 { 334 return compare( rdn1.getValue(), rdn2.getValue() ); 335 } 336 } 337 338 339 344 private int lessThan() 345 { 346 return preferences.getSortOrder() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? -1 : 1; 347 } 348 349 350 355 private int equal() 356 { 357 return 0; 358 } 359 360 361 366 private int greaterThan() 367 { 368 return preferences.getSortOrder() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? 1 : -1; 369 } 370 371 372 381 private int compare( String s1, String s2 ) 382 { 383 return preferences.getSortOrder() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? s1.compareToIgnoreCase( s2 ) 384 : s2.compareToIgnoreCase( s1 ); 385 } 386 387 388 398 private int compare( BigInteger bi1, BigInteger bi2 ) 399 { 400 return preferences.getSortOrder() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? bi1.compareTo( bi2 ) : bi2 401 .compareTo( bi1 ); 402 } 403 404 } 405 | Popular Tags |