001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/list/CmsListItemActionIconComparator.java,v $
003: * Date : $Date: 2008-02-27 12:05:28 $
004: * Version: $Revision: 1.11 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.list;
033:
034: import java.util.Comparator;
035: import java.util.Iterator;
036: import java.util.Locale;
037:
038: /**
039: * Comparator for column sorting by first direct action icon names.<p>
040: *
041: * If the list items column definition has at least one direct action,
042: * the icon of the first visible action is used for sorting
043: * (using the <code>{@link I_CmsListDirectAction#setItem(CmsListItem)}</code> method);
044: * if not, the <code>{@link Comparable}</code> interface is used.<p>
045: *
046: * @author Michael Moossen
047: *
048: * @version $Revision: 1.11 $
049: *
050: * @since 6.0.0
051: *
052: * @see org.opencms.workplace.list.CmsListColumnDefinition
053: */
054: public class CmsListItemActionIconComparator implements
055: I_CmsListItemComparator {
056:
057: /**
058: * Default Constructor.<p>
059: */
060: public CmsListItemActionIconComparator() {
061:
062: // no-op
063: }
064:
065: /**
066: * @see org.opencms.workplace.list.I_CmsListItemComparator#getComparator(java.lang.String, java.util.Locale)
067: */
068: public Comparator getComparator(final String columnId,
069: final Locale locale) {
070:
071: return new Comparator() {
072:
073: /**
074: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
075: */
076: public int compare(Object o1, Object o2) {
077:
078: CmsListItem li1 = (CmsListItem) o1;
079: CmsListItem li2 = (CmsListItem) o2;
080: CmsListColumnDefinition col = li1.getMetadata()
081: .getColumnDefinition(columnId);
082: if (col.getDirectActions().size() > 0) {
083: String i1 = null;
084: String i2 = null;
085: Iterator it = col.getDirectActions().iterator();
086: while (it.hasNext()) {
087: I_CmsListDirectAction action = (I_CmsListDirectAction) it
088: .next();
089: CmsListItem tmp = action.getItem();
090: action.setItem(li1);
091: if (action.isVisible()) {
092: i1 = action.getIconPath();
093: }
094: action.setItem(li2);
095: if (action.isVisible()) {
096: i2 = action.getIconPath();
097: }
098: action.setItem(tmp);
099: }
100: if (i1 != null) {
101: if (i2 == null) {
102: return 1;
103: }
104: return i1.compareTo(i2);
105: } else if (i2 != null) {
106: return -1;
107: }
108: return 0;
109: } else {
110: Comparable c1 = (Comparable) ((CmsListItem) o1)
111: .get(columnId);
112: Comparable c2 = (Comparable) ((CmsListItem) o2)
113: .get(columnId);
114: if (c1 != null) {
115: if (c2 == null) {
116: return 1;
117: }
118: return c1.compareTo(c2);
119: } else if (c2 != null) {
120: return -1;
121: }
122: return 0;
123: }
124: }
125: };
126: }
127: }
|