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.text.Collator; 031import java.util.Comparator; 032import java.util.Locale; 033 034/** 035 * Comparator for case insensitive column sorting with string localization.<p> 036 * 037 * If both list items column values are Strings then a localized collector is used for sorting; 038 * if not, the <code>{@link Comparable}</code> interface is used.<p> 039 * 040 * @since 6.0.0 041 * 042 * @see org.opencms.workplace.list.CmsListColumnDefinition 043 */ 044public class CmsListItemCaseInsensitiveComparator implements I_CmsListItemComparator { 045 046 /** 047 * Default Constructor.<p> 048 */ 049 public CmsListItemCaseInsensitiveComparator() { 050 051 // no-op 052 } 053 054 /** 055 * @see org.opencms.workplace.list.I_CmsListItemComparator#getComparator(java.lang.String, java.util.Locale) 056 */ 057 public Comparator<CmsListItem> getComparator(final String columnId, final Locale locale) { 058 059 final Collator collator = Collator.getInstance(locale); 060 061 return new Comparator<CmsListItem>() { 062 063 /** 064 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 065 */ 066 @SuppressWarnings({"rawtypes", "unchecked"}) 067 public int compare(CmsListItem o1, CmsListItem o2) { 068 069 Comparable c1 = (Comparable)o1.get(columnId); 070 Comparable c2 = (Comparable)o2.get(columnId); 071 if ((c1 instanceof String) && (c2 instanceof String)) { 072 return collator.compare(((String)c1).toUpperCase(), ((String)c2).toUpperCase()); 073 } else if (c1 != null) { 074 if (c2 == null) { 075 return 1; 076 } 077 return c1.compareTo(c2); 078 } else if (c2 != null) { 079 return -1; 080 } 081 return 0; 082 } 083 }; 084 } 085}