Search in sources :

Example 36 with ItemListener

use of java.awt.event.ItemListener 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)

Example 37 with ItemListener

use of java.awt.event.ItemListener in project frostwire by frostwire.

the class MessageService method alwaysUseThisAnswerLabel.

private final JComponent alwaysUseThisAnswerLabel(final String message, final IntSetting setting) {
    JPanel thePanel = new JPanel(new BorderLayout(0, 15));
    JCheckBox option = new JCheckBox(I18n.tr("Always use this answer"));
    JComponent lbl = getLabel(message);
    thePanel.add(lbl, BorderLayout.NORTH);
    thePanel.add(option, BorderLayout.WEST);
    option.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED)
                setting.setValue(REMEMBER_ANSWER);
            else
                setting.setValue(FORGET_ANSWER);
        }
    });
    return thePanel;
}
Also used : ItemEvent(java.awt.event.ItemEvent) ItemListener(java.awt.event.ItemListener)

Example 38 with ItemListener

use of java.awt.event.ItemListener in project frostwire by frostwire.

the class MessageService method doNotDisplayAgainLabel.

private final JComponent doNotDisplayAgainLabel(final String message, final Switch setting) {
    JPanel thePanel = new JPanel(new BorderLayout(0, 15));
    JCheckBox option = new JCheckBox(I18n.tr("Do not display this message again"));
    JComponent lbl = getLabel(message);
    thePanel.add(lbl, BorderLayout.NORTH);
    thePanel.add(option, BorderLayout.WEST);
    option.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
            setting.setValue(e.getStateChange() == ItemEvent.SELECTED);
        }
    });
    return thePanel;
}
Also used : ItemEvent(java.awt.event.ItemEvent) ItemListener(java.awt.event.ItemListener)

Example 39 with ItemListener

use of java.awt.event.ItemListener in project frostwire by frostwire.

the class SearchEnginesPaneItem method createToggleAllCheckbox.

private Component createToggleAllCheckbox() {
    JPanel panel = new JPanel();
    panel.setMinimumSize(new Dimension(300, 60));
    panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 60));
    BoxLayout layout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
    panel.setLayout(layout);
    panel.add(Box.createHorizontalGlue());
    panel.add(new JSeparator());
    panel.add(Box.createRigidArea(new Dimension(0, 10)));
    allCheckbox = new JCheckBox(I18n.tr("Check/Uncheck all"));
    panel.add(allCheckbox);
    allCheckbox.setSelected(areAll(true));
    allCheckbox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            JCheckBox cBox = (JCheckBox) e.getItemSelectable();
            checkAll(cBox.isSelected());
        }
    });
    return panel;
}
Also used : ItemEvent(java.awt.event.ItemEvent) ItemListener(java.awt.event.ItemListener)

Example 40 with ItemListener

use of java.awt.event.ItemListener in project intellij-plugins by JetBrains.

the class MarkdownPreviewSettingsForm method createUIComponents.

private void createUIComponents() {
    //noinspection unchecked
    final List<MarkdownHtmlPanelProvider.ProviderInfo> providerInfos = ContainerUtil.mapNotNull(MarkdownHtmlPanelProvider.getProviders(), provider -> {
        if (provider.isAvailable() == MarkdownHtmlPanelProvider.AvailabilityInfo.UNAVAILABLE) {
            return null;
        }
        return provider.getProviderInfo();
    });
    myPreviewPanelModel = new CollectionComboBoxModel<>(providerInfos, providerInfos.get(0));
    myPreviewProvider = new ComboBox(myPreviewPanelModel);
    mySplitLayoutModel = new EnumComboBoxModel<>(SplitFileEditor.SplitEditorLayout.class);
    myDefaultSplitLayout = new ComboBox(mySplitLayoutModel);
    myLastItem = myPreviewProvider.getSelectedItem();
    myPreviewProvider.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            final Object item = e.getItem();
            if (e.getStateChange() != ItemEvent.SELECTED || !(item instanceof MarkdownHtmlPanelProvider.ProviderInfo)) {
                return;
            }
            final MarkdownHtmlPanelProvider provider = MarkdownHtmlPanelProvider.createFromInfo((MarkdownHtmlPanelProvider.ProviderInfo) item);
            final MarkdownHtmlPanelProvider.AvailabilityInfo availability = provider.isAvailable();
            if (!availability.checkAvailability(myMainPanel)) {
                myPreviewProvider.setSelectedItem(myLastItem);
            } else {
                myLastItem = item;
                updateUseGrayscaleEnabled();
            }
        }
    });
}
Also used : ItemEvent(java.awt.event.ItemEvent) ComboBox(com.intellij.openapi.ui.ComboBox) ItemListener(java.awt.event.ItemListener) MarkdownHtmlPanelProvider(org.intellij.plugins.markdown.ui.preview.MarkdownHtmlPanelProvider)

Aggregations

ItemListener (java.awt.event.ItemListener)331 ItemEvent (java.awt.event.ItemEvent)314 ActionEvent (java.awt.event.ActionEvent)111 ActionListener (java.awt.event.ActionListener)108 JPanel (javax.swing.JPanel)93 JLabel (javax.swing.JLabel)81 JCheckBox (javax.swing.JCheckBox)65 JComboBox (javax.swing.JComboBox)64 JButton (javax.swing.JButton)51 Dimension (java.awt.Dimension)50 BorderLayout (java.awt.BorderLayout)47 JTextField (javax.swing.JTextField)46 ButtonGroup (javax.swing.ButtonGroup)34 JCheckBoxMenuItem (javax.swing.JCheckBoxMenuItem)33 GridBagConstraints (java.awt.GridBagConstraints)29 JMenu (javax.swing.JMenu)29 ChangeEvent (javax.swing.event.ChangeEvent)29 GridBagLayout (java.awt.GridBagLayout)28 JMenuItem (javax.swing.JMenuItem)28 ChangeListener (javax.swing.event.ChangeListener)28