/**
   * Modify the given workbench window shell bits to show the tool bar toggle button.
   *
   * @param window the window to modify
   * @since 3.2
   */
  @SuppressWarnings("restriction")
  protected void modifyWindowShell(MWindow window) {
    if (window.getWidget() == null) {
      return;
    }
    if (window.getMainMenu() == null) {
      return;
    }
    redirectHandledMenuItems(window.getMainMenu());

    // the toolbar button is not available since MacOS X 10.7
    if (OS.VERSION >= 0x1070) {
      return;
    }
    // only add the button when either the cool bar or perspective bar
    // is initially visible. This is so that RCP applications can choose to
    // use this fragment without fear that their explicitly invisible bars
    // can't be shown.
    boolean trimInitiallyVisible = false;
    if (window instanceof MTrimmedWindow && !((MTrimmedWindow) window).getTrimBars().isEmpty()) {
      for (MTrimBar tb : ((MTrimmedWindow) window).getTrimBars()) {
        if (tb.isVisible()) {
          trimInitiallyVisible = true;
        }
      }
    }

    // It would also be worth checking if there's a command defined
    // for COMMAND_ID_TOGGLE_COOLBAR
    if (trimInitiallyVisible) {
      Shell shell = ((Control) window.getWidget()).getShell();
      NSWindow nsWindow = shell.view.window();
      // Add an empty, hidden tool bar to the window. Without this the
      // tool bar button at the top right of the window will not
      // appear even when setShowsToolbarButton(true) is called.
      // Unfortunately cannot just call shell.getToolBar() as it
      // allocates a properly-sized toolbar
      NSToolbar dummyBar = new NSToolbar();
      dummyBar.alloc();
      dummyBar.initWithIdentifier(NSString.stringWith("SWTToolbar")); // $NON-NLS-1$
      dummyBar.setVisible(false);
      nsWindow.setToolbar(dummyBar);
      dummyBar.release();
      nsWindow.setShowsToolbarButton(true);

      // Override the target and action of the toolbar button so we can
      // control it.
      try {
        Object fieldValue = wrapPointer(NSWindowToolbarButton);
        NSButton toolbarButton =
            (NSButton)
                invokeMethod(
                    NSWindow.class,
                    nsWindow,
                    "standardWindowButton",
                    new Object[] {fieldValue}); // $NON-NLS-1$
        if (toolbarButton != null) {
          toolbarButton.setTarget(delegate);
          invokeMethod(
              NSControl.class,
              toolbarButton,
              "setAction", //$NON-NLS-1$
              new Object[] {wrapPointer(sel_toolbarButtonClicked_)});
        }
      } catch (Exception e) {
        // theoretically, one of
        // SecurityException,Illegal*Exception,InvocationTargetException,NoSuch*Exception
        // not expected to happen at all.
        log(e);
      }
    }
  }