下面列出了怎么用org.openide.util.TaskListener的API类实例代码及写法,或者点击链接到github查看源代码。
public void performAction(Node[] nodes) {
JAXBWizardSchemaNode schemaNode =
nodes[0].getLookup().lookup(JAXBWizardSchemaNode.class);
if (schemaNode != null){
Schema schema = schemaNode.getSchema();
final Project prj = schemaNode.getProject();
ProjectHelper.deleteSchemaFromModel(prj, schema);
ProjectHelper.cleanupLocalSchemaDir(prj, schema);
ProjectHelper.cleanCompileXSDs(prj, false, new TaskListener() {
@Override
public void taskFinished( Task arg0 ) {
ProjectHelper.checkAndDeregisterScript(prj);
}
});
}
}
private synchronized void setResult(final Union2<ExecutorTask,Throwable> result) {
this.result = result;
if (result.hasFirst()) {
result.first().addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task task) {
callBack.get().run();
}
});
if (stopped) {
result.first().stop();
}
} else {
callBack.get().run();
}
this.notifyAll();
}
private static void scheduleRefreshProviders () {
refreshUpdateCenters (null);
final int delay = 500;
final long startTime = System.currentTimeMillis ();
RequestProcessor.Task t = Installer.RP.post (doCheckAvailableUpdates, delay);
t.addTaskListener (new TaskListener () {
@Override
public void taskFinished (Task task) {
task.removeTaskListener (this);
long time = (System.currentTimeMillis () - startTime - delay) / 1000;
if (time > 0) {
Utilities.putTimeOfInitialization (time);
}
}
});
}
@Override
public void setEnabled (boolean enabled) {
if (isEnabled () != enabled) {
if (enabled) {
RequestProcessor.Task t = PluginManagerUI.getRunningTask (new Runnable () {
@Override
public void run() {
setEnabled (true);
}
});
if (t != null && ! t.isFinished ()) {
t.addTaskListener (new TaskListener () {
@Override
public void taskFinished (org.openide.util.Task task) {
setEnabled (true);
}
});
} else {
super.setEnabled (true);
}
} else {
super.setEnabled (false);
}
}
}
private void setUpApplyChecker(final OptionsPanel optsPanel) {
final RequestProcessor.Task applyChecker = RP.post(new Runnable() {
@Override
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (!savingInProgress) {
bAPPLY.setEnabled(optsPanel.isChanged() && optsPanel.dataValid());
}
}
});
}
});
applyChecker.addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task task) {
if (dialog != null) {
applyChecker.schedule(DELAY);
}
}
});
}
/** Creates new form CustomizerFrameworks */
public CustomizerFrameworks(ProjectCustomizer.Category category, WebProjectProperties uiProperties) {
this.category = category;
this.uiProperties = uiProperties;
initComponents();
project = uiProperties.getProject();
jListFrameworks.setModel(new DefaultListModel());
((DefaultListModel) jListFrameworks.getModel()).addElement(NbBundle.getMessage(CustomizerFrameworks.class, "LBL_CustomizerFrameworks_Loading"));
// do not load frameworks again but use list from uiProperties; list is being loaded in background thread:
uiProperties.getLoadingFrameworksTask().addTaskListener(new TaskListener() {
public void taskFinished(Task task) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initFrameworksList(project.getAPIWebModule());
}
});
}
});
if (uiProperties.getLoadingFrameworksTask().isFinished()) {
initFrameworksList(project.getAPIWebModule());
}
}
public static void execute(final DataObject context, final String content, final boolean notify) {
Runnable runnable = new Runnable() {
@Override
public void run() {
cssMinify(context, content, notify);
}
};
final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandleFactory.createHandle("Minifying CSS " + context.getPrimaryFile().getName(), theTask);
theTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
ph.finish();
}
});
ph.start();
theTask.schedule(0);
}
public static void execute(final DataObject context, final String content, final boolean notify) {
Runnable runnable = new Runnable() {
@Override
public void run() {
htmlMinify(context, content, notify);
}
};
final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandleFactory.createHandle("Minifying HTML " + context.getPrimaryFile().getName(), theTask);
theTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
ph.finish();
}
});
ph.start();
theTask.schedule(0);
}
public static void execute(final DataObject context, final String content, final boolean notify) {
Runnable runnable = new Runnable() {
@Override
public void run() {
jsonMinify(context, content, notify);
}
};
final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandleFactory.createHandle("Minifying JSON " + context.getPrimaryFile().getName(), theTask);
theTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
ph.finish();
}
});
ph.start();
theTask.schedule(0);
}
public static void execute(final DataObject context, final String content, final boolean notify) {
Runnable runnable = new Runnable() {
@Override
public void run() {
xmlMinify(context, content, notify);
}
};
final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandleFactory.createHandle("Minifying XML " + context.getPrimaryFile().getName(), theTask);
theTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
ph.finish();
}
});
ph.start();
theTask.schedule(0);
}
public static void execute(final DataObject context, final String content, final boolean notify) {
Runnable runnable = new Runnable() {
@Override
public void run() {
jsMinify(context, content, notify);
}
};
final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandle.createHandle("Minifying JS " + context.getPrimaryFile().getName(), theTask);
theTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
ph.finish();
}
});
ph.start();
theTask.schedule(0);
}
@Override
public void actionPerformed(ActionEvent ev) {
Runnable runnable = new Runnable() {
@Override
public void run() {
minify();
}
};
final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandleFactory.createHandle("Minifying JS , CSS and other WEB Content ", theTask);
theTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
ph.finish();
}
});
ph.start();
theTask.schedule(0);
}
@Override
public void actionPerformed(ActionEvent ev) {
Runnable runnable = new Runnable() {
@Override
public void run() {
encode();
}
};
final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandleFactory.createHandle("Base64 Encoding Image " + context.getPrimaryFile().getName(), theTask);
theTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
NotificationDisplayer.getDefault().notify("Image encoded successfully", NotificationDisplayer.Priority.NORMAL.getIcon(), "The encoding of the image was successful.", null);
ph.finish();
}
});
ph.start();
theTask.schedule(0);
}
@Override
public void actionPerformed(ActionEvent ev) {
Runnable runnable = new Runnable() {
@Override
public void run() {
decode();
}
};
final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandleFactory.createHandle("Base64 Decoding Image " + context.getPrimaryFile().getName(), theTask);
theTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
ph.finish();
}
});
ph.start();
theTask.schedule(0);
}
@Override
public void actionPerformed(ActionEvent ev) {
Runnable runnable = new Runnable() {
@Override
public void run() {
compress();
}
};
final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandleFactory.createHandle("Compressing Image " + context.getPrimaryFile().getName(), theTask);
theTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
NotificationDisplayer.getDefault().notify("Image compressed successfully", NotificationDisplayer.Priority.NORMAL.getIcon(), "The compression of the image was successful.", null);
ph.finish();
}
});
ph.start();
theTask.schedule(0);
}
@Override
public void actionPerformed(ActionEvent ev) {
final Dialog d = BusyDialog.getBusyDialog();
RequestProcessor.Task T = RequestProcessor.getDefault().post(new checkCleanRunnable());
T.addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task task) {
// d.setVisible(false);
if(esito) {
StatusBar.setStatus(Bundle.MSG_STATUS_Clean(), 10,5000);
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
Bundle.MSG_CheckCleanInstanceClean(),
NotifyDescriptor.INFORMATION_MESSAGE));
}else{
StatusBar.setStatus(Bundle.MSG_STATUS_Violated(), 10, 5000);
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
message.toString(),
NotifyDescriptor.WARNING_MESSAGE));
}
}
});
// d.setVisible(true);
}
@Override
public void save() throws IOException {
dto = CentralLookup.getDefLookup().lookup(EGTaskDataObjectDataObject.class);
final InputOutput io = IOProvider.getDefault().getIO(dto.getPrimaryFile().getName(), false);
io.select();
OutputWindow.openOutputWindowStream(io.getOut(), io.getErr());
final Dialog d = BusyDialog.getBusyDialog();
RequestProcessor.Task T = RequestProcessor.getDefault().post(new SaveEgtaskRunnable());
T.addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task task) {
// d.setVisible(false);
if(esito) {
System.out.println(Bundle.MSG_SaveEGTask_OK(dto.getPrimaryFile().getName()));
}else{
System.err.println(Bundle.MSG_SaveEGTask_Failed(dto.getPrimaryFile().getName()));
}
OutputWindow.closeOutputWindowStream(io.getOut(), io.getErr());
// d.setVisible(false);
}
});
// d.setVisible(true);
}
public ExecutorTask execute(RunConfig config, InputOutput io, TabContext tc) {
LifecycleManager.getDefault().saveAll();
MavenExecutor exec = new MavenCommandLineExecutor(config, io, tc);
ExecutorTask task = ExecutionEngine.getDefault().execute(config.getTaskDisplayName(), exec, new ProxyNonSelectableInputOutput(exec.getInputOutput()));
exec.setTask(task);
task.addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task t) {
MavenProject mp = config.getMavenProject();
if (mp == null) {
return;
}
final List<Artifact> arts = new ArrayList<Artifact>();
Artifact main = mp.getArtifact();
if (main != null) {
arts.add(main);
}
arts.addAll(mp.getArtifacts());
UPDATE_INDEX_RP.post(new Runnable() {
@Override
public void run() {
RepositoryIndexer.updateIndexWithArtifacts(RepositoryPreferences.getInstance().getLocalRepository(), arts);
}
});
}
});
return task;
}
/** This method is called by one of the "invokers" as a result of
* some user's action that should lead to actual "performing" of the action.
* This default implementation calls the assigned actionPerformer if it
* is not null otherwise the action is ignored.
*/
public void performAction ( final Node[] nodes ) {
if (nodes.length != 1)
return;
// Open the diaog for bean info generation
final BiPanel biPanel = new BiPanel();
// Get pattern analyser & bean info and create BiAnalyser & BiNode
FileObject javaFile = findFileObject(nodes[0]);
final BeanInfoWorker performer = new BeanInfoWorker(javaFile, biPanel);
class Task implements TaskListener, Runnable {
public void taskFinished(org.openide.util.Task task) {
EventQueue.invokeLater(this);
}
public void run() {
if (performer.error != null) {
DialogDisplayer.getDefault().notify(performer.error);
}
if (performer.bia != null) {
performer.bia.openSource();
}
}
}
performer.analyzePatterns().addTaskListener(new Task());
}
private void initialize() {
if (!isInitialized) {
setLayout(new BorderLayout());
biPanel = new BiPanel();
add(biPanel, BorderLayout.CENTER);
isInitialized = true;
} else {
biPanel.setContext(new BiNode.Wait());
}
FileObject biFile = dataObject.getPrimaryFile();
String name = biFile.getName();
name = name.substring(0, name.length() - "BeanInfo".length()); // NOI18N
FileObject javaFile = biFile.getParent().getFileObject(name, biFile.getExt());
BIEditorSupport editor = findEditor(dataObject);
if (javaFile != null) {
final BeanInfoWorker beanInfoWorker = new GenerateBeanInfoAction.BeanInfoWorker(javaFile, biPanel);
editor.worker = beanInfoWorker;
beanInfoWorker.analyzePatterns().addTaskListener(new TaskListener() {
public void taskFinished(Task task) {
beanInfoWorker.updateUI();
}
});
} else {
// notify missing source file
biPanel.setContext(BiNode.createNoSourceNode(biFile));
}
}
void setExecutor(ExecutorTask execTask) {
this.execTask = execTask;
if (execTask != null) {
execTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
// The ANT task was finished
finish();
}
});
}
}
public void runTarget(FileObject scriptFile, String[] targetNameArray, Properties props, final ActionProgress listener) {
try {
ActionUtils.runTarget(scriptFile, targetNameArray, props).addTaskListener(new TaskListener() {
@Override public void taskFinished(Task task) {
listener.finished(((ExecutorTask) task).result() == 0);
}
});
} catch (IOException e) {
ErrorManager.getDefault().notify(e);
listener.finished(false);
}
}
/**
*/
private TaskListener getTaskListener() {
if (taskListener == null) {
taskListener = new MyTaskListener();
}
return taskListener;
}
public HoldInstance (DataObject source, InstanceCookie cookie) {
this.cookie = cookie;
this.source = source;
if (cookie instanceof Task) {
// for example FolderInstance ;-) attach itself for changes
// in the cookie
Task t = (Task)cookie;
t.addTaskListener(WeakListeners.create(TaskListener.class, this, t));
}
}
private UnitTable createTabForModel(final UnitCategoryTableModel model) {
final UnitTable table = new UnitTable(model);
selectFirstRow(table);
final UnitTab tab = new UnitTab(table, new UnitDetails(), this);
if (initTask != null) {
tab.setWaitingState(! initTask.isFinished());
initTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task task) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
tab.setWaitingState(false);
}
});
}
});
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
tpTabs.add(tab, model.getTabIndex());
decorateTabTitle(table);
}
});
return table;
}
/** Make sure it is safe to call waitFinished() on a task from within
* a task listener.
*/
public void testWaitFinishedFromNotification() throws Exception {
class X implements Runnable {
private Task task;
private int cnt;
public synchronized Task start() {
if (task == null) {
task = RequestProcessor.postRequest(this);
}
return task;
}
public void run() {
cnt++;
}
public int getCount() {
return cnt;
}
public void block() {
start().waitFinished();
}
}
final X x = new X();
final Object lock = "wait for task to finish";
final boolean[] finished = new boolean[1];
x.start().addTaskListener(new TaskListener() {
public void taskFinished(Task t) {
x.block();
finished[0] = true;
synchronized (lock) {
lock.notify();
}
}
});
synchronized (lock) {
lock.wait(5000);
}
assertTrue(finished[0]);
assertEquals(1, x.getCount());
}
/** Make sure that successfully canceled task is not performed.
*/
public void testCancel() throws Exception {
class X implements Runnable {
public boolean performed = false;
public void run() {
performed = true;
}
}
X x = new X();
final boolean[] finished = new boolean[1];
finished[0] = false;
// post task with some delay
RequestProcessor.Task task = RequestProcessor.postRequest(x, 1000);
task.addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task t) {
finished[0] = true;
}
});
boolean canceled = task.cancel();
assertTrue("Task is canceled now", canceled);
assertTrue("Cancelling actually means finished", finished[0]);
Thread.sleep(1500); // wait longer than task delay
assertFalse("Task should not be performed", x.performed);
}
private TaskListener onActivationFinished() {
return (task) -> {
if (!progressMonitor.error) {
SwingUtilities.invokeLater(new Runnable() {
private String msg;
public void run() {
ConfigurationPanel.this.removeAll();
ConfigurationPanel.this.setLayout(new BorderLayout());
try {
ConfigurationPanel.this.add(callable.call(), BorderLayout.CENTER);
} catch (Exception ex) {
Exceptions.attachSeverity(ex, Level.INFO);
Exceptions.printStackTrace(ex);
}
ConfigurationPanel.this.invalidate();
ConfigurationPanel.this.revalidate();
ConfigurationPanel.this.repaint();
if (featureInfo != null && !featureInfo.isEnabled()) {
if (featureInfo.isPresent()) {
msg = NbBundle.getMessage(ConfigurationPanel.class, "MSG_EnableFailed");
} else {
msg = NbBundle.getMessage(ConfigurationPanel.class, "MSG_DownloadFailed");
}
progressMonitor.onError(msg);
return;
}
activateButton.setEnabled(true);
progressPanel.removeAll();
progressPanel.revalidate();
progressPanel.repaint();
}
});
}
};
}
/** Associates a listener with currently running computation.
* One the results for {@link #getModulesForEnable()} and
* {@link #getModulesForInstall()} is available, the listener
* will be called back.
*
* @param l the listener which receives <code>this</code> as a task
* once the result is available
*/
public void onFinished(final TaskListener l) {
findingTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(Task task) {
l.taskFinished(FindComponentModules.this);
}
});
}
@Override
protected void performAction(final Node[] activatedNodes) {
final Collection<? extends TestMethodDebuggerProvider> providers = Lookup.getDefault().lookupAll(TestMethodDebuggerProvider.class);
RequestProcessor RP = new RequestProcessor("TestMethodDebuggerAction", 1, true); // NOI18N
debugMethodTask = RP.create(new Runnable() {
@Override
public void run() {
for (TestMethodDebuggerProvider provider : providers) {
if (provider.canHandle(activatedNodes[0])) {
debugMethodProvider = provider;
break;
}
}
}
});
final ProgressHandle ph = ProgressHandleFactory.createHandle(Bundle.Search_For_Provider(), debugMethodTask);
debugMethodTask.addTaskListener(new TaskListener() {
@Override
public void taskFinished(org.openide.util.Task task) {
ph.finish();
if (debugMethodProvider == null) {
StatusDisplayer.getDefault().setStatusText(Bundle.No_Provider_Found());
} else {
debugMethodProvider.debugTestMethod(activatedNodes[0]);
}
}
});
ph.start();
debugMethodTask.schedule(0);
}