我正在使用 SWTBot 测试非 e4 RCP 应用程序,并且我需要更改 View 的大小。 (移动窗框)
尝试失败
- 使用 SWTBot 调整我的 View 大小(没有此类 api)
- 使用 Eclipse 3 API 调整我的 View 大小(不支持)
- 使用底层 e4 模型调整我的 View 大小(调整大小不起作用)
e4 模型看起来很有前途,但我缺少一些东西,所以它不起作用。
我可以
- 获取我的 View 的 MPart:
view = ePartService.findPart(ID)
- 获取 MTrimmedWindow:
window = (view as EObject).eContainer as MTrimmedWindow
我不能
- 区域设置正确的 MPartSashContainer
- 使用
setContainerData()
移动窗框
我想知道
- 如何从 MPart 移动到其直接父级(例如 MPartStack)
- 为什么 M... 对象上不存在常见的 EObject 方法(例如 eContainer())?
最佳答案
好的,我自己找到了解决方案。
问题是,该 View 不是 e4 UI-Tree 的一部分。 view.eContainer
直接是MWindow
。为了放置在正确的位置, View 连接到 MPlaceholder
,它是 e4 UI-Tree 的一部分,具有 getParent() != null
.
为了调整 View 大小,步骤如下:
- 显示 View
- 查找
MPlaceholder
View 的 - 查找
MPartStack
和“MPartSashContainer”对象 - 设置
containerData
- 重绘小部件(是的,自动更新接缝在这种情况下不起作用)
示例:
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 - 在 Eclipse 中以编程方式调整 View 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47202032/