private void resetPerspectiveModel(
      MPerspective persp, MWindow window, boolean removeSharedPlaceholders) {
    if (persp == null) {
      return;
    }

    if (removeSharedPlaceholders) {
      // Remove any views (Placeholders) from the shared area
      EPartService ps = window.getContext().get(EPartService.class);
      List<MArea> areas = findElements(window, null, MArea.class, null);
      if (areas.size() == 1) {
        MArea area = areas.get(0);

        // Strip out the placeholders in visible stacks
        List<MPlaceholder> phList = findElements(area, null, MPlaceholder.class, null);
        for (MPlaceholder ph : phList) {
          ps.hidePart((MPart) ph.getRef());
          ph.getParent().getChildren().remove(ph);
        }

        // Prevent shared stacks ids from clashing with the ones in the perspective
        List<MPartStack> stacks = findElements(area, null, MPartStack.class, null);
        for (MPartStack stack : stacks) {
          String generatedId = "PartStack@" + Integer.toHexString(stack.hashCode()); // $NON-NLS-1$
          stack.setElementId(generatedId);
        }

        // Also remove any min/max tags on the area (or its placeholder)
        MUIElement areaPresentation = area;
        if (area.getCurSharedRef() != null) {
          areaPresentation = area.getCurSharedRef();
        }

        areaPresentation.getTags().remove(IPresentationEngine.MAXIMIZED);
        areaPresentation.getTags().remove(IPresentationEngine.MINIMIZED);
        areaPresentation.getTags().remove(IPresentationEngine.MINIMIZED_BY_ZOOM);
      }
    }

    // Remove any minimized stacks for this perspective
    List<MTrimBar> bars = findElements(window, null, MTrimBar.class, null);
    List<MToolControl> toRemove = new ArrayList<>();
    for (MTrimBar bar : bars) {
      for (MUIElement barKid : bar.getChildren()) {
        if (!(barKid instanceof MToolControl)) {
          continue;
        }
        String id = barKid.getElementId();
        if (id != null && id.contains(persp.getElementId())) {
          toRemove.add((MToolControl) barKid);
        }
      }
    }

    for (MToolControl toolControl : toRemove) {
      // Close any open fast view
      toolControl.setToBeRendered(false);
      toolControl.getParent().getChildren().remove(toolControl);
    }
  }
 @Ignore
 @Test
 public void XXXtestNoFocusChangeOnExplicitWidgetSelection() {
   assertFalse(((PartBackend) part.getObject()).text1.isFocusControl());
   ((TextField) toolControl.getObject()).text.setFocus();
   processEvents();
   assertEquals(part.getElementId(), eps.getActivePart().getElementId());
   assertFalse(((PartBackend) part.getObject()).text1.isFocusControl());
   assertTrue(((TextField) toolControl.getObject()).text.isFocusControl());
 }
 /**
  * @param parentManager
  * @param itemModel
  */
 private void processToolControl(ToolBarManager parentManager, MToolControl itemModel) {
   IContributionItem ici = getContribution(itemModel);
   if (ici != null) {
     return;
   }
   final IEclipseContext lclContext = getContext(itemModel);
   ToolControlContribution ci =
       ContextInjectionFactory.make(ToolControlContribution.class, lclContext);
   ci.setModel(itemModel);
   ci.setVisible(itemModel.isVisible());
   addToManager(parentManager, itemModel, ci);
   linkModelToContribution(itemModel, ci);
 }
  @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());
  }
  @PostConstruct
  void createWidget(Composite parent, MToolControl toolControl) {
    psME = toolControl;
    MUIElement meParent = psME.getParent();
    int orientation = SWT.HORIZONTAL;
    if (meParent instanceof MTrimBar) {
      MTrimBar bar = (MTrimBar) meParent;
      if (bar.getSide() == SideValue.RIGHT || bar.getSide() == SideValue.LEFT)
        orientation = SWT.VERTICAL;
    }
    comp = new Composite(parent, SWT.NONE);
    RowLayout layout = new RowLayout(SWT.HORIZONTAL);
    layout.marginLeft = layout.marginRight = 8;
    layout.marginBottom = 4;
    layout.marginTop = 6;
    comp.setLayout(layout);
    psTB = new ToolBar(comp, SWT.FLAT | SWT.WRAP | SWT.RIGHT + orientation);
    comp.addPaintListener(
        new PaintListener() {

          public void paintControl(PaintEvent e) {
            paint(e);
          }
        });
    toolParent = ((Control) toolControl.getParent().getWidget());
    toolParent.addPaintListener(
        new PaintListener() {

          public void paintControl(PaintEvent e) {
            if (borderColor == null) borderColor = e.display.getSystemColor(SWT.COLOR_BLACK);
            e.gc.setForeground(borderColor);
            Rectangle bounds = ((Control) e.widget).getBounds();
            e.gc.drawLine(0, bounds.height - 1, bounds.width, bounds.height - 1);
          }
        });

    comp.addDisposeListener(
        new DisposeListener() {
          public void widgetDisposed(DisposeEvent e) {
            dispose();
          }
        });

    psTB.addMenuDetectListener(
        new MenuDetectListener() {
          public void menuDetected(MenuDetectEvent e) {
            ToolBar tb = (ToolBar) e.widget;
            Point p = new Point(e.x, e.y);
            p = psTB.getDisplay().map(null, psTB, p);
            ToolItem item = tb.getItem(p);
            if (item == null) E4Util.message("  ToolBar menu"); // $NON-NLS-1$
            else {
              MPerspective persp = (MPerspective) item.getData();
              if (persp == null) E4Util.message("  Add button Menu"); // $NON-NLS-1$
              else openMenuFor(item, persp);
            }
          }
        });

    psTB.addDisposeListener(
        new DisposeListener() {
          public void widgetDisposed(DisposeEvent e) {
            disposeTBImages();
          }
        });

    psTB.getAccessible()
        .addAccessibleListener(
            new AccessibleAdapter() {
              public void getName(AccessibleEvent e) {
                if (0 <= e.childID && e.childID < psTB.getItemCount()) {
                  ToolItem item = psTB.getItem(e.childID);
                  if (item != null) {
                    e.result = item.getToolTipText();
                  }
                }
              }
            });

    hookupDnD(psTB);

    final ToolItem createItem = new ToolItem(psTB, SWT.PUSH);
    createItem.setImage(getOpenPerspectiveImage());
    createItem.setToolTipText(WorkbenchMessages.OpenPerspectiveDialogAction_tooltip);
    createItem.addSelectionListener(
        new SelectionListener() {
          public void widgetSelected(SelectionEvent e) {
            selectPerspective();
          }

          public void widgetDefaultSelected(SelectionEvent e) {
            selectPerspective();
          }
        });
    new ToolItem(psTB, SWT.SEPARATOR);

    MPerspectiveStack stack = getPerspectiveStack();
    if (stack != null) {
      // Create an item for each perspective that should show up
      for (MPerspective persp : stack.getChildren()) {
        if (persp.isToBeRendered()) {
          addPerspectiveItem(persp);
        }
      }
    }
  }