user3649231 user3649231 - 9 months ago 26
Java Question

How can I get active page in eclipse plugin?

I need to get the active page in the eclipse editor. When I googled it up, I found the code below​:

IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
ITextEditor editor = (ITextEditor) page.getActiveEditor();


However, when I use this code, it generates the following error message:

java.lang.NullPointerException
at kr.ac.jbnu.ssel.instantfeedback.views.InstantFeedbackView$1.elementChanged(InstantFeedbackView.java:136)
at org.eclipse.jdt.internal.core.DeltaProcessor$3.run(DeltaProcessor.java:1662)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.jdt.internal.core.DeltaProcessor.notifyListeners(DeltaProcessor.java:1652)
at org.eclipse.jdt.internal.core.DeltaProcessor.fireReconcileDelta(DeltaProcessor.java:1504)
at org.eclipse.jdt.internal.core.DeltaProcessor.fire(DeltaProcessor.java:1463)
at org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:765)
at org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:790)
at org.eclipse.jdt.internal.core.CompilationUnit.reconcile(CompilationUnit.java:1250)
at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:128)
at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.access$0(JavaReconcilingStrategy.java:110)
at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy$1.run(JavaReconcilingStrategy.java:90)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:87)
at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:154)
at org.eclipse.jdt.internal.ui.text.CompositeReconcilingStrategy.reconcile(CompositeReconcilingStrategy.java:89)
at org.eclipse.jdt.internal.ui.text.JavaCompositeReconcilingStrategy.reconcile(JavaCompositeReconcilingStrategy.java:104)
at org.eclipse.jface.text.reconciler.MonoReconciler.process(MonoReconciler.java:73)
at org.eclipse.jface.text.reconciler.AbstractReconciler$BackgroundThread.run(AbstractReconciler.java:207)


How can I resolve this issue?

Add
This is my elementChanged() function. It is in the method.

private void addListenerToCheckChange_save() {
JavaCore.addElementChangedListener(new IElementChangedListener() {

@Override
public void elementChanged(ElementChangedEvent event) {
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
ITextEditor editor = (ITextEditor) page.getActiveEditor();
IJavaElement javaElement = JavaUI.getEditorInputJavaElement(editor.getEditorInput());
if (javaElement instanceof ICompilationUnit) {
ITextSelection sel = (ITextSelection) editor.getSelectionProvider().getSelection();
IJavaElement selected = null;
try {
...
} catch (JavaModelException JME) {
JME.printStackTrace();
}
}
}
});
}


And i tried to modify it to implement IElementChangedListener in my view class. This is modified one.

@Override
public void elementChanged(ElementChangedEvent arg0) {
Display.getCurrent().asyncExec(new Runnable() {

@Override
public void run() {
IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
ITextEditor editor = (ITextEditor) page.getActiveEditor();
IJavaElement javaElement = JavaUI.getEditorInputJavaElement(editor.getEditorInput());
if (javaElement instanceof ICompilationUnit) {
ITextSelection sel = (ITextSelection) editor.getSelectionProvider().getSelection();
IJavaElement selected = null;
try {
...
} catch (JavaModelException JME) {
JME.printStackTrace();
}
}
}
});
}


However, modified one was not working...

Answer Source

You are getting null for the active workbench window from:

IWorkbenchWindow win = wb.getActiveWorkbenchWindow();

This is because your code is being run in a thread which is not the main UI thread - getActiveWorkbenchWindow returns null in this case.

To use UI operations like this you will need to arrange to run your code in the UI thread, perhaps by using Display.asyncExec. Something like:

public void elementChanged(ElementChangedEvent event) {

   ... code not run in UI thread

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

(using Java 8 lambda, for older Java's use a Runnable).