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 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* 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 |
*
|
11 |
* 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 |
*
|
16 |
* 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 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
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 |
import org.gvsig.i18n.I18nException; |
34 |
import org.gvsig.i18n.I18nManager; |
35 |
import org.gvsig.i18n.Messages; |
36 |
|
37 |
/**
|
38 |
* TableModel to show the list of available Locales in gvSIG.
|
39 |
*
|
40 |
* @author <a href="mailto:dcervera@disid.com">David Cervera</a>
|
41 |
*/
|
42 |
public class LocaleTableModel extends AbstractTableModel { |
43 |
|
44 |
// Start from 0 to enable de locale column again
|
45 |
public static final int COLUMN_LOCALE = -1; |
46 |
|
47 |
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 |
private int originalLocaleIndex; |
64 |
|
65 |
/**
|
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 |
case COLUMN_LOCALE:
|
84 |
return locales[rowIndex];
|
85 |
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 |
case COLUMN_LOCALE:
|
101 |
return Messages.getText("I18nPreferencePage.Locale_code"); |
102 |
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 |
case COLUMN_LOCALE:
|
117 |
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 |
*
|
141 |
* @param rowIndex
|
142 |
* the table row index
|
143 |
* @return the {@link Locale}
|
144 |
*/
|
145 |
public Locale getLocale(int rowIndex) { |
146 |
return rowIndex >= 0 ? locales[rowIndex] : null; |
147 |
} |
148 |
|
149 |
/**
|
150 |
* Removes and uninstalls a locale.
|
151 |
*
|
152 |
* @param locale
|
153 |
* to remove
|
154 |
* @throws I18nException
|
155 |
* if there is an error removing a Locale
|
156 |
*/
|
157 |
public void removeLocale(Locale locale) throws I18nException { |
158 |
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 |
manager.setCurrentLocale(getSelectedLocale()); |
174 |
originalLocaleIndex = currentLocaleIndex; |
175 |
} |
176 |
|
177 |
public void selectPreviousLocale() { |
178 |
setSelectedLocale(originalLocaleIndex, COLUMN_ACTIVE); |
179 |
} |
180 |
|
181 |
/**
|
182 |
*
|
183 |
*/
|
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 |
|
200 |
public boolean isValueChanged() { |
201 |
return currentLocaleIndex != originalLocaleIndex;
|
202 |
} |
203 |
|
204 |
public void setChangesApplied() { |
205 |
originalLocaleIndex = currentLocaleIndex; |
206 |
} |
207 |
|
208 |
/**
|
209 |
* Sets the current selected locale.
|
210 |
*/
|
211 |
public void setSelectedLocale(Locale locale) { |
212 |
int localeIndex = getLocaleRowIndex(locale);
|
213 |
setSelectedLocale(localeIndex); |
214 |
} |
215 |
|
216 |
/**
|
217 |
* Returns the current selected locale.
|
218 |
*/
|
219 |
public Locale getSelectedLocale() { |
220 |
Locale selected = getLocale(currentLocaleIndex);
|
221 |
return selected == null ? manager.getCurrentLocale() : selected; |
222 |
} |
223 |
|
224 |
private void setSelectedLocale(int rowIndex) { |
225 |
setSelectedLocale(rowIndex, 0);
|
226 |
} |
227 |
|
228 |
private void setSelectedLocale(int rowIndex, int columnIndex) { |
229 |
int oldCurrentLocaleIndex = currentLocaleIndex;
|
230 |
currentLocaleIndex = rowIndex; |
231 |
fireTableCellUpdated(oldCurrentLocaleIndex, columnIndex); |
232 |
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 |
originalLocaleIndex = currentLocaleIndex; |
243 |
} |
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 |
} |