<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package net.localizethat.gui.dialogs;

import java.beans.Beans;
import java.util.List;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;
import javax.swing.JOptionPane;
import net.localizethat.Main;
import net.localizethat.model.L10n;
import net.localizethat.model.LocalePath;
import net.localizethat.model.Product;
import net.localizethat.util.gui.ModalDialog;
import net.localizethat.util.gui.ModalDialogComponent;

/**
 *
 * @author rpalomares
 */
public class ChooseTreePanel extends javax.swing.JPanel implements ModalDialogComponent {
    EntityManagerFactory emf;
    ModalDialog md;

    /**
     * Creates new form ChooseTreePanel
     */
    public ChooseTreePanel() {
        super();
        emf = Main.emf;
        // The following code is executed inside initComponents()
        // entityManager = emf.createEntityManager();

        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
        refreshL10nList();
        refreshProductList();
        refreshOrigPathList();
    }

    private void refreshL10nList() {
        TypedQuery<L10n> l10nQuery = entityManager.createNamedQuery("L10n.findAll",
                L10n.class);
        l10nListModel.clearAll();
        l10nListModel.addAll(l10nQuery.getResultList());
    }

    private void refreshProductList() {
        TypedQuery<Product> productQuery = entityManager.createNamedQuery("Product.findAll",
                Product.class);
        productListModel.clearAll();
        productListModel.addAll(productQuery.getResultList());
    }

    private void refreshOrigPathList() {
        TypedQuery<LocalePath> localePathQuery = entityManager.createNamedQuery(
                "LocalePath.findOriginals", LocalePath.class);
        localePathListModel.clearAll();
        localePathListModel.addAll(localePathQuery.getResultList());
    }

    @Override
    public void setModalDialogReference(ModalDialog md) {
        this.md = md;
    }

    public L10n getTargetL10n() {
        return l10nListModel.getSelectedTypedItem();
    }

    public Product getSelectedProduct() {
        if (this.productPathsRadio.isSelected()) {
            return productListModel.getSelectedTypedItem();
        } else {
            return null;
        }
    }

    public List<LocalePath> getSelectedPaths() {
        if (this.selectedPathsRadio.isSelected()) {
            return this.pathsList.getSelectedValuesList();
        } else {
            return localePathListModel.getAll();
        }
    }


    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        entityManager = emf.createEntityManager();
        treeContentsRadioGroup = new javax.swing.ButtonGroup();
        l10nListModel = new net.localizethat.gui.models.ListComboBoxGenericModel<L10n>();
        productListModel = new net.localizethat.gui.models.ListComboBoxGenericModel<Product>();
        localePathListModel = new net.localizethat.gui.models.ListComboBoxGenericModel<LocalePath>();
        targetLocaleLabel = new javax.swing.JLabel();
        targetLocaleCombo = new javax.swing.JComboBox();
        treeContentLabel = new javax.swing.JLabel();
        allPathsRadio = new javax.swing.JRadioButton();
        productPathsRadio = new javax.swing.JRadioButton();
        productCombo = new javax.swing.JComboBox();
        selectedPathsRadio = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        pathsList = new javax.swing.JList();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        targetLocaleLabel.setText("Choose target locale:");

        targetLocaleCombo.setModel(l10nListModel);
<commit_msg>Allow to return the EntityManager to reuse it for EditContentPanel (creating an EntityManager is an expensive task).<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package net.localizethat.gui.dialogs;

import java.beans.Beans;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;
import javax.swing.JOptionPane;
import net.localizethat.Main;
import net.localizethat.model.L10n;
import net.localizethat.model.LocalePath;
import net.localizethat.model.Product;
import net.localizethat.util.gui.ModalDialog;
import net.localizethat.util.gui.ModalDialogComponent;

/**
 *
 * @author rpalomares
 */
public class ChooseTreePanel extends javax.swing.JPanel implements ModalDialogComponent {
    EntityManagerFactory emf;
    ModalDialog md;

    /**
     * Creates new form ChooseTreePanel
     */
    public ChooseTreePanel() {
        super();
        emf = Main.emf;
        // The following code is executed inside initComponents()
        // entityManager = emf.createEntityManager();

        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
        refreshL10nList();
        refreshProductList();
        refreshOrigPathList();
    }

    private void refreshL10nList() {
        TypedQuery<L10n> l10nQuery = entityManager.createNamedQuery("L10n.findAll",
                L10n.class);
        l10nListModel.clearAll();
        l10nListModel.addAll(l10nQuery.getResultList());
    }

    private void refreshProductList() {
        TypedQuery<Product> productQuery = entityManager.createNamedQuery("Product.findAll",
                Product.class);
        productListModel.clearAll();
        productListModel.addAll(productQuery.getResultList());
    }

    private void refreshOrigPathList() {
        TypedQuery<LocalePath> localePathQuery = entityManager.createNamedQuery(
                "LocalePath.findOriginals", LocalePath.class);
        localePathListModel.clearAll();
        localePathListModel.addAll(localePathQuery.getResultList());
    }

    @Override
    public void setModalDialogReference(ModalDialog md) {
        this.md = md;
    }

    public L10n getTargetL10n() {
        return l10nListModel.getSelectedTypedItem();
    }

    public Product getSelectedProduct() {
        if (this.productPathsRadio.isSelected()) {
            return productListModel.getSelectedTypedItem();
        } else {
            return null;
        }
    }

    public List<LocalePath> getSelectedPaths() {
        if (this.selectedPathsRadio.isSelected()) {
            return this.pathsList.getSelectedValuesList();
        } else {
            return localePathListModel.getAll();
        }
    }

    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        entityManager = emf.createEntityManager();
        treeContentsRadioGroup = new javax.swing.ButtonGroup();
        l10nListModel = new net.localizethat.gui.models.ListComboBoxGenericModel<L10n>();
        productListModel = new net.localizethat.gui.models.ListComboBoxGenericModel<Product>();
        localePathListModel = new net.localizethat.gui.models.ListComboBoxGenericModel<LocalePath>();
        targetLocaleLabel = new javax.swing.JLabel();
        targetLocaleCombo = new javax.swing.JComboBox();
        treeContentLabel = new javax.swing.JLabel();
        allPathsRadio = new javax.swing.JRadioButton();
        productPathsRadio = new javax.swing.JRadioButton();
        productCombo = new javax.swing.JComboBox();
        selectedPathsRadio = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        pathsList = new javax.swing.JList();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        targetLocaleLabel.setText("Choose target locale:");

        targetLocaleCombo.setModel(l10nListModel);

{'fraction_non_alphanumeric': 0.09903218546027459, 'fraction_numerical': 0.008327706504613999, 'mean_word_length': 3.035876475930972, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '7245066', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2434, 'n_words': 521}