<commit_before>package com.codebrig.jnomad.plugin.intellij.inspection;

import com.google.gson.Gson;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.ui.Messages;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author <NAME> <<EMAIL>>
 */
class JNomadConfigurationPanel extends JPanel {

    private DefaultListModel<JNomadPluginConfiguration.DBEnvironment> environmentListModel = new DefaultListModel<>();
    private DefaultListModel<JNomadPluginConfiguration.DBConnection> databaseListModel = new DefaultListModel<>();
    private JNomadPluginConfiguration pluginConfiguration;
    private int selectedEnvironment = -1;
    private int selectedDatabase = -1;

    JNomadConfigurationPanel() {
        initComponents();

        //button listeners
        testConnectionButton.addActionListener(this::testConnectionButton);
        addConnectionButton.addActionListener(this::addConnectionButton);
        deleteConnectionButton.addActionListener(this::deleteConnectionButton);
        addEnvironmentButton.addActionListener(this::addEnvironmentButton);
        deleteEnvironmentButton.addActionListener(this::deleteEnvironmentButton);

        //UI stuff
        environmentList.setModel(environmentListModel);
        environmentList.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                selectedEnvironment = e.getFirstIndex();
                if (environmentList.isSelectionEmpty()) {
                    selectedEnvironment = -1;
                }
                loadEnvironment();
            }
        });
        databaseList.setModel(databaseListModel);
        databaseList.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                selectedDatabase = e.getFirstIndex();
                if (databaseList.isSelectionEmpty()) {
                    selectedDatabase = -1;
                }
                loadDatabase();
            }
        });

        //load JNomad plugin configuration
        for (Project project : ProjectManager.getInstance().getOpenProjects()) {
            PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(project);
            String configStr = propertiesComponent.getValue("jnomad.plugin.configuration");
            if (configStr != null) {
                pluginConfiguration = new Gson().fromJson(configStr, JNomadPluginConfiguration.class);
            }
        }
        if (pluginConfiguration == null) {
            pluginConfiguration = new JNomadPluginConfiguration();
        }

        for (JNomadPluginConfiguration.DBEnvironment env : pluginConfiguration.getEnvironmentList()) {
            environmentListModel.addElement(env);
        }
        slowQuerySpinner.setValue(pluginConfiguration.getSlowQueryThreshold());
        recommendIndexSpinner.setValue(pluginConfiguration.getRecommendIndexThreshold());

        //default select first environment
        if (!environmentListModel.isEmpty()) {
            environmentList.setSelectedIndex(0);
            databaseList.clearSelection();
            databaseHostTextField.setText("");
            databasePortTextField.setText("");
            databaseDBTextField.setText("");
            databaseUsernameTextField.setText("");
            databasePasswordTextField.setText("");
        }
    }

    private void loadDatabase() {
        boolean databaseSelected = selectedDatabase != -1;
        deleteConnectionButton.setEnabled(databaseSelected);

        if (databaseSelected) {
            JNomadPluginConfiguration.DBConnection conn = databaseListModel.getElementAt(selectedDatabase);
            databaseHostTextField.setText(conn.getHost());
            databasePortTextField.setText(Integer.toString(conn.getPort()));
            databaseDBTextField.setText(conn.getDatabase());
            databaseUsernameTextField.setText(conn.getUsername());
            databasePasswordTextField.setText(conn.<PASSWORD>());
        } else {
            databaseHostTextField.setText("");
            databasePortTextField.setText("");
            databaseDBTextField.setText("");
            databaseUsernameTextField.setText("");
            databasePasswordTextField.setText("");
        }
    }

    private void loadEnvironment() {
        boolean environmentSelected = selectedEnvironment != -1;
        deleteEnvironmentButton.setEnabled(environmentSelected);
        databaseHostTextField.setEnabled(environmentSelected);
        databasePortTextField.setEnabled(environmentSelected);
        databaseDBTextField.setEnabled(environmentSelected);
        databaseUsernameTextField.setEnabled(environmentSelected);
        databasePasswordTextField.setEnabled(environmentSelected);
        testConnectionButton.setEnabled(environmentSelected);
        addConnectionButton.setEnabled(environmentSelected);
        deleteConnectionButton.setEnabled(environmentSelected);
        databaseListModel.clear();
        databaseHostTextField.setText("");
        databasePortTextField.setText("");
        databaseDBTextField.setText("");
        databaseUsernameTextField.setText("");
        databasePasswordTextField.setText("");

        if (environmentSelected) {
            JNomadPluginConfiguration.DBEnvironment env = environmentList.getSelectedValue();
            for (int z = 0; z < env.getConnectionList().size(); z++) {
                databaseListModel.addElement(env.getConnectionList().get(z));
            }
        }
    }
<commit_msg>refactoring<commit_after>package com.codebrig.jnomad.plugin.intellij.inspection;

import com.google.gson.Gson;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.ui.Messages;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author <NAME> <<EMAIL>>
 */
class JNomadConfigurationPanel extends JPanel {

    private DefaultListModel<JNomadPluginConfiguration.DBEnvironment> environmentListModel = new DefaultListModel<>();
    private DefaultListModel<JNomadPluginConfiguration.DBConnection> databaseListModel = new DefaultListModel<>();
    private JNomadPluginConfiguration pluginConfiguration;

    JNomadConfigurationPanel() {
        initComponents();

        //button listeners
        testConnectionButton.addActionListener(this::testConnectionButton);
        addConnectionButton.addActionListener(this::addConnectionButton);
        deleteConnectionButton.addActionListener(this::deleteConnectionButton);
        addEnvironmentButton.addActionListener(this::addEnvironmentButton);
        deleteEnvironmentButton.addActionListener(this::deleteEnvironmentButton);

        //UI stuff
        environmentList.setModel(environmentListModel);
        environmentList.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                loadEnvironment();
            }
        });
        databaseList.setModel(databaseListModel);
        databaseList.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                loadDatabase();
            }
        });

        //load JNomad plugin configuration
        for (Project project : ProjectManager.getInstance().getOpenProjects()) {
            PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(project);
            String configStr = propertiesComponent.getValue("jnomad.plugin.configuration");
            if (configStr != null) {
                pluginConfiguration = new Gson().fromJson(configStr, JNomadPluginConfiguration.class);
            }
        }
        if (pluginConfiguration == null) {
            pluginConfiguration = new JNomadPluginConfiguration();
        }

        for (JNomadPluginConfiguration.DBEnvironment env : pluginConfiguration.getEnvironmentList()) {
            environmentListModel.addElement(env);
        }
        slowQuerySpinner.setValue(pluginConfiguration.getSlowQueryThreshold());
        recommendIndexSpinner.setValue(pluginConfiguration.getRecommendIndexThreshold());

        //default select first environment
        if (!environmentListModel.isEmpty()) {
            environmentList.setSelectedIndex(0);
            databaseList.clearSelection();
            databaseHostTextField.setText("");
            databasePortTextField.setText("");
            databaseDBTextField.setText("");
            databaseUsernameTextField.setText("");
            databasePasswordTextField.setText("");
        }
    }

    private void loadDatabase() {
        boolean databaseSelected = !databaseList.isSelectionEmpty();
        deleteConnectionButton.setEnabled(databaseSelected);

        if (databaseSelected) {
            JNomadPluginConfiguration.DBConnection conn = databaseList.getSelectedValue();
            databaseHostTextField.setText(conn.getHost());
            databasePortTextField.setText(Integer.toString(conn.getPort()));
            databaseDBTextField.setText(conn.getDatabase());
            databaseUsernameTextField.setText(conn.getUsername());
            databasePasswordTextField.setText(conn.getPassword());
        } else {
            databaseHostTextField.setText("");
            databasePortTextField.setText("");
            databaseDBTextField.setText("");
            databaseUsernameTextField.setText("");
            databasePasswordTextField.setText("");
        }
    }

    private void loadEnvironment() {
        boolean environmentSelected = !environmentList.isSelectionEmpty();
        deleteEnvironmentButton.setEnabled(environmentSelected);
        databaseHostTextField.setEnabled(environmentSelected);
        databasePortTextField.setEnabled(environmentSelected);
        databaseDBTextField.setEnabled(environmentSelected);
        databaseUsernameTextField.setEnabled(environmentSelected);
        databasePasswordTextField.setEnabled(environmentSelected);
        testConnectionButton.setEnabled(environmentSelected);
        addConnectionButton.setEnabled(environmentSelected);
        deleteConnectionButton.setEnabled(environmentSelected);
        databaseListModel.clear();
        databaseHostTextField.setText("");
        databasePortTextField.setText("");
        databaseDBTextField.setText("");
        databaseUsernameTextField.setText("");
        databasePasswordTextField.setText("");

        if (environmentSelected) {
            JNomadPluginConfiguration.DBEnvironment env = environmentList.getSelectedValue();
            for (int z = 0; z < env.getConnectionList().size(); z++) {
                databaseListModel.addElement(env.getConnectionList().get(z));
            }
        }
    }

{'fraction_non_alphanumeric': 0.09590299467205585, 'fraction_numerical': 0.0009186110600771634, 'mean_word_length': 3.1348271933156098, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, 'https://': 0, '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': '3231773', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2406, 'n_words': 352}