Statistics
| Revision:

root / branches / v10 / extensions / extI18n / src / org / gvsig / i18n / extension / preferences / I18nPreferencePage.java @ 25963

History | View | Annotate | Download (17 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (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;
28

    
29
import java.awt.*;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.io.File;
33
import java.util.*;
34

    
35
import javax.swing.*;
36
import javax.swing.filechooser.FileFilter;
37
import javax.swing.table.TableColumn;
38

    
39
import org.gvsig.gui.beans.swing.JButton;
40
import org.gvsig.i18n.I18nManager;
41
import org.gvsig.i18n.Messages;
42
import org.gvsig.i18n.extension.preferences.table.*;
43
import org.gvsig.i18n.impl.I18nManagerImpl;
44

    
45
import com.iver.andami.preferences.AbstractPreferencePage;
46
import com.iver.andami.preferences.StoreException;
47

    
48
/**
49
 * Prefence page to manage gvSIG locales.
50
 * 
51
 * @author <a href="mailto:dcervera@disid.com">David Cervera</a>
52
 */
53
public class I18nPreferencePage extends AbstractPreferencePage implements
54
        ActionListener {
55

    
56
    private static final long serialVersionUID = 7164183052397200888L;
57

    
58
    private static final String COMMAND_UNINSTALL = "UNINSTALL";
59

    
60
    private static final String COMMAND_EXPORT = "EXPORT";
61

    
62
    private static final String COMMAND_EXPORT_NEW = "EXPORT_NEW";
63

    
64
    private static final String COMMAND_INSTALL = "INSTALL";
65

    
66
    private static final String EXPORT_JAR_FILE_EXTENSION = ".jar";
67

    
68
    private static final String EXPORT_ZIP_FILE_EXTENSION = ".zip";
69

    
70
    private ImageIcon icon;
71

    
72
    private I18nManager manager = I18nManagerImpl.getInstance();
73

    
74
    private JTable localesTable;
75

    
76
    private LocaleTableModel tableModel;
77

    
78
    private JFileChooser fileChooser;
79

    
80
    private boolean changed = false;
81

    
82
    /**
83
     * Creates a new I18n preferences page.
84
     */
85
    public I18nPreferencePage() {
86
        setParentID("com.iver.core.preferences.general.GeneralPage");
87
        icon = new ImageIcon(this.getClass().getClassLoader().getResource(
88
                "images/babel.png"));
89

    
90
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
91
        // setLayout(new BorderLayout());
92
        // setSize(new java.awt.Dimension(386, 177));
93
        // add(getPN(), java.awt.BorderLayout.NORTH);
94

    
95
        add(getLocalesPanel());
96
        add(Box.createRigidArea(new Dimension(0, 5)));
97
        add(getActiveLocaleLabel());
98
        add(Box.createRigidArea(new Dimension(0, 5)));
99
        add(getButtonsPanel());
100
        // add(getInfoLabel());
101

    
102
        // add(getLocalesPanel(), BorderLayout.NORTH);
103
        // add(getButtonsPanel(), BorderLayout.CENTER);
104
        // add(getInfoLabel(), BorderLayout.SOUTH);
105

    
106
    }
107

    
108
    public String getID() {
109
        return getClass().getName();
110
    }
111

    
112
    public String getTitle() {
113
        return Messages.getText("idioma");
114
    }
115

    
116
    public ImageIcon getIcon() {
117
        return icon;
118
    }
119

    
120
    public JPanel getPanel() {
121
        return this;
122
    }
123

    
124
    public void setChangesApplied() {
125
        changed = false;
126
    }
127

    
128
    public boolean isValueChanged() {
129
        return changed;
130
    }
131

    
132
    public void storeValues() throws StoreException {
133
        tableModel.saveSelectedLocale();
134
    }
135

    
136
    public void initializeDefaults() {
137
        tableModel.loadDefaultSelectedLocale();
138
    }
139

    
140
    public void initializeValues() {
141
        tableModel.loadDefaultSelectedLocale();
142
    }
143

    
144
    public void actionPerformed(ActionEvent event) {
145
        if (COMMAND_INSTALL.equals(event.getActionCommand())) {
146
            installLocale();
147
        }
148
        else if (COMMAND_EXPORT.equals(event.getActionCommand())) {
149
            exportLocaleForUpdate();
150
        }
151
        else if (COMMAND_EXPORT_NEW.equals(event.getActionCommand())) {
152
            exportLocaleForTranslation();
153
        }
154
        else if (COMMAND_UNINSTALL.equals(event.getActionCommand())) {
155
            uninstallSelectedLocale();
156
        }
157
    }
158

    
159
    /**
160
     * Installs a new Locale translation or updates an already existing one.
161
     */
162
    private void installLocale() {
163
        JFileChooser fileChooser = getJarFileChooser();
164

    
165
        int returnVal = fileChooser.showOpenDialog(this);
166

    
167
        if (returnVal == JFileChooser.APPROVE_OPTION) {
168
            File importFile = fileChooser.getSelectedFile();
169
            Locale[] installedLocales = manager.installLocales(importFile);
170
            if (installedLocales == null || installedLocales.length == 0) {
171
                JOptionPane
172
                        .showMessageDialog(
173
                                this,
174
                                Messages
175
                                        .getText("I18nPreferencePage.idiomas_no_encontrados_para_instalar"),
176
                                Messages
177
                                        .getText("I18nPreferencePage.error_instalar_idiomas"),
178
                                JOptionPane.ERROR_MESSAGE);
179
            }
180
            else {
181
                StringBuffer msg = new StringBuffer(Messages
182
                        .getText("I18nPreferencePage.idiomas_instalados"));
183

    
184
                for (int i = 0; i < installedLocales.length; i++) {
185
                    msg.append(manager.getDisplayName(installedLocales[i]));
186
                    if (i < installedLocales.length - 1) {
187
                        msg.append(", ");
188
                    }
189
                }
190
                tableModel.reloadLocales();
191
                JOptionPane.showMessageDialog(this, msg.toString(), Messages
192
                        .getText("I18nPreferencePage.instalar_idiomas"),
193
                        JOptionPane.INFORMATION_MESSAGE);
194
            }
195
        }
196
    }
197

    
198
    /**
199
     * Updates the translation of a locale
200
     */
201
    private void exportLocaleForUpdate() {
202
        Locale locale = getSelectedLocale();
203

    
204
        if (locale == null) {
205
            JOptionPane.showMessageDialog(this, Messages
206
                    .getText("I18nPreferencePage.seleccione_idioma"), Messages
207
                    .getText("I18nPreferencePage.error_actualizar_idioma"),
208
                    JOptionPane.ERROR_MESSAGE);
209
        }
210
        else {
211
            // Select the reference language
212
            LocaleItem[] items = getLocaleItemsForSelection(manager
213
                    .getInstalledLocales(), new Locale[] { locale });
214
            LocaleItem selected;
215
            // Select by default the current locale, or the first one
216
            // if the current locale is the one to be updated
217
            if (locale.equals(manager.getCurrentLocale())) {
218
                selected = items[0];
219
            }
220
            else {
221
                selected = new LocaleItem(manager.getCurrentLocale(), manager);
222
            }
223
            selected = (LocaleItem) JOptionPane
224
                    .showInputDialog(
225
                            this,
226
                            Messages
227
                                    .getText("I18nPreferencePage.seleccione_idioma_referencia"),
228
                            Messages
229
                                    .getText("I18nPreferencePage.exportar_idioma"),
230
                            JOptionPane.QUESTION_MESSAGE, null, items, selected);
231

    
232
            if (selected == null) {
233
                return;
234
            }
235
            // Select the file to export to
236
            JFileChooser fileChooser = getJarFileChooser();
237
            fileChooser.setSelectedFile(new File(getLocaleJarFileName(locale)));
238

    
239
            if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
240
                File saveFile = fileChooser.getSelectedFile();
241
                manager.exportLocaleForUpdate(locale, selected.getLocale(),
242
                        saveFile);
243

    
244
                JOptionPane.showMessageDialog(this, Messages
245
                        .getText("I18nPreferencePage.exportado_textos_idioma")
246
                        + " "
247
                        + manager.getDisplayName(locale)
248
                        + Messages.getText("I18nPreferencePage.al_archivo")
249
                        + saveFile, Messages
250
                        .getText("I18nPreferencePage.exportar_idioma"),
251
                        JOptionPane.INFORMATION_MESSAGE);
252
            }
253
        }
254
    }
255

    
256
    /**
257
     * Prepares a locale for translation.
258
     */
259
    private void exportLocaleForTranslation() {
260
        // Get the selected locale as the one for reference
261
        Locale refLocale = getSelectedLocale();
262

    
263
        if (refLocale == null) {
264
            JOptionPane
265
                    .showMessageDialog(
266
                            this,
267
                            Messages
268
                                    .getText("I18nPreferencePage.seleccione_idioma_actualizar"),
269
                            Messages
270
                                    .getText("I18nPreferencePage.error_actualizar_idioma"),
271
                            JOptionPane.ERROR_MESSAGE);
272
        }
273
        else {
274

    
275
            // Select the locale to translate
276
            LocaleItem[] items = getLocaleItemsForSelection(Locale
277
                    .getAvailableLocales(), manager.getInstalledLocales());
278
            LocaleItem selected = (LocaleItem) JOptionPane
279
                    .showInputDialog(
280
                            this,
281
                            Messages
282
                                    .getText("I18nPreferencePage.seleccione_idioma_traducir"),
283
                            Messages
284
                                    .getText("I18nPreferencePage.exportar_idioma"),
285
                            JOptionPane.QUESTION_MESSAGE, null, items, items[0]);
286

    
287
            if (selected == null) {
288
                return;
289
            }
290

    
291
            // Select the file to export to
292
            JFileChooser fileChooser = getJarFileChooser();
293
            fileChooser.setSelectedFile(new File(getLocaleJarFileName(selected
294
                    .getLocale())));
295

    
296
            if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
297
                File saveFile = fileChooser.getSelectedFile();
298
                manager.exportLocaleForTranslation(selected.getLocale(),
299
                        refLocale, saveFile);
300

    
301
                JOptionPane
302
                        .showMessageDialog(
303
                                this,
304
                                Messages
305
                                        .getText("I18nPreferencePage.idioma_preparado_traducir")
306
                                        + manager.getDisplayName(selected
307
                                                .getLocale())
308
                                        + Messages
309
                                                .getText("I18nPreferencePage.en_archivo")
310
                                        + saveFile,
311
                                Messages
312
                                        .getText("I18nPreferencePage.exportar_idioma"),
313
                                JOptionPane.INFORMATION_MESSAGE);
314
            }
315
        }
316

    
317
    }
318

    
319
    private LocaleItem[] getLocaleItemsForSelection(Locale[] locales,
320
            Locale[] exceptions) {
321
        LocaleItem[] items = new LocaleItem[locales.length - 1];
322
        Set exceptionsSet = new HashSet(exceptions.length);
323
        for (int i = 0; i < exceptions.length; i++) {
324
            exceptionsSet.add(exceptions[i]);
325
        }
326

    
327
        int j = 0;
328
        for (int i = 0; i < locales.length; i++) {
329
            // Only add locales not included in the exceptions list
330
            if (!exceptionsSet.contains(locales[i])) {
331
                items[j] = new LocaleItem(locales[i], manager);
332
                j++;
333
            }
334
        }
335
        return items;
336
    }
337

    
338
    /**
339
     * Returns a name for the jar file to export a locale.
340
     */
341
    private String getLocaleJarFileName(Locale locale) {
342
        return manager.getDisplayName(locale, I18nManager.ENGLISH).replace(' ',
343
                '_').concat(EXPORT_ZIP_FILE_EXTENSION);
344
    }
345

    
346
    /**
347
     * Creates a new JFileChooser to import or export a locale jar file.
348
     */
349
    private JFileChooser getJarFileChooser() {
350
        if (fileChooser == null) {
351
            fileChooser = new JFileChooser();
352
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
353
            fileChooser.setMultiSelectionEnabled(false);
354
            fileChooser.setFileFilter(new FileFilter() {
355

    
356
                public boolean accept(File file) {
357
                    return (file.isDirectory()
358
                            || file.getName().endsWith(
359
                                    EXPORT_JAR_FILE_EXTENSION) || file
360
                            .getName().endsWith(EXPORT_ZIP_FILE_EXTENSION));
361
                }
362

    
363
                public String getDescription() {
364
                    return Messages.getText("I18nPreferencePage.archivos_jar");
365
                }
366

    
367
            });
368
        }
369
        return fileChooser;
370
    }
371

    
372
    /**
373
     * Removes an installed locale from gvSIG.
374
     */
375
    private void uninstallSelectedLocale() {
376
        Locale locale = getSelectedLocale();
377

    
378
        if (locale == null) {
379
            JOptionPane
380
                    .showMessageDialog(
381
                            this,
382
                            Messages
383
                                    .getText("I18nPreferencePage.seleccione_idioma_desinstalar"),
384
                            Messages
385
                                    .getText("I18nPreferencePage.error_desinstalar_idioma"),
386
                            JOptionPane.ERROR_MESSAGE);
387
        }
388
        else if (locale.equals(manager.getCurrentLocale())) {
389
            JOptionPane
390
                    .showMessageDialog(
391
                            this,
392
                            Messages
393
                                    .getText("I18nPreferencePage.idioma_actual_no_puede_desinstalar"),
394
                            Messages
395
                                    .getText("I18nPreferencePage.error_desinstalar_idioma"),
396
                            JOptionPane.ERROR_MESSAGE);
397
        }
398
        else {
399
            int option = JOptionPane
400
                    .showConfirmDialog(
401
                            this,
402
                            Messages
403
                                    .getText("I18nPreferencePage.seguro_desea_desinstalar_idioma")
404
                                    + " "
405
                                    + manager.getDisplayName(locale)
406
                                    + "?",
407
                            Messages
408
                                    .getText("I18nPreferencePage.confirmar_desinstalar_idioma"),
409
                            JOptionPane.YES_NO_OPTION);
410
            if (option == JOptionPane.YES_OPTION) {
411
                tableModel.removeLocale(locale);
412
            }
413
        }
414
    }
415

    
416
    /**
417
     * Returns the Locale selected in the table of available locales.
418
     */
419
    private Locale getSelectedLocale() {
420
        int rowIndex = localesTable.getSelectedRow();
421
        if (rowIndex >= 0) {
422
            return tableModel.getLocale(rowIndex);
423
        }
424
        else {
425
            return null;
426
        }
427
    }
428

    
429
    /**
430
     * Creates the Panel with the table of Locales.
431
     */
432
    private Component getLocalesPanel() {
433
        tableModel = new LocaleTableModel(manager);
434
        localesTable = new JTable(tableModel);
435

    
436
        TableColumn activeColumn = localesTable.getColumnModel().getColumn(
437
                LocaleTableModel.COLUMN_ACTIVE);
438
        activeColumn.setCellEditor(new RadioButtonCellEditor());
439
        activeColumn.setCellRenderer(new RadioButtonCellRenderer());
440

    
441
        // Only single selection, as we will use the selected row as the source
442
        // for the actions of the panel buttons.
443
        localesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
444
        localesTable.getSelectionModel().setSelectionInterval(0, 0);
445
        JScrollPane scrollPane = new JScrollPane(localesTable);
446

    
447
        // Container panel
448
        JPanel localesPanel = new JPanel();
449
        localesPanel.setLayout(new BoxLayout(localesPanel, BoxLayout.Y_AXIS));
450
        localesPanel.add(scrollPane);
451
        localesPanel.setAlignmentX(CENTER_ALIGNMENT);
452
        localesPanel.setPreferredSize(new Dimension(236, 240));
453
        localesPanel.setMaximumSize(new Dimension(500, 240));
454

    
455
        return localesPanel;
456
    }
457

    
458
    /**
459
     * Creates the panel with the buttons to perform the related actions.
460
     */
461
    private Component getButtonsPanel() {
462
        JPanel buttonPanel = new JPanel(new GridBagLayout());
463
        GridBagConstraints constraints = new GridBagConstraints();
464
        constraints.fill = GridBagConstraints.NONE;
465
        constraints.anchor = GridBagConstraints.LINE_START;
466
        Insets btInsets = new Insets(2, 0, 2, 4);
467
        Insets lbInsets = new Insets(2, 2, 2, 0);
468

    
469
        /* ROW 0 */
470
        constraints.gridy = 0;
471

    
472
        constraints.gridx = 0;
473
        constraints.insets = btInsets;
474
        JButton newLocaleButton = new JButton(Messages
475
                .getText("I18nPreferencePage.Instalar"));
476
        newLocaleButton.setActionCommand(COMMAND_INSTALL);
477
        newLocaleButton.addActionListener(this);
478
        newLocaleButton.setToolTipText(Messages
479
                .getText("I18nPreferencePage.Instalar_idioma_tooltip"));
480
        buttonPanel.add(newLocaleButton, constraints);
481

    
482
        constraints.gridx = 1;
483
        constraints.insets = lbInsets;
484
        buttonPanel.add(new JLabel(Messages
485
                .getText("I18nPreferencePage.Instalar_idioma_tooltip")),
486
                constraints);
487

    
488
        /* ROW 1 */
489
        constraints.gridy = 1;
490

    
491
        constraints.gridx = 0;
492
        constraints.insets = btInsets;
493
        JButton removeLocaleButton = new JButton(Messages
494
                .getText("I18nPreferencePage.Desinstalar"));
495
        removeLocaleButton.setActionCommand(COMMAND_UNINSTALL);
496
        removeLocaleButton.addActionListener(this);
497
        removeLocaleButton.setToolTipText(Messages
498
                .getText("I18nPreferencePage.Desinstalar_idioma_tooltip"));
499
        buttonPanel.add(removeLocaleButton, constraints);
500

    
501
        constraints.gridx = 1;
502
        constraints.insets = lbInsets;
503
        buttonPanel.add(new JLabel(Messages
504
                .getText("I18nPreferencePage.Desinstalar_idioma_tooltip")),
505
                constraints);
506

    
507
        /* ROW 2 */
508
        constraints.gridy = 2;
509

    
510
        constraints.gridx = 0;
511
        constraints.insets = btInsets;
512
        JButton exportLocaleButton = new JButton(Messages
513
                .getText("I18nPreferencePage.exportar_actualizar"));
514
        exportLocaleButton.setActionCommand(COMMAND_EXPORT);
515
        exportLocaleButton.addActionListener(this);
516
        exportLocaleButton.setToolTipText(Messages
517
                .getText("I18nPreferencePage.exportar_actualizar_tooltip"));
518
        buttonPanel.add(exportLocaleButton, constraints);
519

    
520
        constraints.gridx = 1;
521
        constraints.insets = lbInsets;
522
        buttonPanel.add(new JLabel(Messages
523
                .getText("I18nPreferencePage.exportar_actualizar_tooltip")),
524
                constraints);
525

    
526
        /* ROW 3 */
527
        constraints.gridy = 3;
528

    
529
        constraints.gridx = 0;
530
        constraints.insets = btInsets;
531
        JButton exportNewLocaleButton = new JButton(Messages
532
                .getText("I18nPreferencePage.exportar_traducir"));
533
        exportNewLocaleButton.setActionCommand(COMMAND_EXPORT_NEW);
534
        exportNewLocaleButton.addActionListener(this);
535
        exportNewLocaleButton.setToolTipText(Messages
536
                .getText("I18nPreferencePage.exportar_traducir_tooltip"));
537
        buttonPanel.add(exportNewLocaleButton, constraints);
538

    
539
        constraints.gridx = 1;
540
        constraints.insets = lbInsets;
541
        buttonPanel.add(new JLabel(Messages
542
                .getText("I18nPreferencePage.exportar_traducir_tooltip")),
543
                constraints);
544

    
545
        buttonPanel.setAlignmentX(CENTER_ALIGNMENT);
546
        return buttonPanel;
547
    }
548

    
549
    /**
550
     * Creates the JLabel to show information about the preference page.
551
     */
552
    private Component getActiveLocaleLabel() {
553
        JLabel label = new JLabel(
554
                "<html><body><strong>Si cambia el idioma activo, ?ste no se visualizar? hasta que se reinicie la aplicaci?n</strong></body></html>");
555
        label.setAlignmentX(CENTER_ALIGNMENT);
556
        return label;
557
    }
558

    
559
    private class LocaleItem {
560
        private final Locale locale;
561
        private final I18nManager manager;
562

    
563
        public LocaleItem(Locale locale, I18nManager manager) {
564
            this.locale = locale;
565
            this.manager = manager;
566
        }
567

    
568
        public Locale getLocale() {
569
            return locale;
570
        }
571

    
572
        public String toString() {
573
            return manager.getDisplayName(locale);
574
        }
575

    
576
        public boolean equals(Object obj) {
577
            if (obj == null || !(obj instanceof LocaleItem)) {
578
                return false;
579
            }
580

    
581
            LocaleItem item = (LocaleItem) obj;
582
            return locale.equals(item.getLocale());
583
        }
584
    }
585
}