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, 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.gwt.client.ui.input.category; 029 030import org.opencms.gwt.client.ui.CmsListItem; 031 032import java.util.Comparator; 033 034/** 035 * Comparator for list items with a data value widget as there main widget.<p> 036 */ 037public class CmsListItemDataComparator implements Comparator<CmsListItem> { 038 039 /** The parameter index to compare. */ 040 private int m_paramIndex; 041 042 /** Flag to indicate sorting ascending or descending. */ 043 private boolean m_ascending; 044 045 /** 046 * Default Constructor.<p> 047 * 048 * @param paramIndex the parameter index to compare 049 * @param ascending the sort order 050 */ 051 public CmsListItemDataComparator(int paramIndex, boolean ascending) { 052 053 m_paramIndex = paramIndex; 054 m_ascending = ascending; 055 } 056 057 /** 058 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 059 */ 060 public int compare(CmsListItem o1, CmsListItem o2) { 061 062 int result = 0; 063 String val1 = null; 064 String val2 = null; 065 try { 066 CmsDataValue d1 = (CmsDataValue)o1.getMainWidget(); 067 CmsDataValue d2 = (CmsDataValue)o2.getMainWidget(); 068 if (m_paramIndex == 0) { 069 val1 = d1.getLabel(); 070 val2 = d2.getLabel(); 071 } else { 072 val1 = d1.getParameter(m_paramIndex); 073 val2 = d2.getParameter(m_paramIndex); 074 } 075 } catch (@SuppressWarnings("unused") Exception e) { 076 // ignore class cast exceptions 077 } 078 079 if (val1 != null) { 080 result = val1.compareTo(val2); 081 } else if (val2 != null) { 082 result = 1; 083 } 084 return m_ascending ? result : -result; 085 } 086 087}