Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » UI methods in normal Job
UI methods in normal Job [message #35060] Mon, 01 December 2008 13:02 Go to next message
Matthias Kohles is currently offline Matthias KohlesFriend
Messages: 71
Registered: July 2009
Member
Hey,

i have a simple Eclipse Job which will start a search. Before i can do
this, i need some information about the opened resource in a editor. So i
would call PlatformUI.get... to get this information. To use this method i
have to run it from a UI Job. I could now simply start a UIJob out of my
normal Job to retrieve the information but there has to be a better way or
not?

Can i use the method somehow in my normal Eclipse job?

regards

Matthias
Re: UI methods in normal Job [message #35338 is a reply to message #35060] Tue, 02 December 2008 22:49 Go to previous messageGo to next message
David Virdefors is currently offline David VirdeforsFriend
Messages: 9
Registered: July 2009
Junior Member
Hi Matthias,

You do not need to create a UIJob just to run code in the UI thread if that
is what you mean, e.g. get information about an editor.

You should be able to do something like this:

org.eclipse.swt.widgets.Display.getCurrent().syncExec(new Runnable() {
@Override
public void run() {
System.out.println("I'm running from the GUI thread, wee!");
}
});

I hope this helps and that I didn't misunderstand your question.
Best of luck!

David Virdefors

Purple Scout / www.purplescout.com

"Matthias Kohles" <m.kohles@gmx.de> wrote in message
news:3fc08b78e0e93f71706e51fa4cc83d16$1@www.eclipse.org...
> Hey,
>
> i have a simple Eclipse Job which will start a search. Before i can do
> this, i need some information about the opened resource in a editor. So i
> would call PlatformUI.get... to get this information. To use this method i
> have to run it from a UI Job. I could now simply start a UIJob out of my
> normal Job to retrieve the information but there has to be a better way or
> not?
>
> Can i use the method somehow in my normal Eclipse job?
>
> regards
>
> Matthias
>
Re: UI methods in normal Job [message #35615 is a reply to message #35338] Thu, 04 December 2008 06:40 Go to previous messageGo to next message
Matthias Kohles is currently offline Matthias KohlesFriend
Messages: 71
Registered: July 2009
Member
Hey,

thank you for your answer.
I tried your code but it did not work for me.

i have a normal NonUI eclipe Job. In this job i need the information of
what file from which project is opened in the active editor. So i tried
your code within this job but i receive a NullPointerException in
Display.getCurrent().

Display.getCurrent().syncExec(new Runnable()
{
public void run()
{
editorPart =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
}
});

JavaDoc says that when i call Display.getCurrent() in a non-UI thread it
will return always null.

is there another way to use this syncExec within my job?

best regards,

Matthias
Re: UI methods in normal Job [message #35683 is a reply to message #35615] Thu, 04 December 2008 11:40 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Marcus.Ilgner.gerig.de

Matthias Kohles wrote:

> Hey,

> thank you for your answer.
> I tried your code but it did not work for me.

> i have a normal NonUI eclipe Job. In this job i need the information of
> what file from which project is opened in the active editor. So i tried
> your code within this job but i receive a NullPointerException in
> Display.getCurrent().

> Display.getCurrent().syncExec(new Runnable()
> {
> public void run()
> {
> editorPart =
>
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
> }
> });

> JavaDoc says that when i call Display.getCurrent() in a non-UI thread it
> will return always null.

> is there another way to use this syncExec within my job?

> best regards,

> Matthias

Hi,

have you tried
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShel l().getDisplay()
instead of
Display.getCurrent()
Afaik that would be the way to go...

HTH
Marcus
Re: UI methods in normal Job [message #35717 is a reply to message #35683] Thu, 04 December 2008 13:22 Go to previous messageGo to next message
Matthias Kohles is currently offline Matthias KohlesFriend
Messages: 71
Registered: July 2009
Member
thanks for your message,

this didn't work either.

Same NullPointerException in getDisplay :(

regards Matthias
Re: UI methods in normal Job [message #35751 is a reply to message #35717] Thu, 04 December 2008 13:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Marcus.Ilgner.gerig.de

Matthias Kohles wrote:

> thanks for your message,
> this didn't work either.

> Same NullPointerException in getDisplay :(

> regards Matthias

Duh... that shouldn't happen. Where does the NPE occur exactly? In
getDisplay() or before that (activeWorkbenchWindow could be null)? Do you
have a stacktrace?
Re: UI methods in normal Job [message #35817 is a reply to message #35751] Thu, 04 December 2008 15:02 Go to previous messageGo to next message
Matthias Kohles is currently offline Matthias KohlesFriend
Messages: 71
Registered: July 2009
Member
Hey,

here is the stacktrace:

java.lang.NullPointerException
at com.myPlugin.jobs.FileScanJob.run(FileScanner.java:414)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

ad the code

try
{
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow wWindow = workbench.getActiveWorkbenchWindow();
Shell shell = wWindow.getShell();
Display display = shell.getDisplay();

display.syncExec(new Runnable()
{
public void run()
{
editorPart =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
}
});
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

the NPE is occuring on Shell shell = wWindow.getShell();

i have no idea why this is happening

regards Matthias
Re: UI methods in normal Job [message #36327 is a reply to message #35817] Mon, 08 December 2008 13:26 Go to previous message
Danail Nachev is currently offline Danail NachevFriend
Messages: 110
Registered: July 2009
Senior Member
getActiveWorkbenchWindow() works only if called from UI thread. This is
not consistent and expected it is a legacy from Eclipse 2.x.

Matthias Kohles wrote:
> Hey,
>
> here is the stacktrace:
>
> java.lang.NullPointerException
> at com.myPlugin.jobs.FileScanJob.run(FileScanner.java:414)
> at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
>
> ad the code
>
> try
> {
> IWorkbench workbench = PlatformUI.getWorkbench();
> IWorkbenchWindow wWindow =
> workbench.getActiveWorkbenchWindow();
> Shell shell = wWindow.getShell();
> Display display = shell.getDisplay();
>
> display.syncExec(new Runnable()
> {
> public void run()
> {
> editorPart =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
>
> }
> });
> }
> catch (Exception e)
> {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> the NPE is occuring on Shell shell = wWindow.getShell();
>
> i have no idea why this is happening
>
> regards Matthias
>
Re: UI methods in normal Job [message #586223 is a reply to message #35060] Tue, 02 December 2008 22:49 Go to previous message
David Virdefors is currently offline David VirdeforsFriend
Messages: 9
Registered: July 2009
Junior Member
Hi Matthias,

You do not need to create a UIJob just to run code in the UI thread if that
is what you mean, e.g. get information about an editor.

You should be able to do something like this:

org.eclipse.swt.widgets.Display.getCurrent().syncExec(new Runnable() {
@Override
public void run() {
System.out.println("I'm running from the GUI thread, wee!");
}
});

I hope this helps and that I didn't misunderstand your question.
Best of luck!

David Virdefors

Purple Scout / www.purplescout.com

"Matthias Kohles" <m.kohles@gmx.de> wrote in message
news:3fc08b78e0e93f71706e51fa4cc83d16$1@www.eclipse.org...
> Hey,
>
> i have a simple Eclipse Job which will start a search. Before i can do
> this, i need some information about the opened resource in a editor. So i
> would call PlatformUI.get... to get this information. To use this method i
> have to run it from a UI Job. I could now simply start a UIJob out of my
> normal Job to retrieve the information but there has to be a better way or
> not?
>
> Can i use the method somehow in my normal Eclipse job?
>
> regards
>
> Matthias
>
Re: UI methods in normal Job [message #586358 is a reply to message #35338] Thu, 04 December 2008 06:40 Go to previous message
Matthias Kohles is currently offline Matthias KohlesFriend
Messages: 71
Registered: July 2009
Member
Hey,

thank you for your answer.
I tried your code but it did not work for me.

i have a normal NonUI eclipe Job. In this job i need the information of
what file from which project is opened in the active editor. So i tried
your code within this job but i receive a NullPointerException in
Display.getCurrent().

Display.getCurrent().syncExec(new Runnable()
{
public void run()
{
editorPart =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
}
});

JavaDoc says that when i call Display.getCurrent() in a non-UI thread it
will return always null.

is there another way to use this syncExec within my job?

best regards,

Matthias
Re: UI methods in normal Job [message #586382 is a reply to message #35615] Thu, 04 December 2008 11:40 Go to previous message
Marcus Ilgner is currently offline Marcus IlgnerFriend
Messages: 15
Registered: July 2009
Junior Member
Matthias Kohles wrote:

> Hey,

> thank you for your answer.
> I tried your code but it did not work for me.

> i have a normal NonUI eclipe Job. In this job i need the information of
> what file from which project is opened in the active editor. So i tried
> your code within this job but i receive a NullPointerException in
> Display.getCurrent().

> Display.getCurrent().syncExec(new Runnable()
> {
> public void run()
> {
> editorPart =
>
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
> }
> });

> JavaDoc says that when i call Display.getCurrent() in a non-UI thread it
> will return always null.

> is there another way to use this syncExec within my job?

> best regards,

> Matthias

Hi,

have you tried
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShel l().getDisplay()
instead of
Display.getCurrent()
Afaik that would be the way to go...

HTH
Marcus
Re: UI methods in normal Job [message #586388 is a reply to message #35683] Thu, 04 December 2008 13:22 Go to previous message
Matthias Kohles is currently offline Matthias KohlesFriend
Messages: 71
Registered: July 2009
Member
thanks for your message,

this didn't work either.

Same NullPointerException in getDisplay :(

regards Matthias
Re: UI methods in normal Job [message #586399 is a reply to message #35717] Thu, 04 December 2008 13:50 Go to previous message
Marcus Ilgner is currently offline Marcus IlgnerFriend
Messages: 15
Registered: July 2009
Junior Member
Matthias Kohles wrote:

> thanks for your message,
> this didn't work either.

> Same NullPointerException in getDisplay :(

> regards Matthias

Duh... that shouldn't happen. Where does the NPE occur exactly? In
getDisplay() or before that (activeWorkbenchWindow could be null)? Do you
have a stacktrace?
Re: UI methods in normal Job [message #586427 is a reply to message #35751] Thu, 04 December 2008 15:02 Go to previous message
Matthias Kohles is currently offline Matthias KohlesFriend
Messages: 71
Registered: July 2009
Member
Hey,

here is the stacktrace:

java.lang.NullPointerException
at com.myPlugin.jobs.FileScanJob.run(FileScanner.java:414)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

ad the code

try
{
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow wWindow = workbench.getActiveWorkbenchWindow();
Shell shell = wWindow.getShell();
Display display = shell.getDisplay();

display.syncExec(new Runnable()
{
public void run()
{
editorPart =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
}
});
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

the NPE is occuring on Shell shell = wWindow.getShell();

i have no idea why this is happening

regards Matthias
Re: UI methods in normal Job [message #586610 is a reply to message #35817] Mon, 08 December 2008 13:26 Go to previous message
Danail Nachev is currently offline Danail NachevFriend
Messages: 110
Registered: July 2009
Senior Member
getActiveWorkbenchWindow() works only if called from UI thread. This is
not consistent and expected it is a legacy from Eclipse 2.x.

Matthias Kohles wrote:
> Hey,
>
> here is the stacktrace:
>
> java.lang.NullPointerException
> at com.myPlugin.jobs.FileScanJob.run(FileScanner.java:414)
> at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
>
> ad the code
>
> try
> {
> IWorkbench workbench = PlatformUI.getWorkbench();
> IWorkbenchWindow wWindow =
> workbench.getActiveWorkbenchWindow();
> Shell shell = wWindow.getShell();
> Display display = shell.getDisplay();
>
> display.syncExec(new Runnable()
> {
> public void run()
> {
> editorPart =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
>
> }
> });
> }
> catch (Exception e)
> {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> the NPE is occuring on Shell shell = wWindow.getShell();
>
> i have no idea why this is happening
>
> regards Matthias
>
Previous Topic:Plug-in compiles and exports, but throws NPE when accessed
Next Topic:How to run executable jar via eclipse update manager
Goto Forum:
  


Current Time: Tue Apr 16 17:24:08 GMT 2024

Powered by FUDForum. Page generated in 0.41848 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top