Example usage for javax.swing SwingWorker run

List of usage examples for javax.swing SwingWorker run

Introduction

In this page you can find the example usage for javax.swing SwingWorker run.

Prototype

public final void run() 

Source Link

Document

Sets this Future to the result of computation unless it has been cancelled.

Usage

From source file:org.genedb.jogra.plugins.TermRationaliser.java

/**
 * Populates the JLists with data from the database
 * This happens right at the start to load the rationaliser
 * and then any time the user decides to refresh the 
 * models from the database. We use two parallel threads 
 * here to fetch the terms, but will eventually implement
 * some sort of caching mechanism./*from  ww  w .j  ava  2s  . c  o m*/
 */
private void initModels() {

    JograProgressBar jpb = new JograProgressBar("Loading terms from database..."); //Progress bar added for better user information    
    this.setSelectedTaxonsAndScopeLabel(jogra.getSelectedOrganismNames());

    logger.info("Are we in EDT when loading data? " + SwingUtilities.isEventDispatchThread());

    /* Loading the terms from the database */
    SwingWorker<Void, Void> worker_1 = new SwingWorker<Void, Void>() {
        @Override
        public Void doInBackground() {
            try {
                long startTime = System.nanoTime();
                long endTime;
                terms = termService.getTerms(getSelectedTaxons(), getTermType());
                /* Java Collections sort is a modified mergesort guaranteeing 
                 * n log(n) performance. The question is, is it slower or faster
                 * than doing a sql order by in postgres? It appears postgres
                 * uses qsort so the 'average' performance should also be n log(n).
                 * After some experimentation, we use Java's sort here since it appeared
                 * marginally faster. */
                Collections.sort(terms);
                endTime = System.nanoTime();
                logger.info("Doing sorting in SQL (specific) took : " + (endTime - startTime) + " ns.");
                for (Term term : terms) {
                    if (isShowEVC()) { //Fetch the evidence codes for the terms if the user wants them
                        term.setEvidenceCodes(termService.getEvidenceCodes(term));
                    }
                }

            } catch (SQLException se) {
                se.printStackTrace(); //How do we process this exception?
            }
            return null;
        }

        @Override
        public void done() {
            logger.info("Finished worker 1");

        }
    };

    SwingWorker<Void, Void> worker_2 = new SwingWorker<Void, Void>() {
        @Override
        public Void doInBackground() {
            try {
                long startTime = System.nanoTime();
                long endTime;
                allTerms = termService.getAllTerms(getTermType());
                Collections.sort(allTerms);
                endTime = System.nanoTime();
                logger.info("Doing sorting in SQL (general) took : " + (endTime - startTime) + " ns.");

                for (Term term : allTerms) {
                    if (isShowEVC()) { //Fetch the evidence codes for the terms if the user wants them
                        term.setEvidenceCodes(termService.getEvidenceCodes(term));
                    }
                }

            } catch (SQLException se) {
                se.printStackTrace();
            }

            return null;
        }

        @Override
        public void done() {
            logger.info("Finished worker 2");

        }
    };

    logger.info("Inside swing worker 1 to fetch and sort the specific terms");
    writeMessage("Fetching organism-specific terms from the database...\n");
    worker_1.run();
    logger.info("Inside swing worker 2 to fetch and sort all the cv terms");
    writeMessage("Fetching all the terms in the cv from the database...\n");
    worker_2.run();

    //After the data is available...
    fromList.addAll(terms);
    toList.addAll(allTerms);
    productCountLabel.setText(String.format("Number of terms for selected organisms: %d terms found (%s)",
            terms.size(), getTermType()));
    textField.setText(""); //Re-set the editable text box     
    fromList.clearSelection(); //Clear any previous selections
    toList.clearSelection();
    fromList.repaint();
    toList.repaint();

    jpb.stop();

}