How to set content of SWT Text element from an instance of a class that is called by an Eclipse command?

138 Views Asked by At

I implemented an Eclipse view by extending ViewPart. In this particular view, I have also implemented a text input field Text and a few buttons which execute a particular Eclipse command.

For example:

IHandlerService handlerService = (IHandlerService) PlatformUI
                                .getWorkbench().getService(
                                        IHandlerService.class);
handlerService.executeCommand("com.company.commandxyz", null);

This command calls a handler which then invokes a service to hook into the DSF extensions. To be specific, I have implemented a custom CommandFactory by extending CommandFactory_6_8. This allows me to queue commands to GDB and then get the result back. However, I want to set the response in the Text field which is located located in the View where the com.example.commandxyz was invoked.

Unfortunately, I cannot pass any reference to this implementation of the DSF extension. I tried the following code to get the Eclipse view in order to subsequently set the text (which is a property of this view):

IViewPart vp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("com.example.myview");

However, I receive the following exception upon calling:

Exception in thread "MI RX Thread" java.lang.NullPointerException

Any idea how I can access the UI thread to retrieve the property of the view in order to set it?

1

There are 1 best solutions below

1
On BEST ANSWER

The PlatformUI code you mention must run in the UI thread.

Use Display.asyncExec or Display.syncExec to execute code in the UI thread from some other thread.

With Java 8 use:

Display.getDefault().asyncExec(() -> { ... code to run in UI thread });

With earlier versions of Java use:

Display.getDefault().asyncExec(new Runnable() {
  public void run() {
     ... code to run in UI thread
  }
});

asyncExec runs the UI code asynchronously the next time the UI is ready to run code. syncExec runs synchronously blocking the current thread until the UI runs the code.