KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > searchresult > SearchResultEditorSorter


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.ui.editors.searchresult;
22
23
24 import java.util.Arrays JavaDoc;
25 import java.util.Comparator JavaDoc;
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         // check sort column
98
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             // toggle sort order
143
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             // set new sort by
150
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 this.sortOrder != SORT_ORDER_NONE;
187
return true;
188     }
189
190
191     public void sort( final Viewer viewer, Object JavaDoc[] elements )
192     {
193
194         if ( isSorted() )
195         {
196             Arrays.sort( elements, new Comparator JavaDoc()
197             {
198                 public int compare( Object JavaDoc a, Object JavaDoc b )
199                 {
200                     return SearchResultEditorSorter.this.compare( viewer, a, b );
201                 }
202             } );
203         }
204
205     }
206
207
208     public int compare( Viewer viewer, Object JavaDoc o1, Object JavaDoc o2 )
209     {
210
211         if ( this.search == null )
212         {
213             return this.equal();
214         }
215
216         // if(o1 == null && o2 == null) {
217
// return this.equal();
218
// }
219
// else if(o1 == null && o2 != null) {
220
// return this.lessThan();
221
// }
222
// else if(o1 != null && o2 == null) {
223
// return this.greaterThan();
224
// }
225
// else {
226
// if(!(o1 instanceof ISearchResult) && !(o2 instanceof ISearchResult))
227
// {
228
// return this.equal();
229
// }
230
// else if(!(o1 instanceof ISearchResult) && (o2 instanceof
231
// ISearchResult)) {
232
// return this.lessThan();
233
// }
234
// else if((o1 instanceof ISearchResult) && !(o2 instanceof
235
// ISearchResult)) {
236
// return this.greaterThan();
237
// }
238
// else {
239
ISearchResult sr1 = ( ISearchResult ) o1;
240         ISearchResult sr2 = ( ISearchResult ) o2;
241
242         IEntry entry1 = sr1.getEntry();
243         IEntry entry2 = sr2.getEntry();
244
245         String JavaDoc 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 JavaDoc value1 = getValue( attribute1 );
285                 String JavaDoc value2 = getValue( attribute2 );
286                 return compare( value1, value2 );
287             }
288         }
289         // }
290
// }
291
}
292
293
294     private String JavaDoc 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 JavaDoc s1, String JavaDoc s2 )
326     {
327         return this.sortOrder == BrowserCoreConstants.SORT_ORDER_ASCENDING ? s1.compareToIgnoreCase( s2 ) : s2
328             .compareToIgnoreCase( s1 );
329     }
330
331 }
332
Popular Tags