/** * Actions added are automatically removed when canceled or stopped. * * @param action Action to run in background * @return True */ @Override public synchronized boolean add(final BackgroundAction action) { action.addListener(this); return super.add(action); }
public void stopButtonClicked(final ID sender) { task.cancel(); }
@Override public synchronized void cancel(final BackgroundAction action) { if(action.isRunning()) { log.debug(String.format("Skip removing action %s currently running", action)); } else { this.remove(action); } }
@Override public <T> Future<T> execute(final Controller controller, final BackgroundActionRegistry registry, final BackgroundAction<T> action) { if(log.isDebugEnabled()) { log.debug(String.format("Run action %s in background", action)); } // Add action to registry of controller. Will be removed automatically when stopped registry.add(action); action.init(); // Start background task final Callable<T> command = new BackgroundCallable<T>(action, controller); try { final Future<T> task = concurrentExecutor.execute(command); if(log.isInfoEnabled()) { log.info(String.format("Scheduled background runnable %s for execution", action)); } return task; } catch(RejectedExecutionException e) { log.error(String.format("Error scheduling background task %s for execution. %s", action, e.getMessage())); action.cancel(); action.cleanup(); return ConcurrentUtils.constantFuture(null); } }
public TaskController(final BackgroundAction task) { this.task = task; this.loadBundle(); if(this.task.isRunning()) { progress.startAnimation(null); this.task.addListener(new BackgroundActionListener() { public void start(BackgroundAction action) { invoke(new DefaultMainAction() {
if(action.isCanceled()) { log.debug(String.format("Prepare background action %s", action)); action.prepare(); try { final T result = this.run(); action.finish(); if(log.isDebugEnabled()) { log.debug(String.format("Invoke cleanup for background action %s", action));
@Override public synchronized boolean remove(final Object action) { if(log.isDebugEnabled()) { log.debug(String.format("Remove action %s", action)); } running.remove(action); if(super.remove(action)) { ((BackgroundAction) action).removeListener(this); } return true; }
log.debug(String.format("Call background action %s", action)); return action.call(); if(action.alert(e)) { if(log.isDebugEnabled()) { log.debug(String.format("Retry background action %s", action)); this.failure(client, e); if(action.alert(new DefaultExceptionMappingService().map(e))) { if(log.isDebugEnabled()) { log.debug(String.format("Retry background action %s", action));
public void setText(NSTextField text) { this.text = text; this.text.setStringValue(task.getActivity()); }
@Override public void run() { try { action.cleanup(); } catch(Exception e) { log.error(String.format("Exception running cleanup task %s", e.getMessage()), e); } } });
public void setName(NSTextField name) { this.name = name; this.name.setStringValue(task.getName()); }
public void stop(BackgroundAction action) { invoke(new DefaultMainAction() { @Override public void run() { progress.stopAnimation(null); } }); action.removeListener(this); } });
public void setStatus() { final BackgroundAction current = registry.getCurrent(); this.message(null != current ? current.getActivity() : null); }
@Action public void disconnectButtonClicked(final ID sender) { // Remove all pending actions for(BackgroundAction action : registry.toArray( new BackgroundAction[registry.size()])) { action.cancel(); } this.disconnect(new Runnable() { @Override public void run() { if(preferences.getBoolean("browser.disconnect.bookmarks.show")) { selectBookmarks(BookmarkSwitchSegement.bookmarks); } else { selectBrowser(BrowserSwitchSegement.byPosition(preferences.getInteger("browser.view"))); } } }); }
@Override public void callback(int returncode) { if(returncode == DEFAULT_OPTION) { //Quit final BackgroundActionRegistry registry = shared.getRegistry(); for(BackgroundAction action : registry.toArray(new BackgroundAction[registry.size()])) { action.cancel(); } app.replyToApplicationShouldTerminate(true); } if(returncode == CANCEL_OPTION) { //Cancel app.replyToApplicationShouldTerminate(false); } } });