Statistics
| Revision:

root / tags / v2_0_0_Build_2051 / extensions / extI18n / src / main / java / org / gvsig / i18n / extension / preferences / I18nPreferencePage.java @ 38726

History | View | Annotate | Download (18.7 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;
28

    
29
import java.awt.Component;
30
import java.awt.Dimension;
31
import java.awt.Font;
32
import java.awt.GridBagConstraints;
33
import java.awt.GridBagLayout;
34
import java.awt.Insets;
35
import java.awt.event.ActionEvent;
36
import java.awt.event.ActionListener;
37
import java.io.File;
38
import java.util.ArrayList;
39
import java.util.HashSet;
40
import java.util.List;
41
import java.util.Locale;
42
import java.util.Set;
43

    
44
import javax.swing.Box;
45
import javax.swing.BoxLayout;
46
import javax.swing.ImageIcon;
47
import javax.swing.JFileChooser;
48
import javax.swing.JLabel;
49
import javax.swing.JOptionPane;
50
import javax.swing.JPanel;
51
import javax.swing.JScrollPane;
52
import javax.swing.JTable;
53
import javax.swing.JTextArea;
54
import javax.swing.ListSelectionModel;
55
import javax.swing.filechooser.FileFilter;
56
import javax.swing.table.TableColumn;
57

    
58
import org.gvsig.andami.PluginServices;
59
import org.gvsig.andami.preferences.AbstractPreferencePage;
60
import org.gvsig.andami.preferences.StoreException;
61
import org.gvsig.gui.beans.swing.JButton;
62
import org.gvsig.i18n.I18nException;
63
import org.gvsig.i18n.I18nManager;
64
import org.gvsig.i18n.Messages;
65
import org.gvsig.i18n.extension.preferences.table.LocaleTableModel;
66
import org.gvsig.i18n.extension.preferences.table.RadioButtonCellEditor;
67
import org.gvsig.i18n.extension.preferences.table.RadioButtonCellRenderer;
68
import org.gvsig.i18n.impl.I18nManagerImpl;
69

    
70

    
71
/**
72
 * Prefence page to manage gvSIG locales.
73
 * 
74
 * @author <a href="mailto:dcervera@disid.com">David Cervera</a>
75
 */
76
public class I18nPreferencePage extends AbstractPreferencePage implements
77
        ActionListener {
78

    
79
    private static final long serialVersionUID = 7164183052397200888L;
80

    
81
    private static final String COMMAND_UNINSTALL = "UNINSTALL";
82

    
83
    private static final String COMMAND_EXPORT = "EXPORT";
84

    
85
    private static final String COMMAND_EXPORT_NEW = "EXPORT_NEW";
86

    
87
    private static final String COMMAND_INSTALL = "INSTALL";
88

    
89
    private static final String EXPORT_JAR_FILE_EXTENSION = ".jar";
90

    
91
    private static final String EXPORT_ZIP_FILE_EXTENSION = ".zip";
92

    
93
    private ImageIcon icon;
94

    
95
    private I18nManager manager = I18nManagerImpl.getInstance();
96

    
97
    private JTable localesTable;
98

    
99
    private LocaleTableModel tableModel;
100

    
101
    private JFileChooser fileChooser;
102

    
103
    /**
104
     * Creates a new I18n preferences page.
105
     */
106
    public I18nPreferencePage() {
107
                setParentID("org.gvsig.coreplugin.preferences.general.GeneralPage");
108
                
109
        icon = PluginServices.getIconTheme().get("i18n-preferences");
110

    
111
        add(getLocalesPanel());
112
        add(Box.createRigidArea(new Dimension(0, 5)));
113
        add(getActiveLocaleLabel());
114
        add(Box.createRigidArea(new Dimension(0, 5)));
115
        add(getButtonsPanel());
116
        add(Box.createRigidArea(new Dimension(0, 5)));
117
        add(getCollaborationLabel());
118
    }
119

    
120
    public String getID() {
121
                return getClass().getName();
122
    }
123

    
124
    public String getTitle() {
125
        return Messages.getText("idioma");
126
    }
127

    
128
    public ImageIcon getIcon() {
129
        return icon;
130
    }
131

    
132
    public JPanel getPanel() {
133
        return this;
134
    }
135

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

    
140
    public boolean isValueChanged() {
141
        return tableModel.isValueChanged();
142
    }
143

    
144
    public void storeValues() throws StoreException {
145
        tableModel.saveSelectedLocale();
146
    }
147

    
148
    public void initializeDefaults() {
149
        tableModel.selectDefaultLocale();
150
    }
151

    
152
    public void initializeValues() {
153
        tableModel.selectPreviousLocale();
154
    }
155

    
156
    public void actionPerformed(ActionEvent event) {
157
        if (COMMAND_INSTALL.equals(event.getActionCommand())) {
158
            installLocale();
159
        }
160
        else if (COMMAND_EXPORT.equals(event.getActionCommand())) {
161
            exportLocaleForUpdate();
162
        }
163
        else if (COMMAND_EXPORT_NEW.equals(event.getActionCommand())) {
164
            exportLocaleForTranslation();
165
        }
166
        else if (COMMAND_UNINSTALL.equals(event.getActionCommand())) {
167
            uninstallSelectedLocale();
168
        }
169
    }
170

    
171
    /**
172
     * Installs a new Locale translation or updates an already existing one.
173
     */
174
    private void installLocale() {
175
        JFileChooser fileChooser = getJarFileChooser();
176

    
177
        int returnVal = fileChooser.showOpenDialog(this);
178

    
179
        if (returnVal == JFileChooser.APPROVE_OPTION) {
180
            File importFile = fileChooser.getSelectedFile();
181
            try {
182
                Locale[] installedLocales = manager.installLocales(importFile);
183
                if (installedLocales == null || installedLocales.length == 0) {
184
                    JOptionPane
185
                            .showMessageDialog(
186
                                    this,
187
                                    Messages
188
                                            .getText("I18nPreferencePage.idiomas_no_encontrados_para_instalar"),
189
                                    Messages
190
                                            .getText("I18nPreferencePage.error_instalar_idiomas"),
191
                                    JOptionPane.ERROR_MESSAGE);
192
                }
193
                else {
194
                    StringBuffer msg = new StringBuffer(Messages
195
                            .getText("I18nPreferencePage.idiomas_instalados"));
196

    
197
                    for (int i = 0; i < installedLocales.length; i++) {
198
                        msg.append(manager.getDisplayName(installedLocales[i]));
199
                        if (i < installedLocales.length - 1) {
200
                            msg.append(", ");
201
                        }
202
                    }
203
                    tableModel.reloadLocales();
204
                    JOptionPane
205
                            .showMessageDialog(
206
                                    this,
207
                                    msg.toString(),
208
                                    Messages
209
                                            .getText("I18nPreferencePage.instalar_idiomas"),
210
                                    JOptionPane.INFORMATION_MESSAGE);
211
                }
212
            } catch (I18nException ex) {
213
                ex.showError();
214
            }
215
        }
216
    }
217

    
218
    /**
219
     * Updates the translation of a locale
220
     */
221
    private void exportLocaleForUpdate() {
222
        Locale[] locales = getSelectedLocales();
223

    
224
        if (locales == null) {
225
            JOptionPane.showMessageDialog(this, Messages
226
                    .getText("I18nPreferencePage.seleccione_idioma"), Messages
227
                    .getText("I18nPreferencePage.error_actualizar_idioma"),
228
                    JOptionPane.ERROR_MESSAGE);
229
        }
230
        else {
231
            // Select the reference language
232
            LocaleItem[] items = getLocaleItemsForSelection(manager
233
                    .getInstalledLocales(), locales);
234
            LocaleItem selected = null;
235
            if (items != null && items.length > 0) {
236
                // Select by default the current locale, or the first one
237
                // if the current locale is one of the ones to be updated
238
                for (int i = 0; i < locales.length && selected == null; i++) {
239
                    if (locales[i].equals(manager.getCurrentLocale())) {
240
                        selected = items[0];
241
                    }
242
                }
243
                if (selected == null) {
244
                    selected = new LocaleItem(manager.getCurrentLocale(),
245
                            manager);
246
                }
247
                selected = (LocaleItem) JOptionPane
248
                        .showInputDialog(
249
                                this,
250
                                Messages
251
                                        .getText("I18nPreferencePage.seleccione_idioma_referencia"),
252
                                Messages
253
                                        .getText("I18nPreferencePage.exportar_idioma"),
254
                                JOptionPane.QUESTION_MESSAGE, null, items,
255
                                selected);
256

    
257
                if (selected == null) {
258
                    return;
259
                }
260
            }
261
            // Select the file to export to
262
            JFileChooser fileChooser = getJarFileChooser();
263
            fileChooser.setSelectedFile(new File(
264
                    getLocaleJarFileName(locales[0])));
265

    
266
            if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
267
                File saveFile = fileChooser.getSelectedFile();
268
                try {
269
                    Locale fileNameLocale = selected == null ? manager
270
                            .getCurrentLocale() : selected.getLocale();
271
                    manager.exportLocalesForUpdate(locales, fileNameLocale,
272
                            saveFile);
273
                    
274
                    JOptionPane
275
                            .showMessageDialog(
276
                                    this,
277
                                    Messages
278
                                            .getText("I18nPreferencePage.exportado_textos_idioma")
279
                                            + " "
280
                                            + saveFile,
281
                                    Messages
282
                                            .getText("I18nPreferencePage.exportar_idioma"),
283
                                    JOptionPane.INFORMATION_MESSAGE);
284
                } catch (I18nException ex) {
285
                    ex.showError();
286
                }
287
            }
288
        }
289
    }
290

    
291
    /**
292
     * Prepares a locale for translation.
293
     */
294
    private void exportLocaleForTranslation() {
295
        // Get the selected locale as the ones for reference
296
        Locale[] refLocales = getSelectedLocales();
297

    
298
        if (refLocales == null) {
299
            JOptionPane
300
                    .showMessageDialog(
301
                            this,
302
                            Messages
303
                                    .getText("I18nPreferencePage.seleccione_idioma_actualizar"),
304
                            Messages
305
                                    .getText("I18nPreferencePage.error_actualizar_idioma"),
306
                            JOptionPane.ERROR_MESSAGE);
307
        }
308
        else {
309

    
310
            // Select the locale to translate
311
            LocaleItem[] items = getLocaleItemsForSelection(Locale
312
                    .getAvailableLocales(), manager.getInstalledLocales());
313
            LocaleItem selected = (LocaleItem) JOptionPane
314
                    .showInputDialog(
315
                            this,
316
                            Messages
317
                                    .getText("I18nPreferencePage.seleccione_idioma_traducir"),
318
                            Messages
319
                                    .getText("I18nPreferencePage.exportar_idioma"),
320
                            JOptionPane.QUESTION_MESSAGE, null, items, items[0]);
321

    
322
            if (selected == null) {
323
                return;
324
            }
325

    
326
            // Select the file to export to
327
            JFileChooser fileChooser = getJarFileChooser();
328
            fileChooser.setSelectedFile(new File(getLocaleJarFileName(selected
329
                    .getLocale())));
330

    
331
            if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
332
                File saveFile = fileChooser.getSelectedFile();
333
                try {
334
                    manager.exportLocaleForTranslation(selected.getLocale(),
335
                            refLocales, saveFile);
336

    
337
                    JOptionPane
338
                            .showMessageDialog(
339
                                    this,
340
                                    Messages
341
                                            .getText("I18nPreferencePage.idioma_preparado_traducir")
342
                                            + manager.getDisplayName(selected
343
                                                    .getLocale())
344
                                            + Messages
345
                                                    .getText("I18nPreferencePage.en_archivo")
346
                                            + saveFile,
347
                                    Messages
348
                                            .getText("I18nPreferencePage.exportar_idioma"),
349
                                    JOptionPane.INFORMATION_MESSAGE);
350
                } catch (I18nException ex) {
351
                    ex.showError();
352
                }
353
            }
354
        }
355

    
356
    }
357

    
358
    private LocaleItem[] getLocaleItemsForSelection(Locale[] locales,
359
            Locale[] exceptions) {
360
        List items = new ArrayList();
361
        Set exceptionsSet = new HashSet(exceptions.length);
362
        for (int i = 0; i < exceptions.length; i++) {
363
            exceptionsSet.add(exceptions[i]);
364
        }
365

    
366
        int j = 0;
367
        for (int i = 0; i < locales.length; i++) {
368
            // Only add locales not included in the exceptions list
369
            if (!exceptionsSet.contains(locales[i])) {
370
                items.add(new LocaleItem(locales[i], manager));
371
                j++;
372
            }
373
        }
374
        return (LocaleItem[]) items.toArray(new LocaleItem[items.size()]);
375
    }
376

    
377
    /**
378
     * Returns a name for the jar file to export a locale.
379
     */
380
    private String getLocaleJarFileName(Locale locale) {
381
        return manager.getDisplayName(locale, I18nManager.ENGLISH).replace(' ',
382
                '_').concat(EXPORT_ZIP_FILE_EXTENSION);
383
    }
384

    
385
    /**
386
     * Creates a new JFileChooser to import or export a locale jar file.
387
     */
388
    private JFileChooser getJarFileChooser() {
389
        if (fileChooser == null) {
390
            fileChooser = new JFileChooser();
391
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
392
            fileChooser.setMultiSelectionEnabled(false);
393
            fileChooser.setFileFilter(new FileFilter() {
394

    
395
                public boolean accept(File file) {
396
                    return (file.isDirectory()
397
                            || file.getName().endsWith(
398
                                    EXPORT_JAR_FILE_EXTENSION) || file
399
                            .getName().endsWith(EXPORT_ZIP_FILE_EXTENSION));
400
                }
401

    
402
                public String getDescription() {
403
                    return Messages.getText("I18nPreferencePage.archivos_jar");
404
                }
405

    
406
            });
407
        }
408
        return fileChooser;
409
    }
410

    
411
    /**
412
     * Removes some installed locales from gvSIG.
413
     */
414
    private void uninstallSelectedLocale() {
415
        Locale[] locales = getSelectedLocales();
416

    
417
        if (locales == null) {
418
            JOptionPane
419
                    .showMessageDialog(
420
                            this,
421
                            Messages
422
                                    .getText("I18nPreferencePage.seleccione_idioma_desinstalar"),
423
                            Messages
424
                                    .getText("I18nPreferencePage.error_desinstalar_idioma"),
425
                            JOptionPane.ERROR_MESSAGE);
426
            return;
427
        }
428
        
429
        for (int i = 0; i < locales.length; i++) {
430

    
431
            if (locales[i].equals(manager.getCurrentLocale())) {
432
                JOptionPane
433
                        .showMessageDialog(
434
                                this,
435
                                Messages
436
                                        .getText("I18nPreferencePage.idioma_actual_no_puede_desinstalar"),
437
                                Messages
438
                                        .getText("I18nPreferencePage.error_desinstalar_idioma"),
439
                                JOptionPane.ERROR_MESSAGE);
440
            } else {
441
                int option = JOptionPane
442
                        .showConfirmDialog(
443
                                this,
444
                                Messages
445
                                        .getText("I18nPreferencePage.seguro_desea_desinstalar_idioma")
446
                                        + " "
447
                                        + manager.getDisplayName(locales[i])
448
                                        + "?",
449
                                Messages
450
                                        .getText("I18nPreferencePage.confirmar_desinstalar_idioma"),
451
                                JOptionPane.YES_NO_OPTION);
452
                if (option == JOptionPane.YES_OPTION) {
453
                    try {
454
                        tableModel.removeLocale(locales[i]);
455
                    } catch (I18nException ex) {
456
                        ex.showError();
457
                    }
458
                }
459
            }
460
        }
461
    }
462

    
463
    /**
464
     * Returns the Locales selected in the table of available locales.
465
     */
466
    private Locale[] getSelectedLocales() {
467
        int[] rowIndexes = localesTable.getSelectedRows();
468
        if (rowIndexes != null && rowIndexes.length > 0) {
469
            Locale[] locales = new Locale[rowIndexes.length];
470
            for (int i = 0; i < locales.length; i++) {
471
                locales[i] = tableModel.getLocale(rowIndexes[i]);
472
            }
473
            return locales;
474
        }
475
        else {
476
            return null;
477
        }
478
    }
479

    
480
    /**
481
     * Creates the Panel with the table of Locales.
482
     */
483
    private Component getLocalesPanel() {
484
        tableModel = new LocaleTableModel(manager);
485
        localesTable = new JTable(tableModel);
486

    
487
        TableColumn activeColumn = localesTable.getColumnModel().getColumn(
488
                LocaleTableModel.COLUMN_ACTIVE);
489
        activeColumn.setCellEditor(new RadioButtonCellEditor());
490
        activeColumn.setCellRenderer(new RadioButtonCellRenderer());
491

    
492
        localesTable
493
                .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
494
        localesTable.getSelectionModel().setSelectionInterval(0, 0);
495
        JScrollPane scrollPane = new JScrollPane(localesTable);
496

    
497
        // Container panel
498
        JPanel localesPanel = new JPanel();
499
        localesPanel.setLayout(new BoxLayout(localesPanel, BoxLayout.Y_AXIS));
500
        localesPanel.add(scrollPane);
501
        localesPanel.setAlignmentX(CENTER_ALIGNMENT);
502
        localesPanel.setPreferredSize(new Dimension(236, 230));
503
        localesPanel.setMaximumSize(new Dimension(500, 230));
504

    
505
        return localesPanel;
506
    }
507

    
508
    /**
509
     * Creates the panel with the buttons to perform the related actions.
510
     */
511
    private Component getButtonsPanel() {
512
        JPanel buttonPanel = new JPanel(new GridBagLayout());
513
        GridBagConstraints constraints = new GridBagConstraints();
514
        constraints.fill = GridBagConstraints.NONE;
515
        constraints.anchor = GridBagConstraints.LINE_START;
516
        Insets btInsets = new Insets(2, 0, 2, 4);
517
        Insets lbInsets = new Insets(2, 2, 2, 0);
518

    
519
        /* ROW 0 */
520
        constraints.gridy = 0;
521

    
522
        constraints.gridx = 0;
523
        constraints.insets = btInsets;
524
        JButton newLocaleButton = new JButton(Messages
525
                .getText("I18nPreferencePage.Instalar"));
526
        newLocaleButton.setActionCommand(COMMAND_INSTALL);
527
        newLocaleButton.addActionListener(this);
528
        newLocaleButton.setToolTipText(Messages
529
                .getText("I18nPreferencePage.Instalar_idioma_tooltip"));
530
        buttonPanel.add(newLocaleButton, constraints);
531

    
532
        constraints.gridx = 1;
533
        constraints.insets = lbInsets;
534
        buttonPanel.add(new JLabel(Messages
535
                .getText("I18nPreferencePage.Instalar_idioma_tooltip")),
536
                constraints);
537

    
538
        /* ROW 1 */
539
        constraints.gridy = 1;
540

    
541
        constraints.gridx = 0;
542
        constraints.insets = btInsets;
543
        JButton removeLocaleButton = new JButton(Messages
544
                .getText("I18nPreferencePage.Desinstalar"));
545
        removeLocaleButton.setActionCommand(COMMAND_UNINSTALL);
546
        removeLocaleButton.addActionListener(this);
547
        removeLocaleButton.setToolTipText(Messages
548
                .getText("I18nPreferencePage.Desinstalar_idioma_tooltip"));
549
        buttonPanel.add(removeLocaleButton, constraints);
550

    
551
        constraints.gridx = 1;
552
        constraints.insets = lbInsets;
553
        buttonPanel.add(new JLabel(Messages
554
                .getText("I18nPreferencePage.Desinstalar_idioma_tooltip")),
555
                constraints);
556

    
557
        /* ROW 2 */
558
        constraints.gridy = 2;
559

    
560
        constraints.gridx = 0;
561
        constraints.insets = btInsets;
562
        JButton exportLocaleButton = new JButton(Messages
563
                .getText("I18nPreferencePage.exportar_actualizar"));
564
        exportLocaleButton.setActionCommand(COMMAND_EXPORT);
565
        exportLocaleButton.addActionListener(this);
566
        exportLocaleButton.setToolTipText(Messages
567
                .getText("I18nPreferencePage.exportar_actualizar_tooltip"));
568
        buttonPanel.add(exportLocaleButton, constraints);
569

    
570
        constraints.gridx = 1;
571
        constraints.insets = lbInsets;
572
        buttonPanel.add(new JLabel(Messages
573
                .getText("I18nPreferencePage.exportar_actualizar_tooltip")),
574
                constraints);
575

    
576
        /* ROW 3 */
577
        constraints.gridy = 3;
578

    
579
        constraints.gridx = 0;
580
        constraints.insets = btInsets;
581
        JButton exportNewLocaleButton = new JButton(Messages
582
                .getText("I18nPreferencePage.exportar_traducir"));
583
        exportNewLocaleButton.setActionCommand(COMMAND_EXPORT_NEW);
584
        exportNewLocaleButton.addActionListener(this);
585
        exportNewLocaleButton.setToolTipText(Messages
586
                .getText("I18nPreferencePage.exportar_traducir_tooltip"));
587
        buttonPanel.add(exportNewLocaleButton, constraints);
588

    
589
        constraints.gridx = 1;
590
        constraints.insets = lbInsets;
591
        buttonPanel.add(new JLabel(Messages
592
                .getText("I18nPreferencePage.exportar_traducir_tooltip")),
593
                constraints);
594

    
595
        buttonPanel.setAlignmentX(CENTER_ALIGNMENT);
596
        return buttonPanel;
597
    }
598

    
599
    /**
600
     * Creates the JLabel to show information about the preference page.
601
     */
602
    private Component getActiveLocaleLabel() {
603
        JTextArea textArea = new JTextArea(Messages
604
                .getText("I18nPreferencePage.ayuda"));
605
        textArea.setEditable(false);
606
        textArea.setAutoscrolls(true);
607
        textArea.setLineWrap(true);
608
        textArea.setWrapStyleWord(true);
609
        textArea.setFont(textArea.getFont().deriveFont(Font.BOLD));
610
        return textArea;
611
    }
612

    
613
    /**
614
     * Creates the JLabel to show information about gvSIG translation
615
     * collaboration.
616
     */
617
    private Component getCollaborationLabel() {
618
        JTextArea textArea = new JTextArea(Messages
619
                .getText("I18nPreferencePage.colaboracion"));
620
        textArea.setEditable(false);
621
        textArea.setAutoscrolls(true);
622
        textArea.setLineWrap(true);
623
        textArea.setWrapStyleWord(true);
624
        return textArea;
625
    }
626

    
627
    private class LocaleItem {
628
        private final Locale locale;
629
        private final I18nManager manager;
630

    
631
        public LocaleItem(Locale locale, I18nManager manager) {
632
            this.locale = locale;
633
            this.manager = manager;
634
        }
635

    
636
        public Locale getLocale() {
637
            return locale;
638
        }
639

    
640
        public String toString() {
641
            return manager.getDisplayName(locale);
642
        }
643

    
644
        public boolean equals(Object obj) {
645
            if (obj == null || !(obj instanceof LocaleItem)) {
646
                return false;
647
            }
648

    
649
            LocaleItem item = (LocaleItem) obj;
650
            return locale.equals(item.getLocale());
651
        }
652
    }
653
}