/** * Construct a {@code Task} with an empty (<code>""</code>) resource name * prefix, whose ResourceMap is the value of * <code>ApplicationContext.getInstance().getResourceMap(this.getClass(), * Task.class)</code>. * @param application */ public Task(Application application) { this.application = application; initTask(defaultResourceMap(application), ""); }
private void fireCompletionListeners() { try { if (isCancelled()) { fireCancelledListeners(); } else { try { fireSucceededListeners(get()); } catch (InterruptedException e) { fireInterruptedListeners(e); } catch (ExecutionException e) { fireFailedListeners(e.getCause()); } } } finally { fireFinishedListeners(); } }
/** * {@inheritDoc} * <p> * This method fires the TaskListeners' {@link TaskListener#process process} * method. If you override {@code process} and do not call * {@code super.process(values)}, then the TaskListeners will not run. * * @param values @{inheritDoc} */ @Override protected void process(List<V> values) { fireProcessListeners(values); }
public void execute(Task task) { if (task == null) { throw new IllegalArgumentException("null task"); } if (!task.isPending() || (task.getTaskService() != null)) { throw new IllegalArgumentException("task has already been executed"); } task.setTaskService(this); // TBD: what if task has already been submitted? List<Task> oldTaskList, newTaskList; synchronized(tasks) { oldTaskList = copyTasksList(); tasks.add(task); newTaskList = copyTasksList(); task.addPropertyChangeListener(taskPCL); } firePropertyChange("tasks", oldTaskList, newTaskList); maybeBlockTask(task); executorService.execute(task); }
/** * Set when a task is executed by a TaskService, cleared when * the task is done and all of its completion methods have run. */ synchronized void setTaskService(TaskService taskService) { TaskService oldTaskService, newTaskService; synchronized(this) { oldTaskService = this.taskService; this.taskService = taskService; newTaskService = this.taskService; } firePropertyChange("taskService", oldTaskService, newTaskService); }
public void windowClosing(WindowEvent e) { if (getTask().getUserCanCancel()) { getTask().cancel(true); dialog.setVisible(false); } } };
private void initTask(ResourceMap resourceMap, String prefix) { this.resourceMap = resourceMap; if ((prefix == null) || (prefix.length() == 0)) { resourcePrefix = ""; } else if (prefix.endsWith(".")) { resourcePrefix = prefix; } else { resourcePrefix = prefix + "."; } if (resourceMap != null) { title = resourceMap.getString(resourceName(PROP_TITLE)); description = resourceMap.getString(resourceName(PROP_DESCRIPTION)); message = resourceMap.getString(resourceName(PROP_MESSAGE)); if (message != null) { messageTime = System.currentTimeMillis(); } } addPropertyChangeListener(new StatePCL()); taskListeners = new CopyOnWriteArrayList<TaskListener<T, V>>(); }
/** * Set this task's InputBlocker. The InputBlocker defines * to what extent the GUI should be blocked while the Task * is executed by a TaskService. It is not used by the Task * directly, it's used by the TaskService that executes the Task. * <p> * This property may only be set before the Task is * {@link TaskService#execute submitted} to a TaskService for * execution. If it's called afterwards, an IllegalStateException * is thrown. * <p> * This is a bound property. * * @see #getInputBlocker */ public final void setInputBlocker(InputBlocker inputBlocker) { if (getTaskService() != null) { throw new IllegalStateException("task already being executed"); } InputBlocker oldInputBlocker, newInputBlocker; synchronized(this) { oldInputBlocker = this.inputBlocker; this.inputBlocker = inputBlocker; newInputBlocker = this.inputBlocker; } firePropertyChange("inputBlocker", oldInputBlocker, newInputBlocker); }
/** * The TaskMonitor's PropertyChangeListeners are fired each time * any property of the the {@code 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 {@code * autoUpdateForegroundTask} is false, then the foregroundTask * property is not reset automatically. * * @param foregroundTask the task whose properties are reflected by this class * @see #setAutoUpdateForegroundTask * @see #getForegroundTask */ public void setForegroundTask(Task foregroundTask) { final Task oldTask = this.foregroundTask; if (oldTask != null) { oldTask.removePropertyChangeListener(taskPCL); } this.foregroundTask = foregroundTask; Task newTask = this.foregroundTask; if (newTask != null) { newTask.addPropertyChangeListener(taskPCL); } firePropertyChange(PROP_FOREGROUND_TASK, oldTask, newTask); }
@Override public void actionPerformed(ActionEvent ignore) { getTask().cancel(true); } };
getTask().addPropertyChangeListener(taskPCL); panel.add(progressBar, BorderLayout.SOUTH); injectBlockingDialogComponents(panel);
/** * Executes the task. * * @param task the task to be executed */ public void execute(Task task) { if (task == null) { throw new IllegalArgumentException("null task"); } if (!task.isPending() || (task.getTaskService() != null)) { throw new IllegalArgumentException("task has already been executed"); } task.setTaskService(this); // TBD: what if task has already been submitted? List<Task> oldTaskList, newTaskList; synchronized (tasks) { oldTaskList = copyTasksList(); tasks.add(task); newTaskList = copyTasksList(); task.addPropertyChangeListener(taskPCL); } firePropertyChange("tasks", oldTaskList, newTaskList); maybeBlockTask(task); executorService.execute(task); }
/** * Set when a task is executed by a TaskService, cleared when * the task is done and all of its completion methods have run. */ synchronized void setTaskService(TaskService taskService) { TaskService oldTaskService, newTaskService; synchronized (this) { oldTaskService = this.taskService; this.taskService = taskService; newTaskService = this.taskService; } firePropertyChange(PROP_TASKSERVICE, oldTaskService, newTaskService); }
@Override public void windowClosing(WindowEvent e) { if (getTask().getUserCanCancel()) { getTask().cancel(true); dialog.setVisible(false); } } };
private void initTask(ResourceMap resourceMap, String prefix) { this.resourceMap = resourceMap; if ((prefix == null) || (prefix.length() == 0)) { resourcePrefix = ""; } else if (prefix.endsWith(".")) { resourcePrefix = prefix; } else { resourcePrefix = prefix + "."; } if (resourceMap != null) { title = resourceMap.getString(resourceName("title")); description = resourceMap.getString(resourceName("description")); message = resourceMap.getString(resourceName("message")); if (message != null) { messageTime = System.currentTimeMillis(); } } addPropertyChangeListener(new StatePCL()); taskListeners = new CopyOnWriteArrayList<TaskListener<T, V>>(); }
/** * Set this task's InputBlocker. The InputBlocker defines * to what extent the GUI should be blocked while the Task * is executed by a TaskService. It is not used by the Task * directly, it's used by the TaskService that executes the Task. * <p> * This property may only be set before the Task is * {@link TaskService#execute submitted} to a TaskService for * execution. If it's called afterwards, an IllegalStateException * is thrown. * <p> * This is a bound property. * * @param inputBlocker * @see #getInputBlocker */ public final void setInputBlocker(InputBlocker inputBlocker) { if (getTaskService() != null) { throw new IllegalStateException("task already being executed"); } InputBlocker oldInputBlocker, newInputBlocker; synchronized (this) { oldInputBlocker = this.inputBlocker; this.inputBlocker = inputBlocker; newInputBlocker = this.inputBlocker; } firePropertyChange(PROP_INPUTBLOCKER, oldInputBlocker, newInputBlocker); }
/** * The TaskMonitor's PropertyChangeListeners are fired each time * any property of the the {@code 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 {@code * autoUpdateForegroundTask} is false, then the foregroundTask * property is not reset automatically. * * @param foregroundTask the task whose properties are reflected by this class * @see #setAutoUpdateForegroundTask * @see #getForegroundTask */ public void setForegroundTask(Task foregroundTask) { final Task oldTask = this.foregroundTask; if (oldTask != null) { oldTask.removePropertyChangeListener(taskPCL); } this.foregroundTask = foregroundTask; Task newTask = this.foregroundTask; if (newTask != null) { newTask.addPropertyChangeListener(taskPCL); } firePropertyChange("foregroundTask", oldTask, newTask); }
public void actionPerformed(ActionEvent ignore) { getTask().cancel(true); } };
getTask().addPropertyChangeListener(taskPCL); panel.add(progressBar, BorderLayout.SOUTH); injectBlockingDialogComponents(panel);
private void fireCompletionListeners() { try { if (isCancelled()) { fireCancelledListeners(); } else { try { fireSucceededListeners(get()); } catch (InterruptedException e) { fireInterruptedListeners(e); } catch (ExecutionException e) { fireFailedListeners(e.getCause()); } } } finally { fireFinishedListeners(); } }