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