svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.i18n.app / org.gvsig.i18n.app.mainplugin / src / main / java / org / gvsig / i18n / extension / preferences / table / LocaleTableModel.java @ 40557
History | View | Annotate | Download (6.94 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/*
|
25 |
* AUTHORS (In addition to CIT):
|
26 |
* 2008 {DiSiD Technologies} {New extension for installation and update of text translations}
|
27 |
*/
|
28 |
package org.gvsig.i18n.extension.preferences.table; |
29 |
|
30 |
import java.util.Locale; |
31 |
|
32 |
import javax.swing.table.AbstractTableModel; |
33 |
|
34 |
import org.gvsig.i18n.I18nException; |
35 |
import org.gvsig.i18n.I18nManager; |
36 |
import org.gvsig.i18n.Messages; |
37 |
|
38 |
/**
|
39 |
* TableModel to show the list of available Locales in gvSIG.
|
40 |
*
|
41 |
* @author <a href="mailto:dcervera@disid.com">David Cervera</a>
|
42 |
*/
|
43 |
public class LocaleTableModel extends AbstractTableModel { |
44 |
|
45 |
// Start from 0 to enable de locale column again
|
46 |
public static final int COLUMN_LOCALE = -1; |
47 |
|
48 |
public static final int COLUMN_LANGUAGE = 0; |
49 |
|
50 |
public static final int COLUMN_COUNTRY = 1; |
51 |
|
52 |
public static final int COLUMN_VARIANT = 2; |
53 |
|
54 |
public static final int COLUMN_ACTIVE = 3; |
55 |
|
56 |
private static final long serialVersionUID = -3855372372788577788L; |
57 |
|
58 |
private final I18nManager manager; |
59 |
|
60 |
private Locale[] locales; |
61 |
|
62 |
private int currentLocaleIndex; |
63 |
|
64 |
private int originalLocaleIndex; |
65 |
|
66 |
/**
|
67 |
* Creates a LocaleTableModel with a I18nManager to handle locales.
|
68 |
*/
|
69 |
public LocaleTableModel(I18nManager manager) {
|
70 |
this.manager = manager;
|
71 |
loadLocales(); |
72 |
} |
73 |
|
74 |
public int getColumnCount() { |
75 |
return 4; |
76 |
} |
77 |
|
78 |
public int getRowCount() { |
79 |
return locales.length;
|
80 |
} |
81 |
|
82 |
public Object getValueAt(int rowIndex, int columnIndex) { |
83 |
switch (columnIndex) {
|
84 |
case COLUMN_LOCALE:
|
85 |
return locales[rowIndex];
|
86 |
case COLUMN_LANGUAGE:
|
87 |
return manager.getLanguageDisplayName(locales[rowIndex]);
|
88 |
case COLUMN_COUNTRY:
|
89 |
return locales[rowIndex].getDisplayCountry();
|
90 |
case COLUMN_VARIANT:
|
91 |
return locales[rowIndex].getDisplayVariant();
|
92 |
case COLUMN_ACTIVE:
|
93 |
return rowIndex == currentLocaleIndex ? Boolean.TRUE |
94 |
: Boolean.FALSE;
|
95 |
} |
96 |
return null; |
97 |
} |
98 |
|
99 |
public String getColumnName(int columnIndex) { |
100 |
switch (columnIndex) {
|
101 |
case COLUMN_LOCALE:
|
102 |
return Messages.getText("I18nPreferencePage.Locale_code"); |
103 |
case COLUMN_LANGUAGE:
|
104 |
return Messages.getText("I18nPreferencePage.Idioma"); |
105 |
case COLUMN_COUNTRY:
|
106 |
return Messages.getText("I18nPreferencePage.Pais"); |
107 |
case COLUMN_VARIANT:
|
108 |
return Messages.getText("I18nPreferencePage.Variante"); |
109 |
case COLUMN_ACTIVE:
|
110 |
return Messages.getText("I18nPreferencePage.Activar"); |
111 |
} |
112 |
return ""; |
113 |
} |
114 |
|
115 |
public Class getColumnClass(int columnIndex) { |
116 |
switch (columnIndex) {
|
117 |
case COLUMN_LOCALE:
|
118 |
case COLUMN_LANGUAGE:
|
119 |
case COLUMN_COUNTRY:
|
120 |
case COLUMN_VARIANT:
|
121 |
return String.class; |
122 |
case COLUMN_ACTIVE:
|
123 |
return Boolean.class; |
124 |
} |
125 |
return Object.class; |
126 |
} |
127 |
|
128 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
129 |
return columnIndex == 3; |
130 |
} |
131 |
|
132 |
public void setValueAt(Object value, int rowIndex, int columnIndex) { |
133 |
// Only edit current locale value
|
134 |
if (columnIndex == COLUMN_ACTIVE && Boolean.TRUE.equals(value)) { |
135 |
setSelectedLocale(rowIndex, columnIndex); |
136 |
} |
137 |
} |
138 |
|
139 |
/**
|
140 |
* Returns the {@link Locale} shown in a table row.
|
141 |
*
|
142 |
* @param rowIndex
|
143 |
* the table row index
|
144 |
* @return the {@link Locale}
|
145 |
*/
|
146 |
public Locale getLocale(int rowIndex) { |
147 |
return rowIndex >= 0 ? locales[rowIndex] : null; |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Removes and uninstalls a locale.
|
152 |
*
|
153 |
* @param locale
|
154 |
* to remove
|
155 |
* @throws I18nException
|
156 |
* if there is an error removing a Locale
|
157 |
*/
|
158 |
public void removeLocale(Locale locale) throws I18nException { |
159 |
int rowIndex = getLocaleRowIndex(locale);
|
160 |
manager.uninstallLocale(locale); |
161 |
loadLocales(); |
162 |
fireTableRowsDeleted(rowIndex, rowIndex); |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Reloads the list of installed locales.
|
167 |
*/
|
168 |
public void reloadLocales() { |
169 |
loadLocales(); |
170 |
fireTableDataChanged(); |
171 |
} |
172 |
|
173 |
public void saveSelectedLocale() { |
174 |
manager.setCurrentLocale(getSelectedLocale()); |
175 |
originalLocaleIndex = currentLocaleIndex; |
176 |
} |
177 |
|
178 |
public void selectPreviousLocale() { |
179 |
setSelectedLocale(originalLocaleIndex, COLUMN_ACTIVE); |
180 |
} |
181 |
|
182 |
/**
|
183 |
*
|
184 |
*/
|
185 |
public void selectDefaultLocale() { |
186 |
Locale defLocale = manager.getDefaultSystemLocale();
|
187 |
int pos = getLocaleRowIndex(defLocale);
|
188 |
// Maybe the locale has country and/or variant and is not registered
|
189 |
// (ex: en_US). Look for the locale with only the language (ex: en).
|
190 |
if (pos == -1) { |
191 |
defLocale = new Locale(defLocale.getLanguage()); |
192 |
pos = getLocaleRowIndex(defLocale); |
193 |
} |
194 |
// If the locale could'nt be found, use English as default
|
195 |
if (pos == -1) { |
196 |
pos = getLocaleRowIndex(I18nManager.ENGLISH); |
197 |
} |
198 |
setSelectedLocale(pos, COLUMN_ACTIVE); |
199 |
} |
200 |
|
201 |
public boolean isValueChanged() { |
202 |
return currentLocaleIndex != originalLocaleIndex;
|
203 |
} |
204 |
|
205 |
public void setChangesApplied() { |
206 |
originalLocaleIndex = currentLocaleIndex; |
207 |
} |
208 |
|
209 |
/**
|
210 |
* Sets the current selected locale.
|
211 |
*/
|
212 |
public void setSelectedLocale(Locale locale) { |
213 |
int localeIndex = getLocaleRowIndex(locale);
|
214 |
setSelectedLocale(localeIndex); |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Returns the current selected locale.
|
219 |
*/
|
220 |
public Locale getSelectedLocale() { |
221 |
Locale selected = getLocale(currentLocaleIndex);
|
222 |
return selected == null ? manager.getCurrentLocale() : selected; |
223 |
} |
224 |
|
225 |
private void setSelectedLocale(int rowIndex) { |
226 |
setSelectedLocale(rowIndex, 0);
|
227 |
} |
228 |
|
229 |
private void setSelectedLocale(int rowIndex, int columnIndex) { |
230 |
int oldCurrentLocaleIndex = currentLocaleIndex;
|
231 |
currentLocaleIndex = rowIndex; |
232 |
fireTableCellUpdated(oldCurrentLocaleIndex, columnIndex); |
233 |
fireTableCellUpdated(rowIndex, columnIndex); |
234 |
} |
235 |
|
236 |
/**
|
237 |
* Loads the locales from the I18nManager.
|
238 |
*/
|
239 |
private void loadLocales() { |
240 |
locales = manager.getInstalledLocales(); |
241 |
Locale currentLocale = manager.getCurrentLocale();
|
242 |
currentLocaleIndex = getLocaleRowIndex(currentLocale); |
243 |
originalLocaleIndex = currentLocaleIndex; |
244 |
} |
245 |
|
246 |
/**
|
247 |
* Returns the table row position for a Locale.
|
248 |
*/
|
249 |
private int getLocaleRowIndex(Locale locale) { |
250 |
for (int i = 0; i < locales.length; i++) { |
251 |
if (locale.equals(locales[i])) {
|
252 |
return i;
|
253 |
} |
254 |
} |
255 |
return -1; |
256 |
} |
257 |
} |