My View is derived from ViewPart, but I have a listener on it which receives Events from non-GUI threads.
If something is supposed to happen on within the GUI thread, it has to go through asyncExec()
.
So far so good.
The thing is, the GUI elements are created in createPartControl()
, so they can't be final. At the moment I just put them in a AtomicReference
which can be final just fine.
What is your approach?
Update
to clarify the problem using the example from one of the answers below:
public class MyView extends ViewPart implements SomeNetWorkActionListener {
private Text text1;
private final AtomicReference<Text> text3 = new AtomicReference<Text>();
public void createPartControl(Composite parent) {
text1 = new Text(parent, SWT.None);
final Text text2 = new Text(parent, SWT.None);
text3.set(new Text(parent, SWT.None));
parent.getDisplay().asyncExec(new Runnable() {
public void run() {
text1.setText("Hello");
text2.setText("World");
}
});
}
public void setFocus() {
text1.forceFocus();
}
@Override
public void someNetworkMessageReceived(MyMessage message) {
getSite ().getShell ().getDisplay().asyncExec(new Runnable() {
public void run() {
//... how to reference text 1 or text 2?
text3.get().setText(message.toString()); // this is what I do at the moment
}
});
}
}