KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > list > CmsListItemActionIconComparator


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/list/CmsListItemActionIconComparator.java,v $
3  * Date : $Date: 2006/03/27 14:52:28 $
4  * Version: $Revision: 1.8 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.list;
33
34 import java.util.Comparator JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Locale JavaDoc;
37
38 /**
39  * Comparator for column sorting by first direct action icon names.<p>
40  *
41  * If the list items column definition has at least one direct action,
42  * the icon of the first visible action is used for sorting
43  * (using the <code>{@link I_CmsListDirectAction#setItem(CmsListItem)}</code> method);
44  * if not, the <code>{@link Comparable}</code> interface is used.<p>
45  *
46  * @author Michael Moossen
47  *
48  * @version $Revision: 1.8 $
49  *
50  * @since 6.0.0
51  *
52  * @see org.opencms.workplace.list.CmsListColumnDefinition
53  */

54 public class CmsListItemActionIconComparator implements I_CmsListItemComparator {
55
56     /**
57      * Default Constructor.<p>
58      */

59     public CmsListItemActionIconComparator() {
60
61         // no-op
62
}
63
64     /**
65      * @see org.opencms.workplace.list.I_CmsListItemComparator#getComparator(java.lang.String, java.util.Locale)
66      */

67     public Comparator JavaDoc getComparator(final String JavaDoc columnId, final Locale JavaDoc locale) {
68
69         return new Comparator JavaDoc() {
70
71             /**
72              * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
73              */

74             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
75
76                 CmsListItem li1 = (CmsListItem)o1;
77                 CmsListItem li2 = (CmsListItem)o2;
78                 CmsListColumnDefinition col = li1.getMetadata().getColumnDefinition(columnId);
79                 if (col.getDirectActions().size() > 0) {
80                     String JavaDoc i1 = null;
81                     String JavaDoc i2 = null;
82                     Iterator JavaDoc it = col.getDirectActions().iterator();
83                     while (it.hasNext()) {
84                         I_CmsListDirectAction action = (I_CmsListDirectAction)it.next();
85                         CmsListItem tmp = action.getItem();
86                         action.setItem(li1);
87                         if (action.isVisible()) {
88                             i1 = action.getIconPath();
89                         }
90                         action.setItem(li2);
91                         if (action.isVisible()) {
92                            i2 = action.getIconPath();
93                         }
94                         action.setItem(tmp);
95                     }
96                     if (i1 != null) {
97                         if (i2 == null) {
98                             return 1;
99                         }
100                         return i1.compareTo(i2);
101                     } else if (i2 != null) {
102                         return -1;
103                     }
104                     return 0;
105                 } else {
106                     Comparable JavaDoc c1 = (Comparable JavaDoc)((CmsListItem)o1).get(columnId);
107                     Comparable JavaDoc c2 = (Comparable JavaDoc)((CmsListItem)o2).get(columnId);
108                     if (c1 != null) {
109                         if (c2 == null) {
110                             return 1;
111                         }
112                         return c1.compareTo(c2);
113                     } else if (c2 != null) {
114                         return -1;
115                     }
116                     return 0;
117                 }
118             }
119         };
120     }
121 }
Popular Tags