Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 23:54 Go to next message
Luiz E. is currently offline Luiz E.Friend
Messages: 100
Registered: September 2010
Senior Member
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 05:13 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.Friend
Messages: 621
Registered: July 2009
Senior Member
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 16:12 Go to previous message
Federico Gaule is currently offline Federico GauleFriend
Messages: 21
Registered: September 2010
Location: Buenos Aires, Argentina
Junior Member
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: Tue Apr 23 11:00:49 GMT 2024

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

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

Back to the top