【问题标题】:Double click listener on eclipse workspace resource双击 Eclipse 工作区资源上的侦听器
【发布时间】:2014-01-01 15:40:52
【问题描述】:

当用户在项目资源管理器中双击某些文件类型(例如:.pc、.mn 等)时,我想打开一个对话框。

我不知道这是否是正确的做法。我为这些文件类型创建了一个虚拟编辑器(通过扩展 org.eclipse.ui.editors.text.TextEditor),并在 init 方法中创建了对话框并打开了它。

但在用户关闭对话框后(按确定或取消按钮)。编辑器打开时出现错误,如下所示。

我在 init 方法中的代码

@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {

    FileEditorInput fileEdi = null;
    if (input instanceof FileEditorInput) {
        fileEdi = (FileEditorInput)input;
    }
    ABCTitleAreaDialog dia = new ABCTitleAreaDialog(site.getShell(), fileEdi.getFile().getLocation().toString(),
            null);
    dia.setHelpAvailable(false);
    if (Window.OK == dia.open()) {
        Display.getDefault().asyncExec(new Runnable() {

            @Override
            public void run() {
                //Code to open Perspective
            }
        });
    }
    IWorkbenchPart part = site.getPart();
    if (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeEditor((IEditorPart)part, false)) {
        System.out.println("closed the editor");
    }
    throw new PartInitException("Error opening editor"));//$NON-NLS-1$
}

是否可以取消/中止编辑器创建过程,这样我就可以避免打开错误编辑器部分。

任何替代解决方案对我来说也可以。

【问题讨论】:

    标签: eclipse eclipse-plugin swt eclipse-rcp jface


    【解决方案1】:

    您可能在 init(...) 方法的开头缺少以下调用:

    super.setInput(input);
    super.setSite(site);
    

    增强:

    您应该考虑通过扩展点 org.eclipse.ui.navigator.navigatorContentorg.eclipse.ui.navigator.viewer 使用可重定向的操作来贡献一个新的org.eclipse.ui.navigator.ProjectExplorer 的资源内容。 因此,您可以根据对话框的退出状态调用打开对话框和编辑器的操作

    进一步阅读见

    【讨论】:

      【解决方案2】:

      您可以使用asyncExec 关闭编辑器,而不是抛出PartInitException

      @Override
      public void init(final IEditorSite site, final IEditorInput input)
      {
        setSite(site);
        setInput(input);
      
        Display.getCurrent().asyncExec(new Runnable()
        {
          @Override
          public void run()
          {
            site.getPage().closeEditor(Editor.this, false);
          }
        });
      }
      

      编辑:更改为显示完整的初始化方法

      【讨论】:

      • @ChandrayyaGK 为我工作。会发生什么?
      • 错误编辑器再次打开并显示消息:“编辑器初始化失败:。站点不正确。”
      • @ChandrayyaGK 你必须做足够的正常编辑器初始化来保持编辑器系统的运行。
      • 在我调用 setSite(site);setInput(fileEdi); 之后我没有收到错误编辑器,而是从 bundle '278' org.eclipse.e4.core.di.InjectionException: java.lang.IllegalArgumentException : 参数无效 ==!MESSAGE 从插件调用代码时出现问题:“org.eclipse.e4.ui.workbench.swt”。 org.eclipse.swt.SWTException: Widget is disposed ===。有没有办法在 init 方法本身中中止编辑器创建流程?尝试抛出 PartInitException 但不起作用。是否可以避免这些异常
      • 更改代码以显示完整的init 方法。这对我来说运行没有错误。您遇到的错误可能是因为您正在使用已在您的其他 asyncExec Runnable 中处理的东西。
      猜你喜欢
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多