Search in sources :

Example 1 with LanguageComboBoxRenderer

use of org.omegat.util.gui.LanguageComboBoxRenderer in project omegat by omegat-org.

the class AlignFilePickerController method show.

/**
 * Display the align tool file picker. The picker is not modal, so this call will return immediately.
 *
 * @param parent
 *            Parent window of file picker and align window
 */
@SuppressWarnings("serial")
public void show(final Component parent) {
    final JFrame frame = new JFrame(OStrings.getString("ALIGNER_FILEPICKER"));
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    StaticUIUtils.setEscapeClosable(frame);
    final AlignFilePicker picker = new AlignFilePicker();
    picker.sourceLanguagePicker.setModel(new DefaultComboBoxModel<>(new Vector<>(Language.getLanguages())));
    picker.sourceLanguagePicker.setRenderer(new LanguageComboBoxRenderer());
    picker.sourceLanguagePicker.setSelectedItem(sourceLanguage);
    picker.sourceLanguagePicker.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() != ItemEvent.SELECTED) {
                return;
            }
            if (e.getItem() instanceof String) {
                String newVal = (String) e.getItem();
                if (Language.verifySingleLangCode(newVal)) {
                    sourceLanguage = new Language(newVal);
                } else {
                    sourceLanguage = null;
                    JOptionPane.showMessageDialog(frame, OStrings.getString("NP_INVALID_SOURCE_LOCALE") + OStrings.getString("NP_LOCALE_SUGGESTION"), OStrings.getString("TF_ERROR"), JOptionPane.ERROR_MESSAGE);
                    picker.sourceLanguagePicker.requestFocusInWindow();
                }
            } else if (e.getItem() instanceof Language) {
                sourceLanguage = (Language) e.getItem();
            } else {
                throw new IllegalArgumentException();
            }
            updatePicker(picker);
        }
    });
    picker.targetLanguagePicker.setModel(new DefaultComboBoxModel<>(new Vector<>(Language.getLanguages())));
    picker.targetLanguagePicker.setRenderer(new LanguageComboBoxRenderer());
    picker.targetLanguagePicker.setSelectedItem(targetLanguage);
    picker.targetLanguagePicker.addItemListener(e -> {
        if (e.getStateChange() != ItemEvent.SELECTED) {
            return;
        }
        if (e.getItem() instanceof String) {
            String newVal = (String) e.getItem();
            if (Language.verifySingleLangCode(newVal)) {
                targetLanguage = new Language(newVal);
            } else {
                targetLanguage = null;
                JOptionPane.showMessageDialog(frame, OStrings.getString("NP_INVALID_TARGET_LOCALE") + OStrings.getString("NP_LOCALE_SUGGESTION"), OStrings.getString("TF_ERROR"), JOptionPane.ERROR_MESSAGE);
                picker.targetLanguagePicker.requestFocusInWindow();
            }
        } else if (e.getItem() instanceof Language) {
            targetLanguage = (Language) e.getItem();
        } else {
            throw new IllegalArgumentException();
        }
        updatePicker(picker);
    });
    picker.sourceChooseFileButton.addActionListener(e -> {
        File file = chooseFile(frame, OStrings.getString("ALIGNER_FILEPICKER_CHOOSE_SOURCE"), StringUtil.isEmpty(sourceFile) ? sourceDefaultDir : sourceFile);
        if (file != null) {
            sourceDefaultDir = file.getParent();
            targetDefaultDir = targetDefaultDir == null ? sourceDefaultDir : targetDefaultDir;
            defaultSaveDir = defaultSaveDir == null ? sourceDefaultDir : defaultSaveDir;
            picker.sourceLanguageFileField.setText(file.getAbsolutePath());
        }
    });
    picker.targetChooseFileButton.addActionListener(e -> {
        File file = chooseFile(frame, OStrings.getString("ALIGNER_FILEPICKER_CHOOSE_TARGET"), StringUtil.isEmpty(targetFile) ? targetDefaultDir : targetFile);
        if (file != null) {
            targetDefaultDir = file.getParent();
            sourceDefaultDir = sourceDefaultDir == null ? targetDefaultDir : sourceDefaultDir;
            defaultSaveDir = defaultSaveDir == null ? targetDefaultDir : defaultSaveDir;
            picker.targetLanguageFileField.setText(file.getAbsolutePath());
        }
    });
    picker.sourceLanguageFileField.setText(sourceFile);
    picker.sourceLanguageFileField.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void removeUpdate(DocumentEvent e) {
            update();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            update();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            update();
        }

        private void update() {
            sourceFile = picker.sourceLanguageFileField.getText();
            updatePicker(picker);
        }
    });
    picker.targetLanguageFileField.setText(targetFile);
    picker.targetLanguageFileField.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void removeUpdate(DocumentEvent e) {
            update();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            update();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            update();
        }

        private void update() {
            targetFile = picker.targetLanguageFileField.getText();
            updatePicker(picker);
        }
    });
    TransferHandler transferHandler = new TransferHandler() {

        @Override
        public boolean canImport(TransferSupport support) {
            return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
        }

        @Override
        public boolean importData(TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            try {
                List<?> list = (List<?>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                List<File> files = getSupportedFiles(list);
                if (files.isEmpty()) {
                    return false;
                }
                JTextComponent field = (JTextComponent) support.getComponent();
                field.setText(files.get(0).getAbsolutePath());
                return true;
            } catch (Exception e) {
                Log.log(e);
                return false;
            }
        }
    };
    picker.sourceLanguageFileField.setTransferHandler(transferHandler);
    picker.targetLanguageFileField.setTransferHandler(transferHandler);
    picker.setTransferHandler(new TransferHandler() {

        @Override
        public boolean canImport(TransferSupport support) {
            return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
        }

        @Override
        public boolean importData(TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            try {
                List<?> list = (List<?>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                List<File> files = getSupportedFiles(list);
                if (files.isEmpty()) {
                    return false;
                } else if (files.size() == 1) {
                    JTextComponent insertTarget = picker.sourceLanguageFileField;
                    if (picker.sourceLanguageFileField.getDocument().getLength() != 0 && picker.targetLanguageFileField.getDocument().getLength() == 0) {
                        insertTarget = picker.targetLanguageFileField;
                    }
                    insertTarget.setText(files.get(0).getAbsolutePath());
                } else {
                    picker.sourceLanguageFileField.setText(files.get(0).getAbsolutePath());
                    picker.targetLanguageFileField.setText(files.get(1).getAbsolutePath());
                }
                return true;
            } catch (Exception e) {
                Log.log(e);
                return false;
            }
        }
    });
    picker.okButton.addActionListener(e -> {
        picker.bottomPanel.remove(picker.messageTextArea);
        picker.bottomPanel.add(picker.progressBar, BorderLayout.CENTER);
        picker.bottomPanel.revalidate();
        new SwingWorker<Aligner, Void>() {

            @Override
            protected Aligner doInBackground() throws Exception {
                Aligner aligner = new Aligner(sourceFile, sourceLanguage, targetFile, targetLanguage);
                aligner.loadFiles();
                return aligner;
            }

            @Override
            protected void done() {
                try {
                    Aligner aligner = get();
                    new AlignPanelController(aligner, defaultSaveDir).show(parent);
                } catch (CancellationException e) {
                // Ignore
                } catch (Exception e) {
                    Log.log(e);
                    JOptionPane.showMessageDialog(frame, OStrings.getString("ALIGNER_ERROR_LOADING"), OStrings.getString("ERROR_TITLE"), JOptionPane.ERROR_MESSAGE);
                }
                frame.dispose();
            }
        }.execute();
    });
    picker.cancelButton.addActionListener(e -> frame.dispose());
    frame.getRootPane().setDefaultButton(picker.okButton);
    updatePicker(picker);
    frame.add(picker);
    frame.pack();
    frame.setLocationRelativeTo(parent);
    frame.setVisible(true);
}
Also used : DocumentListener(javax.swing.event.DocumentListener) ItemEvent(java.awt.event.ItemEvent) JTextComponent(javax.swing.text.JTextComponent) Language(org.omegat.util.Language) JFrame(javax.swing.JFrame) ArrayList(java.util.ArrayList) List(java.util.List) Vector(java.util.Vector) DocumentEvent(javax.swing.event.DocumentEvent) CancellationException(java.util.concurrent.CancellationException) LanguageComboBoxRenderer(org.omegat.util.gui.LanguageComboBoxRenderer) CancellationException(java.util.concurrent.CancellationException) TransferHandler(javax.swing.TransferHandler) ItemListener(java.awt.event.ItemListener) File(java.io.File)

Aggregations

ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Vector (java.util.Vector)1 CancellationException (java.util.concurrent.CancellationException)1 JFrame (javax.swing.JFrame)1 TransferHandler (javax.swing.TransferHandler)1 DocumentEvent (javax.swing.event.DocumentEvent)1 DocumentListener (javax.swing.event.DocumentListener)1 JTextComponent (javax.swing.text.JTextComponent)1 Language (org.omegat.util.Language)1 LanguageComboBoxRenderer (org.omegat.util.gui.LanguageComboBoxRenderer)1