Programmatically resize a view in Eclipse

我只是一个虾纸丫 提交于 2019-12-23 19:01:43

问题


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

I unsuccessfully tried

  • Resize my view using SWTBot (no such api)
  • Resize my view using Eclipse 3 API (no supported)
  • Resize my view using underlying e4 model (resizing not working)

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

I can

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

I can't

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

I would like to know

  • How can I move from MPart to its direct parent (e.g. MPartStack)
  • Why common EObject methods like eContainer() are not present on M... objects?

回答1:


Ok, I found a solution myself.

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:

  • 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)

Example:

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


来源:https://stackoverflow.com/questions/47202032/programmatically-resize-a-view-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!