如何在eclipse插件中获取活动页面?

时间:2017-03-11 09:50:06

标签: java eclipse eclipse-plugin

我需要在eclipse编辑器中获取活动页面。当我用Google搜索时,我找到了以下代码:

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

但是,当我使用此代码时,它会生成以下错误消息:

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)

如何解决此问题?

添加 这是我的elementChanged()函数。它在方法中。

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();
        }
      }
    }
  });
}

我尝试修改它以在我的视图类中实现IElementChangedListener。这是修改过的。

@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();
        }
      }
    }
  });       
}

然而,修改后的一个没有用......

1 个答案:

答案 0 :(得分:2)

您从活动工作台窗口获取null:

IWorkbenchWindow win = wb.getActiveWorkbenchWindow();

这是因为您的代码是在不是主UI线程的线程中运行的 - getActiveWorkbenchWindow在这种情况下返回null。

要使用这样的UI操作,您需要安排在UI线程中运行代码,可能使用Display.asyncExec。类似的东西:

public void elementChanged(ElementChangedEvent event) {

   ... code not run in UI thread

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

(使用Java 8 lambda,对于较旧的Java,使用Runnable)。