/**
  * Verifies, whether value exists in row..
  *
  * @param row The row of the cell.
  * @param rowOperator the row header operator
  * @param value The cell text to verify.
  * @param operator The operation used to verify
  * @param searchType Determines where the search begins ("relative" or "absolute")
  * @param exists true if value exists, false otherwise
  * @throws StepExecutionException If the row or the column is invalid, or if the rendered text
  *     cannot be extracted.
  */
 public void rcVerifyValueInRow(
     final String row,
     final String rowOperator,
     final String value,
     final String operator,
     final String searchType,
     boolean exists)
     throws StepExecutionException {
   final TreeTableOperationContext adapter = getContext();
   final int implRow = adapter.getRowFromString(row, rowOperator);
   boolean valueIsExisting = false;
   // if row is header
   if (implRow == -1) {
     for (int i = getStartingColIndex(searchType); i < adapter.getColumnCount(); ++i) {
       if (MatchUtil.getInstance().match(adapter.getColumnHeaderText(i), value, operator)) {
         valueIsExisting = true;
         break;
       }
     }
   } else {
     for (int i = getStartingColIndex(searchType); i < adapter.getColumnCount(); ++i) {
       if (MatchUtil.getInstance().match(getCellText(implRow, i), value, operator)) {
         valueIsExisting = true;
         break;
       }
     }
   }
   Verifier.equals(exists, valueIsExisting);
 }
  /**
   * Verifies if the selected node underneath <code>treePath</code> at column <code>column</code>
   * has a rendered text which is equal to <code>selection</code>.
   *
   * @param pattern the pattern
   * @param operator The operator to use when comparing the expected and actual values.
   * @param column the column or column path of the item to select
   * @param colOperator the operator for the column path
   * @throws StepExecutionException If there is no tree node selected, the tree path contains no
   *     selection or the verification fails
   */
  public void rcVerifySelectedValueAtPath(
      String pattern, String operator, String column, String colOperator)
      throws StepExecutionException {

    TreeTableOperationContext context = getContext();
    final int implCol = context.getColumnFromString(column, colOperator, true);
    checkColumnIndex(implCol);
    context.setColumn(implCol);

    String text = context.getRenderedTextOfColumn(context.getSelectedNode());

    Verifier.match(text, pattern, operator);
  }
  /**
   * Verifies if the selected node underneath <code>treePath</code> at column <code>column</code>
   * has a rendered text which is equal to <code>selection</code>.
   *
   * @param pattern the pattern
   * @param operator The operator to use when comparing the expected and actual values.
   * @param column The column containing the text to verify
   * @throws StepExecutionException If there is no tree node selected, the tree path contains no
   *     selection or the verification fails
   */
  public void rcVerifySelectedValue(String pattern, String operator, int column)
      throws StepExecutionException {

    final int implCol = IndexConverter.toImplementationIndex(column);
    checkColumnIndex(implCol);

    TreeTableOperationContext context = getContext();
    context.setColumn(implCol);

    String text = context.getRenderedTextOfColumn(context.getSelectedNode());

    Verifier.match(text, pattern, operator);
  }
  /**
   * Action to read the value of the passed cell of the table to store it in a variable in the
   * Client
   *
   * @param variable the name of the variable
   * @param row the row to select
   * @param rowOperator the row header operator
   * @param col the column to select
   * @param colOperator the column header operator
   * @return the text value.
   */
  public String rcReadValue(
      String variable, String row, String rowOperator, String col, String colOperator) {
    TreeTableOperationContext adapter = getContext();
    final int implRow = adapter.getRowFromString(row, rowOperator);
    final int implCol = adapter.getColumnFromString(col, colOperator, implRow != -1);

    // if row is header and column is existing
    if (implRow == -1 && implCol > -1) {
      return adapter.getColumnHeaderText(implCol);
    }
    checkRowColBounds(implRow, implCol);
    adapter.scrollCellToVisible(implRow, implCol);
    return getCellText(implRow, implCol);
  }
  /**
   * Verifies, whether value exists in column.
   *
   * @param col The column of the cell.
   * @param colOperator the column header operator
   * @param value The cell text to verify.
   * @param operator The operation used to verify
   * @param searchType Determines where the search begins ("relative" or "absolute")
   * @param exists true if value exists, false otherwise
   * @throws StepExecutionException If the row or the column is invalid, or if the rendered text
   *     cannot be extracted.
   */
  public void rcVerifyValueInColumn(
      final String col,
      final String colOperator,
      final String value,
      final String operator,
      final String searchType,
      boolean exists)
      throws StepExecutionException {
    TreeTableOperationContext adapter = getContext();
    final int implCol = adapter.getColumnFromString(col, colOperator, true);

    boolean valueExists = isValueExisting(adapter, implCol, value, operator, searchType);

    Verifier.equals(exists, valueExists);
  }
  /**
   * Traverses the tree by searching for the nodes in the tree path entry and calling the given
   * operation on the last element in the path.
   *
   * @param treePath The tree path.
   * @param pathType For example, "relative" or "absolute".
   * @param preAscend Relative traversals will start this many parent nodes above the current node.
   *     Absolute traversals ignore this parameter.
   * @param operation The tree node operation.
   * @param column The target column for the operation.
   * @throws StepExecutionException If the path traversal fails.
   */
  private void traverseLastElementByPath(
      INodePath treePath, String pathType, int preAscend, TreeNodeOperation operation, int column)
      throws StepExecutionException {

    Validate.notNull(treePath);
    Validate.notNull(operation);

    TreeTableOperationContext context = getContext();
    context.setColumn(column);
    TreeItem<?> startNode = (TreeItem<?>) getStartNode(pathType, preAscend, context);

    AbstractTreeNodeTraverser traverser =
        new PathBasedTraverser(context, treePath, new TreeNodeOperationConstraint());
    traverser.traversePath(operation, startNode);
  }
 /**
  * Verifies the editable property of the given indices.
  *
  * @param editable The editable property to verify.
  * @param row the row to select
  * @param rowOperator the row header operator
  * @param col the column to select
  * @param colOperator the column header operator
  */
 public void rcVerifyEditable(
     boolean editable, String row, String rowOperator, String col, String colOperator) {
   TreeTableOperationContext context = getContext();
   if (context.getRowFromString(row, rowOperator) == -1) {
     throw new StepExecutionException(
         "Unsupported Header Action", //$NON-NLS-1$
         EventFactory.createActionError(TestErrorEvent.UNSUPPORTED_HEADER_ACTION));
   }
   selectCell(
       row,
       rowOperator,
       col,
       colOperator,
       ClickOptions.create(),
       ValueSets.BinaryChoice.no.rcValue());
   rcVerifySelectedEditable(editable);
 }
 /**
  * Looks if value exists in the Column.
  *
  * @param adapter the table adapter working on.
  * @param implCol the implementation column of the cell.
  * @param value the cell text to verify.
  * @param operator The operation used to verify.
  * @param searchType searchType Determines where the search begins ("relative" or "absolute")
  * @return <code>true</code> it the value exists in the column
  */
 private boolean isValueExisting(
     TreeTableOperationContext adapter,
     int implCol,
     String value,
     String operator,
     final String searchType) {
   final int rowCount = adapter.getRowCount();
   for (int i = getStartingRowIndex(searchType); i < rowCount; ++i) {
     if (MatchUtil.getInstance().match(getCellText(i, implCol), value, operator)) {
       return true;
     }
   }
   if (adapter.isHeaderVisible()) {
     String header = adapter.getColumnHeaderText(implCol);
     if (MatchUtil.getInstance().match(header, value, operator)) {
       return true;
     }
   }
   return false;
 }
  /**
   * Finds the first column which contains the value <code>value</code> in the given row and selects
   * the cell.
   *
   * @param row the row
   * @param rowOperator the row header operator
   * @param value the value
   * @param regex search using regex
   * @param extendSelection Should this selection be part of a multiple selection
   * @param searchType Determines where the search begins ("relative" or "absolute")
   * @param co the click options to use
   */
  protected void selectCellByColValue(
      String row,
      String rowOperator,
      final String value,
      final String regex,
      final String extendSelection,
      final String searchType,
      ClickOptions co) {
    TreeTableOperationContext adapter = getContext();
    final int implRow = adapter.getRowFromString(row, rowOperator);
    int colCount = adapter.getColumnCount();
    Integer implCol = null;
    if (implRow == -1) {

      for (int i = getStartingColIndex(searchType); i < colCount; ++i) {
        if (MatchUtil.getInstance().match(adapter.getColumnHeaderText(i), value, regex)) {
          implCol = new Integer(i);
          break;
        }
      }
    } else {
      for (int i = getStartingColIndex(searchType); i < colCount; ++i) {
        if (MatchUtil.getInstance().match(getCellText(implRow, i), value, regex)) {

          implCol = new Integer(i);
          break;
        }
      }
    }
    if (implCol == null) {
      throw new StepExecutionException(
          "no such cell found",
          EventFactory //$NON-NLS-1$
              .createActionError(TestErrorEvent.NOT_FOUND));
    }

    String usrIdxRowStr = new Integer(IndexConverter.toUserIndex(implRow)).toString();
    String usrIdxColStr = new Integer(IndexConverter.toUserIndex(implCol.intValue())).toString();

    selectCell(usrIdxRowStr, rowOperator, usrIdxColStr, MatchUtil.EQUALS, co, extendSelection);
  }
  /**
   * Finds the first row which contains the value <code>value</code> in column <code>col</code> and
   * selects this row.
   *
   * @param col the column
   * @param colOperator the column header operator
   * @param value the value
   * @param regexOp the regex operator
   * @param extendSelection Should this selection be part of a multiple selection
   * @param searchType Determines where the search begins ("relative" or "absolute")
   * @param co the clickOptions to use
   */
  protected void selectRowByValue(
      String col,
      String colOperator,
      final String value,
      final String regexOp,
      final String extendSelection,
      final String searchType,
      ClickOptions co) {
    TreeTableOperationContext adapter = getContext();
    final int implCol = adapter.getColumnFromString(col, colOperator, true);
    Integer implRow = null;
    final int rowCount = adapter.getRowCount();

    for (int i = getStartingRowIndex(searchType); i < rowCount; ++i) {
      if (MatchUtil.getInstance().match(getCellText(i, implCol), value, regexOp)) {

        implRow = new Integer(i);
        break;
      }
    }
    if (implRow == null) {
      String header = adapter.getColumnHeaderText(implCol);
      if (MatchUtil.getInstance().match(header, value, regexOp)) {
        implRow = new Integer(-1);
      }
    }

    if (implRow == null) {
      throw new StepExecutionException(
          "no such row found", //$NON-NLS-1$
          EventFactory.createActionError(TestErrorEvent.NOT_FOUND));
    }

    String userIdxRow = new Integer(IndexConverter.toUserIndex(implRow.intValue())).toString();
    String userIdxCol = new Integer(IndexConverter.toUserIndex(implCol)).toString();

    selectCell(userIdxRow, MatchUtil.EQUALS, userIdxCol, colOperator, co, extendSelection);
  }
  /**
   * Verifies the rendered text inside the passed cell.
   *
   * @param row The row of the cell.
   * @param rowOperator The row header operator
   * @param col The column of the cell.
   * @param colOperator The column header operator
   * @param text The cell text to verify.
   * @param operator The operation used to verify
   * @throws StepExecutionException If the row or the column is invalid, or if the rendered text
   *     cannot be extracted.
   */
  public void rcVerifyText(
      String text,
      String operator,
      final String row,
      final String rowOperator,
      final String col,
      final String colOperator)
      throws StepExecutionException {
    TreeTableOperationContext adapter = getContext();
    final int implRow = adapter.getRowFromString(row, rowOperator);
    final int implCol = adapter.getColumnFromString(col, colOperator, implRow != -1);
    String current;
    // if row is header and column is existing
    if (implRow == -1 && implCol > -1) {
      current = adapter.getColumnHeaderText(implCol);
    } else {
      checkRowColBounds(implRow, implCol);
      adapter.scrollCellToVisible(implRow, implCol);
      current = getCellText(implRow, implCol);
    }

    Verifier.match(current, text, operator);
  }
  /**
   * Selects the cell of the Table.<br>
   * With the xPos, yPos, xUnits and yUnits the click position inside the cell can be defined.
   *
   * @param row The row of the cell.
   * @param rowOperator The row header operator
   * @param col The column of the cell.
   * @param colOperator The column header operator
   * @param clickCount The number of clicks with the right mouse button
   * @param xPos what x position
   * @param xUnits should x position be pixel or percent values
   * @param yPos what y position
   * @param yUnits should y position be pixel or percent values
   * @param extendSelection Should this selection be part of a multiple selection
   * @param button what mouse button should be used
   * @throws StepExecutionException If the row or the column is invalid
   */
  public void rcSelectCell(
      final String row,
      final String rowOperator,
      final String col,
      final String colOperator,
      final int clickCount,
      final int xPos,
      final String xUnits,
      final int yPos,
      final String yUnits,
      final String extendSelection,
      int button)
      throws StepExecutionException {
    TreeTableOperationContext adapter = getContext();
    final int implRow = adapter.getRowFromString(row, rowOperator);
    final int implCol = adapter.getColumnFromString(col, colOperator, implRow != -1);
    final boolean isExtendSelection = extendSelection.equals(ValueSets.BinaryChoice.yes.rcValue());
    if (log.isDebugEnabled()) {
      log.debug("Selecting row, col: " + row + ", " + col); // $NON-NLS-1$//$NON-NLS-2$
    }

    Rectangle cellBounds;
    Object source = getRealComponent();
    // if row is header and col is existing
    if (implRow == -1 && implCol > -1) {
      cellBounds = adapter.getHeaderBounds(implCol);
      source = adapter.getTableHeader();
    } else {
      cellBounds = adapter.scrollCellToVisible(implRow, implCol);
    }
    ClickOptions clickOptions = ClickOptions.create();
    clickOptions.setClickCount(clickCount).setScrollToVisible(false);
    clickOptions.setMouseButton(button);
    try {
      if (isExtendSelection) {
        getRobot().keyPress(getRealComponent(), getExtendSelectionModifier());
      }
      getRobot()
          .click(
              source,
              cellBounds,
              clickOptions,
              xPos,
              xUnits.equalsIgnoreCase(ValueSets.Unit.pixel.rcValue()),
              yPos,
              yUnits.equalsIgnoreCase(ValueSets.Unit.pixel.rcValue()));
    } finally {
      if (isExtendSelection) {
        getRobot().keyRelease(getRealComponent(), getExtendSelectionModifier());
      }
    }
  }
 /**
  * Checks if the passed row and column are inside the bounds of the Table.
  *
  * @param row The row
  * @param column The column
  * @throws StepExecutionException If the row or the column is outside of the Table's bounds.
  */
 protected void checkRowColBounds(int row, int column) throws StepExecutionException {
   TreeTableOperationContext adapter = getContext();
   checkBounds(row, adapter.getRowCount());
   checkBounds(column, adapter.getColumnCount());
 }