1 33 34 package edu.rice.cs.util.swing; 35 36 import javax.swing.ProgressMonitor ; 37 38 45 public abstract class AsyncTaskLauncher { 46 47 63 protected abstract boolean shouldSetEnabled(); 64 65 74 protected abstract void setParentContainerEnabled(boolean enabled); 75 76 86 protected abstract IAsyncProgress createProgressMonitor(String description, int min, int max); 87 88 105 public <P, R> void executeTask(final AsyncTask<P, R> task, final P param, final boolean showProgress, 106 final boolean lockUI) { 107 Runnable uiInit = new Runnable () { 108 public void run() { 109 final boolean shouldUnlockUI = shouldSetEnabled() && lockUI; 110 final IAsyncProgress monitor = createProgressMonitor(task.getDiscriptionMessage(), 111 task.getMinProgress(), 112 task.getMaxProgress()); 113 if (shouldSetEnabled() && lockUI) { 114 setParentContainerEnabled(false); 115 } 116 117 Thread taskThread = new Thread (new Runnable () { 118 public void run() { 119 R result = null; 120 Exception caughtException = null; 121 try { 122 result = task.runAsync(param, monitor); 123 } catch (Exception e) { 124 caughtException = e; 125 } 126 127 final AsyncCompletionArgs<R> args = new AsyncCompletionArgs<R>(result, caughtException, monitor 128 .isCanceled()); 129 130 Runnable cleanup = new Runnable () { 131 public void run() { 132 task.complete(args); 133 134 if (shouldUnlockUI) { 135 setParentContainerEnabled(true); 136 } 137 } 138 }; 139 140 Utilities.invokeLater(cleanup); 141 } 142 }, "Task Thread - " + task.getName()); 143 144 taskThread.start(); 145 } 146 }; 147 148 Utilities.invokeLater(uiInit); 149 } 150 } 151 | Popular Tags |