/*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands
   * .ExecutionEvent)
   */
  public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPart part = HandlerUtil.getActivePart(event);
    if (part != null) {
      IWorkbenchPartSite site = part.getSite();
      if (site instanceof PartSite) {
        final MPart model = ((PartSite) site).getModel();
        Composite partContainer = (Composite) model.getWidget();
        if (partContainer != null) {
          Composite parent = partContainer.getParent();
          while (parent != null && parent instanceof Composite) {
            if (parent instanceof CTabFolder) {
              CTabFolder ctf = (CTabFolder) parent;
              final Control topRight = ctf.getTopRight();
              if (topRight instanceof Composite) {
                for (Control child : ((Composite) topRight).getChildren()) {
                  if (child instanceof ToolBar
                      && "ViewMenu".equals(child.getData())) { // $NON-NLS-1$
                    ToolBar tb = (ToolBar) child;
                    ToolItem ti = tb.getItem(0);
                    Event sevent = new Event();
                    sevent.type = SWT.Selection;
                    sevent.widget = ti;
                    ti.notifyListeners(SWT.Selection, sevent);
                  }
                }
              }
              return null;
            }
            parent = parent.getParent();
          }

          MMenu menuModel = StackRenderer.getViewMenu(model);
          if (menuModel != null) {
            showStandaloneViewMenu(event, model, menuModel, partContainer);
          }
        }
      }
    }
    return null;
  }
  @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());
  }
  public static void setDescription(MPart part, String description) {
    if (!(part.getWidget() instanceof Composite)) return;

    Composite c = (Composite) part.getWidget();

    // Do we already have a label?
    if (c.getChildren().length == 3) {
      Label label = (Label) c.getChildren()[0];
      if (description == null) description = ""; // $NON-NLS-1$
      // hide the label if there is no text to show
      boolean hasText = !description.equals(""); // $NON-NLS-1$
      label.setVisible(hasText);
      label.setText(description);
      label.setToolTipText(description);

      // also hide the separator
      c.getChildren()[1].setVisible(hasText);
      c.layout();
    } else if (c.getChildren().length == 1) {
      c.setLayout(
          new Layout() {

            @Override
            protected Point computeSize(
                Composite composite, int wHint, int hHint, boolean flushCache) {
              return new Point(0, 0);
            }

            @Override
            protected void layout(Composite composite, boolean flushCache) {
              Rectangle bounds = composite.getBounds();
              if (composite.getChildren().length == 1) {
                composite.getChildren()[0].setBounds(composite.getBounds());
              } else if (composite.getChildren().length == 3) {
                Label label = (Label) composite.getChildren()[0];
                Label separator = (Label) composite.getChildren()[1];
                Control partCtrl = composite.getChildren()[2];

                // if the label is not visible, give it a zero size
                int labelHeight =
                    label.isVisible() ? label.computeSize(bounds.width, SWT.DEFAULT).y : 0;
                label.setBounds(0, 0, bounds.width, labelHeight);

                int separatorHeight =
                    separator.isVisible() ? separator.computeSize(bounds.width, SWT.DEFAULT).y : 0;
                separator.setBounds(0, labelHeight, bounds.width, separatorHeight);

                partCtrl.setBounds(
                    0,
                    labelHeight + separatorHeight,
                    bounds.width,
                    bounds.height - labelHeight - separatorHeight);
              }
            }
          });

      Label separator = new Label(c, SWT.SEPARATOR | SWT.HORIZONTAL);
      separator.moveAbove(null);
      Label label = new Label(c, SWT.NONE);
      label.setText(description);
      label.setToolTipText(description);
      label.moveAbove(null);
      c.layout();
    }
  }