KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > misc > PFLocale


1 package org.enhydra.jawe.misc;
2
3 import java.util.*;
4
5 import org.enhydra.jawe.*;
6
7 /**
8  * Represents a locale that will be shown within the list. Contains a factory
9  * method that creates instance of the class by parsing the locale string that
10  * could be made of language, country and variant part.
11  * This class is introduced mainly because of presenting the default locale by
12  * "Default" key.
13  */

14 public class PFLocale {
15    private Locale loc;
16
17    /**
18    * Parses given string representing property file extendion. The given string
19    * could be made of language, country and variant part.
20    */

21    public static PFLocale createPFLocale (String JavaDoc pfExt) {
22       PFLocale pel=null;
23       try {
24          pfExt=pfExt.trim();
25          if (pfExt.equals("default")) {
26             pel=new PFLocale();
27          } else {
28             boolean hasCountry=pfExt.length()>2 && pfExt.substring(2,3).equals("_");
29             if (hasCountry) {
30                boolean hasVariant=pfExt.length()>5 && pfExt.substring(5,6).equals("_");
31                if (hasVariant) {
32                   pel=new PFLocale(pfExt.substring(0,2),
33                         pfExt.substring(3,5),
34                         pfExt.substring(6,pfExt.length()));
35                } else {
36                   if (pfExt.length()==5) {
37                      pel=new PFLocale(pfExt.substring(0,2),pfExt.substring(3,5));
38                   }
39                }
40             } else {
41                if (pfExt.length()==2) {
42                   pel=new PFLocale(pfExt);
43                }
44             }
45          }
46       } catch (Exception JavaDoc ex) {}
47
48       return pel;
49    }
50
51    public PFLocale () {
52       loc=ResourceManager.getDefaultLocale();
53    }
54
55    public PFLocale (Locale l) {
56       loc=l;
57    }
58
59    public PFLocale (String JavaDoc lang) {
60       loc=new Locale(lang);
61    }
62
63    public PFLocale (String JavaDoc lang,String JavaDoc country) {
64       loc=new Locale(lang,country);
65    }
66
67    public PFLocale (String JavaDoc lang,String JavaDoc country,String JavaDoc variant) {
68       loc=new Locale(lang,country,variant);
69    }
70
71    public String JavaDoc toString () {
72       if (loc!=ResourceManager.getDefaultLocale()) {
73          return loc.getDisplayName(ResourceManager.getChoosenLocale());
74       } else {
75          return ResourceManager.getLanguageDependentString("EnglishDefaultKey");
76       }
77    }
78
79    public Locale getLocale () {
80      return loc;
81    }
82
83    public String JavaDoc getLanguage () {
84       return loc.getLanguage();
85    }
86
87    public String JavaDoc getCountry () {
88       return loc.getCountry();
89    }
90
91    public String JavaDoc getVariant () {
92       return loc.getVariant();
93    }
94
95    public String JavaDoc getLocaleString () {
96       String JavaDoc locStr="";
97       if (loc==ResourceManager.getDefaultLocale()) {
98          return "default";
99       } else {
100          locStr=loc.getLanguage();
101          if (loc.getCountry().length()>0){
102             locStr+="_"+loc.getCountry();
103             if (loc.getVariant().length()>0) {
104                locStr+="_"+loc.getVariant();
105             }
106          }
107       }
108       return locStr;
109    }
110
111    public boolean equals(Object JavaDoc pfLoc) {
112       if (pfLoc instanceof PFLocale) {
113          return loc.equals(((PFLocale)pfLoc).loc);
114       } else {
115          return false;
116       }
117    }
118
119 }
120
121
Popular Tags