1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util.comparator;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  import com.liferay.portal.model.Portlet;
19  import com.liferay.portal.util.PortalUtil;
20  
21  import java.io.Serializable;
22  
23  import java.util.Comparator;
24  import java.util.Locale;
25  
26  import javax.servlet.ServletContext;
27  
28  /**
29   * <a href="PortletTitleComparator.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class PortletTitleComparator
34      implements Comparator<Portlet>, Serializable {
35  
36      public PortletTitleComparator(Locale locale) {
37          _locale = locale;
38      }
39  
40      public PortletTitleComparator(
41          ServletContext servletContext, Locale locale) {
42  
43          _servletContext = servletContext;
44          _locale = locale;
45      }
46  
47      public int compare(Portlet portlet1, Portlet portlet2) {
48          String portletTitle1 = StringPool.BLANK;
49          String portletTitle2 = StringPool.BLANK;
50  
51          if (_servletContext != null) {
52              portletTitle1 = PortalUtil.getPortletTitle(
53                  portlet1, _servletContext, _locale);
54              portletTitle2 = PortalUtil.getPortletTitle(
55                  portlet2, _servletContext, _locale);
56          }
57          else {
58              portletTitle1 = PortalUtil.getPortletTitle(portlet1, _locale);
59              portletTitle2 = PortalUtil.getPortletTitle(portlet2, _locale);
60          }
61  
62          return portletTitle1.compareTo(portletTitle2);
63      }
64  
65      private Locale _locale;
66      private ServletContext _servletContext;
67  
68  }