KickJava   Java API By Example, From Geeks To Geeks.

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


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.entryeditor;
22
23
24 import java.util.Arrays JavaDoc;
25 import java.util.Comparator JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
28 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
29 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
30 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
31 import org.apache.directory.ldapstudio.browser.core.model.IValue;
32 import org.eclipse.jface.viewers.TreeViewer;
33 import org.eclipse.jface.viewers.Viewer;
34 import org.eclipse.jface.viewers.ViewerSorter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.events.SelectionListener;
37 import org.eclipse.swt.widgets.TreeColumn;
38
39
40 /**
41  * The EntryEditorWidgetSorter implements the Sorter for the entry editor widget.
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class EntryEditorWidgetSorter extends ViewerSorter implements SelectionListener
47 {
48
49     /** The tree viewer. */
50     private TreeViewer viewer;
51
52     /** The sort property. */
53     private int sortBy;
54
55     /** The sort order. */
56     private int sortOrder;
57
58     /** The preferences. */
59     private EntryEditorWidgetPreferences preferences;
60
61
62     /**
63      * Creates a new instance of EntryEditorWidgetSorter.
64      *
65      * @param preferences the preferences
66      */

67     public EntryEditorWidgetSorter( EntryEditorWidgetPreferences preferences )
68     {
69         this.sortBy = BrowserCoreConstants.SORT_BY_NONE;
70         this.sortOrder = BrowserCoreConstants.SORT_ORDER_NONE;
71         this.preferences = preferences;
72     }
73
74
75     /**
76      * Connects this sorter to the given tree viewer.
77      *
78      * @param viewer the viewer
79      */

80     public void connect( TreeViewer viewer )
81     {
82         this.viewer = viewer;
83         viewer.setSorter( this );
84
85         TreeColumn[] columns = ( ( TreeViewer ) viewer ).getTree().getColumns();
86         for ( int i = 0; i < columns.length; i++ )
87         {
88             columns[i].addSelectionListener( this );
89         }
90     }
91
92
93     /**
94      * Disposes this sorter.
95      */

96     public void dispose()
97     {
98         viewer = null;
99         preferences = null;
100     }
101
102
103     /**
104      * {@inheritDoc}
105      */

106     public void widgetDefaultSelected( SelectionEvent e )
107     {
108     }
109
110
111     /**
112      * {@inheritDoc}
113      *
114      * Switches the sort property and sort order when clicking the table headers.
115      */

116     public void widgetSelected( SelectionEvent e )
117     {
118         if ( e.widget instanceof TreeColumn && viewer != null )
119         {
120             int index = viewer.getTree().indexOf( ( ( TreeColumn ) e.widget ) );
121             switch ( index )
122             {
123                 case EntryEditorWidgetTableMetadata.KEY_COLUMN_INDEX:
124                     if ( sortBy == BrowserCoreConstants.SORT_BY_ATTRIBUTE_DESCRIPTION )
125                     {
126                         // toggle sort order
127
sortOrder = sortOrder == BrowserCoreConstants.SORT_ORDER_NONE ? BrowserCoreConstants.SORT_ORDER_ASCENDING
128                             : sortOrder == BrowserCoreConstants.SORT_ORDER_ASCENDING ? BrowserCoreConstants.SORT_ORDER_DESCENDING
129                                 : BrowserCoreConstants.SORT_ORDER_NONE;
130                     }
131                     else
132                     {
133                         // set new sort by
134
sortBy = BrowserCoreConstants.SORT_BY_ATTRIBUTE_DESCRIPTION;
135                         sortOrder = BrowserCoreConstants.SORT_ORDER_ASCENDING;
136                     }
137                     break;
138                 case EntryEditorWidgetTableMetadata.VALUE_COLUMN_INDEX:
139                     if ( sortBy == BrowserCoreConstants.SORT_BY_VALUE )
140                     {
141                         // toggle sort order
142
sortOrder = sortOrder == BrowserCoreConstants.SORT_ORDER_NONE ? BrowserCoreConstants.SORT_ORDER_ASCENDING
143                             : sortOrder == BrowserCoreConstants.SORT_ORDER_ASCENDING ? BrowserCoreConstants.SORT_ORDER_DESCENDING
144                                 : BrowserCoreConstants.SORT_ORDER_NONE;
145                     }
146                     else
147                     {
148                         // set new sort by
149
sortBy = BrowserCoreConstants.SORT_BY_VALUE;
150                         sortOrder = BrowserCoreConstants.SORT_ORDER_ASCENDING;
151                     }
152                     break;
153                 default:
154                     ;
155             }
156             if ( sortOrder == BrowserCoreConstants.SORT_ORDER_NONE )
157             {
158                 sortBy = BrowserCoreConstants.SORT_BY_NONE;
159             }
160
161             TreeColumn[] columns = viewer.getTree().getColumns();
162             for ( int i = 0; i < columns.length; i++ )
163             {
164                 columns[i].setImage( null );
165             }
166
167             if ( sortOrder == BrowserCoreConstants.SORT_ORDER_ASCENDING )
168             {
169                 ( ( TreeColumn ) e.widget ).setImage( BrowserCommonActivator.getDefault().getImage(
170                     BrowserCommonConstants.IMG_SORT_ASCENDING ) );
171             }
172             else if ( sortOrder == BrowserCoreConstants.SORT_ORDER_DESCENDING )
173             {
174                 ( ( TreeColumn ) e.widget ).setImage( BrowserCommonActivator.getDefault().getImage(
175                     BrowserCommonConstants.IMG_SORT_DESCENDING ) );
176             }
177
178             viewer.refresh();
179         }
180     }
181
182
183     /**
184      * {@inheritDoc}
185      */

186     public void sort( final Viewer viewer, Object JavaDoc[] elements )
187     {
188         Arrays.sort( elements, new Comparator JavaDoc<Object JavaDoc>()
189         {
190             public int compare( Object JavaDoc a, Object JavaDoc b )
191             {
192                 return EntryEditorWidgetSorter.this.compare( viewer, a, b );
193             }
194         } );
195
196     }
197
198
199     /**
200      * {@inheritDoc}
201      */

202     public int compare( Viewer viewer, Object JavaDoc o1, Object JavaDoc o2 )
203     {
204         // check o1
205
IAttribute attribute1 = null;
206         IValue value1 = null;
207         if ( o1 instanceof IAttribute )
208         {
209             attribute1 = ( IAttribute ) o1;
210         }
211         else if ( o1 instanceof IValue )
212         {
213             value1 = ( IValue ) o1;
214             attribute1 = value1.getAttribute();
215         }
216
217         // check o2
218
IAttribute attribute2 = null;
219         IValue value2 = null;
220         if ( o2 instanceof IAttribute )
221         {
222             attribute2 = ( IAttribute ) o2;
223         }
224         else if ( o2 instanceof IValue )
225         {
226             value2 = ( IValue ) o2;
227             attribute2 = value2.getAttribute();
228         }
229
230         // compare
231
if ( value1 != null && value2 != null )
232         {
233             if ( getSortByOrDefault() == BrowserCoreConstants.SORT_BY_ATTRIBUTE_DESCRIPTION )
234             {
235                 if ( value1.getAttribute() != value2.getAttribute() )
236                 {
237                     return compareAttributes( value1.getAttribute(), value2.getAttribute() );
238                 }
239                 else
240                 {
241                     return compareValues( value1, value2 );
242                 }
243             }
244             else if ( getSortByOrDefault() == BrowserCoreConstants.SORT_BY_VALUE )
245             {
246                 return compareValues( value1, value2 );
247             }
248             else
249             {
250                 return equal();
251             }
252         }
253         else if ( attribute1 != null && attribute2 != null )
254         {
255             return compareAttributes( attribute1, attribute2 );
256         }
257         else
258         {
259             return equal();
260         }
261     }
262
263
264     /**
265      * Compares attribute kind or description.
266      *
267      * @param attribute1 the attribute1
268      * @param attribute2 the attribute2
269      *
270      * @return the compare result
271      */

272     private int compareAttributes( IAttribute attribute1, IAttribute attribute2 )
273     {
274         if ( this.sortOrder == BrowserCoreConstants.SORT_ORDER_NONE )
275         {
276             if ( preferences == null || preferences.isObjectClassAndMustAttributesFirst() )
277             {
278                 if ( attribute1.isObjectClassAttribute() )
279                 {
280                     return lessThan();
281                 }
282                 else if ( attribute2.isObjectClassAttribute() )
283                 {
284                     return greaterThan();
285                 }
286
287                 if ( attribute1.isMustAttribute() && !attribute2.isMustAttribute() )
288                 {
289                     return lessThan();
290                 }
291                 else if ( attribute2.isMustAttribute() && !attribute1.isMustAttribute() )
292                 {
293                     return greaterThan();
294                 }
295             }
296             if ( preferences == null || preferences.isOperationalAttributesLast() )
297             {
298                 if ( attribute1.isOperationalAttribute() && !attribute2.isOperationalAttribute() )
299                 {
300                     return greaterThan();
301                 }
302                 else if ( attribute2.isOperationalAttribute() && !attribute1.isOperationalAttribute() )
303                 {
304                     return lessThan();
305                 }
306             }
307         }
308
309         return compare( attribute1.getDescription(), attribute2.getDescription() );
310     }
311
312
313     /**
314      * Compares values.
315      *
316      * @param value1 the value1
317      * @param value2 the value2
318      *
319      * @return the compare result
320      */

321     private int compareValues( IValue value1, IValue value2 )
322     {
323         if ( value1.isEmpty() && value2.isEmpty() )
324         {
325             return equal();
326         }
327         else if ( value1.isEmpty() && !value2.isEmpty() )
328         {
329             return greaterThan();
330         }
331         else if ( !value1.isEmpty() && value2.isEmpty() )
332         {
333             return lessThan();
334         }
335         else
336         {
337             return compare( value1.getStringValue(), value2.getStringValue() );
338         }
339     }
340
341
342     /**
343      * Gets the current sort order or the default sort order from the preferences .
344      *
345      * @return the current sort order or default sort order
346      */

347     private int getSortOrderOrDefault()
348     {
349         if ( preferences == null )
350         {
351             return BrowserCoreConstants.SORT_ORDER_ASCENDING;
352         }
353         else if ( sortOrder == BrowserCoreConstants.SORT_ORDER_NONE )
354         {
355             return preferences.getDefaultSortOrder();
356         }
357         else
358         {
359             return sortOrder;
360         }
361     }
362
363
364     /**
365      * Gets the current sort property or the default sort property from the preferences .
366      *
367      * @return the current sort property or default sort property
368      */

369     private int getSortByOrDefault()
370     {
371         if ( preferences == null )
372         {
373             return BrowserCoreConstants.SORT_BY_ATTRIBUTE_DESCRIPTION;
374         }
375         else if ( sortBy == BrowserCoreConstants.SORT_BY_NONE )
376         {
377             return preferences.getDefaultSortBy();
378         }
379         else
380         {
381             return sortBy;
382         }
383     }
384
385
386     /**
387      * Returns +1 or -1, depending on the sort order.
388      *
389      * @return +1 or -1, depending on the sort order
390      */

391     private int lessThan()
392     {
393         return this.getSortOrderOrDefault() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? -1 : 1;
394     }
395
396
397     /**
398      * Returns 0.
399      *
400      * @return 0
401      */

402     private int equal()
403     {
404         return 0;
405     }
406
407
408     /**
409      * Returns +1 or -1, depending on the sort order.
410      *
411      * @return +1 or -1, depending on the sort order
412      */

413     private int greaterThan()
414     {
415         return this.getSortOrderOrDefault() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? 1 : -1;
416     }
417
418
419     /**
420      * Compares the two strings using the Strings's compareToIgnoreCase method,
421      * pays attention for the sort order.
422      *
423      * @param s1 the first string to compare
424      * @param s2 the second string to compare
425      * @return a negative integer, zero, or a positive integer
426      * @see java.lang.String#compareToIgnoreCase(String)
427      */

428     private int compare( String JavaDoc s1, String JavaDoc s2 )
429     {
430         return this.getSortOrderOrDefault() == BrowserCoreConstants.SORT_ORDER_ASCENDING ? s1.compareToIgnoreCase( s2 )
431             : s2.compareToIgnoreCase( s1 );
432     }
433
434 }
435
Popular Tags