public static BackgroundActionRegistry global() { synchronized(lock) { if(null == global) { global = new BackgroundActionRegistry(); } return global; } }
@Override public void init() { // Add to the registry so it will be displayed in the activity window. global.add(this); super.init(); }
@Override public void awakeFromNib() { super.awakeFromNib(); // Initialize to listen for background tasks registry.addListener(backgroundActionListener); // Add already running background actions final BackgroundAction[] actions = registry.toArray( new BackgroundAction[registry.size()]); for(final BackgroundAction action : actions) { tasks.put(action, new TaskController(action)); } this.reload(); }
@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 synchronized void stop(final BackgroundAction action) { this.remove(action); }
public boolean isIdle() { if(pool == SessionPool.DISCONNECTED) { return false; } return registry.isEmpty(); }
/** * Will queue up the <code>BackgroundAction</code> to be run in a background thread * * @param action The runnable to execute in a secondary thread */ @Override public <T> Future<T> background(final BackgroundAction<T> action) { if(registry.contains(action)) { log.warn(String.format("Skip duplicate background action %s found in registry", action)); return ConcurrentUtils.constantFuture(null); } return DefaultBackgroundExecutor.get().execute(this, registry, action); }
public void setStatus() { final BackgroundAction current = registry.getCurrent(); this.message(null != current ? current.getActivity() : null); }
@Override public void invalidate() { registry.removeListener(backgroundActionListener); table.setDataSource(null); table.setDelegate(null); super.invalidate(); }
@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); } } });
@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); } }
@Action public void stopAllButtonClicked(final ID sender) { final Collection<Transfer> transfers = transferTableModel.getSource(); for(final Transfer transfer : transfers) { if(transfer.isRunning()) { // Find matching background task for(BackgroundAction action : registry.toArray(new BackgroundAction[registry.size()])) { if(action instanceof TransferBackgroundAction) { final TransferBackgroundAction t = (TransferBackgroundAction) action; if(t.getTransfer().equals(transfer)) { t.cancel(); } } } } } }
@Override public void cleanup() { global.remove(this); super.cleanup(); } }
@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); } }
@Action public void stopButtonClicked(final ID sender) { final NSIndexSet selected = transferTable.selectedRowIndexes(); for(NSUInteger index = selected.firstIndex(); !index.equals(NSIndexSet.NSNotFound); index = selected.indexGreaterThanIndex(index)) { final Transfer transfer = transferTableModel.getSource().get(index.intValue()); if(transfer.isRunning()) { // Find matching background task for(BackgroundAction action : registry.toArray(new BackgroundAction[registry.size()])) { if(action instanceof TransferBackgroundAction) { final TransferBackgroundAction t = (TransferBackgroundAction) action; if(t.getTransfer().equals(transfer)) { t.cancel(); } } } } } }
@Action public void bandwidthPopupChanged(NSMenuItem sender) { final NSIndexSet selected = transferTable.selectedRowIndexes(); final float bandwidth = Float.valueOf(sender.representedObject()); for(NSUInteger index = selected.firstIndex(); !index.equals(NSIndexSet.NSNotFound); index = selected.indexGreaterThanIndex(index)) { final Transfer transfer = collection.get(index.intValue()); transfer.setBandwidth(bandwidth); if(transfer.isRunning()) { // Find matching background task for(BackgroundAction action : registry.toArray(new BackgroundAction[registry.size()])) { if(action instanceof TransferBackgroundAction) { final TransferBackgroundAction t = (TransferBackgroundAction) action; if(t.getTransfer().equals(transfer)) { final TransferSpeedometer meter = t.getMeter(); meter.reset(); } } } } } }