<commit_before>package com.phraseapp.androidstudio;

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


public class PhraseAppConfigurable implements Configurable {
    private JComponent settingsUI;
    private TextFieldWithBrowseButton clientPathField;
    private JTextField accessTokenField;
    private JList projectSelect;
    private JCheckBox updateTranslationsCheckbox;
    private TextFieldWithBrowseButton defaultStringsPathField;
    private JList defaultLocaleSelect;
    private APIResourceListModel projects = new APIResourceListModel();
    private APIResourceListModel locales = new APIResourceListModel();
    private String projectId = "";
    private String localeId = "";
    private Boolean generateConfig = false;

    @Nls
    @Override
    public String getDisplayName() {
        return "PhraseApp";
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return null;
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        clientPathField = new TextFieldWithBrowseButton();
        final FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(true, false, false, false, false, false) {
            public boolean isFileSelectable(VirtualFile file) {
                return file.getName().startsWith("phraseapp");
            }
        };
        clientPathField.addBrowseFolderListener("Choose PhraseApp Client", "", null, fileChooserDescriptor);

        if(TokenRepository.getInstance().getClientPath() == null){
            String detected = ClientDetection.findClientInstallation();
            if (detected != null){
                TokenRepository.getInstance().setClientPath(detected);
                JOptionPane.showMessageDialog(settingsUI, "We found a PhraseApp client on your system: " + detected);
            }
        }

        clientPathField.setText(TokenRepository.getInstance().getClientPath());

        JLabel clientPathLabel = new JLabel();
        clientPathLabel.setText("PhraseApp Client Path");

        final JPanel settingsUI = new JPanel(new GridBagLayout());
        GridBagConstraints cs = new GridBagConstraints();
        cs.fill = GridBagConstraints.HORIZONTAL;
        cs.insets = new Insets(0, 0, 10, 0);
        cs.gridx = 0;
        cs.gridy = 0;
        cs.gridwidth = 1;
        settingsUI.add(clientPathLabel, cs);
        cs.gridx = 1;
        cs.gridy = 0;
        cs.gridwidth = 2;
        settingsUI.add(clientPathField, cs);
        
        String config = TokenRepository.getInstance().loadPhraseAppConfig();

        if (config.startsWith("phraseapp")) {

            JTextArea configLabel = new JTextArea();
            configLabel.setEditable(false);
            configLabel.setOpaque(false);
            configLabel.setText("A .phraseap.yml configuration file has been found in your project.\nLearn more: http://docs.phraseapp.com/developers/cli/configuration/");

            cs.gridx = 0;
            cs.gridy = 1;
            cs.gridwidth = 3;
            settingsUI.add(configLabel, cs);

        } else {
            generateConfig = true;
            initializeDynamicFields();

            JScrollPane localesScrollPane = new JScrollPane(defaultLocaleSelect);
            localesScrollPane.setPreferredSize(new Dimension(120, 60));
            JScrollPane projectsScrollPane = new JScrollPane(projectSelect);
            projectsScrollPane.setPreferredSize(new Dimension(120, 60));

            JLabel projectIdLabel = new JLabel();
            projectIdLabel.setText("PhraseApp Project");

            JLabel accessTokenLabel = new JLabel();
            accessTokenLabel.setText("PhraseApp Access Token");

            JLabel accessTokenHint = new JLabel();
            accessTokenHint.setText("Please generate a APIv2 token at: https://phraseapp.com/settings/oauth_access_tokens");

            JLabel defaultLocaleLabel = new JLabel();
            defaultLocaleLabel.setText("Default locale");

            defaultStringsPathField = new TextFieldWithBrowseButton();
            final FileChooserDescriptor localeFileChooserDesc = new FileChooserDescriptor(true, false, false, false, false, false) {
                public boolean isFileSelectable(VirtualFile file) {
                    return file.getName().startsWith("strings");
                }
            };

            defaultStringsPathField.addBrowseFolderListener("Choose default locale", "", null, localeFileChooserDesc);
            defaultStringsPathField.setText(TokenRepository.getInstance().getDefaultStringsPath());
            JLabel defaultStringsPathLabel = new JLabel();
            defaultStringsPathLabel.setText("Default strings");

            updateTranslationsCheckbox = new JCheckBox("Update Translations");
            updateTranslationsCheckbox.setSelected(TokenRepository.getInstance().getUpdateTranslations());

            JTextArea generateConfigLabel = new JTextArea();
            generateConfigLabel.setEditable(false);
            generateConfigLabel.setOpaque(false);
            generateConfigLabel.setText("A .phraseapp.yml will be generated with your settings. The .phraseapp.yml will be added to your projects root folder and be editable.\nSee here for more information: http://docs.phraseapp.com/developers/cli/configuration/");

            cs.gridx = 0;
            cs.gridy = 1;
            cs.gridwidth = 1;
            settingsUI.add(accessTokenLabel, cs);
            cs.gridx = 1;
            cs.gridy = 1;
            cs.gridwidth = 2;
            settingsUI.add(accessTokenField, cs);

            cs.gridx = 1;
            cs.gridy = 2;
            cs.gridwidth = 2;
            settingsUI.add(accessTokenHint, cs);

            cs.gridx = 0;
            cs.gridy = 3;
            cs.gridwidth = 1;
            settingsUI.add(projectIdLabel, cs);
            cs.gridx = 1;
            cs.gridy = 3;
            cs.gridwidth = 2;
            settingsUI.add(projectsScrollPane, cs);

            cs.gridx = 0;
            cs.gridy = 4;
            cs.gridwidth = 1;
            settingsUI.add(defaultLocaleLabel, cs);
            cs.gridx = 1;
            cs.gridy = 4;
            cs.gridwidth = 2;
            settingsUI.add(localesScrollPane, cs);

            cs.gridx = 0;
            cs.gridy = 5;
            cs.gridwidth = 1;
            settingsUI.add(defaultStringsPathLabel, cs);
            cs.gridx = 1;
            cs.gridy = 5;
            cs.gridwidth = 2;
            settingsUI.add(defaultStringsPathField, cs);

            cs.gridx = 1;
            cs.gridy = 6;
            cs.gridwidth = 2;
            settingsUI.add(updateTranslationsCheckbox, cs);

            cs.gridx = 1;
            cs.gridy = 7;
            cs.gridwidth = 2;
            settingsUI.add(generateConfigLabel, cs);
        }

        return settingsUI;
    }

    private void initializeDynamicFields() {
        accessTokenField = new JTextField(64);
        accessTokenField.setText(TokenRepository.getInstance().getAccessToken());

        projectSelect = new JList(projects);
        projectSelect.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        projectSelect.setSelectedIndex(0);

        defaultLocaleSelect = new JList(locales);
        defaultLocaleSelect.setEnabled(false);
        defaultLocaleSelect.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        defaultLocaleSelect.setSelectedIndex(0);

        if (accessTokenField.getText().isEmpty()) {
            projectSelect.setEnabled(false);
        } else {
            API api = new API(accessTokenField.getText().trim());
            projects = api.getProjects();
            projectSelect.setModel(projects);
            projectSelect.setEnabled(true);
        }<commit_msg>styling settings layout<commit_after>package com.phraseapp.androidstudio;

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


public class PhraseAppConfigurable implements Configurable {
    private JComponent settingsUI;
    private TextFieldWithBrowseButton clientPathField;
    private JTextField accessTokenField;
    private JList projectSelect;
    private JCheckBox updateTranslationsCheckbox;
    private TextFieldWithBrowseButton defaultStringsPathField;
    private JList defaultLocaleSelect;
    private APIResourceListModel projects = new APIResourceListModel();
    private APIResourceListModel locales = new APIResourceListModel();
    private String projectId = "";
    private String localeId = "";
    private Boolean generateConfig = false;

    @Nls
    @Override
    public String getDisplayName() {
        return "PhraseApp";
    }

    @Nullable
    @Override
    public String getHelpTopic() {
        return null;
    }

    @Nullable
    @Override
    public JComponent createComponent() {

        if(TokenRepository.getInstance().getClientPath() == null){
            String detected = ClientDetection.findClientInstallation();
            if (detected != null){
                TokenRepository.getInstance().setClientPath(detected);
                JOptionPane.showMessageDialog(settingsUI, "We found a PhraseApp client on your system: " + detected);
            }
        }

        JPanel rootPanel = new JPanel(new GridBagLayout());

        final JPanel topPanel = getTopPanel();
        final JPanel settingsUI = getSettingsUI();

        GridBagConstraints cs = new GridBagConstraints();

        cs.fill = GridBagConstraints.HORIZONTAL;
        cs.anchor = GridBagConstraints.NORTHWEST;
        cs.insets = new Insets(0, 0, 20, 0);
        cs.weightx = 0.5;
        cs.gridx = 0;
        cs.gridy = 0;
        cs.gridwidth = 3;
        rootPanel.add(topPanel, cs);
        cs.insets = new Insets(0, 0, 10, 0);
        cs.weighty = 0.5;
        cs.gridx = 0;
        cs.gridy = 1;
        rootPanel.add(settingsUI,cs);

        return rootPanel;
    }

    private JPanel getTopPanel() {
        final JPanel infoPanel = new JPanel(new GridLayout(0,1));

        JTextArea infoText = new JTextArea();
        infoText.setLineWrap(true);
        infoText.setWrapStyleWord(true);
        infoText.setEditable(false);
        infoText.setOpaque(false);
        Font infoTextFont = infoText.getFont();
        Font font = new Font(infoTextFont.getFontName(), Font.BOLD, infoTextFont.getSize());
        infoText.setFont(font);
        infoText.setText("The PhraseApp plugin requires a installed PhraseApp client and a .phraseapp.yml configuration file.\nSee here for more information: http://docs.phraseapp.com/developers/android_studio");
        infoPanel.add(infoText);
        return infoPanel;
    }

    @NotNull
    private JPanel getSettingsUI() {
        final JPanel settingsUI = new JPanel(new GridBagLayout());

        clientPathField = new TextFieldWithBrowseButton();
        final FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(true, false, false, false, false, false) {
            public boolean isFileSelectable(VirtualFile file) {
                return file.getName().startsWith("phraseapp");
            }
        };
        clientPathField.addBrowseFolderListener("Choose PhraseApp Client", "", null, fileChooserDescriptor);
        clientPathField.setText(TokenRepository.getInstance().getClientPath());

        JLabel clientPathLabel = new JLabel();
        clientPathLabel.setText("PhraseApp Client Path");

        GridBagConstraints cs = new GridBagConstraints();

        cs.fill = GridBagConstraints.HORIZONTAL;
        cs.anchor = GridBagConstraints.PAGE_START;
        cs.insets = new Insets(0, 0, 10, 0);
        cs.weightx = 0;
        cs.gridx = 0;
        cs.gridy = 0;
        cs.gridwidth = 1;
        settingsUI.add(clientPathLabel, cs);
        cs.weightx = 0.5;
        cs.gridx = 1;
        cs.gridy = 0;
        cs.gridwidth = 2;
        settingsUI.add(clientPathField, cs);

        String config = TokenRepository.getInstance().loadPhraseAppConfig();

        if (config.startsWith("phraseapp")) {

            JTextArea configLabel = new JTextArea();
            configLabel.setLineWrap(true);
            configLabel.setEditable(false);
            configLabel.setOpaque(false);
            configLabel.setText("A .phraseap.yml configuration file has been found in your project. Please verify that it matches your projects requirenments.");
            cs.weightx = 0.5;
            cs.gridx = 0;
            cs.gridy = 1;
            cs.gridwidth = 3;
            settingsUI.add(configLabel, cs);

        } else {
            generateConfig = true;
            initializeDynamicFields();

            JScrollPane localesScrollPane = new JScrollPane(defaultLocaleSelect);
            JScrollPane projectsScrollPane = new JScrollPane(projectSelect);

            JLabel projectIdLabel = new JLabel();
            projectIdLabel.setText("PhraseApp Project");

            JLabel accessTokenLabel = new JLabel();
            accessTokenLabel.setText("PhraseApp Access Token");

            JLabel accessTokenHint = new JLabel();
            accessTokenHint.setText("Please generate a APIv2 token at: https://phraseapp.com/settings/oauth_access_tokens");

            JLabel defaultLocaleLabel = new JLabel();
            defaultLocaleLabel.setText("Default locale");

            JTextArea generateConfigLabel = new JTextArea();
            generateConfigLabel.setLineWrap(true);
            generateConfigLabel.setWrapStyleWord(true);
            generateConfigLabel.setEditable(false);
            generateConfigLabel.setOpaque(false);
            generateConfigLabel.setText("A .phraseapp.yml will be generated with the provided settings. The .phraseapp.yml will be added to your projects root folder.");

            defaultStringsPathField = new TextFieldWithBrowseButton();
            final FileChooserDescriptor localeFileChooserDesc = new FileChooserDescriptor(true, false, false, false, false, false) {
                public boolean isFileSelectable(VirtualFile file) {
                    return file.getName().startsWith("strings");
                }
            };

            defaultStringsPathField.addBrowseFolderListener("Choose default locale", "", null, localeFileChooserDesc);
            defaultStringsPathField.setText(TokenRepository.getInstance().getDefaultStringsPath());
            JLabel defaultStringsPathLabel = new JLabel();
            defaultStringsPathLabel.setText("Default strings");

            updateTranslationsCheckbox = new JCheckBox("Update Translations");
            updateTranslationsCheckbox.setSelected(TokenRepository.getInstance().getUpdateTranslations());

            cs.weightx = 0;
            cs.gridx = 0;
            cs.gridy = 1;
            cs.gridwidth = 1;
            settingsUI.add(accessTokenLabel, cs);
            cs.weightx = 0.5;
            cs.gridx = 1;
            cs.gridy = 1;
            cs.gridwidth = 2;
            settingsUI.add(accessTokenField, cs);

            cs.weightx = 0.5;
            cs.gridx = 1;
            cs.gridy = 2;
            cs.gridwidth = 2;
            settingsUI.add(accessTokenHint, cs);

            cs.weightx = 0;
            cs.gridx = 0;
            cs.gridy = 3;
            cs.gridwidth = 1;
            settingsUI.add(projectIdLabel, cs);
            cs.weightx = 0.5;
            cs.gridx = 1;
            cs.gridy = 3;
            cs.gridwidth = 2;
            settingsUI.add(projectsScrollPane, cs);

            cs.weightx = 0;
            cs.gridx = 0;
            cs.gridy = 4;
            cs.gridwidth = 1;
            settingsUI.add(defaultLocaleLabel, cs);
            cs.weightx = 0.5;
            cs.gridx = 1;
            cs.gridy = 4;
            cs.gridwidth = 2;
            settingsUI.add(localesScrollPane, cs);

            cs.weightx = 0;
            cs.gridx = 0;
            cs.gridy = 5;
            cs.gridwidth = 1;
            settingsUI.add(defaultStringsPathLabel, cs);
            cs.weightx = 0.5;
            cs.gridx = 1;
            cs.gridy = 5;
            cs.gridwidth = 2;
            settingsUI.add(defaultStringsPathField, cs);

            cs.weightx = 0.5;
            cs.gridx = 1;
            cs.gridy = 6;
            cs.gridwidth = 2;
            settingsUI.add(updateTranslationsCheckbox, cs);

            cs.weightx = 0.5;
            cs.gridx = 0;
            cs.gridy = 7;
            cs.gridwidth = 3;
            settingsUI.add(generateConfigLabel, cs);
        }
        return settingsUI;
    }

    private void initializeDynamicFields() {
        accessTokenField = new JTextField();
        accessTokenField.setText(TokenRepository.getInstance().getAccessToken());

        projectSelect = new JList(projects);
        projectSelect.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        projectSelect.setSelectedIndex(0);

        defaultLocaleSelect = new JList(locales);
        defaultLocaleSelect.setEnabled(false);
        defaultLocaleSelect.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        defaultLocaleSelect.setSelectedIndex(0);

        if (accessTokenField.getText().isEmpty()) {
            projectSelect.setEnabled(false);
        } else {
            API api = new API(accessTokenField.getText().trim());
            projects = api.getProjects();
            projectSelect.setModel(projects);
            projectSelect.setEnabled(true);
        }
{'fraction_non_alphanumeric': 0.09242408210771347, 'fraction_numerical': 0.008253094910591471, 'mean_word_length': 2.4469365426695844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '135050', 'n_tokens_mistral': 5241, 'n_tokens_neox': 4988, 'n_words': 1052}