Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.modeler.util;
 
 
 
A base class for monitoring progress of long running tasks. It can runshowing the exact percentage of the task progress or in "indeterminate" mode.

Warning: If the task started via "startAndWait()", caller must ensure that she is not running in the Swing EventDispatchThread, otherwise an exception is thrown, as the EvenDispatchThread will be blocked, preventing LongRunningTask from showing progress dialog.

Author(s):
Andrei Adamchik
 
 public abstract class LongRunningTask {
 
     private static final Logger logObj = Logger.getLogger(LongRunningTask.class);
 
     protected static final int DEFAULT_MS_TO_DECIDE_TO_POPUP = 500;
 
     protected ProgressDialog dialog;
     protected JFrame frame;
     protected String title;
     protected Timer taskPollingTimer;
     protected boolean canceled;
     protected int minValue;
     protected int maxValue;
     protected boolean finished;
 
     public LongRunningTask(JFrame frameString title) {
         this. = frame;
         this. = title;
     }

    
Starts current task, and blocks current thread until the task is done.
 
     public synchronized void startAndWait() {
         // running from Event Dispatch Thread is bad, as this will block the timers...
         if (SwingUtilities.isEventDispatchThread()) {
             throw new CayenneRuntimeException(
                     "Can't block EventDispatchThread. Call 'startAndWait' from another thread.");
         }
 
         start();
 
         if () {
             return;
         }
 
         try {
             wait();
         }
         catch (InterruptedException e) {
             setCanceled(true);
         }
 
         notifyAll();
     }

    
Configures the task to run in a separate thread, and immediately exits the method. This method is allowed to be invoked from EventDispatchThread.
 
     public void start() {
         // prepare...
         setCanceled(false);
         this. = false;
 
        Thread task = new Thread(new Runnable() {
            public void run() {
                internalExecute();
            }
        });
        Timer progressDisplayTimer = new Timer(
                ,
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        showProgress();
                    }
                });
        progressDisplayTimer.setRepeats(false);
        // start
        progressDisplayTimer.start();
        task.start();
    }

    
Starts progress dialog if the task is not finished yet.
    protected synchronized void showProgress() {
        .debug("will show progress...");
        if () {
            return;
        }
        int currentValue = getCurrentValue();
        if (!isCanceled() && currentValue < getMaxValue()) {
            .debug("task still in progress, will show progress dialog...");
            this. = new ProgressDialog("Progress...");
            this..getCancelButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    setCanceled(true);
                }
            });
            .getProgressBar().setMinimum(getMinValue());
            .getProgressBar().setMaximum(getMaxValue());
            updateProgress();
            this. = new Timer(500, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    updateProgress();
                }
            });
            this..start();
            this..setVisible(true);
        }
    }

    
Updates current state of the progress dialog.
    protected void updateProgress() {
        if (isCanceled()) {
            stop();
            return;
        }
        JProgressBar progressBar = .getProgressBar();
        if (!isIndeterminate()) {
            progressBar.setValue(getCurrentValue());
            progressBar.setIndeterminate(false);
        }
        else {
            progressBar.setIndeterminate(true);
        }
    }
    protected synchronized void stop() {
        if ( != null) {
            .stop();
        }
        if ( != null) {
            .dispose();
        }
        // there maybe other threads waiting on this task to finish...
         = true;
        notifyAll();
    }
    public boolean isFinished() {
        return ;
    }
    public boolean isCanceled() {
        return ;
    }
    public void setCanceled(boolean b) {
        if (b) {
            .debug("task canceled");
        }
        this. = b;
    }
    protected void internalExecute() {
        try {
            execute();
        }
        catch (Throwable th) {
            setCanceled(true);
            .warn("task error"th);
        }
        finally {
            stop();
        }
    }

    
Runs the actual task, consulting "isCanceled()" state to make sure that the task wasn't canceled.
    protected abstract void execute();
    protected abstract String getCurrentNote();
    protected abstract int getCurrentValue();
    protected abstract boolean isIndeterminate();
    public int getMaxValue() {
        return ;
    }
    public void setMaxValue(int maxValue) {
        this. = maxValue;
    }
    public int getMinValue() {
        return ;
    }
    public void setMinValue(int minValue) {
        this. = minValue;
    }
New to GrepCode? Check out our FAQ X