【问题标题】:Eclipse RCP IPageLayout woesEclipse RCP IPageLayout 问题
【发布时间】:2012-08-18 16:31:13
【问题描述】:

我有一个具有三列布局的 Eclipse RCP 应用程序:

编辑器区域位于最右侧。现在,当你得到一个IPageLayout 可以使用时,编辑器区域已经添加进去了。这很好:我们将区域 B 添加到编辑器的左侧,将区域 A 添加到 B 的左侧,布局就是这样我们需要。

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

似乎根本原因是当您获得IPageView 时编辑器就位,因此您必须相对于它定位IFolderLayouts。相反,如果您可以相对于 B 定位编辑器,那么调整大小将是正确的。

所以我的问题:

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

【问题讨论】:

    标签: java layout swt eclipse-rcp


    【解决方案1】:

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

    因此,如果您考虑将应用程序迁移到 Eclipse 4,那么这个解决方案可能是一个选择。为了尽可能保持原始应用程序和 UI 代码不变,此解决方案将

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

    我从 Eclipse 3 的常规 RCP 邮件模板开始,并更改了透视图以重现问题。这是我在测试应用程序中使用的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);
      }
    }
    

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

    然后我开始迁移应用程序并更改应用程序模型。

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

    有可用于此过程的在线教程。我发现Eclipse 4.1: Run your 3.x RCP in 4.1Eclipse 4 and the Compatibility Layer - Tutorial 很有帮助。

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

    应用程序启动后,窗扇仍会以相同的方式运行。在您的应用程序窗口中打开实时编辑器并查看您的模型。

    您可以看到PartSashContainer(包括AView 的占位符)包含另一个PartSashContainer。在AView 和该容器之间移动窗扇将更新布局树的其余部分,而在BView 和编辑器之间移动窗扇不会影响布局的其他部分。

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

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

    由于可能的话我不想接触原始代码,所以我创建了另一个插件来为应用程序模型做出贡献。

    在不使用模板的情况下创建不带Activator 的插件项目。

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

    添加Model Fragment:选择New->Other-Eclipse 4->Model->New Model Fragment。打开创建的fragment.e4xmi 文件并添加Model Fragment。对于元素 ID,输入 org.eclipse.e4.legacy.ide.application(这是遗留应用程序的标准 ID)和功能名称 addons。将Addon 添加到Model 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>
    

    将您的贡献插件添加到您的应用程序产品的依赖项中。当您启动应用程序并使用实时编辑器查看模型时,您应该会在模型中看到您的 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);
        }
      }
    }
    

    (请注意,上面的代码有点hack,因为它只适合这个特定的问题。)

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

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

    【讨论】:

    • 这太好了,谢谢!我们计划很快迁移到 Eclipse 4;这对我们来说听起来像是一些额外的动力。再次感谢!
    【解决方案2】:

    我不认为,完全可以实现您想要的(因此您的问题的答案是 1. 不,2. 不)。但这是第三种选择,IMO 表现得非常好。

    在 Eclipse 中尝试时:从左侧的 viewA 和右侧的 Editor 开始。然后,当您将 viewB 拖到 viewA 的右侧时,您会得到您描述的(错误的)设置。但是随后将其拖动到编辑器的左侧,然后您将获得不同的配置,其中拖动右窗扇的行为如您所愿。拖动左窗框可调整 viewA 和 Editor 以及 MOVES viewB 的大小。

    我想说实现这一点的代码是:

    IFolderLayout areaA = layout.createFolder("A", IPageLayout.LEFT, 0.33f, editorArea);
    IFolderLayout areaB = layout.createFolder("B", IPageLayout.LEFT, 0.5f, editorArea);
    

    【讨论】:

    • 感谢您的回答!这绝对是不同,但我不知道它是否更好,直到我将它展示给抱怨当前安排的用户。
    猜你喜欢
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2012-03-25
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    相关资源
    最近更新 更多