001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public int compare(Portlet portlet1, Portlet portlet2) {
046                    String portletTitle1 = StringPool.BLANK;
047                    String portletTitle2 = StringPool.BLANK;
048    
049                    if (_servletContext != null) {
050                            portletTitle1 = PortalUtil.getPortletTitle(
051                                    portlet1, _servletContext, _locale);
052                            portletTitle2 = PortalUtil.getPortletTitle(
053                                    portlet2, _servletContext, _locale);
054                    }
055                    else {
056                            portletTitle1 = PortalUtil.getPortletTitle(portlet1, _locale);
057                            portletTitle2 = PortalUtil.getPortletTitle(portlet2, _locale);
058                    }
059    
060                    return portletTitle1.compareTo(portletTitle2);
061            }
062    
063            private Locale _locale;
064            private ServletContext _servletContext;
065    
066    }