@Override
  public Object compute(IEclipseContext context, String contextKey) {
    // look for the top-most MWindow in the context chain:

    // 1st: go up the tree to find topmost MWindow
    MWindow window = null;
    IEclipseContext current = context;
    do {
      MContext model = current.get(MContext.class);
      if (model instanceof MWindow) window = (MWindow) model;
      current = current.getParent();
    } while (current != null);

    if (window == null) {
      if (context.get(MApplication.class) != null) {
        // called from Application scope
        return ContextInjectionFactory.make(ApplicationPartServiceImpl.class, context);
      }
      return IInjector.NOT_A_VALUE;
    }

    IEclipseContext windowContext = window.getContext();
    PartServiceImpl service = windowContext.getLocal(PartServiceImpl.class);
    if (service == null) {
      service = ContextInjectionFactory.make(PartServiceImpl.class, windowContext);
      windowContext.set(PartServiceImpl.class, service);
    }
    return service;
  }
  @Override
  public void processContents(MElementContainer<MUIElement> element) {
    if (!(((MUIElement) element) instanceof MWindow)) {
      return;
    }
    MWindow mWindow = (MWindow) ((MUIElement) element);
    Shell shell = (Shell) element.getWidget();

    // Populate the main menu
    IPresentationEngine2 renderer =
        (IPresentationEngine2) context.get(IPresentationEngine.class.getName());
    if (mWindow.getMainMenu() != null) {
      renderer.createGui(mWindow.getMainMenu(), element);
      shell.setMenuBar((Menu) mWindow.getMainMenu().getWidget());
    }

    // create Detached Windows
    for (MWindow dw : mWindow.getWindows()) {
      renderer.createGui(dw, element);
    }

    // Populate the trim (if any)
    if (mWindow instanceof MTrimmedWindow) {
      MTrimmedWindow tWindow = (MTrimmedWindow) mWindow;
      for (MTrimBar trimBar : tWindow.getTrimBars()) {
        renderer.createGui(trimBar, element);
      }
    }

    shell.pack();
    shell.open();
  }
  /*
   * Action implementation for the toolbar button
   */
  @SuppressWarnings("restriction")
  void toolbarButtonClicked(NSControl source) {
    try {
      NSWindow window = source.window();
      Field idField = NSWindow.class.getField("id"); // $NON-NLS-1$
      Object idValue = idField.get(window);

      Display display = Display.getCurrent();
      Widget widget =
          (Widget)
              invokeMethod(
                  Display.class, display, "findWidget", new Object[] {idValue}); // $NON-NLS-1$

      if (!(widget instanceof Shell)) {
        return;
      }
      Shell shell = (Shell) widget;
      for (MWindow mwin : app.getChildren()) {
        if (mwin.getWidget() == shell) {
          if (!runCommand(COMMAND_ID_TOGGLE_COOLBAR)) {
            // there may be a menu item to do the toggle...
            runAction(COMMAND_ID_TOGGLE_COOLBAR);
          }
        }
      }
    } catch (Exception e) {
      // theoretically, one of
      // SecurityException,Illegal*Exception,InvocationTargetException,NoSuch*Exception
      // not expected to happen at all.
      log(e);
    }
  }
  @Override
  public int countRenderableChildren(MUIElement element) {
    if (!(element instanceof MElementContainer<?>)) {
      return 0;
    }

    @SuppressWarnings("unchecked")
    MElementContainer<MUIElement> container = (MElementContainer<MUIElement>) element;
    int count = 0;
    List<MUIElement> kids = container.getChildren();
    for (MUIElement kid : kids) {
      if (kid.isToBeRendered()) {
        count++;
      }
    }

    if (element instanceof MPerspective) {
      MPerspective perspective = (MPerspective) element;
      for (MWindow window : perspective.getWindows()) {
        if (window.isToBeRendered()) {
          count++;
        }
      }
    }
    return count;
  }
  public void testCreateMenu() {
    final MWindow window = createWindowWithOneViewAndMenu();
    wb = new E4Workbench(window, appContext);

    Widget topWidget = (Widget) window.getWidget();
    assertTrue(topWidget instanceof Shell);
    Shell shell = (Shell) topWidget;
    final Menu menuBar = shell.getMenuBar();
    assertNotNull(menuBar);
    assertEquals(1, menuBar.getItemCount());
    final MenuItem fileItem = menuBar.getItem(0);
    assertEquals("File", fileItem.getText());
    final Menu fileMenu = fileItem.getMenu();
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(2, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);

    MMenu mainMenu = window.getMainMenu();
    MMenu modelFileMenu = (MMenu) mainMenu.getChildren().get(0);
    final MMenuItem item2Model = (MMenuItem) modelFileMenu.getChildren().get(0);
    item2Model.setToBeRendered(false);
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(1, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);

    item2Model.setToBeRendered(true);
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(2, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);
  }
  @Override
  public void detach(MPartSashContainerElement element, int x, int y, int width, int height) {
    // If we're showing through a placehoilder then detach it...
    if (element.getCurSharedRef() != null) {
      element = element.getCurSharedRef();
    }

    // Determine the correct parent for the new window
    MWindow window = getTopLevelWindowFor(element);
    MPerspective thePersp = getPerspectiveFor(element);

    MTrimmedWindow newWindow = MBasicFactory.INSTANCE.createTrimmedWindow();

    newWindow.setX(x);
    newWindow.setY(y);
    newWindow.setWidth(width);
    newWindow.setHeight(height);

    element.getParent().getChildren().remove(element);
    MWindowElement uiRoot = wrapElementForWindow(element);
    newWindow.getChildren().add(uiRoot);

    if (thePersp != null) {
      thePersp.getWindows().add(newWindow);
    } else if (window != null) {
      window.getWindows().add(newWindow);
    }
  }
  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());
  }
  @Override
  public MPlaceholder createSharedPart(String id, boolean force) {
    MWindow sharedWindow = getWindow();
    // Do we already have the part to share?
    MPart sharedPart = null;

    // check for existing parts if necessary
    if (!force) {
      for (MUIElement element : sharedWindow.getSharedElements()) {
        if (element.getElementId().equals(id)) {
          sharedPart = (MPart) element;
          break;
        }
      }
    }

    if (sharedPart == null) {
      MPartDescriptor descriptor = modelService.getPartDescriptor(id);
      sharedPart = createPart(descriptor);
      if (sharedPart == null) {
        return null;
      }

      // Replace the id to ensure that multi-instance parts work correctly
      sharedPart.setElementId(id);

      sharedWindow.getSharedElements().add(sharedPart);
    }

    return createSharedPart(sharedPart);
  }
 protected boolean someAreVisible(List<MWindow> windows) {
   for (MWindow win : windows) {
     if (win.isToBeRendered() && win.isVisible() && win.getWidget() != null) {
       return true;
     }
   }
   return false;
 }
 /** Process defined windows and menu contributions */
 protected void processModelMenus() {
   for (MWindow window : app.getChildren()) {
     redirectHandledMenuItems(window.getMainMenu());
   }
   for (MMenuContribution contribution : app.getMenuContributions()) {
     processMenuContribution(contribution);
   }
 }
  private void closePerspective(MPerspective persp) {
    MWindow win = modelService.getTopLevelWindowFor(persp);
    WorkbenchPage page = (WorkbenchPage) win.getContext().get(IWorkbenchPage.class);
    String perspectiveId = persp.getElementId();
    IPerspectiveDescriptor desc = getDescriptorFor(perspectiveId);
    page.closePerspective(desc, perspectiveId, true, false);

    // removePerspectiveItem(persp);
  }
  public void testCreateWindow() {
    final MWindow window = BasicFactoryImpl.eINSTANCE.createWindow();
    window.setLabel("MyWindow");
    wb = new E4Workbench(window, appContext);

    Widget topWidget = (Widget) window.getWidget();
    assertTrue(topWidget instanceof Shell);
    assertEquals("MyWindow", ((Shell) topWidget).getText());
    assertEquals(topWidget, appContext.get(IServiceConstants.ACTIVE_SHELL));
  }
  @Override
  public void hostElement(
      MUIElement element, MWindow hostWindow, Object uiContainer, IEclipseContext hostContext) {
    // This is subtle; unless the element is hooked into the model it won't fire events
    hostWindow.getSharedElements().add(element);
    element.getTags().add(HOSTED_ELEMENT);

    IPresentationEngine renderer = hostWindow.getContext().get(IPresentationEngine.class);
    renderer.createGui(element, uiContainer, hostContext);
  }
  /**
   * Locate an action (a menu item, actually) with the given id in the current menu bar and run it.
   *
   * @param actionId the action to find
   * @return true if an action was found, false otherwise
   */
  private boolean runAction(String actionId) {
    MWindow window = app.getSelectedElement();
    if (window == null) {
      return false;
    }
    MMenu topMenu = window.getMainMenu();
    MMenuItem item = findAction(actionId, topMenu);
    if (item == null || !item.isEnabled()) {
      return false;
    }
    try {
      // disable the about and prefs items -- they shouldn't be
      // able to be run when another item is being triggered
      final Display display = Display.getDefault();
      MenuItem aboutItem = null;
      boolean aboutEnabled = true;
      MenuItem prefsItem = null;
      boolean prefsEnabled = true;

      Menu appMenuBar = display.getMenuBar();
      if (appMenuBar != null) {
        aboutItem = findMenuItemById(appMenuBar, SWT.ID_ABOUT);
        if (aboutItem != null) {
          aboutEnabled = aboutItem.getEnabled();
          aboutItem.setEnabled(false);
        }
        prefsItem = findMenuItemById(appMenuBar, SWT.ID_PREFERENCES);
        if (prefsItem != null) {
          prefsEnabled = prefsItem.getEnabled();
          prefsItem.setEnabled(false);
        }
      }
      try {
        simulateMenuSelection(item);
      } finally {
        if (prefsItem != null) {
          prefsItem.setEnabled(prefsEnabled);
        }
        if (aboutItem != null) {
          aboutItem.setEnabled(aboutEnabled);
        }
      }
    } catch (Exception e) {
      // theoretically, one of
      // SecurityException,Illegal*Exception,InvocationTargetException,NoSuch*Exception
      // not expected to happen at all.
      log(e);
      // return false?
    }
    return true;
  }
 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;
 }
  @Override
  public MUIElement cloneSnippet(
      MSnippetContainer snippetContainer, String snippetId, MWindow refWin) {
    if (snippetContainer == null || snippetId == null || snippetId.length() == 0) {
      return null;
    }

    MApplicationElement elementToClone = null;
    for (MApplicationElement snippet : snippetContainer.getSnippets()) {
      if (snippetId.equals(snippet.getElementId())) {
        elementToClone = snippet;
        break;
      }
    }
    if (elementToClone == null) {
      return null;
    }

    EObject eObj = (EObject) elementToClone;
    MUIElement element = (MUIElement) EcoreUtil.copy(eObj);

    MUIElement appElement = refWin == null ? null : refWin.getParent();
    if (appElement instanceof MApplication) {
      handleNullRefPlaceHolders(element, refWin, true);
    }

    return element;
  }
 private void addResetItem(final Menu menu) {
   final MenuItem resetMenuItem = new MenuItem(menu, SWT.Activate);
   resetMenuItem.setText(WorkbenchMessages.PerspectiveBar_reset);
   final IWorkbenchWindow workbenchWindow = window.getContext().get(IWorkbenchWindow.class);
   workbenchWindow
       .getWorkbench()
       .getHelpSystem()
       .setHelp(resetMenuItem, IWorkbenchHelpContextIds.RESET_PERSPECTIVE_ACTION);
   resetMenuItem.addSelectionListener(
       new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
           if (psTB.isDisposed()) return;
           IHandlerService handlerService =
               (IHandlerService) workbenchWindow.getService(IHandlerService.class);
           IStatus status = Status.OK_STATUS;
           try {
             handlerService.executeCommand(
                 IWorkbenchCommandConstants.WINDOW_RESET_PERSPECTIVE, null);
           } catch (ExecutionException e) {
             status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, e.getMessage(), e);
           } catch (NotDefinedException e) {
             status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, e.getMessage(), e);
           } catch (NotEnabledException e) {
             status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, e.getMessage(), e);
           } catch (NotHandledException e) {
           }
           if (!status.isOK())
             StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.LOG);
         }
       });
 }
  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);
    }
  }
  boolean isInContainer(MElementContainer<?> container, MUIElement element) {
    for (Object object : container.getChildren()) {
      if (object == element) {
        return true;
      } else if (object instanceof MElementContainer<?>) {
        if (isInContainer((MElementContainer<?>) object, element)) {
          return true;
        }
      } else if (object instanceof MPlaceholder) {
        MUIElement ref = ((MPlaceholder) object).getRef();
        if (ref == element) {
          return true;
        } else if (ref instanceof MElementContainer<?>) {
          if (isInContainer((MElementContainer<?>) ref, element)) {
            return true;
          }
        }
      } else if (object instanceof MPerspective) {
        MPerspective persp = (MPerspective) object;
        for (MWindow dw : persp.getWindows()) {
          if (isInContainer(dw, element)) return true;
        }
      } else if (object instanceof MWindow) {
        MWindow win = (MWindow) object;
        for (MWindow dw : win.getWindows()) {
          if (isInContainer(dw, element)) return true;
        }
      }
    }

    if (container instanceof MWindow) {
      MWindow win = (MWindow) container;
      for (MWindow dw : win.getWindows()) {
        if (isInContainer(dw, element)) return true;
      }
    }

    if (container instanceof MPerspective) {
      MPerspective persp = (MPerspective) container;
      for (MWindow dw : persp.getWindows()) {
        if (isInContainer(dw, element)) return true;
      }
    }

    return false;
  }
  @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;
  }
 public void stop() {
   // FIXME Without this call the test-suite fails
   cleanUp();
   if (theApp != null) {
     for (MWindow window : theApp.getChildren()) {
       if (window.getWidget() != null) {
         removeGui(window);
       }
     }
   } else if (testShell != null && !testShell.isDisposed()) {
     Object model = testShell.getData(AbstractPartRenderer.OWNING_ME);
     if (model instanceof MUIElement) {
       removeGui((MUIElement) model);
     } else {
       testShell.close();
     }
   }
 }
  @Override
  public void bringToTop(MUIElement element) {
    if (element instanceof MApplication) {
      return;
    }

    MWindow window = getTopLevelWindowFor(element);
    if (window == element) {
      if (!element.isToBeRendered()) {
        element.setToBeRendered(true);
      }

      window.getParent().setSelectedElement(window);
    } else {
      showElementInWindow(window, element);
    }
    UIEvents.publishEvent(UIEvents.UILifeCycle.BRINGTOTOP, element);
  }
  private MWindow createWindowWithOneViewAndMenu() {
    final MWindow window = createWindowWithOneView();
    final MMenu menuBar = MenuFactoryImpl.eINSTANCE.createMenu();
    window.setMainMenu(menuBar);
    final MMenu fileMenu = MenuFactoryImpl.eINSTANCE.createMenu();
    fileMenu.setLabel("File");
    fileMenu.setElementId("file");
    menuBar.getChildren().add(fileMenu);

    final MMenuItem item1 = MenuFactoryImpl.eINSTANCE.createDirectMenuItem();
    item1.setElementId("item1");
    item1.setLabel("item1");
    fileMenu.getChildren().add(item1);
    final MMenuItem item2 = MenuFactoryImpl.eINSTANCE.createDirectMenuItem();
    item2.setElementId("item2");
    item2.setLabel("item2");
    fileMenu.getChildren().add(item2);

    return window;
  }
        @Override
        public void handleEvent(Event event) {
          final MUIElement changedElement = (MUIElement) event.getProperty(EventTags.ELEMENT);
          if (!changedElement.getTags().contains(HOSTED_ELEMENT)) {
            return;
          }

          if (changedElement.getWidget() != null) {
            return;
          }

          EObject eObj = (EObject) changedElement;
          if (!(eObj.eContainer() instanceof MWindow)) {
            return;
          }

          MWindow hostingWindow = (MWindow) eObj.eContainer();
          hostingWindow.getSharedElements().remove(changedElement);
          changedElement.getTags().remove(HOSTED_ELEMENT);
        }
  public void testContextChildren() {
    final MWindow window = createWindowWithOneView();
    wb = new E4Workbench(window, appContext);

    Widget topWidget = (Widget) window.getWidget();
    assertTrue(topWidget instanceof Shell);
    Shell shell = (Shell) topWidget;
    assertEquals("MyWindow", shell.getText());

    // should get the window context
    IEclipseContext child = appContext.getActiveChild();
    assertNotNull(child);
    assertEquals(window.getContext(), child);

    MPart modelPart = getContributedPart(window);
    assertNotNull(modelPart);
    assertEquals(window, modelPart.getParent().getParent().getParent());

    // "activate" the part, same as (in theory) an
    // SWT.Activate event.
    AbstractPartRenderer factory = (AbstractPartRenderer) modelPart.getRenderer();
    factory.activate(modelPart);

    IEclipseContext next = child.getActiveChild();
    while (next != null) {
      child = next;
      next = child.getActiveChild();
      if (next == child) {
        fail("Cycle detected in part context");
        break;
      }
    }
    assertFalse(window.getContext() == child);

    MPart contextPart = (MPart) child.get(MPart.class.getName());

    assertNotNull(contextPart);
    assertEquals(window, contextPart.getParent().getParent().getParent());
  }
  public void testCreateView() {
    final MWindow window = createWindowWithOneView();
    wb = new E4Workbench(window, appContext);

    Widget topWidget = (Widget) window.getWidget();
    assertTrue(topWidget instanceof Shell);
    Shell shell = (Shell) topWidget;
    assertEquals("MyWindow", shell.getText());
    Control[] controls = shell.getChildren();
    assertEquals(1, controls.length);
    SashForm sash = (SashForm) controls[0];
    Control[] sashChildren = sash.getChildren();
    assertEquals(1, sashChildren.length);

    CTabFolder folder = (CTabFolder) sashChildren[0];
    assertEquals(1, folder.getItemCount());
    Control c = folder.getItem(0).getControl();
    assertTrue(c instanceof Composite);
    Control[] viewPart = ((Composite) c).getChildren();
    assertEquals(1, viewPart.length);
    assertTrue(viewPart[0] instanceof Tree);
  }
 @Override
 public void createWidget(MUIElement element, MElementContainer<MUIElement> parent) {
   if (element instanceof MWindow) {
     MWindow mWindow = (MWindow) element;
     Shell shell = new Shell(Display.getCurrent());
     shell.setText(mWindow.getLabel());
     shell.setLayout(new SimpleTrimLayout());
     shell.setBounds(mWindow.getX(), mWindow.getY(), mWindow.getWidth(), mWindow.getHeight());
     mWindow.getContext().set(Shell.class, shell);
     element.setWidget(shell);
   }
 }
  @Override
  public boolean isHostedElement(MUIElement element, MWindow hostWindow) {
    MUIElement curElement = element;
    while (curElement != null && !curElement.getTags().contains(HOSTED_ELEMENT)) {
      if (curElement.getCurSharedRef() != null) {
        curElement = curElement.getCurSharedRef();
      } else {
        curElement = curElement.getParent();
      }
    }

    if (curElement == null) {
      return false;
    }

    return hostWindow.getSharedElements().contains(curElement);
  }
 public static void initializeWindowServices(MWindow childWindow) {
   IEclipseContext windowContext = childWindow.getContext();
   initWindowContext(windowContext);
   // Mostly MWindow contexts are lazily created by renderers and is not
   // set at this point.
   ((EObject) childWindow)
       .eAdapters()
       .add(
           new AdapterImpl() {
             @Override
             public void notifyChanged(Notification notification) {
               if (notification.getFeatureID(MWindow.class) != BasicPackageImpl.WINDOW__CONTEXT)
                 return;
               IEclipseContext windowContext = (IEclipseContext) notification.getNewValue();
               initWindowContext(windowContext);
             }
           });
 }