Skip to main content



      Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Better way to update UI with a job
Better way to update UI with a job [message #629920] Wed, 29 September 2010 19:54 Go to next message
Eclipse UserFriend
hi all
i have a job (and a uijob) that update my UI. but i was thinking if there's a better wat to do this.
actually, i'm doing like this:
public class CarregarMapaJob extends Job
{

	private Location loc;
	private Image img;
	private AhmCastView view;

	public CarregarMapaJob(String name, Location loc, AhmCastView view) {
	    super(name);
	    this.loc = loc;
	    this.view = view;
    }

	@Override
	protected IStatus run(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();

        } catch (Exception e)
        {
        	e.printStackTrace();
        }
        finally {
        	
	        UIJob uiCarregarMapa = new UIJob("Carregando mapa") {
				
				public IStatus runInUIThread(IProgressMonitor monitor)
				{
					try
                    {
						monitor.beginTask("Adicionando mapa", -1);
						view.addMapaLabel(img);

						monitor.done();
                    } catch (Exception e)
                    {
                    	e.printStackTrace();
                    }
					return Status.OK_STATUS;
				}
			};
			
			uiCarregarMapa.setUser(true);
			uiCarregarMapa.schedule();
        }
        return Status.OK_STATUS;
	}


as you can see, i'm passing the view as a parameter to the constructor, and inside the UIJob, I update her (the ui, of course)
is this a good approach?

sorry for the english.
thanks Very Happy
Re: Better way to update UI with a job [message #629943 is a reply to message #629920] Thu, 30 September 2010 01:13 Go to previous messageGo to next message
Eclipse UserFriend
On 30/09/10 5:24 AM, zorba@live.com wrote:
> hi all
> i have a job (and a uijob) that update my UI. but i was thinking if
> there's a better wat to do this.
> actually, i'm doing like this:

If the view.addMapaLabel(img) method is short you can do it inside
display.syncExec()

--
- Prakash
Platform UI Team, IBM

www.eclipse-tips.com
Re: Better way to update UI with a job [message #634162 is a reply to message #629943] Wed, 20 October 2010 12:12 Go to previous message
Eclipse UserFriend
Prakash G.R.is right. But i will add that its recommended to use Display.asynchExec (cause using synch will force Display to run all requests it has queued to run yours).

in your code would look like

Display.getDefault().asyncExec(new Runnable() {

@Override
public void run() {
view.addMapaLabel(img);
}
})

That will run the call to addMapaLabel in the UI thread.

Regards
Previous Topic:How to save editor
Next Topic:Re: Better way to update UI with a job
Goto Forum:
  


Current Time: Wed Jul 23 01:12:24 EDT 2025

Powered by FUDForum. Page generated in 0.08689 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top