Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Auto-refreshing ViewPart by using Job(refreshing UI content)
Auto-refreshing ViewPart by using Job [message #820977] Wed, 14 March 2012 16:23 Go to next message
Ronald So is currently offline Ronald So
Messages: 198
Registered: April 2011
Senior Member
Hello,

I have an issue updating a view. It's just a label that I am hoping to present the current time.

public class TestView extends ViewPart {

	private Label label;
	private String jobId;
	private Display display;

	public TestView() {
		System.out.println("TestView.TestView()");
	}

        @Override
	public void createPartControl(Composite parent) {
		this.setContentDescription("This is just a test.");

		GridLayoutFactory.fillDefaults().applyTo(parent);

		label = new Label(parent, SWT.BOLD | SWT.BORDER);
		jobId = UUID.randomUUID().toString();

		System.out.println("Job ID: " + jobId);

		display = parent.getDisplay();

		Job job = new Job(jobId) {
			protected IStatus run(IProgressMonitor monitor) {
				// Assume this getTime() method takes a long time
				final String newString = GregorianCalendar.getInstance()
						.getTime().toString();
				System.out.println("new Time: " + newString);

				display.asyncExec(new Runnable() {
					public void run() {
						// Updating UI has to happen in a thread that is called
						// by display.asyncExec.
						updateLabel(newString);
					}
				});
				return Status.OK_STATUS;
			}
		};
		job.schedule();
	}

	@Override
	public void setFocus() {
		System.out.println("TestView.setFocus()");
		label.setFocus();
	}

	private void updateLabel(String newString) {
		System.out.println("TestView.updateLabel()");
		if (!label.isDisposed()) {
			label.setText(newString);
		}
	}
}


The current time stamp will show up fine initially, but it will never get updated anymore. How do I

1) Update this label with the current time continuously? I know I can use UICallBack but I think the Job should handle this for me automagically.

2) Update this label when user gets to the page (pull model instead of push).


Thank you,
Ronald
Re: Auto-refreshing ViewPart by using Job [message #821193 is a reply to message #820977] Wed, 14 March 2012 23:17 Go to previous messageGo to next message
Ronald So is currently offline Ronald So
Messages: 198
Registered: April 2011
Senior Member
Not sure why the forum creates two threads for my question. Some discussions are going on in a different thread.

http://www.eclipse.org/forums/index.php/t/309839/
Re: Auto-refreshing ViewPart by using Job [message #821676 is a reply to message #820977] Thu, 15 March 2012 13:21 Go to previous messageGo to next message
Elias Volanakis is currently offline Elias Volanakis
Messages: 26
Registered: July 2009
Junior Member
Hi Ronald,

your code looks good. The only thing missing is a loop in your job. Currently you only update the label once Smile.

See attached file.

Best regards,
Elias.


Elias Volanakis | http://eclipsesource.com
elias (AT) eclipsesource.com | @evolanakis
Re: Auto-refreshing ViewPart by using Job [message #821749 is a reply to message #821676] Thu, 15 March 2012 15:29 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten Schlathölter
Messages: 207
Registered: February 2012
Location: Düsseldorf
Senior Member
You can also decide to reschedule the job at the end of the run method. And I think if you use an UIJob you can also get rid of the async exec.

UIJob job = new UIJob(jobId) {
   protected IStatus runInUIThread(IProgressMonitor monitor) {
     // Assume this getTime() method takes a long time
     String newString = GregorianCalendar.getInstance().getTime().toString();
     System.out.println("new Time: " + newString);
     updateLabel(newString);

     schedule(2000);
     return Status.OK_STATUS;
   }
};

job.schedule();
Re: Auto-refreshing ViewPart by using Job [message #822497 is a reply to message #821749] Fri, 16 March 2012 14:17 Go to previous messageGo to next message
Ronald So is currently offline Ronald So
Messages: 198
Registered: April 2011
Senior Member
Thanks guys.

Thorsten, I tried using your code but the label never gets updated unless I make changes to the UI, such as resizing the window, or go to other views and come back to the view with the label. Also, shouldn't all code in UIJob run in the UI Thread? In that case, I really should not be running any long lasting job inside UIJob, right?
Re: Auto-refreshing ViewPart by using Job [message #822501 is a reply to message #821749] Fri, 16 March 2012 14:17 Go to previous messageGo to next message
Ronald So is currently offline Ronald So
Messages: 198
Registered: April 2011
Senior Member
Thanks guys.

Thorsten, I tried using your code but the label never gets updated unless I make changes to the UI, such as resizing the window, or go to other views and come back to the view with the label. Also, shouldn't all code in UIJob run in the UI Thread? In that case, I really should not be running any long lasting job inside UIJob, right?
Re: Auto-refreshing ViewPart by using Job [message #822523 is a reply to message #822501] Fri, 16 March 2012 15:24 Go to previous messageGo to next message
Ronald So is currently offline Ronald So
Messages: 198
Registered: April 2011
Senior Member
Ok I missed the "UICallBack.activate()" call. The view is now refreshing as expected. However, is it true that we should not run any long lasting job inside UIJob since it runs in the UI Thread?
Re: Auto-refreshing ViewPart by using Job [message #822525 is a reply to message #822523] Fri, 16 March 2012 15:40 Go to previous messageGo to next message
Elias Volanakis is currently offline Elias Volanakis
Messages: 26
Registered: July 2009
Junior Member
Hi Ronald,

yes, if you look at the RAP source, you will see that runInUIThread() is called from a:

// simplified for brevity
Display.asyncExec(new Runnable() {
public void run() {
UIJob.this.runInUIThread();
});

Elias.


Elias Volanakis | http://eclipsesource.com
elias (AT) eclipsesource.com | @evolanakis
Re: Auto-refreshing ViewPart by using Job [message #822528 is a reply to message #822501] Fri, 16 March 2012 16:10 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten Schlathölter
Messages: 207
Registered: February 2012
Location: Düsseldorf
Senior Member
One problem is, that your label has zero width before the update. If you initialize the label with a date string, you see the update coming without resizing or so.

I changed your code to:

label = new Label(parent, SWT.BOLD | SWT.BORDER);
label.setText(GregorianCalendar.getInstance()
	.getTime().toString());


To have the width of the label calculated before the updates. Indeed I first also had problems with the update coming only after UI interaction but after I once activated a different perspective and came back, the view updated regularly.

But you are of course right the long lasting job should not run in UIThread anyway. I overlooked the comment in your code.

Re: Auto-refreshing ViewPart by using Job [message #825447 is a reply to message #822528] Tue, 20 March 2012 17:16 Go to previous message
Ronald So is currently offline Ronald So
Messages: 198
Registered: April 2011
Senior Member
Thanks for your help!
Re: Auto-refreshing ViewPart by using Job [message #825452 is a reply to message #822528] Tue, 20 March 2012 17:16 Go to previous message
Ronald So is currently offline Ronald So
Messages: 198
Registered: April 2011
Senior Member
Thanks for your help!
Previous Topic:Is there a tutorial?
Next Topic:How to integrate custom javascript
Goto Forum:
  


Current Time: Sun Apr 20 18:09:33 EDT 2014

Powered by FUDForum. Page generated in 0.06311 seconds