1 20 21 package org.apache.directory.ldapstudio.browser.ui.editors.searchresult; 22 23 24 import java.util.Arrays ; 25 import java.util.Comparator ; 26 27 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 28 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 29 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 30 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 31 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 32 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 33 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants; 34 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 35 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 36 import org.eclipse.jface.viewers.Viewer; 37 import org.eclipse.jface.viewers.ViewerSorter; 38 import org.eclipse.swt.events.SelectionEvent; 39 import org.eclipse.swt.events.SelectionListener; 40 import org.eclipse.swt.widgets.TableColumn; 41 42 43 public class SearchResultEditorSorter extends ViewerSorter implements SelectionListener 44 { 45 46 protected SearchResultEditorContentProvider contentProvider; 47 48 private ISearch search; 49 50 private TableColumn[] columns; 51 52 private boolean showDn; 53 54 private int sortBy; 55 56 private int sortOrder; 57 58 59 public SearchResultEditorSorter() 60 { 61 super(); 62 } 63 64 65 public void connect( SearchResultEditorContentProvider contentProvider ) 66 { 67 68 this.contentProvider = contentProvider; 69 70 this.sortBy = 0; 71 this.sortOrder = BrowserCoreConstants.SORT_ORDER_NONE; 72 73 this.columns = this.contentProvider.getViewer().getTable().getColumns(); 74 for ( int i = 0; i < this.columns.length; i++ ) 75 { 76 this.columns[i].addSelectionListener( this ); 77 } 78 } 79 80 81 public void inputChanged( ISearch newSearch, boolean showDn ) 82 { 83 84 this.search = newSearch; 85 this.showDn = showDn; 86 87 for ( int i = 0; this.columns != null && i < this.columns.length; i++ ) 88 { 89 this.columns[i].removeSelectionListener( this ); 90 } 91 this.columns = this.contentProvider.getViewer().getTable().getColumns(); 92 for ( int i = 0; i < this.columns.length; i++ ) 93 { 94 this.columns[i].addSelectionListener( this ); 95 } 96 97 int visibleColumns = this.search.getReturningAttributes().length; 99 if ( this.showDn ) 100 visibleColumns++; 101 if ( visibleColumns < this.sortBy + 1 ) 102 { 103 this.setSortColumn( 0 ); 104 this.setSortColumn( 0 ); 105 this.setSortColumn( 0 ); 106 } 107 } 108 109 110 public void dispose() 111 { 112 for ( int i = 0; this.columns != null && i < this.columns.length; i++ ) 113 { 114 if ( !this.columns[i].isDisposed() ) 115 this.columns[i].removeSelectionListener( this ); 116 } 117 this.columns = null; 118 this.search = null; 119 this.contentProvider = null; 120 } 121 122 123 public void widgetDefaultSelected( SelectionEvent e ) 124 { 125 } 126 127 128 public void widgetSelected( SelectionEvent e ) 129 { 130 if ( e.widget instanceof TableColumn ) 131 { 132 int index = this.contentProvider.getViewer().getTable().indexOf( ( ( TableColumn ) e.widget ) ); 133 this.setSortColumn( index ); 134 } 135 } 136 137 138 private void setSortColumn( int index ) 139 { 140 if ( this.sortBy == index ) 141 { 142 this.sortOrder = this.sortOrder == BrowserCoreConstants.SORT_ORDER_ASCENDING ? BrowserCoreConstants.SORT_ORDER_DESCENDING 144 : this.sortOrder == BrowserCoreConstants.SORT_ORDER_DESCENDING ? BrowserCoreConstants.SORT_ORDER_NONE 145 : BrowserCoreConstants.SORT_ORDER_ASCENDING; 146 } 147 else 148 { 149 this.sortBy = index; 151 this.sortOrder = BrowserCoreConstants.SORT_ORDER_ASCENDING; 152 } 153 if ( this.sortOrder == BrowserCoreConstants.SORT_ORDER_NONE ) 154 { 155 this.sortBy = BrowserCoreConstants.SORT_BY_NONE; 156 } 157 158 TableColumn[] columns = this.contentProvider.getViewer().getTable().getColumns(); 159 for ( int i = 0; i < columns.length; i++ ) 160 { 161 columns[i].setImage( null ); 162 } 163 164 if ( this.sortOrder == BrowserCoreConstants.SORT_ORDER_ASCENDING ) 165 { 166 ( columns[index] ).setImage( BrowserCommonActivator.getDefault().getImage( 167 BrowserCommonConstants.IMG_SORT_ASCENDING ) ); 168 } 169 else if ( this.sortOrder == BrowserCoreConstants.SORT_ORDER_DESCENDING ) 170 { 171 ( columns[index] ).setImage( BrowserCommonActivator.getDefault().getImage( 172 BrowserCommonConstants.IMG_SORT_DESCENDING ) ); 173 } 174 else 175 { 176 ( columns[index] ).setImage( null ); 177 } 178 179 this.contentProvider.refresh(); 180 181 } 182 183 184 public boolean isSorted() 185 { 186 return true; 188 } 189 190 191 public void sort( final Viewer viewer, Object [] elements ) 192 { 193 194 if ( isSorted() ) 195 { 196 Arrays.sort( elements, new Comparator () 197 { 198 public int compare( Object a, Object b ) 199 { 200 return SearchResultEditorSorter.this.compare( viewer, a, b ); 201 } 202 } ); 203 } 204 205 } 206 207 208 public int compare( Viewer viewer, Object o1, Object o2 ) 209 { 210 211 if ( this.search == null ) 212 { 213 return this.equal(); 214 } 215 216 ISearchResult sr1 = ( ISearchResult ) o1; 240 ISearchResult sr2 = ( ISearchResult ) o2; 241 242 IEntry entry1 = sr1.getEntry(); 243 IEntry entry2 = sr2.getEntry(); 244 245 String attributeName; 246 if ( showDn && this.sortBy == 0 ) 247 { 248 attributeName = BrowserUIConstants.DN; 249 } 250 else if ( showDn && this.sortBy > 0 ) 251 { 252 attributeName = this.search.getReturningAttributes()[this.sortBy - 1]; 253 } 254 else 255 { 256 attributeName = this.search.getReturningAttributes()[this.sortBy]; 257 } 258 259 if ( attributeName == BrowserUIConstants.DN ) 260 { 261 return compare( entry1.getDn().toString(), entry2.getDn().toString() ); 262 } 263 else 264 { 265 AttributeHierarchy ah1 = entry1.getAttributeWithSubtypes( attributeName ); 266 AttributeHierarchy ah2 = entry2.getAttributeWithSubtypes( attributeName ); 267 if ( ah1 == null && ah2 == null ) 268 { 269 return this.equal(); 270 } 271 else if ( ah1 == null && ah2 != null ) 272 { 273 return this.lessThan(); 274 } 275 else if ( ah1 != null && ah2 == null ) 276 { 277 return this.greaterThan(); 278 } 279 else 280 { 281 IAttribute attribute1 = ah1.getAttribute(); 282 IAttribute attribute2 = ah2.getAttribute(); 283 284 String value1 = getValue( attribute1 ); 285 String value2 = getValue( attribute2 ); 286 return compare( value1, value2 ); 287 } 288 } 289 } 292 293 294 private String getValue( IAttribute attribute ) 295 { 296 if ( attribute.getValueSize() > 0 ) 297 { 298 return attribute.getStringValue(); 299 } 300 else 301 { 302 return ""; 303 } 304 } 305 306 307 private int lessThan() 308 { 309 return this.sortOrder == BrowserCoreConstants.SORT_ORDER_ASCENDING ? -1 : 1; 310 } 311 312 313 private int equal() 314 { 315 return 0; 316 } 317 318 319 private int greaterThan() 320 { 321 return this.sortOrder == BrowserCoreConstants.SORT_ORDER_ASCENDING ? 1 : -1; 322 } 323 324 325 private int compare( String s1, String s2 ) 326 { 327 return this.sortOrder == BrowserCoreConstants.SORT_ORDER_ASCENDING ? s1.compareToIgnoreCase( s2 ) : s2 328 .compareToIgnoreCase( s1 ); 329 } 330 331 } 332 | Popular Tags |