5

I am trying to pop up a dialog (i.e. a FileDialog) in an Eclipse Plugin, actually before of an Acceleo transformation I am running through the related UI Launcher project (http://lowcoupling.com/post/51877317162/the-acceleo-ui-launcher-project)

I am trying to do this in the related doGenerate method...

public void doGenerate(IProgressMonitor monitor) throws IOException {

    Display display = Display.getCurrent();
    System.out.println(display);
    //....

but the display I get is null How should I do that?

lowcoupling
  • 2,151
  • 6
  • 35
  • 53

4 Answers4

5

The documentation of IWorkbench#getDisplay() states:

Code should always ask the workbench for the display rather than rely on Display.getDefault().

So use:

PlatformUI.getWorkbench().getDisplay()

instead.

Baz
  • 36,440
  • 11
  • 68
  • 94
  • 2
    Note that this can't be used in a 'pure' e4 application where `PlatformUI` can't be used. For `syncExec` and `asyncExec` use `org.eclipse.e4.ui.di.UISynchronize` which can be injected. – greg-449 Oct 05 '13 at 08:48
  • @greg-449 Good to know. Haven't used e4 yet. – Baz Oct 05 '13 at 09:23
2

I'm not sure if you looked at this yet, but in the documentation for the class Display, it says that Display.getCurrent() returns

null if the currently running thread is not a user-interface thread for any display.

This might be the problem, but without more information I can't tell.

Baz
  • 36,440
  • 11
  • 68
  • 94
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
1

You can try getting the default display Display.getDefault() or the workbench display PlatformUI.getWorkbench().getDisplay()

If you are not running in the UI thread, try the following:

Display.getDefault().syncExec(new Runnable() {
    public void run() {
        // ... do work that updates the screen ...
    }
});
cmd
  • 11,622
  • 7
  • 51
  • 61
  • You are aware, that the second method you suggest states that using the first one is not recommended? ;) – Baz Oct 04 '13 at 21:41
1

I was facing the same issue. I used getShell().getDisplay() instead of Display.getCurrent() and it worked for me.

Madhu
  • 53
  • 1
  • 11