Author: adietish
Date: 2011-12-22 07:17:53 -0500 (Thu, 22 Dec 2011)
New Revision: 37521
Added:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobResultFuture.java
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/WizardUtils.java
Log:
[JBIDE-10549] implemented JobResultFuture to be able to wait for the job result while
timeouting
Added:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobResultFuture.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobResultFuture.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobResultFuture.java 2011-12-22
12:17:53 UTC (rev 37521)
@@ -0,0 +1,75 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.common.ui;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.jobs.IJobChangeEvent;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.core.runtime.jobs.JobChangeAdapter;
+
+/**
+ * A Future that allows you to wait with a timeout for the result of a job.
+ *
+ * @author André Dietisheim
+ */
+public class JobResultFuture implements Future<IStatus> {
+
+ private AtomicBoolean cancelled = new AtomicBoolean();
+ private ArrayBlockingQueue<IStatus> queue = new
ArrayBlockingQueue<IStatus>(1);
+
+ public JobResultFuture(Job job) {
+ onJobFinished(job);
+ }
+
+ @Override
+ public boolean cancel(boolean mayInterruptIfRunning) {
+ cancelled.set(true);
+ return true;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled.get();
+ }
+
+ @Override
+ public boolean isDone() {
+ return queue.size() == 1;
+ }
+
+ @Override
+ public IStatus get() throws InterruptedException, ExecutionException {
+ return queue.poll();
+ }
+
+ @Override
+ public IStatus get(long timeout, TimeUnit unit) throws InterruptedException,
ExecutionException,
+ TimeoutException {
+ return queue.poll(timeout, unit);
+ }
+
+ private JobChangeAdapter onJobFinished(final Job job) {
+ return new JobChangeAdapter() {
+
+ @Override
+ public void done(IJobChangeEvent event) {
+ queue.offer(job.getResult());
+ }
+ };
+ }
+}
Property changes on:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobResultFuture.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/WizardUtils.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/WizardUtils.java 2011-12-22
10:59:24 UTC (rev 37520)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/WizardUtils.java 2011-12-22
12:17:53 UTC (rev 37521)
@@ -27,6 +27,10 @@
*/
public class WizardUtils {
+ private WizardUtils() {
+ // inhibit instantiation
+ }
+
/**
* Runs the given job in the given wizard container. In order to have the
* wizard displaying a progress bar, you need to set
@@ -47,13 +51,14 @@
* @see IWizardContainer#run(boolean, boolean, IRunnableWithProgress)
* @see Job
*/
- public static void runInWizard(final Job job, IWizardContainer container) throws
InvocationTargetException,
+ public static void runInWizard(final Job job, IWizardContainer container)
+ throws InvocationTargetException,
InterruptedException {
runInWizard(job, null, container);
}
- public static void runInWizard(final Job job, final DelegatingProgressMonitor
delegatingMonitor, IWizardContainer container) throws InvocationTargetException,
- InterruptedException {
+ public static void runInWizard(final Job job, final DelegatingProgressMonitor
delegatingMonitor,
+ IWizardContainer container) throws InvocationTargetException, InterruptedException {
container.run(true, false, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException,
InterruptedException {
@@ -63,10 +68,10 @@
delegatingMonitor.add(monitor);
delegatingMonitor.beginTask(job.getName(), IProgressMonitor.UNKNOWN);
}
-
+
job.schedule();
job.join();
-
+
if (delegatingMonitor == null) {
monitor.done();
} else {
@@ -75,7 +80,7 @@
}
});
}
-
+
/**
* Runs the given job in the given wizard container.
* <p>
@@ -98,17 +103,16 @@
* @throws InterruptedException
* the interrupted exception
*/
- public static void runInWizard(final Job job, IWizardContainer container, final
DataBindingContext dbc)
+ public static void runInWizard(final Job job, IWizardContainer container,
DataBindingContext dbc)
throws InvocationTargetException, InterruptedException {
runInWizard(job, container);
dbc.updateTargets();
dbc.updateModels();
}
- public static void runInWizard(final Job job, IWizardContainer container,
DelegatingProgressMonitor monitor,
- final DataBindingContext dbc)
- throws InvocationTargetException, InterruptedException {
- runInWizard(job, container);
+ public static void runInWizard(Job job, DelegatingProgressMonitor monitor,
IWizardContainer container,
+ DataBindingContext dbc) throws InvocationTargetException, InterruptedException {
+ runInWizard(job, monitor, container);
dbc.updateTargets();
dbc.updateModels();
}