Statistics
| Revision:

root / tags / v2_0_0_Build_2051 / extensions / extI18n / src / main / java / org / gvsig / i18n / extension / preferences / table / LocaleTableModel.java @ 38726

History | View | Annotate | Download (6.89 KB)

1 25963 cordinyana
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4 31544 cordinyana
 * of the Valencian Government (CIT)
5 28944 fdiaz
 *
6 25963 cordinyana
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10 28944 fdiaz
 *
11 25963 cordinyana
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15 28944 fdiaz
 *
16 25963 cordinyana
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18 28944 fdiaz
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 25963 cordinyana
 * MA  02110-1301, USA.
20 28944 fdiaz
 *
21 25963 cordinyana
 */
22
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {New extension for installation and update of text translations}
26
 */
27
package org.gvsig.i18n.extension.preferences.table;
28
29
import java.util.Locale;
30
31
import javax.swing.table.AbstractTableModel;
32
33 38564 jjdelcerro
import org.gvsig.i18n.I18nException;
34
import org.gvsig.i18n.I18nManager;
35
import org.gvsig.i18n.Messages;
36 25963 cordinyana
37
/**
38
 * TableModel to show the list of available Locales in gvSIG.
39 28944 fdiaz
 *
40 25963 cordinyana
 * @author <a href="mailto:dcervera@disid.com">David Cervera</a>
41
 */
42
public class LocaleTableModel extends AbstractTableModel {
43
44 28461 cordinyana
    // Start from 0 to enable de locale column again
45
    public static final int COLUMN_LOCALE = -1;
46
47 25963 cordinyana
    public static final int COLUMN_LANGUAGE = 0;
48
49
    public static final int COLUMN_COUNTRY = 1;
50
51
    public static final int COLUMN_VARIANT = 2;
52
53
    public static final int COLUMN_ACTIVE = 3;
54
55
    private static final long serialVersionUID = -3855372372788577788L;
56
57
    private final I18nManager manager;
58
59
    private Locale[] locales;
60
61
    private int currentLocaleIndex;
62
63 26058 cordinyana
    private int originalLocaleIndex;
64
65 25963 cordinyana
    /**
66
     * Creates a LocaleTableModel with a I18nManager to handle locales.
67
     */
68
    public LocaleTableModel(I18nManager manager) {
69
        this.manager = manager;
70
        loadLocales();
71
    }
72
73
    public int getColumnCount() {
74
        return 4;
75
    }
76
77
    public int getRowCount() {
78
        return locales.length;
79
    }
80
81
    public Object getValueAt(int rowIndex, int columnIndex) {
82
        switch (columnIndex) {
83 28461 cordinyana
        case COLUMN_LOCALE:
84
            return locales[rowIndex];
85 25963 cordinyana
        case COLUMN_LANGUAGE:
86
            return manager.getLanguageDisplayName(locales[rowIndex]);
87
        case COLUMN_COUNTRY:
88
            return locales[rowIndex].getDisplayCountry();
89
        case COLUMN_VARIANT:
90
            return locales[rowIndex].getDisplayVariant();
91
        case COLUMN_ACTIVE:
92
            return rowIndex == currentLocaleIndex ? Boolean.TRUE
93
                    : Boolean.FALSE;
94
        }
95
        return null;
96
    }
97
98
    public String getColumnName(int columnIndex) {
99
        switch (columnIndex) {
100 28461 cordinyana
        case COLUMN_LOCALE:
101
            return Messages.getText("I18nPreferencePage.Locale_code");
102 25963 cordinyana
        case COLUMN_LANGUAGE:
103
            return Messages.getText("I18nPreferencePage.Idioma");
104
        case COLUMN_COUNTRY:
105
            return Messages.getText("I18nPreferencePage.Pais");
106
        case COLUMN_VARIANT:
107
            return Messages.getText("I18nPreferencePage.Variante");
108
        case COLUMN_ACTIVE:
109
            return Messages.getText("I18nPreferencePage.Activar");
110
        }
111
        return "";
112
    }
113
114
    public Class getColumnClass(int columnIndex) {
115
        switch (columnIndex) {
116 28461 cordinyana
        case COLUMN_LOCALE:
117 25963 cordinyana
        case COLUMN_LANGUAGE:
118
        case COLUMN_COUNTRY:
119
        case COLUMN_VARIANT:
120
            return String.class;
121
        case COLUMN_ACTIVE:
122
            return Boolean.class;
123
        }
124
        return Object.class;
125
    }
126
127
    public boolean isCellEditable(int rowIndex, int columnIndex) {
128
        return columnIndex == 3;
129
    }
130
131
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
132
        // Only edit current locale value
133
        if (columnIndex == COLUMN_ACTIVE && Boolean.TRUE.equals(value)) {
134
            setSelectedLocale(rowIndex, columnIndex);
135
        }
136
    }
137
138
    /**
139
     * Returns the {@link Locale} shown in a table row.
140 28944 fdiaz
     *
141 25963 cordinyana
     * @param rowIndex
142
     *            the table row index
143
     * @return the {@link Locale}
144
     */
145
    public Locale getLocale(int rowIndex) {
146 28944 fdiaz
            return rowIndex >= 0 ? locales[rowIndex] : null;
147 25963 cordinyana
    }
148
149
    /**
150
     * Removes and uninstalls a locale.
151 28944 fdiaz
     *
152 25963 cordinyana
     * @param locale
153
     *            to remove
154 26048 cordinyana
     * @throws I18nException
155
     *             if there is an error removing a Locale
156 25963 cordinyana
     */
157 26048 cordinyana
    public void removeLocale(Locale locale) throws I18nException {
158 25963 cordinyana
        int rowIndex = getLocaleRowIndex(locale);
159
        manager.uninstallLocale(locale);
160
        loadLocales();
161
        fireTableRowsDeleted(rowIndex, rowIndex);
162
    }
163
164
    /**
165
     * Reloads the list of installed locales.
166
     */
167
    public void reloadLocales() {
168
        loadLocales();
169
        fireTableDataChanged();
170
    }
171
172
    public void saveSelectedLocale() {
173 26058 cordinyana
        manager.setCurrentLocale(getSelectedLocale());
174
        originalLocaleIndex = currentLocaleIndex;
175 25963 cordinyana
    }
176 26058 cordinyana
177
    public void selectPreviousLocale() {
178
        setSelectedLocale(originalLocaleIndex, COLUMN_ACTIVE);
179
    }
180 26324 cordinyana
181
    /**
182 28944 fdiaz
     *
183 26324 cordinyana
     */
184
    public void selectDefaultLocale() {
185
        Locale defLocale = manager.getDefaultSystemLocale();
186
        int pos = getLocaleRowIndex(defLocale);
187
        // Maybe the locale has country and/or variant and is not registered
188
        // (ex: en_US). Look for the locale with only the language (ex: en).
189
        if (pos == -1) {
190
            defLocale = new Locale(defLocale.getLanguage());
191
            pos = getLocaleRowIndex(defLocale);
192
        }
193
        // If the locale could'nt be found, use English as default
194
        if (pos == -1) {
195
            pos = getLocaleRowIndex(I18nManager.ENGLISH);
196
        }
197
        setSelectedLocale(pos, COLUMN_ACTIVE);
198
    }
199 28944 fdiaz
200 26058 cordinyana
    public boolean isValueChanged() {
201
        return currentLocaleIndex != originalLocaleIndex;
202 25963 cordinyana
    }
203 28944 fdiaz
204 26058 cordinyana
    public void setChangesApplied() {
205
        originalLocaleIndex = currentLocaleIndex;
206
    }
207 28944 fdiaz
208 26058 cordinyana
    /**
209
     * Sets the current selected locale.
210
     */
211
    public void setSelectedLocale(Locale locale) {
212 25963 cordinyana
        int localeIndex = getLocaleRowIndex(locale);
213 26058 cordinyana
        setSelectedLocale(localeIndex);
214 25963 cordinyana
    }
215
216 26058 cordinyana
    /**
217
     * Returns the current selected locale.
218
     */
219
    public Locale getSelectedLocale() {
220 28944 fdiaz
        Locale selected = getLocale(currentLocaleIndex);
221
        return selected == null ? manager.getCurrentLocale() : selected;
222 26058 cordinyana
    }
223
224
    private void setSelectedLocale(int rowIndex) {
225
        setSelectedLocale(rowIndex, 0);
226
    }
227
228 25963 cordinyana
    private void setSelectedLocale(int rowIndex, int columnIndex) {
229 26341 cordinyana
        int oldCurrentLocaleIndex = currentLocaleIndex;
230 25963 cordinyana
        currentLocaleIndex = rowIndex;
231 26341 cordinyana
        fireTableCellUpdated(oldCurrentLocaleIndex, columnIndex);
232 25963 cordinyana
        fireTableCellUpdated(rowIndex, columnIndex);
233
    }
234
235
    /**
236
     * Loads the locales from the I18nManager.
237
     */
238
    private void loadLocales() {
239
        locales = manager.getInstalledLocales();
240
        Locale currentLocale = manager.getCurrentLocale();
241
        currentLocaleIndex = getLocaleRowIndex(currentLocale);
242 26058 cordinyana
        originalLocaleIndex = currentLocaleIndex;
243 25963 cordinyana
    }
244
245
    /**
246
     * Returns the table row position for a Locale.
247
     */
248
    private int getLocaleRowIndex(Locale locale) {
249
        for (int i = 0; i < locales.length; i++) {
250
            if (locale.equals(locales[i])) {
251
                return i;
252
            }
253
        }
254
        return -1;
255
    }
256
}