My problem is the following one : I am working with 2 plugins : let's call them A and B.
When I start the application, A starts B and gives him a parameter : the current ActiveEditor. B can now perform some actions on the current editor. Everything is fine for this point.
However, the parameter is given only when A is instantiated, so if the current editor changes, B is not aware of the change.
I tried to use a Thread, started by A, getting the active editor every two seconds and setting it into B. Problem : NullPointerException every time =>
java.lang.NullPointerException: while trying to invoke the method org.eclipse.ui.IWorkbenchWindow.getActivePage() of a null object returned from org.eclipse.ui.IWorkbench.getActiveWorkbenchWindow()
at com.highdeal.tests.cortex.eclipse.RefreshThread.run(RefreshThread.java:14)
Here is the code of the thread started by the plugin A :
public class RefreshThread extends Thread {
public void run(){
while (true){
try{
Thread.sleep(1000);
IEditorPart iep = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
new SampleAction(iep);
} catch (Exception e){
e.printStackTrace();
}
}
}
}
When I migrate the following piece of code into the constructor of the Plugin A, it works correctly :
IEditorPart iep = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
new SampleAction(iep);
Thanks for reading. Ask me if you need further explanations.
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
always returns null
when it is called from a background thread (that is a thread which is not the UI thread).
A better way to do this is to use IPartListener
to listen for the active part being changed.
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
window.getPartService().addPartListener(part listener);
(must be run in the UI thread).
The part listener will be told about each part being activated and you can check if this is a new editor.
要在其他线程而不是UIThread中运行UI代码,请使用UIThreadSynchronizer或Display.syncExec()\\ asyncExec()调用。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.