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

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
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.ArbilDataNode;
import nl.mpi.arbil.data.ArbilDataNodeLoader;
import nl.mpi.arbil.ui.ArbilTable;
import nl.mpi.arbil.ui.ArbilTree;
import nl.mpi.arbil.util.ArbilBugCatcher;
import nl.mpi.kinnate.entityindexer.EntityCollection;

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

    private EntityCollection entityCollection;
    private ArbilTree leftTree;
    private JTextArea resultsArea = new JTextArea();
    private JTextField searchField;

    public EntitySearchPanel(EntityCollection entityCollectionLocal, ArbilTable arbilTable) {
        entityCollection = entityCollectionLocal;
        this.setLayout(new BorderLayout());
        leftTree = new ArbilTree();
        leftTree.setCustomPreviewTable(arbilTable);
        leftTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Test Tree"), true));
//                ArrayList<URI> allEntityUris = new ArrayList<URI>();
//        String[] treeNodesArray = LinorgSessionStorage.getSingleInstance().loadStringArray("KinGraphTree");
//        if (treeNodesArray != null) {
//            ArrayList<ArbilTreeObject> tempArray = new ArrayList<ArbilTreeObject>();
//            for (String currentNodeString : treeNodesArray) {
//                try {
//                    ArbilTreeObject currentArbilNode = ArbilLoader.getSingleInstance().getArbilObject(null, new URI(currentNodeString));
//                    tempArray.add(currentArbilNode);
//                    allEntityUris.add(currentArbilNode.getURI());
//                } catch (URISyntaxException exception) {
//                    GuiHelper.linorgBugCatcher.logError(exception);
//                }
//            }
//            ArbilTreeObject[] allEntities = tempArray.toArray(new ArbilTreeObject[]{});
//            leftTree.rootNodeChildren = allEntities;
//            imdiTableModel.removeAllArbilRows();
//            imdiTableModel.addArbilObjects(leftTree.rootNodeChildren);
//        } //else {
//        //   leftTree.rootNodeChildren = new ArbilTreeObject[]{graphPanel.imdiNode};
//        // }
        leftTree.setRootVisible(false);
        leftTree.requestResort();
        JLabel searchLabel = new JLabel("Search Entity Names");
        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);
            }
        });
        JButton searchButton = new JButton("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EntitySearchPanel.this.performSearch();
            }
        });
        JPanel searchPanel = new JPanel();
        searchPanel.setLayout(new BorderLayout());
        searchPanel.add(searchLabel, BorderLayout.PAGE_START);
        searchPanel.add(searchField, BorderLayout.CENTER);
        searchPanel.add(searchButton, BorderLayout.PAGE_END);
        this.add(searchPanel, BorderLayout.PAGE_START);
        this.add(new JScrollPane(leftTree), BorderLayout.CENTER);
        this.add(resultsArea, BorderLayout.PAGE_END);
    }

    public void setTransferHandler(DragTransferHandler dragTransferHandler) {
        leftTree.setTransferHandler(dragTransferHandler);
    }

    protected void performSearch() {
        ArrayList<ArbilDataNode> resultsArray = new ArrayList<ArbilDataNode>();
        EntityCollection.SearchResults searchResults = entityCollection.searchByName(searchField.getText());
        String[] rawResultsArray = searchResults.resultsPathArray;
        resultsArea.setText(searchResults.statusMessage + "\n");
        for (String resultLine : rawResultsArray) {
            try {
                if (resultsArray.size() < 100) {
                    ArbilDataNode currentArbilObject = ArbilDataNodeLoader.getSingleInstance().getArbilDataNode(null, new URI(resultLine));
                    currentArbilObject.reloadNode();
                    resultsArray.add(currentArbilObject);
                } else {
                    resultsArea.append("results limited to 100\n");
                    break;
                }
            } catch (URISyntaxException exception) {
                new ArbilBugCatcher().logError(exception);
                resultsArea.append("error: " + resultLine + "\n");
            }
        }
        leftTree.rootNodeChildren = resultsArray.toArray(new ArbilDataNode[]{});
        leftTree.requestResort();
    }
}<commit_msg>Modified the search pane to use the KinTree nodes and query the database with a single query that returns all the entity data so that no further processing needs to be done to show the results. The resulting speed is very much improved with 8222 entities being shown in the tree in a reasonable amount of time.<commit_after>package nl.mpi.kinnate.ui;

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
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.ArbilNode;
import nl.mpi.arbil.ui.ArbilTable;
import nl.mpi.kinnate.data.KinTreeNode;
import nl.mpi.kinnate.entityindexer.EntityCollection;
import nl.mpi.kinnate.entityindexer.IndexerParameters;
import nl.mpi.kinnate.kindata.EntityData;

/**
 *  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 JTextField searchField;
    IndexerParameters indexParameters;

    public EntitySearchPanel(EntityCollection entityCollectionLocal, ArbilTable arbilTable, IndexerParameters indexParameters) {
        this.indexParameters = indexParameters;
        entityCollection = entityCollectionLocal;
        this.setLayout(new BorderLayout());
        resultsTree = new KinTree();
        resultsTree.setCustomPreviewTable(arbilTable);
        resultsTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Test Tree"), true));
        resultsTree.setRootVisible(false);
        resultsTree.requestResort();
        JLabel searchLabel = new JLabel("Search Entity Names");
        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);
            }
        });
        JButton searchButton = new JButton("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EntitySearchPanel.this.performSearch();
            }
        });
        JPanel searchPanel = new JPanel();
        searchPanel.setLayout(new BorderLayout());
        searchPanel.add(searchLabel, BorderLayout.PAGE_START);
        searchPanel.add(searchField, BorderLayout.CENTER);
        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);
    }

    public void setTransferHandler(DragTransferHandler dragTransferHandler) {
        resultsTree.setTransferHandler(dragTransferHandler);
    }

    protected void performSearch() {
        ArrayList<ArbilNode> resultsArray = new ArrayList<ArbilNode>();
        EntityData[] searchResults = entityCollection.getEntityByKeyWord(searchField.getText(), indexParameters);
        resultsArea.setText("Found " + searchResults.length + " entities\n");
        for (EntityData entityData : searchResults) {
//            if (resultsArray.size() < 1000) {
                resultsArray.add(new KinTreeNode(entityData));
//            } else {
//                resultsArea.append("results limited to 1000\n");
//                break;
//            }
        }
        resultsTree.rootNodeChildren = resultsArray.toArray(new KinTreeNode[]{});
        resultsTree.requestResort();
    }
}
{'fraction_non_alphanumeric': 0.09824523396880416, 'fraction_numerical': 0.004332755632582322, 'mean_word_length': 2.887578947368421, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '2402968', 'n_tokens_mistral': 2528, 'n_tokens_neox': 2428, 'n_words': 477}