001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util.comparator;
016    
017    import com.liferay.portal.kernel.model.Portlet;
018    import com.liferay.portal.kernel.util.PortalUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.Serializable;
023    
024    import java.text.Collator;
025    
026    import java.util.Comparator;
027    import java.util.Locale;
028    
029    import javax.servlet.ServletContext;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PortletTitleComparator
035            implements Comparator<Portlet>, Serializable {
036    
037            public PortletTitleComparator(Locale locale) {
038                    this(null, locale);
039            }
040    
041            public PortletTitleComparator(
042                    ServletContext servletContext, Locale locale) {
043    
044                    _servletContext = servletContext;
045                    _locale = locale;
046            }
047    
048            @Override
049            public int compare(Portlet portlet1, Portlet portlet2) {
050                    String portletTitle1 = StringPool.BLANK;
051                    String portletTitle2 = StringPool.BLANK;
052    
053                    if (_servletContext != null) {
054                            portletTitle1 = PortalUtil.getPortletTitle(
055                                    portlet1, _servletContext, _locale);
056                            portletTitle2 = PortalUtil.getPortletTitle(
057                                    portlet2, _servletContext, _locale);
058                    }
059                    else {
060                            portletTitle1 = PortalUtil.getPortletTitle(portlet1, _locale);
061                            portletTitle2 = PortalUtil.getPortletTitle(portlet2, _locale);
062                    }
063    
064                    if (Validator.isNull(portletTitle1) &&
065                            Validator.isNull(portletTitle2)) {
066    
067                            return 0;
068                    }
069    
070                    if (Validator.isNull(portletTitle1)) {
071                            return 1;
072                    }
073    
074                    if (Validator.isNull(portletTitle2)) {
075                            return -1;
076                    }
077    
078                    Collator collator = Collator.getInstance(_locale);
079    
080                    return collator.compare(portletTitle1, portletTitle2);
081            }
082    
083            private final Locale _locale;
084            private final ServletContext _servletContext;
085    
086    }