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);
    }
  }
  @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;
  }
 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 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);
  }
  @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);
  }
 @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);
   }
 }
  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 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);
             }
           });
 }
  private void activate(MPart part, boolean requiresFocus, boolean activateBranch) {
    if (part == null) {
      if (constructed && activePart != null) {
        firePartDeactivated(activePart);
      }
      activePart = part;
      return;
    }

    // Delegate activations to a CompositePart's inner part (if any)
    if (part instanceof MCompositePart) {
      if (part.getContext() != null) {
        IEclipseContext pContext = part.getContext();
        if (pContext.getActiveLeaf() != null) {
          MPart inner = pContext.getActiveLeaf().get(MPart.class);
          if (inner != null) {
            part = inner;
          }
        }
      }
    }

    // only activate parts that is under our control
    if (!isInContainer(part)) {
      return;
    }

    MWindow window = getWindow();
    IEclipseContext windowContext = window.getContext();
    // check if the active part has changed or if we are no longer the active window
    if (windowContext.getParent().getActiveChild() == windowContext && part == activePart) {
      // insert it in the beginning of the activation history, it may not have been inserted
      // pending when this service was instantiated
      partActivationHistory.prepend(part);
      UIEvents.publishEvent(UIEvents.UILifeCycle.ACTIVATE, part);
      return;
    }
    if (contextService != null) {
      contextService.deferUpdates(true);
    }
    if (contextManager != null) {
      contextManager.deferUpdates(true);
    }

    MPart lastActivePart = activePart;
    activePart = part;

    if (constructed && lastActivePart != null && lastActivePart != activePart) {
      firePartDeactivated(lastActivePart);
    }

    try {
      // record any sibling into the activation history if necessary, this will allow it to be
      // reselected again in the future as it will be an activation candidate in the future,
      // this
      // prevents other unrendered elements from being selected arbitrarily which would cause
      // unwanted bundle activation
      recordStackActivation(part);

      delegateBringToTop(part);
      window.getParent().setSelectedElement(window);

      partActivationHistory.activate(part, activateBranch);

      if (requiresFocus) {
        IPresentationEngine pe = part.getContext().get(IPresentationEngine.class);
        pe.focusGui(part);
      }

      firePartActivated(part);
      UIEvents.publishEvent(UIEvents.UILifeCycle.ACTIVATE, part);
    } finally {
      if (contextService != null) {
        contextService.deferUpdates(false);
      }
      if (contextManager != null) {
        contextManager.deferUpdates(false);
      }
    }
  }
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());
  }