Re: Better way to update UI with a job [message #634212] |
Wed, 20 October 2010 19:24 |
Heiko Ahlig Messages: 62 Registered: July 2009 |
Member |
|
|
This is a multi-part message in MIME format.
--------------010304020104080501090709
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Hi,
for updating my stuff after long runing methods, I have written a
combined helper job like yours (attached), where I do at first the
concurent things and afterwards I update my UI with an UIJob, too.
For your case I think it should work with the also attached ExampleJob.
Sorry for bad english too ;)
Greetings
--------------010304020104080501090709
Content-Type: text/plain;
name="CombinedJob.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="CombinedJob.java"
package de.nacorswelt.commons.jobs;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.progress.UIJob;
import de.nacorswelt.commons.internal.Activator;
public abstract class CombinedJob extends Job {
private final Display display;
public CombinedJob(Display display, String name) {
super(name);
this.display = display;
}
@Override
protected final IStatus run(IProgressMonitor monitor) {
IStatus runResult = doRun(monitor);
if (!runResult.isOK())
return runResult;
DisplayJob displayJob = new DisplayJob(display, this.getName());
displayJob.schedule();
return Status.OK_STATUS;
}
protected abstract IStatus doRun(IProgressMonitor monitor);
protected abstract void updateUI() throws Exception;
private final class DisplayJob extends UIJob {
private DisplayJob(Display display, String name) {
super(display, name);
}
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
try {
CombinedJob.this.updateUI();
return Status.OK_STATUS;
} catch (Exception e) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"Update Ui from " + CombinedJob.this.getName()
+ " failed.", e);
}
}
}
}
--------------010304020104080501090709
Content-Type: text/plain;
name="ExampleJob.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="ExampleJob.java"
package de.nacorswelt.commons.jobs;
import java.net.URL;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import de.nacorswelt.commons.internal.Activator;
public class ExampleJob extends CombinedJob {
private Location loc;
private Image img;
private AhmCastView view;
public ExampleJob(Display display, String name, Location loc, AhmCastView view) {
super(display, name);
this.loc = loc;
this.view = view;
}
@Override
protected IStatus doRun(IProgressMonitor monitor) {
try {
monitor.beginTask("Criando URL e baixando mapa", -1);
String center = loc.getLatitude() + "," + loc.getLongitude();
String def = "http://maps.google.com/staticmap?center=" + center
+ "&zoom=12&" + "size=800x800&key=" + DataUtils.GMAP_KEY
+ "&sensor=true";
URL url = new URL(def);
img = ImageDescriptor.createFromURL(url).createImage();
monitor.done();
return Status.OK_STATUS;
} catch (Exception e) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(),e);
}
}
@Override
protected void updateUI() {
view.addMapaLabel(img);
}
}
--------------010304020104080501090709--
|
|
|
Powered by
FUDForum. Page generated in 0.03017 seconds