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;
 }
Exemplo n.º 5
0
  protected Command computeCommand() {
    MUIElement selectedPart = model;

    if (selectedPart instanceof MPart) {
      MGenericStack<MUIElement> partStack = findParentStack();
      if (partStack != null) {
        selectedPart = partStack;
      }
    }

    MElementContainer<MUIElement> parent = partStack.getParent();
    List<MUIElement> children = parent.getChildren();
    int index = children.indexOf(partStack);
    if (parent instanceof MGenericTile<?>) {
      MGenericTile<?> genericTile = (MGenericTile<?>) parent;
      int modelIndex = children.indexOf(selectedPart);
      if (modelIndex == 0 && index == 1 && children.size() == 2 && genericTile.isHorizontal()) {
        return UnexecutableCommand.INSTANCE;
      }
    }

    CompoundCommand result = new CompoundCommand();

    MPartSashContainer newSash = MBasicFactory.INSTANCE.createPartSashContainer();
    String preferData = partStack.getContainerData();
    newSash.setContainerData(preferData);
    newSash.setHorizontal(true);

    if (selectedPart instanceof MPartStack) {
      if (selectedPart.getParent() == null) {
        selectedPart.setContainerData(preferData);
        result.add(CommandFactory.createAddChildCommand(newSash, selectedPart, 0));
      } else {
        result.add(new ChangeParentCommand(newSash, selectedPart, 0));
        if (!preferData.equals(selectedPart.getContainerData())) {
          result.add(
              new ApplyAttributeSettingCommand(
                  (EObject) selectedPart, "containerData", preferData));
        }
      }
    } else if (selectedPart instanceof MPart) {
      MPart part = (MPart) selectedPart;
      MPartStack createPartStack = MBasicFactory.INSTANCE.createPartStack();
      createPartStack.setContainerData(preferData);
      if (part.getParent() != null) {
        result.add(new ChangeParentCommand(createPartStack, part, 0));
      } else {
        result.add(CommandFactory.createAddChildCommand(createPartStack, part, 0));
      }
      result.add(CommandFactory.createAddChildCommand(newSash, createPartStack, 0));
    }

    result.add(new ChangeParentCommand(newSash, partStack, 1));

    result.add(CommandFactory.createAddChildCommand(parent, newSash, index));
    return result.unwrap();
  }
  @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;
  }
 /**
  * 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;
 }
  @Override
  public void insert(
      MPartSashContainerElement toInsert, MPartSashContainerElement relTo, int where, float ratio) {
    assert (toInsert != null && relTo != null);
    if (ratio >= 1) {
      warn("EModelService#insert() expects the ratio to be between (0,100)"); // $NON-NLS-1$
      ratio = ratio / 100; // reduce it
    }
    assert (ratio > 0 && ratio < 1);

    // determine insertion order
    boolean insertBefore = where == ABOVE || where == LEFT_OF;
    boolean horizontal = where == LEFT_OF || where == RIGHT_OF;

    MPartSashContainer newSash = BasicFactoryImpl.eINSTANCE.createPartSashContainer();
    newSash.setHorizontal(horizontal);

    // Maintain the existing weight in the new sash
    newSash.setContainerData(relTo.getContainerData());

    combine(toInsert, relTo, newSash, insertBefore, ratio);
  }
Exemplo n.º 10
0
  @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());
  }