java - Eclipse RCP IPageLayout 问题

标签 java layout swt eclipse-rcp

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

enter image description here

编辑区在最右边。现在,当您收到 IPageLayout要使用,编辑器区域已经添加进去了。这很好:我们在编辑器的左侧添加区域 B,在 B 的左侧添加区域 A,布局正是我们需要的。

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

看起来这个问题的根本原因是当你拿到 IPageView 时编辑器就位。 ,因此您必须定位 IFolderLayout s 相对于它。如果您可以相对于 B 定位编辑器,那么调整大小会做正确的事情。

所以我的问题:

  • 有什么办法告诉IPageView相对于 View 定位编辑器,而不是相反?
  • 除此之外,是否还有其他方法可以影响布局算法,例如编写某种布局管理器?
  • 最佳答案

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

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

  • 充分利用 Eclipse 4 的兼容层从基于 Eclipse 3 的 RCP 应用程序创建应用程序模型。无需创建应用程序模型或更改应用程序的 UI 代码。
  • 在应用程序处于 Activity 状态后重新排列编辑器区域的布局。这是通过在单独的插件中创建插件类来完成的。
  • 允许在 future 轻松迁移到更多 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及其产品依赖项中所需的插件。使用实时编辑器,您可以查看由兼容层创建的应用程序模型。

    一旦应用程序启动,窗扇的行为仍然相同。在您的应用程序窗口中打开实时编辑器并查看您的模型。

    layout tree created by the compatibility layer

    您可以看到 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 .添加 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>
    

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

    现在我们可以实现 Addon .这是我的Addon的代码类(class):
    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);
        }
      }
    }
    

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

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

    需要注意的一件事是本地更改保存在文件 .metadata\.plugins\org.eclipse.e4.workbench\workbench.xmi 中的运行时工作区中。如果打开了保存,那么为了重新创建用于测试的未更改模型,必须删除此文件。

    关于java - Eclipse RCP IPageLayout 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11891387/

    相关文章:

    java - Spring-Data JPA : save new entity referencing existing one

    java - 如何创建一个以泛型类作为类型的泛型类?

    java - 以用户友好的格式转换字节

    android - 显示软键盘时垂直滚动整个 View

    java - HelloWorld Spring 应用程序在线程 "main": java. lang.NoClassDefFound 中抛出异常

    javascript - 有没有办法批量套用多个CSS样式,避免多次回流?

    css - 如何使用 ionic 框架显示元素符号列表?

    java - Mac OS X 10.5 上的 SWT 和 Java WebStart

    java - 带有 WEBKIT 的 SWT 浏览器高级功能

    java - 在ant中运行java项目