private void combine(
      MPartSashContainerElement toInsert,
      MPartSashContainerElement relTo,
      MPartSashContainer newSash,
      boolean newFirst,
      float ratio) {
    MElementContainer<MUIElement> curParent = relTo.getParent();
    if (curParent == null) {
      // if relTo is a shared element, use its current placeholder
      MWindow win = getTopLevelWindowFor(relTo);
      relTo = findPlaceholderFor(win, relTo);
      curParent = relTo.getParent();
    }
    Assert.isLegal(relTo != null && curParent != null);
    int index = curParent.getChildren().indexOf(relTo);
    curParent.getChildren().remove(relTo);
    if (newFirst) {
      newSash.getChildren().add(toInsert);
      newSash.getChildren().add(relTo);
    } else {
      newSash.getChildren().add(relTo);
      newSash.getChildren().add(toInsert);
    }

    // Set up the container data before adding the new sash to the model
    // To raise the granularity assume 100% == 10,000
    int adjustedPct = (int) (ratio * 10000);
    toInsert.setContainerData(Integer.toString(adjustedPct));
    relTo.setContainerData(Integer.toString(10000 - adjustedPct));

    // add the new sash at the same location
    curParent.getChildren().add(index, newSash);
  }
  private MElementContainer<? extends MUIElement> getLastContainer() {
    MElementContainer<? extends MUIElement> searchRoot = getContainer();
    @SuppressWarnings("unchecked")
    List<MUIElement> children = (List<MUIElement>) searchRoot.getChildren();
    if (children.size() == 0) {
      MPartStack stack = modelService.createModelElement(MPartStack.class);
      children.add(stack);
      return stack;
    }

    MElementContainer<?> lastContainer = getLastContainer(searchRoot, children);
    if (lastContainer instanceof MPartStack) {
      return lastContainer;
    }

    // No stacks found make one and add it
    MPartStack stack = modelService.createModelElement(MPartStack.class);
    stack.setElementId("CreatedByGetLastContainer"); // $NON-NLS-1$
    if (children.get(0) instanceof MPartSashContainer) {
      MPartSashContainer psc = (MPartSashContainer) children.get(0);
      psc.getChildren().add(stack);
    } else {
      // We need a sash so 'insert' the new stack
      modelService.insert(
          stack, (MPartSashContainerElement) children.get(0), EModelService.RIGHT_OF, 0.5f);
    }
    return stack;
  }
  public void testCreateView() {
    final MWindow window = createWindowWithOneView("Part Name");

    MApplication application = ApplicationFactoryImpl.eINSTANCE.createApplication();
    application.getChildren().add(window);
    application.setContext(appContext);
    appContext.set(MApplication.class.getName(), application);

    wb = new E4Workbench(application, appContext);
    wb.createAndRunUI(window);

    MPartSashContainer container = (MPartSashContainer) window.getChildren().get(0);
    MPartStack stack = (MPartStack) container.getChildren().get(0);
    MPart part = (MPart) stack.getChildren().get(0);

    CTabFolder folder = (CTabFolder) stack.getWidget();
    CTabItem item = folder.getItem(0);
    assertEquals("Part Name", item.getText());

    assertFalse(part.isDirty());

    part.setDirty(true);
    assertEquals("*Part Name", item.getText());

    part.setDirty(false);
    assertEquals("Part Name", item.getText());
  }
 private MPart getContributedPart(MWindow window) {
   MPartSashContainer psc = (MPartSashContainer) window.getChildren().get(0);
   MPartStack stack = (MPartStack) psc.getChildren().get(0);
   MPart part = (MPart) stack.getChildren().get(0);
   assertTrue("part is incorrect type " + part, part instanceof MPart);
   return part;
 }
 /**
  * If this window has a primary perspective stack, return it. Otherwise return null. A primary
  * stack is a single MPerspectiveStack found either as the window's immediate children or the
  * child under a single MPartSashContainer.
  *
  * @param window the window
  * @return the stack or {@code null}
  */
 private MPerspectiveStack getPrimaryPerspectiveStack(MWindow window) {
   List<MWindowElement> winKids = window.getChildren();
   if (winKids.isEmpty()) {
     return null;
   }
   // Check if we have a MPerspectiveStack in window's children
   if (instanceCount(winKids, MPerspectiveStack.class) == 1) {
     return firstInstance(winKids, MPerspectiveStack.class);
   }
   // Traditional shape of IWorkbenchWindow:
   // MWindow{ MPartSashContainer{ MPerspectiveStack, MPartStack (intro
   // + cheatsheets + help)}}
   if (winKids.size() == 1 && winKids.get(0) instanceof MPartSashContainer) {
     MPartSashContainer topLevelPSC = (MPartSashContainer) winKids.get(0);
     if (instanceCount(topLevelPSC.getChildren(), MPerspectiveStack.class) == 1) {
       return firstInstance(topLevelPSC.getChildren(), MPerspectiveStack.class);
     }
   }
   return null;
 }
  @Test
  public void testBug310027() {
    MApplication application = ApplicationFactoryImpl.eINSTANCE.createApplication();
    MWindow window = BasicFactoryImpl.eINSTANCE.createWindow();
    MPartSashContainer container = BasicFactoryImpl.eINSTANCE.createPartSashContainer();
    MPartStack partStackA = BasicFactoryImpl.eINSTANCE.createPartStack();
    MPartStack partStackB = BasicFactoryImpl.eINSTANCE.createPartStack();
    MPart partA = BasicFactoryImpl.eINSTANCE.createPart();
    MPart partB = BasicFactoryImpl.eINSTANCE.createPart();

    window.setWidth(600);
    window.setHeight(400);

    partStackA.setContainerData("50");
    partStackB.setContainerData("50");

    application.getChildren().add(window);
    application.setSelectedElement(window);

    window.getChildren().add(container);
    window.setSelectedElement(container);

    container.getChildren().add(partStackA);
    container.getChildren().add(partStackB);
    container.setSelectedElement(partStackA);

    partStackA.getChildren().add(partA);
    partStackA.setSelectedElement(partA);
    partStackA.getChildren().add(partB);
    partStackA.setSelectedElement(partB);

    application.setContext(appContext);
    appContext.set(MApplication.class, application);

    wb = new E4Workbench(application, appContext);
    wb.createAndRunUI(window);

    assertEquals("50", partStackA.getContainerData());
    assertEquals("50", partStackB.getContainerData());

    partStackB.setToBeRendered(false);

    while (Display.getDefault().readAndDispatch()) ;

    assertEquals("50", partStackA.getContainerData());
  }
  private MWindow createWindowWithOneView(String partName) {
    final MWindow window = BasicFactoryImpl.eINSTANCE.createWindow();
    window.setHeight(300);
    window.setWidth(400);
    window.setLabel("MyWindow");
    MPartSashContainer sash = BasicFactoryImpl.eINSTANCE.createPartSashContainer();
    window.getChildren().add(sash);
    MPartStack stack = BasicFactoryImpl.eINSTANCE.createPartStack();
    sash.getChildren().add(stack);
    MPart contributedPart = BasicFactoryImpl.eINSTANCE.createPart();
    stack.getChildren().add(contributedPart);
    contributedPart.setLabel(partName);
    contributedPart.setContributionURI(
        "bundleclass://org.eclipse.e4.ui.tests/org.eclipse.e4.ui.tests.workbench.SampleView");

    return window;
  }
  @Before
  public void setUp() throws Exception {
    appContext = E4Application.createDefaultContext();
    appContext.set(E4Workbench.PRESENTATION_URI_ARG, PartRenderingEngine.engineURI);

    ems = appContext.get(EModelService.class);

    window = ems.createModelElement(MWindow.class);
    window.setWidth(500);
    window.setHeight(500);

    MPartSashContainer sash = ems.createModelElement(MPartSashContainer.class);
    window.getChildren().add(sash);
    window.setSelectedElement(sash);

    MPartStack stack = ems.createModelElement(MPartStack.class);
    sash.getChildren().add(stack);
    sash.setSelectedElement(stack);

    part = ems.createModelElement(MPart.class);
    part.setElementId("Part");
    part.setLabel("Part");
    part.setToolbar(ems.createModelElement(MToolBar.class));
    part.setContributionURI(Activator.asURI(PartBackend.class));
    stack.getChildren().add(part);

    toolControl = ems.createModelElement(MToolControl.class);
    toolControl.setElementId("ToolControl");
    toolControl.setContributionURI(Activator.asURI(TextField.class));
    part.getToolbar().getChildren().add(toolControl);

    stack = ems.createModelElement(MPartStack.class);
    sash.getChildren().add(stack);
    sash.setSelectedElement(stack);

    otherPart = ems.createModelElement(MPart.class);
    otherPart.setElementId("OtherPart");
    otherPart.setLabel("OtherPart");
    otherPart.setContributionURI(Activator.asURI(PartBackend.class));
    stack.getChildren().add(otherPart);

    MApplication application = ems.createModelElement(MApplication.class);
    application.getChildren().add(window);
    application.setContext(appContext);
    appContext.set(MApplication.class, application);

    wb = new E4Workbench(application, appContext);
    wb.createAndRunUI(window);

    eps = window.getContext().get(EPartService.class);
    // ensure the parts are populated and the contributions instantiated
    eps.activate(part);
    eps.activate(otherPart);
    processEvents();

    // ensure our model backend objects are created
    assertNotNull(part.getObject());
    assertNotNull(toolControl.getObject());
    assertNotNull(otherPart.getObject());

    assertNotNull(part.getWidget());
    assertNotNull(toolControl.getWidget());
    assertNotNull(otherPart.getWidget());

    // ensure focus is set to otherPart.text1
    eps.activate(otherPart);
    processEvents();
    assertTrue(((PartBackend) otherPart.getObject()).text1.isFocusControl());
  }