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