Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Auto-refreshing ViewPart by using Job
Auto-refreshing ViewPart by using Job [message #820980] Wed, 14 March 2012 20: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 #821044 is a reply to message #820980] Wed, 14 March 2012 21:48 Go to previous messageGo to next message
Chris Fairhall is currently offline Chris Fairhall
Messages: 101
Registered: February 2011
Senior Member
Either I'm missing something or you never reschedule the job to run more than once.
Re: Auto-refreshing ViewPart by using Job [message #821047 is a reply to message #821044] Wed, 14 March 2012 21:53 Go to previous messageGo to next message
Ronald So is currently offline Ronald So
Messages: 198
Registered: April 2011
Senior Member
Smile Yeah Chris. You got that right.

I am now attaching a IPartListener2 and run the job again when partActivated() is invoked. The time label refreshes fine now. However, I noticed that partActivated() does not only get called when user visits the View. It gets called also when user leaves the view. That said, the Job will be invoked when the user leaves the page. Is there a way to get around this problem?

Also, what if I have a view that I want to keep the port open and so the time label will keep updating without user intervention?

Thanks!
Re: Auto-refreshing ViewPart by using Job [message #821197 is a reply to message #820980] Thu, 15 March 2012 03: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 #821680 is a reply to message #820980] Thu, 15 March 2012 17:21 Go to previous messageGo to next message
Elias Volanakis is currently offline Elias Volanakis
Messages: 43
Registered: July 2009
Member
Hi Ronald,

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

See attached file.

Best regards,
Elias.
begin 644 TestView.java
M<&%C:V%G92!H96QL;SL*"FEM<&]R="!J879A+G5T:6PN1W)E9V]R:6%N0V%L
M96YD87(["FEM<&]R="!J879A+G5T:6PN555)1#L*"FEM<&]R="!O<F<N96-L
M:7!S92YC;W)E+G)U;G1I;64N25!R;V=R97-S36]N:71O<CL*:6UP;W)T(&]R
M9RYE8VQI<'-E+F-O<F4N<G5N=&EM92Y)4W1A='5S.PII;7!O<G0@;W)G+F5C
M;&EP<V4N8V]R92YR=6YT:6UE+E-T871U<SL*:6UP;W)T(&]R9RYE8VQI<'-E
M+F-O<F4N<G5N=&EM92YJ;V)S+DIO8CL*:6UP;W)T(&]R9RYE8VQI<'-E+FIF
M86-E+FQA>6]U="Y'<FED3&%Y;W5T1F%C=&]R>3L*:6UP;W)T(&]R9RYE8VQI
M<'-E+G-W="Y35U0["FEM<&]R="!O<F<N96-L:7!S92YS=W0N=VED9V5T<RY#
M;VUP;W-I=&4["FEM<&]R="!O<F<N96-L:7!S92YS=W0N=VED9V5T<RY$:7-P
M;&%Y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elias (AT) eclipsesource.com | @evolanakis
Re: Auto-refreshing ViewPart by using Job [message #821758 is a reply to message #821047] Thu, 15 March 2012 19:42 Go to previous message
Elias Volanakis is currently offline Elias Volanakis
Messages: 26
Registered: July 2009
Junior Member
Hi Ronald,

the part listener gets call for EVERY view, i.e. not only your particular view.

However, you can use the 'part' parameter to figure out if the notification is relevant for you, i.e.:

@Override
public void partActivated(IWorkbenchPart part) {
if (part == this) {
System.out.println("TestView.partActivated() " + part);
// ...
}
}

@Override
public void partDeactivated(IWorkbenchPart part) {
if (part == this) {
System.out.println("TestView.partDeactivated() " + part);
// ...
}
}

Hope this helps,
Elias.


Elias Volanakis | http://eclipsesource.com
elias (AT) eclipsesource.com | @evolanakis
Re: Auto-refreshing ViewPart by using Job [message #821761 is a reply to message #821047] Thu, 15 March 2012 19:42 Go to previous message
Elias Volanakis is currently offline Elias Volanakis
Messages: 43
Registered: July 2009
Member
Hi Ronald,

the part listener gets call for EVERY view, i.e. not only your particular view.

However, you can use the 'part' parameter to figure out if the notification is relevant for you, i.e.:

@Override
public void partActivated(IWorkbenchPart part) {
if (part == this) {
System.out.println("TestView.partActivated() " + part);
// ...
}
}

@Override
public void partDeactivated(IWorkbenchPart part) {
if (part == this) {
System.out.println("TestView.partDeactivated() " + part);
// ...
}
}

Hope this helps,
Elias.

--
Elias Volanakis | http://eclipsesource.com
elias (AT) eclipsesource.com | @evolanakis
Previous Topic:Finding iFrame that corrensponds to TreeItem or TableRow
Next Topic:Row remains highlighted after delete item in TableViewer
Goto Forum:
  


Current Time: Thu Oct 23 03:43:13 GMT 2014

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

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