Kawai Wiki

Rev

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