Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package org.jdesktop.application;
  
  import java.util.Iterator;
 import java.util.List;
This class is intended to serve as the model for GUI components, like status bars, that display the state of an application's background tasks. TaskMonitor provides an overview of all the ApplicationContext's Tasks, as well as the state of a single foreground Task.

The value of getTasks() is a list of all of the Tasks whose state is not org.jdesktop.swingworker.SwingWorker.isDone() for all of the ApplicationContext's TaskServices. In other words: all of the ApplicationContext's background tasks that haven't finished executing. Each time a new TaskService Task is executed it's added to the list; when the Task finishes it's removed. Each time the list changes PropertyChangeListeners are fired. Applications that wish to create a detailed visualization of all Tasks should monitor the TaskMonitor "tasks" property.

Users are often only interested in the status of a single foreground task, typically the one associated with GUI element they're working with, or with the most recent command they've issued. The TaskMonitor's PropertyChangeListener is notified each time a property of the foregroundTask changes. Additionally the TaskMonitor fires synthetic PropertyChangeEvents for properties named "pending", "started", and "done" when the corresponding Task state property changes occur.

TaskMonitor manages a queue of new Tasks. The foregroundTask is automatically set to the first new Task, and when that Task finishes, the next Task in the queue, and so on. Applications can set the foregroundTask explicitly, to better reflect what the user is doing. For example, a tabbed browsing GUI that launched one Task per tab might set the foreground Task each time the user selected a tab. To prevent the foregroundTask property from (ever) being reset automatically, one must set setAutoUpdateForegroundTask(boolean) to false.

This class is not thread-safe. All of its methods must be called on the event dispatching thread (EDT) and all of its listeners will run on the EDT.

 
 
 public class TaskMonitor extends AbstractBean {
     private final PropertyChangeListener applicationPCL;
     private final PropertyChangeListener taskServicePCL;
     private final PropertyChangeListener taskPCL;
     private final LinkedList<TasktaskQueue;
     private boolean autoUpdateForegroundTask = true;
     private Task foregroundTask = null;

    
Construct a TaskMonitor.
 
     public TaskMonitor(ApplicationContext context) {
 	 = new TaskPCL();
 	 = new LinkedList<Task>();
 	for(TaskService taskService : context.getTaskServices()) {
 	}
     }

    
The TaskMonitor's PropertyChangeListeners are fired each time any property of the the foregroundTask changes. By default this property is set to the first Task to be executed and then, when that Task finishes, reset to the next most recently executed Task. If the autoUpdateForegroundTask is false, then the foregroundTask property is not reset automatically.

Parameters:
foregroundTask the task whose properties are reflected by this class
See also:
setAutoUpdateForegroundTask(boolean)
getForegroundTask()
    public void setForegroundTask(Task foregroundTask) {
	final Task oldTask = this.;
	if (oldTask != null) {
	}
	this. = foregroundTask;
	Task newTask = this.;
	if (newTask != null) {
	}
	firePropertyChange("foregroundTask"oldTasknewTask);
    }

    
Indicates the Task whose status the ApplicationContext's GUI wants to be displayed, typically in the main window's status bar.

Returns:
the value of the foregroundTask property.
See also:
setForegroundTask(org.jdesktop.application.Task)
    public Task getForegroundTask() {
    }


    
True if the foregroundTask property should be automatically reset to the oldest Task in the queue when it finishes running.

This property is true by default.

Returns:
true if the foregroundTask should be set automatically.
See also:
setAutoUpdateForegroundTask(boolean)
setForegroundTask(org.jdesktop.application.Task)
    public boolean getAutoUpdateForegroundTask() { 
    }

    
True if the foregroundTask property should be automatically reset to the oldest Task in the queue when it finishes running. An application that wants explicit control over the Task being monitored can set this property to false.

This property is true by default.

Parameters:
autoUpdateForegroundTask true if the foregroundTask should be set automatically
See also:
getAutoUpdateForegroundTask()
    public void setAutoUpdateForegroundTask(boolean autoUpdateForegroundTask) {
	boolean oldValue = this.;
	this. = autoUpdateForegroundTask;
	firePropertyChange("autoUpdateForegroundTask"oldValuethis.);
    }
    private List<TaskcopyTaskQueue() {
	synchronized() {
	    if (.isEmpty()) {
		return Collections.emptyList();
	    }
	    else {
		return new ArrayList<Task>();
	    }
	}
    }

    
All of the Application Tasks whose state is not DONE.

Each time the list of Tasks changes, a PropertyChangeEvent for the property named "tasks" is fired. Applications that want to monitor all background Tasks should monitor the tasks property.

Returns:
a list of all Tasks that aren't DONE
    public List<TaskgetTasks() {
	return copyTaskQueue();
    }
    /* Called on the EDT, each time a TaskService's list of tasks changes,
     * i.e. each time a new Task is executed and each time a Task's
     * state changes to DONE.
     */
    private void updateTasks(List<TaskoldTasksList<TasknewTasks) {
	boolean tasksChanged = false;  // has the "tasks" property changed?
	List<TaskoldTaskQueue = copyTaskQueue();
	// Remove each oldTask that's not in the newTasks list from taskQueue
	for(Task oldTask : oldTasks) {
	    if (!(newTasks.contains(oldTask))) {
		if (.remove(oldTask)) {
		    tasksChanged = true;
		}
	    }
	}
	// Add each newTask that's not in the oldTasks list to the taskQueue
	for(Task newTask : newTasks) {
	    if (!(.contains(newTask))) {
		tasksChanged = true;
	    }
	}
	// Remove any tasks that are DONE for the sake of tasksChanged
	while(tasks.hasNext()) {
	    Task task = tasks.next();
	    if (task.isDone()) {
		tasks.remove();
		tasksChanged = true;
	    }
	}
	// Maybe fire the "tasks" PCLs
	if (tasksChanged) {
	    List<TasknewTaskQueue = copyTaskQueue();
	    firePropertyChange("tasks"oldTaskQueuenewTaskQueue);
	}
	if ( && (getForegroundTask() == null)) {
	}
    }
    /* Each time an ApplicationContext TaskService is added or removed, we 
     * remove our taskServicePCL from the old ones, add it to the new
     * ones.  In a typical application, this will happen infrequently
     * and the number of TaskServices will be small, often just one.
     * This listener runs on the EDT.
     */
    private class ApplicationPCL implements PropertyChangeListener {
	    String propertyName = e.getPropertyName();
	    if ("taskServices".equals(propertyName)) {
		for(TaskService oldTaskService : oldList) {
		}
		for(TaskService newTaskService : newList) {
		}
	    }
	}
    }
    /* Each time a TaskService's list of Tasks (the "tasks" property) changes,
     * update the taskQueue (the "tasks" property) and possibly the 
     * foregroundTask property.  See updateTasks().
     * This listener runs on the EDT.
     */
    private class TaskServicePCL implements PropertyChangeListener {
	    String propertyName = e.getPropertyName();
	    if ("tasks".equals(propertyName)) {
		List<TaskoldList = (List<Task>)e.getOldValue();
		List<TasknewList = (List<Task>)e.getNewValue();
		updateTasks(oldListnewList);
	    }
	}
    }
    /* Each time a property of the foregroundTask that's also a 
     * TaskMonitor property changes, update the TaskMonitor's state
     * and fire a TaskMonitor ProprtyChangeEvent.  
     * This listener runs on the EDT.
     */
    private class TaskPCL implements PropertyChangeListener {
	private void fireStateChange(Task taskString propertyName) {
	    firePropertyChange(new PropertyChangeEvent(taskpropertyNamefalsetrue));
	}
	    String propertyName = e.getPropertyName();
	    Task task = (Task)(e.getSource());
	    Object newValue = e.getNewValue();
	    if ((task != null) && (task == getForegroundTask())) {
		if ("state".equals(propertyName)) {
		    StateValue newState = (StateValue)(e.getNewValue());
		    switch(newState) {
		    case fireStateChange(task"pending"); break;
		    case fireStateChange(task"started"); break;
		    case 
			fireStateChange(task"done");
		    }
		}
	    }
	}
    }
New to GrepCode? Check out our FAQ X