The Use Case
UISyncronize is the recommended way for accessing the UI in E4 Application to decouple the code from the details of the underlying UI Framework.
But beyond simple use-cases some problems has arise with the previous approach:
- UISyncronize provides no mean to distinguish if one is on the UI thread or not, you either know it or your kind of lost and has to be safe and trigger an ui-exec regardless if its required or not.
- Sometimes one wants to provide some feedback to the user that something (short) happens in the background, without fire up a full progress-monitor dialog or alike.
- Often it is necessary to exchange data with the UI thread, it requires a lot of code to be done right, often people avoid this extra effort and simply do to much in the UI thread.
The Solution
With the upcoming 4.19 release eclipse will contain an enhanced version of the UISyncronize class that makes writing responsive Uis much more easy. Even though it is still not a high level API it provides now the building blocks to create more advanced ones and to perform basic common tasks more easy.
Execute code in the UI thread regardless of current thread
UISynchronize.exec(Runnable) can be called from any thread, the given code is executed either directly (if called form the UI-Thread) or dispatched if called from a non-UI thread.
This is very handy if used in a public method where the class don’t want to impose any restrictions on the thread from where it is called and still need to block until everything is done:
public void updateChart(double[] data) { this.data = data.clone(); uiSynchronize.exec(chart::repaint); }
UISynchronize.call(Callable<T>) can be called from any thread as above with the difference that it includes the opportunity to return a value to the caller. This is particular useful if one needs to access single values from the UI in a background working thread:
UISynchronize uiSynchronize = ...; String location = uiSynchronize.call(textfield::getText);
Execute code in the background, showing busy indicator
The two variants UISynchronize.busyExec(Runnable) and UISynchronize.busyCall(Callable<T>) allow to execute code outside the UI and showing a busy indicator while the code is running. This is very useful if you like to inform the user that something is happen in a lightweight way and keep the UI responsive, a common case are event handlers:
Button button = new Button(parent, SWT.PUSH); button.setText("Compute next prime number"); button.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { button.setEnabled(false); int next = uiSynchronize.busyCall(this::computeNextPrimeNumber); label.setText("The next prime number is: "+next); button.setEnabled(true); } });
This will disable the button and show a progress-wheel until the computation is done.
Current Limitations
- None
Let us know if you think there would be more things to add. If you feel that there is a missing feature (or bug you have discovered) which is crucial to your mission, consider the following:
- Let us know by opening a bug report
- Provide a patch that fixes the problem. It’s all open source and everyone is encouraged to participate!
- You can always contact us and offer funding for a feature or bug fix