Kawai Wiki

Rev

Rev 143 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 soliveira 1
package org.kawai.model;
6 soliveira 2
 
137 soliveira 3
import java.util.ArrayList;
4
import java.util.List;
6 soliveira 5
import java.util.Locale;
6
 
137 soliveira 7
import org.mentawai.list.ListData;
8
import org.mentawai.list.SimpleListData;
6 soliveira 9
 
137 soliveira 10
public class Language {
11
 
12
        private static final List<Language> LANGUAGES = new ArrayList<Language>(16);
6 soliveira 13
 
27 soliveira 14
        private final int id;
137 soliveira 15
        private final String name;
6 soliveira 16
        private final Locale loc;
139 soliveira 17
        private final String imageFile;
6 soliveira 18
 
139 soliveira 19
    public Language(int code, String name, String locale, String imageFile) {
27 soliveira 20
        this.id = code;
137 soliveira 21
        this.name = name;
6 soliveira 22
        this.loc = getLocaleFromString(locale);
139 soliveira 23
        this.imageFile = imageFile;
6 soliveira 24
    }
137 soliveira 25
 
139 soliveira 26
    public Language(int code, String name, String locale) {
27
        this(code, name, locale, null);
28
    }
29
 
137 soliveira 30
    public static void add(Language language) {
31
        LANGUAGES.add(language);
32
    }
33
 
34
    public static Language getDefault() {
35
        if (LANGUAGES.isEmpty()) throw new IllegalStateException("No languages were defined!");
36
        return LANGUAGES.get(0);
37
    }
6 soliveira 38
 
27 soliveira 39
    public int getId() {
40
        return id;
6 soliveira 41
    }
42
 
137 soliveira 43
    public String getName() {
44
        return name;
45
    }
46
 
6 soliveira 47
    public Locale getLocale() {
48
        return loc;
49
    }
50
 
139 soliveira 51
    public String getImageFile() {
52
        return imageFile;
53
    }
54
 
137 soliveira 55
    @Override
56
    public String toString() {
57
        return name;
58
    }
59
 
60
    public static ListData getListData() {
61
        SimpleListData list = new SimpleListData("languages");
62
        for(Language l : LANGUAGES) {
63
                list.add(l.getId(), l.getName());
64
        }
65
        return list;
66
    }
67
 
27 soliveira 68
    public static Language fromId(int id) {
137 soliveira 69
        for(Language l : LANGUAGES) {
27 soliveira 70
                if (l.getId() == id) return l;
6 soliveira 71
        }
72
        return null;
73
    }
74
 
20 soliveira 75
    public static Language fromLocale(Locale loc) {
63 soliveira 76
        if (loc == null) return null;
137 soliveira 77
        for(Language l : LANGUAGES) {
20 soliveira 78
                if (l.getLocale().equals(loc)) return l;
79
        }
138 soliveira 80
        for(Language l : LANGUAGES) {
81
                if (l.getLocale().getLanguage().equals(loc.getLanguage())) {
82
                        return l;
83
                }
84
        }
20 soliveira 85
        return null;
86
    }
87
 
137 soliveira 88
    public static Language fromLocale(String s) {
63 soliveira 89
        if (s == null) return null;
40 soliveira 90
        Locale loc = getLocaleFromString(s);
91
        return fromLocale(loc);
92
    }
93
 
137 soliveira 94
    public static Language fromName(String name) {
95
        if (name == null) return null;
96
        for(Language l : LANGUAGES) {
97
                if (l.getName().equalsIgnoreCase(name)) return l;
98
        }
99
        return null;
100
    }
101
 
6 soliveira 102
    public static Locale getLocaleFromString(String s) {
63 soliveira 103
        if (s == null) return null;
6 soliveira 104
        String[] temp = s.split("_");
105
        if (temp.length == 1) {
106
            return new Locale(temp[0]);
107
        } else if (temp.length == 2) {
108
            return new Locale(temp[0], temp[1]);
109
        } else if (temp.length == 3) {
110
            return new Locale(temp[0], temp[1], temp[2]);
111
        }
112
        return null;
113
    }
140 soliveira 114
 
115
    public static List<Language> all() {
116
        return LANGUAGES;
117
    }
143 soliveira 118
 
119
    public static void clear() {
120
        LANGUAGES.clear();
121
    }
6 soliveira 122
}