Rev 154 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package org.kawai.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.mentawai.list.ListData;
import org.mentawai.list.SimpleListData;
/**
* Model class Language.
*
* @author Sergio Oliveira
*
*/
public class Language
{
/** Attribute LANGUAGES of Language. */
private static final List<Language
> LANGUAGES =
new ArrayList<Language
>(16);
/** Attribute id of Language. */
private final int id
;
/** Attribute name of Language. */
private final String name
;
/** Attribute loc of Language. */
private final Locale loc
;
/** Attribute imageFile of Language. */
private final String imageFile
;
public Language
(int code,
String name,
String locale,
String imageFile
) {
this.
id = code
;
this.
name = name
;
this.
loc = getLocaleFromString
(locale
);
this.
imageFile = imageFile
;
}
public Language
(int code,
String name,
String locale
) {
this(code, name, locale,
null);
}
public static void add
(Language language
) {
LANGUAGES.
add(language
);
}
public static Language getDefault
() {
if (LANGUAGES.
isEmpty()) throw new IllegalStateException("No languages were defined!");
return LANGUAGES.
get(0);
}
public int getId
() {
return id
;
}
public String getName
() {
return name
;
}
public Locale getLocale
() {
return loc
;
}
public String getImageFile
() {
return imageFile
;
}
@
Override
public String toString
() {
return name
;
}
public static ListData getListData
() {
SimpleListData list =
new SimpleListData
("languages");
for(Language l : LANGUAGES
) {
list.
add(l.
getId(), l.
getName());
}
return list
;
}
public static Language fromId
(int id
) {
for(Language l : LANGUAGES
) {
if (l.
getId() == id
) return l
;
}
return null;
}
public static Language fromLocale
(Locale loc
) {
if (loc ==
null) return null;
for(Language l : LANGUAGES
) {
if (l.
getLocale().
equals(loc
)) return l
;
}
for(Language l : LANGUAGES
) {
if (l.
getLocale().
getLanguage().
equals(loc.
getLanguage())) {
return l
;
}
}
return null;
}
public static Language fromLocale
(String s
) {
if (s ==
null) return null;
Locale loc = getLocaleFromString
(s
);
return fromLocale
(loc
);
}
public static Language fromName
(String name
) {
if (name ==
null) return null;
for(Language l : LANGUAGES
) {
if (l.
getName().
equalsIgnoreCase(name
)) return l
;
}
return null;
}
public static Locale getLocaleFromString
(String s
) {
if (s ==
null) return null;
String[] temp = s.
split("_");
if (temp.
length ==
1) {
return new Locale(temp
[0]);
} else if (temp.
length ==
2) {
return new Locale(temp
[0], temp
[1]);
} else if (temp.
length ==
3) {
return new Locale(temp
[0], temp
[1], temp
[2]);
}
return null;
}
public static List<Language
> all
() {
return LANGUAGES
;
}
public static void clear
() {
LANGUAGES.
clear();
}
}