tags:

views:

41

answers:

3

In one of my class I have the following code:

private static final Color GREEN = new Color(null, 0, 255, 0);

I know the Display class provide the following method:

Display.getSystemColor(int id)

in order to return "default" system colors instead of instantiating new colors. What is the best solution to get a Display instance in the context of static code called in a non Ui thread ?

+1  A: 

Do all the UI informational calls in the main initialization before creating any threads. Save the data for subsequent use in non UI threads.

wallyk
Thanks for the answer, but I am writing a "library" eclipse plugin that doesn't have access to main thread.
Manuel Selva
+1  A: 

You will have to use Display.getDefault().syncExec(...) call. Inside your runnable you can execute something like Display.getDefault().getSystemColor(...). In order to return the value, you will need to keep it in a field in your runnable and then access that field from the invoking method.

Since the executing is with syncExec, make sure not to enter a dead-lock (the UI calls your service which tried to call the UI can cause it).

zvikico
A: 
PlatformUi.getWorkbench().getDisplay()
Manuel Selva
You may be able to get the display, but you cannot issue any UI related calls outside the UI tread, including getSystemColor. If this succeeds, this means you are at the UI thread. If there may be a case where you are not in the UI thread, you should protect yourself as shown in my answer. If might work on your machine and fail on another.
zvikico
Yes you are right. I am using my answer to get a Display and then I perform syncExec on it. +1 for your answer.
Manuel Selva