<commit_before>package nl.mpi.kinnate.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import nl.mpi.arbil.data.ArbilDataNodeLoader;
import nl.mpi.arbil.data.ArbilNode;
import nl.mpi.arbil.util.MessageDialogHandler;
import nl.mpi.kinnate.data.KinTreeNode;
import nl.mpi.kinnate.entityindexer.EntityCollection;
import nl.mpi.kinnate.kindata.EntityData;
import nl.mpi.kinnate.svg.GraphPanel;
import nl.mpi.kinnate.uniqueidentifiers.UniqueIdentifier;

/**
 * Document : EntitySearchPanel
 * Created on : Mar 14, 2011, 4:01:11 PM
 * Author : <NAME>
 */
public class EntitySearchPanel extends JPanel {

    private EntityCollection entityCollection;
    private KinTree resultsTree;
    private JTextArea resultsArea = new JTextArea();
    private JCheckBox graphSelectionCheckBox;
    private JCheckBox expandByKinTypeCheckBox;
    private JTextField kinTypeStringTextArea;
    private JTextField searchField;
    private JProgressBar progressBar;
    private JButton searchButton;
    private JPanel searchPanel;
    private GraphPanel graphPanel;
    private MessageDialogHandler dialogHandler;
    private ArbilDataNodeLoader dataNodeLoader;

    public EntitySearchPanel(EntityCollection entityCollection, KinDiagramPanel kinDiagramPanel, GraphPanel graphPanel, MessageDialogHandler dialogHandler, ArbilDataNodeLoader dataNodeLoader, String nodeSetTitle, UniqueIdentifier[] entityIdentifiers) {
        InitPanel(entityCollection, kinDiagramPanel, graphPanel, dialogHandler, dataNodeLoader, nodeSetTitle, entityIdentifiers);
    }

    public EntitySearchPanel(EntityCollection entityCollection, KinDiagramPanel kinDiagramPanel, GraphPanel graphPanel, MessageDialogHandler dialogHandler, ArbilDataNodeLoader dataNodeLoader) {
        InitPanel(entityCollection, kinDiagramPanel, graphPanel, dialogHandler, dataNodeLoader, "Search Entity Names", null);
    }

    private void InitPanel(EntityCollection entityCollection, KinDiagramPanel kinDiagramPanel, GraphPanel graphPanel, MessageDialogHandler dialogHandler, ArbilDataNodeLoader dataNodeLoader, String nodeSetTitle, UniqueIdentifier[] entityIdentifiers) {
        this.entityCollection = entityCollection;
        this.graphPanel = graphPanel;
        this.dialogHandler = dialogHandler;
        this.dataNodeLoader = dataNodeLoader;
        this.setLayout(new BorderLayout());
        resultsTree = new KinTree(kinDiagramPanel, graphPanel);
        resultsTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Test Tree"), true));
        resultsTree.setRootVisible(false);
        // resultsTree.requestResort();// this resort is unrequred
        JLabel searchLabel = new JLabel(nodeSetTitle);
        searchField = new JTextField();
        searchField.addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    EntitySearchPanel.this.performSearch();
                }
                super.keyReleased(e);
            }
        });
        progressBar = new JProgressBar();
        searchButton = new JButton("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EntitySearchPanel.this.performSearch();
            }
        });
        searchPanel = new JPanel();
        searchPanel.setLayout(new BorderLayout());
        searchPanel.add(searchLabel, BorderLayout.PAGE_START);
        final JPanel optionsPanel = new JPanel();
        optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.PAGE_AXIS));
        searchPanel.add(optionsPanel, BorderLayout.CENTER);
        if (entityIdentifiers == null) {
            optionsPanel.add(searchField);
        }
        graphSelectionCheckBox = new JCheckBox("Graph selection", true);
        optionsPanel.add(graphSelectionCheckBox);
        expandByKinTypeCheckBox = new JCheckBox("Expand selection by kin type string", true);
        optionsPanel.add(expandByKinTypeCheckBox);
        kinTypeStringTextArea = new JTextField();
        optionsPanel.add(kinTypeStringTextArea);
        graphSelectionCheckBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                expandByKinTypeCheckBox.setEnabled(graphSelectionCheckBox.isSelected());
                kinTypeStringTextArea.setEnabled(expandByKinTypeCheckBox.isSelected() && graphSelectionCheckBox.isSelected());

            }
        });
        expandByKinTypeCheckBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                kinTypeStringTextArea.setEnabled(expandByKinTypeCheckBox.isSelected());
            }
        });
        optionsPanel.add(new JCheckBox("Link to graph selection to this tree", false));
        if (entityIdentifiers == null) {
            searchPanel.add(searchButton, BorderLayout.PAGE_END);
        }
        this.add(searchPanel, BorderLayout.PAGE_START);
        this.add(new JScrollPane(resultsTree), BorderLayout.CENTER);
        this.add(resultsArea, BorderLayout.PAGE_END);
        if (entityIdentifiers != null) {
            loadCollection(entityIdentifiers);
        }
    }

    public void setTransferHandler(KinDragTransferHandler dragTransferHandler) {
        resultsTree.setTransferHandler(dragTransferHandler);
        resultsTree.setDragEnabled(true);
    }

    protected void performSearch() {
        searchPanel.remove(searchButton);
        progressBar.setIndeterminate(true);
        searchPanel.add(progressBar, BorderLayout.PAGE_END);
        searchPanel.revalidate();
        new Thread() {

            @Override
            public void run() {
                ArrayList<ArbilNode> resultsArray = new ArrayList<ArbilNode>();
                EntityData[] searchResults = entityCollection.getEntityByKeyWord(searchField.getText(), graphPanel.getIndexParameters());
                resultsArea.setText("Found " + searchResults.length + " entities\n");
                for (EntityData entityData : searchResults) {
//            if (resultsArray.size() < 1000) {
                    resultsArray.add(new KinTreeNode(entityData, graphPanel.getIndexParameters(), dialogHandler, entityCollection, dataNodeLoader));
//            } else {
//                resultsArea.append("results limited to 1000\n");
//                break;
//            }
                }
                resultsTree.rootNodeChildren = resultsArray.toArray(new KinTreeNode[]{});
                resultsTree.requestResort();
                searchPanel.remove(progressBar);
                searchPanel.add(searchButton, BorderLayout.PAGE_END);
                searchPanel.revalidate();
            }
        }.start();
    }

    protected void loadCollection(final UniqueIdentifier[] entityIdentifiers) {
        progressBar.setIndeterminate(false);
        progressBar.setMinimum(0);
        progressBar.setMaximum(entityIdentifiers.length);
        progressBar.setValue(0);
        searchPanel.add(progressBar, BorderLayout.PAGE_END);
        searchPanel.revalidate();
        new Thread() {

            @Override
            public void run() {
                ArrayList<ArbilNode> resultsArray = new ArrayList<ArbilNode>();
                resultsArea.setText("Loading " + entityIdentifiers.length + " entities\n");
                int loadedCount = 0;
                for (UniqueIdentifier entityId : entityIdentifiers) {
                    EntityData entityData = entityCollection.getEntity(entityId, graphPanel.getIndexParameters());
                    resultsArray.add(new KinTreeNode(entityData, graphPanel.getIndexParameters(), dialogHandler, entityCollection, dataNodeLoader));
                    resultsTree.rootNodeChildren = resultsArray.toArray(new KinTreeNode[]{});
                    resultsTree.requestResort();
                    loadedCount++;
                    resultsArea.setText("Loaded " + loadedCount + " of " + entityIdentifiers.length + " entities\n");
                    progressBar.setValue(loadedCount);
                }
                resultsArea.setText("");
                resultsArea.setVisible(false);
                searchPanel.remove(progressBar);
                searchPanel.revalidate();
            }
        }.start();
    }
}
<commit_msg>In order to make the import results easier for users to examine: Added options to the search panel to show any selected node on the results tree in the graph and to expand that selection by kin type string. Added a diagram tree for imported entities so that the imported entities can be browsed by the user. Added the drag handler to the imported nodes tree. When an import is complete the imported entities tree is shown in a graph window. Updated the gedcom import to be more friendly in the format of the date resulting from the import, including type labels so that the user knows a submitter from a submission etc. Reduced the stress on the application when importing by providing the user with the imported entities in a tree rather than trying to graph them all at the same time. Modified the search and imported node trees to show the selected nodes in the graph and to expand that selection by user entered kintype strings.<commit_after>package nl.mpi.kinnate.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import nl.mpi.arbil.data.ArbilDataNodeLoader;
import nl.mpi.arbil.data.ArbilNode;
import nl.mpi.arbil.util.MessageDialogHandler;
import nl.mpi.kinnate.data.KinTreeNode;
import nl.mpi.kinnate.entityindexer.EntityCollection;
import nl.mpi.kinnate.kindata.EntityData;
import nl.mpi.kinnate.kintypestrings.ParserHighlight;
import nl.mpi.kinnate.svg.GraphPanel;
import nl.mpi.kinnate.uniqueidentifiers.UniqueIdentifier;

/**
 * Document : EntitySearchPanel
 * Created on : Mar 14, 2011, 4:01:11 PM
 * Author : <NAME>
 */
public class EntitySearchPanel extends JPanel implements KinTypeStringProvider {

    private EntityCollection entityCollection;
    private KinTree resultsTree;
    private JTextArea resultsArea = new JTextArea();
    private JCheckBox graphSelectionCheckBox;
    private JCheckBox expandByKinTypeCheckBox;
    private JTextField kinTypeStringTextArea;
    private JTextField searchField;
    private JProgressBar progressBar;
    private JButton searchButton;
    private JPanel searchPanel;
    private GraphPanel graphPanel;
    private MessageDialogHandler dialogHandler;
    private ArbilDataNodeLoader dataNodeLoader;
    private String kinTypeString = "*";

    public EntitySearchPanel(EntityCollection entityCollection, KinDiagramPanel kinDiagramPanel, GraphPanel graphPanel, MessageDialogHandler dialogHandler, ArbilDataNodeLoader dataNodeLoader, String nodeSetTitle, UniqueIdentifier[] entityIdentifiers) {
        InitPanel(entityCollection, kinDiagramPanel, graphPanel, dialogHandler, dataNodeLoader, nodeSetTitle, entityIdentifiers);
    }

    public EntitySearchPanel(EntityCollection entityCollection, KinDiagramPanel kinDiagramPanel, GraphPanel graphPanel, MessageDialogHandler dialogHandler, ArbilDataNodeLoader dataNodeLoader) {
        InitPanel(entityCollection, kinDiagramPanel, graphPanel, dialogHandler, dataNodeLoader, "Search Entity Names", null);
    }

    private void InitPanel(EntityCollection entityCollection, final KinDiagramPanel kinDiagramPanel, GraphPanel graphPanel, MessageDialogHandler dialogHandler, ArbilDataNodeLoader dataNodeLoader, String nodeSetTitle, UniqueIdentifier[] entityIdentifiers) {
        this.entityCollection = entityCollection;
        this.graphPanel = graphPanel;
        this.dialogHandler = dialogHandler;
        this.dataNodeLoader = dataNodeLoader;
        this.setLayout(new BorderLayout());
        resultsTree = new KinTree(kinDiagramPanel, graphPanel);
        resultsTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Test Tree"), true));
//        resultsTree.setRootVisible(false);
        // resultsTree.requestResort();// this resort is unrequred
        JLabel searchLabel = new JLabel(nodeSetTitle);
        searchField = new JTextField();
        searchField.addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    EntitySearchPanel.this.performSearch();
                }
                super.keyReleased(e);
            }
        });
        progressBar = new JProgressBar();
        searchButton = new JButton("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EntitySearchPanel.this.performSearch();
            }
        });
        searchPanel = new JPanel();
        searchPanel.setLayout(new BorderLayout());
        searchPanel.add(searchLabel, BorderLayout.PAGE_START);
        final JPanel optionsPanel = new JPanel();
        optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.PAGE_AXIS));
        searchPanel.add(optionsPanel, BorderLayout.CENTER);
        if (entityIdentifiers == null) {
            optionsPanel.add(searchField);
        }
        graphSelectionCheckBox = new JCheckBox("Graph selection", true);
        // graph the selection when checked
        optionsPanel.add(graphSelectionCheckBox);
        expandByKinTypeCheckBox = new JCheckBox("Expand selection by kin type string", true);
        // expand the selection when checked
        optionsPanel.add(expandByKinTypeCheckBox);
        kinTypeStringTextArea = new JTextField(kinTypeString);
        kinTypeStringTextArea.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {
            }

            public void keyPressed(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
                synchronized (e) {
                    if (!kinTypeStringTextArea.getText().equals(kinTypeString)) {
                        kinTypeString = kinTypeStringTextArea.getText();
                        if (expandByKinTypeCheckBox.isSelected() && graphSelectionCheckBox.isSelected()) {
                            // update if text changed and selected
                            kinDiagramPanel.drawGraph();
                        }
                    }

                }
            }
        });
        optionsPanel.add(kinTypeStringTextArea);
        graphSelectionCheckBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                expandByKinTypeCheckBox.setEnabled(graphSelectionCheckBox.isSelected());
                kinTypeStringTextArea.setEnabled(expandByKinTypeCheckBox.isSelected() && graphSelectionCheckBox.isSelected());
                kinDiagramPanel.drawGraph();
            }
        });
        expandByKinTypeCheckBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                kinTypeStringTextArea.setEnabled(expandByKinTypeCheckBox.isSelected());
                kinDiagramPanel.drawGraph();
            }
        });
//        optionsPanel.add(new JCheckBox("Link to graph selection to this tree", false));
        // todo: link this selection when checked
        if (entityIdentifiers == null) {
            searchPanel.add(searchButton, BorderLayout.PAGE_END);
        }
        this.add(searchPanel, BorderLayout.PAGE_START);
        this.add(new JScrollPane(resultsTree), BorderLayout.CENTER);
        this.add(resultsArea, BorderLayout.PAGE_END);
        if (entityIdentifiers != null) {
            loadCollection(entityIdentifiers);
        }
    }

    public void setTransferHandler(KinDragTransferHandler dragTransferHandler) {
        resultsTree.setTransferHandler(dragTransferHandler);
        resultsTree.setDragEnabled(true);
    }

    protected void performSearch() {
        searchPanel.remove(searchButton);
        progressBar.setIndeterminate(true);
        searchPanel.add(progressBar, BorderLayout.PAGE_END);
        searchPanel.revalidate();
        new Thread() {

            @Override
            public void run() {
                ArrayList<ArbilNode> resultsArray = new ArrayList<ArbilNode>();
                EntityData[] searchResults = entityCollection.getEntityByKeyWord(searchField.getText(), graphPanel.getIndexParameters());
                resultsArea.setText("Found " + searchResults.length + " entities\n");
                for (EntityData entityData : searchResults) {
//            if (resultsArray.size() < 1000) {
                    resultsArray.add(new KinTreeNode(entityData, graphPanel.getIndexParameters(), dialogHandler, entityCollection, dataNodeLoader));
//            } else {
//                resultsArea.append("results limited to 1000\n");
//                break;
//            }
                }
                resultsTree.rootNodeChildren = resultsArray.toArray(new KinTreeNode[]{});
                resultsTree.requestResort();
                searchPanel.remove(progressBar);
                searchPanel.add(searchButton, BorderLayout.PAGE_END);
                searchPanel.revalidate();
            }
        }.start();
    }

    protected void loadCollection(final UniqueIdentifier[] entityIdentifiers) {
        progressBar.setIndeterminate(false);
        progressBar.setMinimum(0);
        progressBar.setMaximum(entityIdentifiers.length);
        progressBar.setValue(0);
        searchPanel.add(progressBar, BorderLayout.PAGE_END);
        searchPanel.revalidate();
        new Thread() {

            @Override
            public void run() {
                ArrayList<ArbilNode> resultsArray = new ArrayList<ArbilNode>();
                resultsArea.setText("Loading " + entityIdentifiers.length + " entities\n");
                int loadedCount = 0;
                for (UniqueIdentifier entityId : entityIdentifiers) {
                    EntityData entityData = entityCollection.getEntity(entityId, graphPanel.getIndexParameters());
                    resultsArray.add(new KinTreeNode(entityData, graphPanel.getIndexParameters(), dialogHandler, entityCollection, dataNodeLoader));
                    resultsTree.rootNodeChildren = resultsArray.toArray(new KinTreeNode[]{});
                    resultsTree.requestResort();
                    loadedCount++;
                    resultsArea.setText("Loaded " + loadedCount + " of " + entityIdentifiers.length + " entities\n");
                    progressBar.setValue(loadedCount);
                }
                resultsArea.setText("");
                resultsArea.setVisible(false);
                searchPanel.remove(progressBar);
                searchPanel.revalidate();
            }
        }.start();
    }

    public String[] getCurrentStrings() {
        ArrayList<String> currentStrings = new ArrayList<String>();
        if (graphSelectionCheckBox.isSelected()) {
            String kinTypeStringExtention = "";
            if (expandByKinTypeCheckBox.isSelected()) {
                kinTypeStringExtention = kinTypeString;
            }
            for (ArbilNode arbilNode : resultsTree.getSelectedNodeArray()) { // return getSelectedNodesOfType(ArbilNode.class).toArray(new ArbilNode[]{});
                if (arbilNode instanceof KinTreeNode) {
                    currentStrings.add("x[Entity.Identifier=" + ((KinTreeNode) arbilNode).entityData.getUniqueIdentifier().getQueryIdentifier() + "]" + kinTypeStringExtention);
                }
            }
        }
        return currentStrings.toArray(new String[]{});
    }

    public int getTotalLength() {
        if (graphSelectionCheckBox.isSelected()) {
            return resultsTree.getSelectedNodeArray().length;
        } else {
            return 0;
        }
    }

    public void highlightKinTypeStrings(ParserHighlight[] parserHighlight, String[] kinTypeStrings) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }
}

{'fraction_non_alphanumeric': 0.08585138730520715, 'fraction_numerical': 0.002137970353477765, 'mean_word_length': 2.893636699963004, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '1692393', 'n_tokens_mistral': 5355, 'n_tokens_neox': 5137, 'n_words': 1125}