Boris Brodski
Boris Brodski

Reputation: 8715

Programmatically resize a view in Eclipse

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

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

I can

I can't

I would like to know

Upvotes: 3

Views: 850

Answers (1)

Boris Brodski
Boris Brodski

Reputation: 8715

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

Upvotes: 1

Related Questions