001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.ui.web.component;
023
024import java.io.Serializable;
025import java.text.Collator;
026import java.util.Comparator;
027import java.util.Locale;
028
029import javax.faces.context.FacesContext;
030import javax.faces.model.SelectItem;
031
032import org.apache.commons.lang.StringUtils;
033
034/**
035 * Orders select items by id or label, handling localization.
036 *
037 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
038 */
039public class SelectItemComparator implements Comparator<SelectItem>, Serializable {
040
041    private static final long serialVersionUID = 1L;
042
043    private final String[] ordering;
044
045    private final Boolean caseSensitive;
046
047    private final Collator collator;
048
049    private final Locale locale;
050
051    public SelectItemComparator(String ordering, Boolean caseSentitive, Locale locale) {
052        this.ordering = StringUtils.split(ordering, ",");
053        caseSensitive = caseSentitive;
054        if (locale == null) {
055            FacesContext context = FacesContext.getCurrentInstance();
056            this.locale = context.getViewRoot().getLocale();
057        } else {
058            this.locale = locale;
059        }
060        collator = Collator.getInstance(this.locale);
061        if (Boolean.TRUE.equals(caseSensitive)) {
062            collator.setStrength(Collator.TERTIARY); // TERTIARY will make a
063                                                     // difference between 'a'
064                                                     // and 'A'
065        } else {
066            collator.setStrength(Collator.SECONDARY);
067        }
068    }
069
070    public SelectItemComparator(String ordering, Boolean caseSensitive) {
071        this(ordering, caseSensitive, null);
072    }
073
074    protected int compareField(String field, SelectItem item1, SelectItem item2) {
075        if ("label".equals(field)) {
076            String label1 = item1.getLabel();
077            String label2 = item2.getLabel();
078            return collator.compare(label1, label2);
079        } else if ("id".equals(field)) {
080            String value1 = String.valueOf(item1.getValue());
081            String value2 = String.valueOf(item2.getValue());
082            // TODO: maybe deal with numbers comparisons (?)
083            return collator.compare(value1, value2);
084        } else {
085            throw new RuntimeException("Invalid sort criteria " + field);
086        }
087    }
088
089    @Override
090    public int compare(SelectItem item1, SelectItem item2) {
091        for (String field : ordering) {
092            int compare = compareField(field, item1, item2);
093            if (compare != 0) {
094                return compare;
095            }
096        }
097        return 0;
098    }
099
100}