在Eclipse中以编程方式调整视图的大小 [英] Programmatically resize a view in Eclipse

查看:324
本文介绍了在Eclipse中以编程方式调整视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SWTBot测试 non-e4 RCP应用程序,并且需要更改视图的大小。 (移动窗框)

I'm testing an non-e4 RCP application using SWTBot and I need to change the size of my view. (Move the sash-bar)

我尝试失败


  • 调整视图大小使用SWTBot(无此类api)

  • 使用Eclipse 3 API调整视图大小(不支持)

  • 使用基础e4模型调整视图大小(不调整大小)

e4模型接缝是有希望的,但是我错过了一些东西,所以它不起作用。

e4 model seams to be promising, but I'm missing something, so it doesn't work.

我可以


  • 获取我的视图的MPart: view = ePartService .findPart(ID)

  • 获取MTrimmedWindow: window =(以EObject的视图).eContainer为MTrimmedWindow

  • Get MPart of my view: view = ePartService.findPart(ID)
  • Get MTrimmedWindow: window = (view as EObject).eContainer as MTrimmedWindow

我不能


  • 语言环境正确的MPartSashContainer

  • 使用 setContainerData()

  • locale correct MPartSashContainer
  • move sash-bar with setContainerData()

我想知道


  • 如何从MPart迁移到其直接父级(例如MPartStack)
  • 为什么常见的EObject方法(例如eContainer())不存在于M ...对象?

推荐答案

好,我自己找到了解决方案。

Ok, I found a solution myself.

问题是,该视图不是e4 UI-Tree的一部分。 view.eContainer 直接是 MWindow 。要放置在正确的位置,视图将连接到 MPlaceholder ,它是e4 UI-Tree的一部分,具有 getParent()! = null

The thing is, that the view is not a part of the e4 UI-Tree. view.eContainer is directly the MWindow. To be placed at the right spot the view is connected to the MPlaceholder, that is a part of the e4 UI-Tree and has getParent() != null.

为了调整视图的大小,步骤如下:

In order to resize a view the steps are:


  • 显示视图

  • 查找视图的 MPlaceholder

  • 查找 MPartStack 和`MPartSashContainer´对象

  • 设置 containerData

  • 重画窗口小部件(是的,在这种情况下无法自动更新接缝)

  • Show view
  • Find MPlaceholder of the view
  • Find MPartStack and `MPartSashContainer´ object
  • Set containerData
  • Redraw widget (yes, auto-update seam not to work in this case)

示例:

EModelService modelService = PlatformUI.getWorkbench().getService(EModelService.class);
EPartService  partService  = PlatformUI.getWorkbench().getService(EPartService.class);

// Show view
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.showView(MyView.ID, null, IWorkbenchPage.VIEW_ACTIVATE);

MPart view = partService.findPart(MyView.ID);
// view.getParent() => null, because 'view' is not a part of the e4 UI-model!
// It is connected to the Model using MPlaceholder

// Let's find the placeholder
MWindow window = (MWindow)(((EObject)eView).eContainer);
MPlaceholder placeholder = modelService.findPlaceholderFor(window, view);

MUIElement element = placeholder;
MPartStack partStack = null;
while (element != null) {
    // This may not suite your configuration of views/stacks/sashes
    if (element instanceof MPartStack && ((Object)element.parent) instanceof MPartSashContainer) {
            partStack = (MPartStack)element;
            break;
        }
        element = element.parent;
    }
}
if (partStack == null) { /* handle error */ }

// Now let's change the width weights
for (MUIElement element : partStack.getParent().getChildren()) {
    if (element == partStack) {
        element.setContainerData("50"); // Width for my view
    } else {
        element.setContainerData("25"); // Widths for other views & editors
    }
}

// Surprisingly I had to redraw tho UI manually
// There is for sure a better way to do it. Here is my (quick & very dirty):
partStack.toBeRendered = false
partStack.toBeRendered = true

这篇关于在Eclipse中以编程方式调整视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆