All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.becauseQA.window.swingworker.AbstractSwingTasker Maven / Gradle / Ivy

package com.github.becauseQA.window.swingworker;

import java.awt.Component;
import java.awt.Toolkit;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;

import org.apache.log4j.Logger;

/*
 * 

You can, but the SwingWorker has methods designed to report progress of a background task: you call publish() from doInBackground() to publish progress, and you override process() (which is called in the EDT) in order to display the progress. So the above code could be rewritten as:
 * SwingUtilities.invokeLater takes a Runnable and invokoes it in the ui thread later. Usually for short running ui related work.

SwingWorker runs the main work in a non ui thread - the worker thread. After the long running work is done the done() method is invoked in the ui thread (Event Dispatch Thread).

But the SwingWorker's doInBackground() method can also publish intermediate results by invoking the publish() method. The SwingWorker will than ensure that the results to publish are processed by the Event Dispatch Thread. You can hook in by implementing the process() method.
 * 
 * UI Thread
 * Event dispatcher Thread
 */
public class AbstractSwingTasker extends SwingWorker implements PropertyChangeListener {

	private static final Logger log = Logger.getLogger(AbstractSwingTasker.class);
	private ProgressMonitor progressMonitor;

	public AbstractSwingTasker(Component component) {
		// TODO Auto-generated constructor stub
		this.progressMonitor = new ProgressMonitor(component, "", "", 0, 100);;
		progressMonitor.setProgress(0);
		addPropertyChangeListener(this);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub
		if ("progress" == evt.getPropertyName()) {

			int progress = (Integer) evt.getNewValue();
			// int progress = this.getProgress();
			progressMonitor.setProgress(progress);
			String message = String.format("Completed %d%%.\n", progress);
			progressMonitor.setNote(message);
			log.info(message);
			if (progressMonitor.isCanceled() || isDone()) {
				// progressMonitor.close();
				Toolkit.getDefaultToolkit().beep(); // show an audio to complete
													// the setup
				if (progressMonitor.isCanceled()) {
					this.cancel(true);
					log.info("Task cancelled.\n");
				} else {
					log.info("Task completed.\n");
				}
			}
		}

	}

	@Override
	protected T doInBackground() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void done() {
		// TODO Auto-generated method stub
		// this.setCursor(null); //turn off the wait cursor
		progressMonitor.setProgress(100);
		log.info("Complete the job,finished!");
	}

	@Override
	protected void process(List chunks) {
		// TODO Auto-generated method stub
		for (V message : chunks) {
			log.info(message.toString());
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy