001    /**
002     * Copyright (c) 2000-2013 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.util.comparator;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.model.Portlet;
019    import com.liferay.portal.util.PortalUtil;
020    
021    import java.io.Serializable;
022    
023    import java.util.Comparator;
024    import java.util.Locale;
025    
026    import javax.servlet.ServletContext;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class PortletTitleComparator
032            implements Comparator<Portlet>, Serializable {
033    
034            public PortletTitleComparator(Locale locale) {
035                    _locale = locale;
036            }
037    
038            public PortletTitleComparator(
039                    ServletContext servletContext, Locale locale) {
040    
041                    _servletContext = servletContext;
042                    _locale = locale;
043            }
044    
045            @Override
046            public int compare(Portlet portlet1, Portlet portlet2) {
047                    String portletTitle1 = StringPool.BLANK;
048                    String portletTitle2 = StringPool.BLANK;
049    
050                    if (_servletContext != null) {
051                            portletTitle1 = PortalUtil.getPortletTitle(
052                                    portlet1, _servletContext, _locale);
053                            portletTitle2 = PortalUtil.getPortletTitle(
054                                    portlet2, _servletContext, _locale);
055                    }
056                    else {
057                            portletTitle1 = PortalUtil.getPortletTitle(portlet1, _locale);
058                            portletTitle2 = PortalUtil.getPortletTitle(portlet2, _locale);
059                    }
060    
061                    return portletTitle1.compareTo(portletTitle2);
062            }
063    
064            private Locale _locale;
065            private ServletContext _servletContext;
066    
067    }