KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > util > LocaleComparator


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.util;
24
25 import java.io.*;
26 import java.util.*;
27
28 /**
29  * Compares locales to sort them according to their display name.
30  */

31 public class LocaleComparator implements Comparator, Serializable {
32   private Locale locale;
33
34   /**
35    * Creates a new instance using the system default locale to get display names.
36    */

37   public LocaleComparator() {
38     locale = null;
39   }
40
41   /**
42    * Creates a new instance using the given locale to get display names.
43    */

44   public LocaleComparator(Locale locale) {
45     setLocale(locale);
46   }
47
48   /**
49    * Sets the locale used to get the display names, so one can sort the locales
50    * according to a specific language.
51    */

52   public void setLocale(Locale locale) {
53     this.locale = locale;
54   }
55
56   /**
57    * @return the locale used to get the display names.
58    */

59   public Locale getLocale() {
60     return locale;
61   }
62
63   /**
64    * Compares two locales.
65    */

66   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
67     try {
68       Locale l1 = (Locale) o1;
69       Locale l2 = (Locale) o2;
70
71       if (locale == null) {
72         return l1.getDisplayName().compareTo(l2.getDisplayName());
73       } else {
74         return l1.getDisplayName(locale).compareTo(l2.getDisplayName(locale));
75       }
76     } catch (Exception JavaDoc ex) {}
77
78     return 0;
79   }
80 }
81
Popular Tags