Eclipse RCP IPageLayout困扰

Ern*_*ill 7 java layout swt eclipse-rcp

我有一个Eclipse RCP应用程序,有三种列布局:

在此输入图像描述

编辑区位于最右侧.现在,当你开始IPageLayout使用时,编辑器区域已经被添加.没关系:我们在编辑器的左边添加区域B,在B的左边添加区域A,布局正是我们需要的.

问题是,当您在A和B之间移动窗框时,视图A和B会在不调整编辑区域大小的情况下发生变化(好;)但是当您在B和编辑区域之间移动另一个窗框时,所有三个视图都会调整大小; 布局管理器用于保持A和B宽度的比例,这不是我们想要的.我们希望用户能够独立移动每个窗格,并使其仅影响它接触的两个视图.

看起来这样做的根本原因是当你得到你的编辑器时,编辑器已就位IPageView,因此你必须将IFolderLayouts相对于它定位.如果您可以相对于B定位编辑器,那么调整大小会做正确的事情.

所以我的问题:

  1. 有没有办法告诉IPageView编辑器相对于视图的位置,而不是相反?
  2. 除此之外,是否还有其他方法可以影响布局算法,比如编写某种布局管理器?

Mod*_*ens 7

我知道无法改变IPageLayoutEclipse 3.x中的布局树.但是,在Eclipse 4.2中,可以在运行时动态更改应用程序模型.

因此,如果您考虑将应用程序迁移到Eclipse 4,则可以选择此解决方案.为了保持原始应用程序和UI代码不受影响,此解决方案将

  • 充分利用Eclipse 4的兼容层,从基于Eclipse 3的RCP应用程序创建应用程序模型.无需创建应用程序模型或更改应用程序的UI代码.
  • 应用程序处于活动状态后重新排列编辑器区域的布局.这是通过在单独的插件中创建插件类来完成的.
  • 允许以后轻松迁移到更多Eclipse 4功能:如果您决定构建自己的应用程序模型,则可以取消挂载addon插件.

我从Eclipse 3的常规RCP Mail模板开始,并改变了透视图以重新创建问题.这是Perspective我在测试应用程序中使用的类:

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

  public static final String ID = "wag.perspective";

  public void createInitialLayout(IPageLayout layout) {
    String editorArea = layout.getEditorArea();
    layout.setEditorAreaVisible(true);

    layout.addStandaloneView(AView.ID, false, IPageLayout.LEFT,
                             0.25f, editorArea);
    layout.addStandaloneView(BView.ID, false, IPageLayout.LEFT,
                             0.25f, editorArea);

    layout.getViewLayout(AView.ID).setCloseable(false);
    layout.getViewLayout(BView.ID).setCloseable(false);
  }
}
Run Code Online (Sandbox Code Playgroud)

它基本上创建了您描述的场景:三列布局,其中一个窗框影响所有三个部分,另一个窗格仅影响两个部分.

然后我继续迁移应用程序并更改应用程序模型.

将基于Eclipse 3的RCP应用程序迁移到Eclipse 4

有这个过程的在线教程.我发现Eclipse 4.1:在4.1Eclipse 4中运行你的3.x RCP 和兼容性层 - 教程非常有帮助.

我建议org.eclipse.e4.tools.emf.liveeditor在产品依赖项中包含它及其所需的插件.使用实时编辑器,您可以查看兼容层创建的应用程序模型.

一旦应用程序启动,thes sash仍然会以相同的方式运行.在应用程序窗口中打开实时编辑器,然后查看您的模型.

兼容层创建的布局树

您可以看到包含另一个PartSashContainer占位符.移动窗框和该容器将更新布局树的其余部分,同时在两者之间移动窗格,编辑器不会影响布局的其他部分.AViewPartSashContainerAViewBView

您现在可以将占位符拖动AViewBView编辑器所在的容器.这会立即产生你想要的效果:腰带只会影响他们的直接邻居.但是这些更改只会保存在自己的运行时工作区中.还需要其他一些东西来自动改变布局结构.

在运行时更改应用程序模型

由于我不想在可能的情况下触摸原始代码,因此我创建了另一个插件来为应用程序模型做出贡献.

Activator无需使用模板即可创建插件项目.

添加一个Addon类:选择New-> Other-> Eclipse 4-> Classes-> New Addon Class

添加Model Fragment:选择New-> Other-Eclipse 4-> Model-> New Model Fragment.打开创建的fragment.e4xmi文件并添加一个Model Fragment.对于Element Id,put org.eclipse.e4.legacy.ide.application(这是遗留应用程序的标准id)和Featurename addons.添加AddonModel Fragment.输入ID并将其设置Class URI为您的插件类.

现在添加fragment.e4xmi到您的org.eclipse.e4.workbench.model扩展点:

<extension
  id="id1"
  point="org.eclipse.e4.workbench.model">
  <fragment
    uri="fragment.e4xmi">
  </fragment>
</extension>
Run Code Online (Sandbox Code Playgroud)

将您的贡献插件添加到应用程序产品的依赖项中.当您启动应用程序并使用实时编辑器查看模型时,您应该会看到Addon模型中列出的内容.

现在我们可以实现了Addon.这是我Addon班级的代码:

package wag.contribution.addons;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

public class LayoutSorter {

  @Inject private IEventBroker broker;

  private EventHandler handler;

  // The part IDs we are interested in, sorted in the sequence they should be
  // shown
  private static List<String> PART_IDS = Arrays.asList(new String[] {
              "wag.aView", "wag.bView", "org.eclipse.ui.editorss" });

  // Listen to the e4 core service's event broker to find the magical time
  // when the application is created and try to sort the layout.
  @PostConstruct
  void hookListeners(final MApplication application,
                     final EModelService service) {

    if (handler == null) {
      handler = new EventHandler() {
        // Try to sort the layout. Unsubscribe from event broker if
        // successful.
        @Override
        public void handleEvent(Event event) {
          try {
            sort(application, service);
            // sort did finish: stop listening to the broker.
            broker.unsubscribe(handler);
          } catch (Exception e) {
            // Something went wrong, the application model was not ready yet.
            // Keep on listening.
          }
        }
      };

      // Subscribe "ServiceEvent.MODIFIED" to grab the application.STARTED
      // event. Does anybody know how to do this in a better way?
      broker.subscribe("org/osgi/framework/ServiceEvent/MODIFIED",
                       handler);
    }
  }

  private void sort(MApplication application, EModelService service) {

    // find all placeholders
    List<MPlaceholder> placeholders = service.findElements(application,
              null, MPlaceholder.class, null);

    // only keep the ones we are interested in
    for (int i = placeholders.size() - 1; i > -1; i--) {
      if (!PART_IDS.contains(placeholders.get(i).getElementId())) {
        placeholders.remove(i);
      }
    }

    // find the parents of the placeholders
    List<MElementContainer<MUIElement>> parents = new ArrayList<>(
             placeholders.size());
    for (MPlaceholder placeholder : placeholders) {
      parents.add(placeholder.getParent());
    }

    // find the parent that is "deepest down" in the tree
    MElementContainer<MUIElement> targetParent = null;
    for (MElementContainer<MUIElement> parent : parents) {
      for (MUIElement child : parent.getChildren()) {
        if (parents.contains(child)) {
          continue;
        }
        targetParent = parent;
      }
    }

    // move all parts to the target parent
    if (targetParent != null) {
      for (int i = 0; i < placeholders.size(); i++) {
        if (targetParent != placeholders.get(i).getParent()) {
          service.move(placeholders.get(i), targetParent, i);
        }
      }
    }
  }

  @PreDestroy
  void unhookListeners() {
    if (handler != null) {
      // in case it wasn't unhooked earlier
      broker.unsubscribe(handler);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

(请注意,上面的代码有点像黑客,因为它只适用于这个特定的问题.)

重新启动后,应用程序现在应该以所需的方式运行.查看应用程序模型以查看更改.

需要注意的一点是,.metadata\.plugins\org.eclipse.e4.workbench\workbench.xmi如果保存已打开,则本地更改将保存在文件的运行时工作空间中,因此,为了重新创建未更改的模型以进行测试,必须删除此文件.