Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extI18n / src / org / gvsig / i18n / extension / preferences / I18nPreferencePage.java @ 26324

History | View | Annotate | Download (17.3 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.*;
41
import org.gvsig.i18n.extension.preferences.table.*;
42
import org.gvsig.i18n.impl.I18nManagerImpl;
43

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

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

    
55
    private static final long serialVersionUID = 7164183052397200888L;
56

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

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

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

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

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

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

    
69
    private ImageIcon icon;
70

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

    
73
    private JTable localesTable;
74

    
75
    private LocaleTableModel tableModel;
76

    
77
    private JFileChooser fileChooser;
78

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

    
87
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
88
        // setLayout(new BorderLayout());
89
        // setSize(new java.awt.Dimension(386, 177));
90
        // add(getPN(), java.awt.BorderLayout.NORTH);
91

    
92
        add(getLocalesPanel());
93
        add(Box.createRigidArea(new Dimension(0, 5)));
94
        add(getActiveLocaleLabel());
95
        add(Box.createRigidArea(new Dimension(0, 5)));
96
        add(getButtonsPanel());
97
        // add(getInfoLabel());
98

    
99
        // add(getLocalesPanel(), BorderLayout.NORTH);
100
        // add(getButtonsPanel(), BorderLayout.CENTER);
101
        // add(getInfoLabel(), BorderLayout.SOUTH);
102

    
103
    }
104

    
105
    public String getID() {
106
        return getClass().getName();
107
    }
108

    
109
    public String getTitle() {
110
        return Messages.getText("idioma");
111
    }
112

    
113
    public ImageIcon getIcon() {
114
        return icon;
115
    }
116

    
117
    public JPanel getPanel() {
118
        return this;
119
    }
120

    
121
    public void setChangesApplied() {
122
        tableModel.setChangesApplied();
123
    }
124

    
125
    public boolean isValueChanged() {
126
        return tableModel.isValueChanged();
127
    }
128

    
129
    public void storeValues() throws StoreException {
130
        tableModel.saveSelectedLocale();
131
    }
132

    
133
    public void initializeDefaults() {
134
        tableModel.selectDefaultLocale();
135
    }
136

    
137
    public void initializeValues() {
138
        tableModel.selectPreviousLocale();
139
    }
140

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

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

    
162
        int returnVal = fileChooser.showOpenDialog(this);
163

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

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

    
203
    /**
204
     * Updates the translation of a locale
205
     */
206
    private void exportLocaleForUpdate() {
207
        Locale locale = getSelectedLocale();
208

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

    
237
            if (selected == null) {
238
                return;
239
            }
240
            // Select the file to export to
241
            JFileChooser fileChooser = getJarFileChooser();
242
            fileChooser.setSelectedFile(new File(getLocaleJarFileName(locale)));
243

    
244
            if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
245
                File saveFile = fileChooser.getSelectedFile();
246
                try {
247
                    manager.exportLocaleForUpdate(locale, selected.getLocale(),
248
                            saveFile);
249

    
250
                    JOptionPane
251
                            .showMessageDialog(
252
                                    this,
253
                                    Messages
254
                                            .getText("I18nPreferencePage.exportado_textos_idioma")
255
                                            + " "
256
                                            + manager.getDisplayName(locale)
257
                                            + Messages
258
                                                    .getText("I18nPreferencePage.al_archivo")
259
                                            + saveFile,
260
                                    Messages
261
                                            .getText("I18nPreferencePage.exportar_idioma"),
262
                                    JOptionPane.INFORMATION_MESSAGE);
263
                } catch (I18nException ex) {
264
                    ex.showError();
265
                }
266
            }
267
        }
268
    }
269

    
270
    /**
271
     * Prepares a locale for translation.
272
     */
273
    private void exportLocaleForTranslation() {
274
        // Get the selected locale as the one for reference
275
        Locale refLocale = getSelectedLocale();
276

    
277
        if (refLocale == null) {
278
            JOptionPane
279
                    .showMessageDialog(
280
                            this,
281
                            Messages
282
                                    .getText("I18nPreferencePage.seleccione_idioma_actualizar"),
283
                            Messages
284
                                    .getText("I18nPreferencePage.error_actualizar_idioma"),
285
                            JOptionPane.ERROR_MESSAGE);
286
        }
287
        else {
288

    
289
            // Select the locale to translate
290
            LocaleItem[] items = getLocaleItemsForSelection(Locale
291
                    .getAvailableLocales(), manager.getInstalledLocales());
292
            LocaleItem selected = (LocaleItem) JOptionPane
293
                    .showInputDialog(
294
                            this,
295
                            Messages
296
                                    .getText("I18nPreferencePage.seleccione_idioma_traducir"),
297
                            Messages
298
                                    .getText("I18nPreferencePage.exportar_idioma"),
299
                            JOptionPane.QUESTION_MESSAGE, null, items, items[0]);
300

    
301
            if (selected == null) {
302
                return;
303
            }
304

    
305
            // Select the file to export to
306
            JFileChooser fileChooser = getJarFileChooser();
307
            fileChooser.setSelectedFile(new File(getLocaleJarFileName(selected
308
                    .getLocale())));
309

    
310
            if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
311
                File saveFile = fileChooser.getSelectedFile();
312
                try {
313
                    manager.exportLocaleForTranslation(selected.getLocale(),
314
                            refLocale, saveFile);
315

    
316
                    JOptionPane
317
                            .showMessageDialog(
318
                                    this,
319
                                    Messages
320
                                            .getText("I18nPreferencePage.idioma_preparado_traducir")
321
                                            + manager.getDisplayName(selected
322
                                                    .getLocale())
323
                                            + Messages
324
                                                    .getText("I18nPreferencePage.en_archivo")
325
                                            + saveFile,
326
                                    Messages
327
                                            .getText("I18nPreferencePage.exportar_idioma"),
328
                                    JOptionPane.INFORMATION_MESSAGE);
329
                } catch (I18nException ex) {
330
                    ex.showError();
331
                }
332
            }
333
        }
334

    
335
    }
336

    
337
    private LocaleItem[] getLocaleItemsForSelection(Locale[] locales,
338
            Locale[] exceptions) {
339
        LocaleItem[] items = new LocaleItem[locales.length - 1];
340
        Set exceptionsSet = new HashSet(exceptions.length);
341
        for (int i = 0; i < exceptions.length; i++) {
342
            exceptionsSet.add(exceptions[i]);
343
        }
344

    
345
        int j = 0;
346
        for (int i = 0; i < locales.length; i++) {
347
            // Only add locales not included in the exceptions list
348
            if (!exceptionsSet.contains(locales[i])) {
349
                items[j] = new LocaleItem(locales[i], manager);
350
                j++;
351
            }
352
        }
353
        return items;
354
    }
355

    
356
    /**
357
     * Returns a name for the jar file to export a locale.
358
     */
359
    private String getLocaleJarFileName(Locale locale) {
360
        return manager.getDisplayName(locale, I18nManager.ENGLISH).replace(' ',
361
                '_').concat(EXPORT_ZIP_FILE_EXTENSION);
362
    }
363

    
364
    /**
365
     * Creates a new JFileChooser to import or export a locale jar file.
366
     */
367
    private JFileChooser getJarFileChooser() {
368
        if (fileChooser == null) {
369
            fileChooser = new JFileChooser();
370
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
371
            fileChooser.setMultiSelectionEnabled(false);
372
            fileChooser.setFileFilter(new FileFilter() {
373

    
374
                public boolean accept(File file) {
375
                    return (file.isDirectory()
376
                            || file.getName().endsWith(
377
                                    EXPORT_JAR_FILE_EXTENSION) || file
378
                            .getName().endsWith(EXPORT_ZIP_FILE_EXTENSION));
379
                }
380

    
381
                public String getDescription() {
382
                    return Messages.getText("I18nPreferencePage.archivos_jar");
383
                }
384

    
385
            });
386
        }
387
        return fileChooser;
388
    }
389

    
390
    /**
391
     * Removes an installed locale from gvSIG.
392
     */
393
    private void uninstallSelectedLocale() {
394
        Locale locale = getSelectedLocale();
395

    
396
        if (locale == null) {
397
            JOptionPane
398
                    .showMessageDialog(
399
                            this,
400
                            Messages
401
                                    .getText("I18nPreferencePage.seleccione_idioma_desinstalar"),
402
                            Messages
403
                                    .getText("I18nPreferencePage.error_desinstalar_idioma"),
404
                            JOptionPane.ERROR_MESSAGE);
405
        }
406
        else if (locale.equals(manager.getCurrentLocale())) {
407
            JOptionPane
408
                    .showMessageDialog(
409
                            this,
410
                            Messages
411
                                    .getText("I18nPreferencePage.idioma_actual_no_puede_desinstalar"),
412
                            Messages
413
                                    .getText("I18nPreferencePage.error_desinstalar_idioma"),
414
                            JOptionPane.ERROR_MESSAGE);
415
        }
416
        else {
417
            int option = JOptionPane
418
                    .showConfirmDialog(
419
                            this,
420
                            Messages
421
                                    .getText("I18nPreferencePage.seguro_desea_desinstalar_idioma")
422
                                    + " "
423
                                    + manager.getDisplayName(locale)
424
                                    + "?",
425
                            Messages
426
                                    .getText("I18nPreferencePage.confirmar_desinstalar_idioma"),
427
                            JOptionPane.YES_NO_OPTION);
428
            if (option == JOptionPane.YES_OPTION) {
429
                try {
430
                    tableModel.removeLocale(locale);
431
                } catch (I18nException ex) {
432
                    ex.showError();
433
                }
434
            }
435
        }
436
    }
437

    
438
    /**
439
     * Returns the Locale selected in the table of available locales.
440
     */
441
    private Locale getSelectedLocale() {
442
        int rowIndex = localesTable.getSelectedRow();
443
        if (rowIndex >= 0) {
444
            return tableModel.getLocale(rowIndex);
445
        }
446
        else {
447
            return null;
448
        }
449
    }
450

    
451
    /**
452
     * Creates the Panel with the table of Locales.
453
     */
454
    private Component getLocalesPanel() {
455
        tableModel = new LocaleTableModel(manager);
456
        localesTable = new JTable(tableModel);
457

    
458
        TableColumn activeColumn = localesTable.getColumnModel().getColumn(
459
                LocaleTableModel.COLUMN_ACTIVE);
460
        activeColumn.setCellEditor(new RadioButtonCellEditor());
461
        activeColumn.setCellRenderer(new RadioButtonCellRenderer());
462

    
463
        // Only single selection, as we will use the selected row as the source
464
        // for the actions of the panel buttons.
465
        localesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
466
        localesTable.getSelectionModel().setSelectionInterval(0, 0);
467
        JScrollPane scrollPane = new JScrollPane(localesTable);
468

    
469
        // Container panel
470
        JPanel localesPanel = new JPanel();
471
        localesPanel.setLayout(new BoxLayout(localesPanel, BoxLayout.Y_AXIS));
472
        localesPanel.add(scrollPane);
473
        localesPanel.setAlignmentX(CENTER_ALIGNMENT);
474
        localesPanel.setPreferredSize(new Dimension(236, 240));
475
        localesPanel.setMaximumSize(new Dimension(500, 240));
476

    
477
        return localesPanel;
478
    }
479

    
480
    /**
481
     * Creates the panel with the buttons to perform the related actions.
482
     */
483
    private Component getButtonsPanel() {
484
        JPanel buttonPanel = new JPanel(new GridBagLayout());
485
        GridBagConstraints constraints = new GridBagConstraints();
486
        constraints.fill = GridBagConstraints.NONE;
487
        constraints.anchor = GridBagConstraints.LINE_START;
488
        Insets btInsets = new Insets(2, 0, 2, 4);
489
        Insets lbInsets = new Insets(2, 2, 2, 0);
490

    
491
        /* ROW 0 */
492
        constraints.gridy = 0;
493

    
494
        constraints.gridx = 0;
495
        constraints.insets = btInsets;
496
        JButton newLocaleButton = new JButton(Messages
497
                .getText("I18nPreferencePage.Instalar"));
498
        newLocaleButton.setActionCommand(COMMAND_INSTALL);
499
        newLocaleButton.addActionListener(this);
500
        newLocaleButton.setToolTipText(Messages
501
                .getText("I18nPreferencePage.Instalar_idioma_tooltip"));
502
        buttonPanel.add(newLocaleButton, constraints);
503

    
504
        constraints.gridx = 1;
505
        constraints.insets = lbInsets;
506
        buttonPanel.add(new JLabel(Messages
507
                .getText("I18nPreferencePage.Instalar_idioma_tooltip")),
508
                constraints);
509

    
510
        /* ROW 1 */
511
        constraints.gridy = 1;
512

    
513
        constraints.gridx = 0;
514
        constraints.insets = btInsets;
515
        JButton removeLocaleButton = new JButton(Messages
516
                .getText("I18nPreferencePage.Desinstalar"));
517
        removeLocaleButton.setActionCommand(COMMAND_UNINSTALL);
518
        removeLocaleButton.addActionListener(this);
519
        removeLocaleButton.setToolTipText(Messages
520
                .getText("I18nPreferencePage.Desinstalar_idioma_tooltip"));
521
        buttonPanel.add(removeLocaleButton, constraints);
522

    
523
        constraints.gridx = 1;
524
        constraints.insets = lbInsets;
525
        buttonPanel.add(new JLabel(Messages
526
                .getText("I18nPreferencePage.Desinstalar_idioma_tooltip")),
527
                constraints);
528

    
529
        /* ROW 2 */
530
        constraints.gridy = 2;
531

    
532
        constraints.gridx = 0;
533
        constraints.insets = btInsets;
534
        JButton exportLocaleButton = new JButton(Messages
535
                .getText("I18nPreferencePage.exportar_actualizar"));
536
        exportLocaleButton.setActionCommand(COMMAND_EXPORT);
537
        exportLocaleButton.addActionListener(this);
538
        exportLocaleButton.setToolTipText(Messages
539
                .getText("I18nPreferencePage.exportar_actualizar_tooltip"));
540
        buttonPanel.add(exportLocaleButton, constraints);
541

    
542
        constraints.gridx = 1;
543
        constraints.insets = lbInsets;
544
        buttonPanel.add(new JLabel(Messages
545
                .getText("I18nPreferencePage.exportar_actualizar_tooltip")),
546
                constraints);
547

    
548
        /* ROW 3 */
549
        constraints.gridy = 3;
550

    
551
        constraints.gridx = 0;
552
        constraints.insets = btInsets;
553
        JButton exportNewLocaleButton = new JButton(Messages
554
                .getText("I18nPreferencePage.exportar_traducir"));
555
        exportNewLocaleButton.setActionCommand(COMMAND_EXPORT_NEW);
556
        exportNewLocaleButton.addActionListener(this);
557
        exportNewLocaleButton.setToolTipText(Messages
558
                .getText("I18nPreferencePage.exportar_traducir_tooltip"));
559
        buttonPanel.add(exportNewLocaleButton, constraints);
560

    
561
        constraints.gridx = 1;
562
        constraints.insets = lbInsets;
563
        buttonPanel.add(new JLabel(Messages
564
                .getText("I18nPreferencePage.exportar_traducir_tooltip")),
565
                constraints);
566

    
567
        buttonPanel.setAlignmentX(CENTER_ALIGNMENT);
568
        return buttonPanel;
569
    }
570

    
571
    /**
572
     * Creates the JLabel to show information about the preference page.
573
     */
574
    private Component getActiveLocaleLabel() {
575
        JLabel label = new JLabel(Messages.getText("I18nPreferencePage.ayuda"));
576
        label.setAlignmentX(CENTER_ALIGNMENT);
577
        return label;
578
    }
579

    
580
    private class LocaleItem {
581
        private final Locale locale;
582
        private final I18nManager manager;
583

    
584
        public LocaleItem(Locale locale, I18nManager manager) {
585
            this.locale = locale;
586
            this.manager = manager;
587
        }
588

    
589
        public Locale getLocale() {
590
            return locale;
591
        }
592

    
593
        public String toString() {
594
            return manager.getDisplayName(locale);
595
        }
596

    
597
        public boolean equals(Object obj) {
598
            if (obj == null || !(obj instanceof LocaleItem)) {
599
                return false;
600
            }
601

    
602
            LocaleItem item = (LocaleItem) obj;
603
            return locale.equals(item.getLocale());
604
        }
605
    }
606
}