001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.component; 021 022import java.io.Serializable; 023import java.text.Collator; 024import java.util.Comparator; 025import java.util.Locale; 026 027import javax.faces.context.FacesContext; 028import javax.faces.model.SelectItem; 029 030import org.apache.commons.lang.StringUtils; 031 032/** 033 * Orders select items by id or label, handling localization. 034 * 035 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a> 036 */ 037public class SelectItemComparator implements Comparator<SelectItem>, Serializable { 038 039 private static final long serialVersionUID = 1L; 040 041 private final String[] ordering; 042 043 private final Boolean caseSensitive; 044 045 private final Collator collator; 046 047 private final Locale locale; 048 049 public SelectItemComparator(String ordering, Boolean caseSentitive, Locale locale) { 050 this.ordering = StringUtils.split(ordering, ","); 051 caseSensitive = caseSentitive; 052 if (locale == null) { 053 FacesContext context = FacesContext.getCurrentInstance(); 054 this.locale = context.getViewRoot().getLocale(); 055 } else { 056 this.locale = locale; 057 } 058 collator = Collator.getInstance(this.locale); 059 if (Boolean.TRUE.equals(caseSensitive)) { 060 collator.setStrength(Collator.TERTIARY); // TERTIARY will make a 061 // difference between 'a' 062 // and 'A' 063 } else { 064 collator.setStrength(Collator.SECONDARY); 065 } 066 } 067 068 public SelectItemComparator(String ordering, Boolean caseSensitive) { 069 this(ordering, caseSensitive, null); 070 } 071 072 protected int compareField(String field, SelectItem item1, SelectItem item2) { 073 if ("label".equals(field)) { 074 String label1 = item1.getLabel(); 075 String label2 = item2.getLabel(); 076 return collator.compare(label1, label2); 077 } else if ("id".equals(field)) { 078 String value1 = String.valueOf(item1.getValue()); 079 String value2 = String.valueOf(item2.getValue()); 080 // TODO: maybe deal with numbers comparisons (?) 081 return collator.compare(value1, value2); 082 } else { 083 throw new RuntimeException("Invalid sort criteria " + field); 084 } 085 } 086 087 @Override 088 public int compare(SelectItem item1, SelectItem item2) { 089 for (String field : ordering) { 090 int compare = compareField(field, item1, item2); 091 if (compare != 0) { 092 return compare; 093 } 094 } 095 return 0; 096 } 097 098}