You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Eclipse E4中于当前Part Stack打开同一视图多实例?

在Eclipse E4中实现视图副本在当前激活Part Stack打开的需求

我已经通过实现IPerspectiveFactory创建了视图及对应占位符,支持打开同一视图的多实例,代码如下:

@Override
public void createInitialLayout(final IPageLayout layout)
{
  String editorArea = layout.getEditorArea();
  IFolderLayout right = layout.createFolder("myViewLayout", IPageLayout.RIGHT, 0.22, IPageLayout.ID_PROJECT_EXPLORER);
  right.addView("com.example.myView");
  right.addPlaceholder("com.example.myView:*");
}

我在视图上添加了一个菜单按钮,触发时需要在原视图所在的同一Part Stack中以标签页形式打开该视图的副本;如果将某视图实例拖拽到其他Part Stack后,触发操作时新实例应在当前激活的Part Stack中打开。

我尝试了Eclipse E4方式的实现,代码如下:

public class NewMyView extends AbstractHandler {
 int counter = 1;
 @Override
  public Object execute(final ExecutionEvent event) throws ExecutionException
  {
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    for (; activePart.getSite().getPage().findViewReference("com.example.myView", getNextSecondaryId()) != null; this.counter++) 
    {
    
    }

    IEclipseContext serviceContext = E4Workbench.getServiceContext();
    final IEclipseContext appContext = serviceContext.getActiveChild();

    EModelService modelService = appContext.get(EModelService.class);
    MApplication app = serviceContext.get(MApplication.class);
    EPartService partService = serviceContext.get(EPartService.class);

    MPart part = partService.findPart("com.example.myView");
    if (part == null)
    {
      MPlaceholder placeholder = partService.createSharedPart("com.example.myView");
      part = (MPart) placeholder.getRef();
    }
    partService.showPart(part, PartState.ACTIVATE);
    this.counter++;
 }

private String getNextSecondaryId()
  {
    return "myView#" + this.counter;
  }
}

此前用E3方式可部分实现该需求(类似E3的Console视图),代码如下:

page.showView("com.example.myView", getNextSecondaryId(), IWorkbenchPage.VIEW_ACTIVATE);
this.counter++

但E3方案存在缺陷:将视图实例拖拽到新Part Stack后,触发操作时新实例仍会在IFolderLayout初始添加的默认位置打开,E3的Console视图也存在该问题。

请问:

  • 该需求能否通过Eclipse E4实现?
  • 如果可以,我当前使用EPartService和MPlaceholder的代码是否正确?

内容的提问来源于stack exchange,提问作者Sangli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 23:35:13