/**
  * Choose Server Popup Menu with specified label on Server with position specified by index
  *
  * @param bot
  * @param index
  * @param menuLabel
  * @param timeOut
  * @param uiTimeOut
  */
 public static void chooseServerPopupMenu(
     SWTWorkbenchBot bot, int index, String menuLabel, long timeOut, long uiTimeOut) {
   SWTEclipseExt swtEclipseExt = new SWTEclipseExt();
   SWTBot servers = swtEclipseExt.showView(ViewType.SERVERS);
   SWTBotTree serverTree = servers.tree();
   ContextMenuHelper.prepareTreeItemForContextMenu(serverTree, index);
   SWTTestExt.util.waitForAll(uiTimeOut);
   SWTBotMenu menu =
       new SWTBotMenu(ContextMenuHelper.getContextMenu(serverTree, menuLabel, false));
   SWTTestExt.util.waitForAll(uiTimeOut);
   menu.click();
   SWTTestExt.util.waitForAll(timeOut);
 }
  /**
   * Delete Project from workspace
   *
   * @param projectName
   */
  public void deleteProject(String projectName) {

    removeProjectFromServers(projectName);

    SWTBot packageExplorer = eclipse.showView(ViewType.PACKAGE_EXPLORER);
    delay();
    SWTBotTree tree = packageExplorer.tree();
    delay();

    ContextMenuHelper.prepareTreeItemForContextMenu(tree, tree.getTreeItem(projectName));
    new SWTBotMenu(ContextMenuHelper.getContextMenu(tree, IDELabel.Menu.DELETE, false)).click();
    bot.shell(IDELabel.Shell.DELETE_RESOURCES).activate();
    bot.button(IDELabel.Button.OK).click();

    new SWTUtilExt(bot).waitForNonIgnoredJobs();
  }
  /**
   * Remove Project from all Servers
   *
   * @param projectName
   * @param stringToContain
   */
  public void removeProjectFromServers(String projectName, String stringToContain) {

    eclipse.showView(ViewType.SERVERS);

    delay();

    try {
      SWTBotTree serverTree = bot.viewByTitle(IDELabel.View.SERVERS).bot().tree();

      delay();

      // Expand All
      for (SWTBotTreeItem serverTreeItem : serverTree.getAllItems()) {
        serverTreeItem.expand();
        // if JSF Test Project is deployed to server remove it
        SWTBotTreeItem[] serverTreeItemChildren = serverTreeItem.getItems();
        if (serverTreeItemChildren != null && serverTreeItemChildren.length > 0) {
          int itemIndex = 0;
          boolean found = false;
          String treeItemlabel = null;
          do {
            treeItemlabel = serverTreeItemChildren[itemIndex].getText();
            found =
                treeItemlabel.startsWith(projectName)
                    && (stringToContain == null || treeItemlabel.indexOf(stringToContain) >= 0);
          } while (!found && ++itemIndex < serverTreeItemChildren.length);
          // Server Tree Item has Child with Text equal to JSF TEst Project
          if (found) {
            log.info("Found project to be removed from server: " + treeItemlabel);
            ContextMenuHelper.prepareTreeItemForContextMenu(
                serverTree, serverTreeItemChildren[itemIndex]);
            new SWTBotMenu(
                    ContextMenuHelper.getContextMenu(serverTree, IDELabel.Menu.REMOVE, false))
                .click();
            bot.shell("Server").activate();
            bot.button(IDELabel.Button.OK).click();
            log.info("Removed project from server: " + treeItemlabel);
            bot.sleep(10 * 1000L);
          }
        }
      }
      delay();

    } catch (WidgetNotFoundException wnfe) {
      // do nothing it means there is no server defined
    }
  }
 /**
  * Debug Drools Rule and checks result
  *
  * @param droolsRuletName
  */
 private void debugDroolsRule(String droolsRuleName) {
   packageExplorer.show();
   SWTBotTreeItem tiDroolsRule =
       packageExplorer.selectTreeItem(
           DroolsAllBotTests.SAMPLE_DROOLS_RULE_NAME,
           new String[] {
             DroolsAllBotTests.DROOLS_PROJECT_NAME, DroolsAllBotTests.SRC_MAIN_RULES_TREE_NODE
           });
   SWTBot packageExplorerBot = bot.viewByTitle(ViewType.PACKAGE_EXPLORER.getViewLabel()).bot();
   SWTBotTree tree = packageExplorerBot.tree();
   // Select and Open Rule File
   ContextMenuHelper.prepareTreeItemForContextMenu(tree, tiDroolsRule);
   new SWTBotMenu(ContextMenuHelper.getContextMenu(tree, IDELabel.Menu.OPEN, true)).click();
   SWTBotEclipseEditor ruleEditor = bot.editorByTitle(droolsRuleName).toTextEditor();
   ruleEditor.selectRange(8, 0, 0);
   bot.menu(IDELabel.Menu.RUN).menu(IDELabel.Menu.TOGGLE_BREAKPOINT).click();
   SWTBotTreeItem tiDroolsTest =
       packageExplorer.selectTreeItem(
           DroolsAllBotTests.DROOLS_TEST_JAVA_TREE_NODE,
           new String[] {
             DroolsAllBotTests.DROOLS_PROJECT_NAME,
             DroolsAllBotTests.SRC_MAIN_JAVA_TREE_NODE,
             DroolsAllBotTests.COM_SAMPLE_TREE_NODE
           });
   console.clearConsole();
   eclipse.debugTreeItemAsDroolsApplication(tiDroolsTest);
   eclipse.closeConfirmPerspectiveSwitchShellIfOpened(false);
   String consoleText = console.getConsoleText(3 * 1000L, 3 * 1000L, true);
   assertTrue(
       "Drools Rule was not debuged properly.\nConsole content should have been empty but is:\n"
           + consoleText,
       consoleText.length() == 0);
   SWTBotView debugView = bot.viewByTitle(ViewType.DEBUG.getViewLabel());
   debugView.toolbarButton(IDELabel.DebugView.BUTTON_STEP_OVER_TOOLTIP).click();
   consoleText = console.getConsoleText(3 * 1000L, 60 * 1000L, true);
   assertTrue(
       "Drools Rule was not debuged properly.\nConsole content should be:\n'Hello World\n' but is:\n"
           + consoleText,
       consoleText.equals("Hello World\n"));
   debugView.toolbarButton(IDELabel.DebugView.BUTTON_RESUME_TOOLTIP).click();
   consoleText = console.getConsoleText(3 * 1000L, 60 * 1000L, true);
   assertTrue(
       "Drools Rule was not debuged properly.\nConsole content should be:Hello World\nGoodbye cruel world\n"
           + consoleText,
       consoleText.equals("Hello World\nGoodbye cruel world\n"));
 }
  private SWTBotMenu nodeContextMenu(
      final SWTBotTree tree, SWTBotTreeItem item, final String... menu) {
    assert menu.length > 0;
    ContextMenuHelper.prepareTreeItemForContextMenu(tree, item);
    return UIThreadRunnable.syncExec(
        new Result<SWTBotMenu>() {

          public SWTBotMenu run() {
            SWTBotMenu m = new SWTBotMenu(ContextMenuHelper.getContextMenu(tree, menu[0], false));
            for (int i = 1; i < menu.length; i++) {
              m = m.menu(menu[i]);
            }
            return m;
          }
        });
  }