<commit_before>﻿//
//Copyright 2016 <NAME>
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//
using System;
using System.ComponentModel;
using System.Drawing;

namespace APE.Language
{
    /// <summary>
    /// Specifies what cell(s) property to get
    /// </summary>
    public enum CellProperty
    {
        /// <summary>
        /// Cell(s) value formatted as a string
        /// </summary>
        TextDisplay,
        /// <summary>
        /// Cells(s) back colour name as a string
        /// </summary>
        BackColourName,
        /// <summary>
        /// Cells(s) fore colour name as a string
        /// </summary>
        ForeColourName,
        /// <summary>
        /// The data type of the cell as a string
        /// Supported in GetCellValue only
        /// </summary>
        DataType,
        /// <summary>
        /// Cells(s) checkbox state as a string
        /// </summary>
        CheckBox,
        /// <summary>
        /// For GetCellValue this returns the actual image
        /// For GetCellRange this returns whether the cells contain a image
        /// </summary>
        Image,
        /// <summary>
        /// For GetCellValue this returns the actual background image
        /// For GetCellRange this returns whether the cells contain a background image
        /// </summary>
        BackgroundImage,
        /// <summary>
        /// The font style of the cell
        /// Supported in GetCellValue only
        /// </summary>
        FontStyle,
        /// <summary>
        /// The type of the user data associated with a cell (if any) as a string
        /// Supported in GetCellValue only
        /// </summary>
        UserDataType,
    }

    /// <summary>
    /// The location inside a grid cell where to click
    /// </summary>
    public enum CellClickLocation
    {
        /// <summary>
        /// Slightly in from the left side of the cell, centred vertically
        /// </summary>
        LeftSideOfCell = 0,
        /// <summary>
        /// Centre of the cell both horizontally and vertically
        /// </summary>
        CentreOfCell = 1,
        /// <summary>
        /// Slightly in from the right side of the cell, centred vertically
        /// </summary>
        RightSideOfCell = 2,
        /// <summary>
        /// The centre of the expand / collapse icon on a tree view cell
        /// </summary>
        ExpandCollapseIconOfCell = 3,
        /// <summary>
        /// Slightly to the right of the expand / collapse icon of a tree view cell, centred vertically
        /// </summary>
        LeftSideOfTreeItem = 4,
        /// <summary>
        /// The child column picker icon the elementstripgrid
        /// </summary>
        ChildColumnPicker = 5,
        /// <summary>
        /// Slightly in from the top right corner of the cell
        /// </summary>
        TopRightCorner = 6,
    }

    /// <summary>
    /// What checking to perform
    /// </summary>
    public enum ComparisonMethod
    {
        /// <summary>
        /// Compare the value before setting (to check if the value needs to be set) and after setting (to check it has been sucessfully set)
        /// </summary>
        CompareUsingDefaultEqualityComparer,
        /// <summary>
        /// Do not do any checking before or after setting a value
        /// </summary>
        DoNotCompare,
    }

    /// <summary>
    /// Automation class used to automate grid controls
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    public abstract class GUIGridObject : GUIFocusableObject
    {
        /// <summary>
        /// Initialises a new instance of the GUIElementStripGrid class
        /// </summary>
        /// <param name="parentForm">The top level form the control belongs to</param>
        /// <param name="descriptionOfControl">A description of the control which would make sense to a human.
        /// <para/>This text is used in the logging method.  For example: OK button</param>
        /// <param name="identParams">One or more identifier object(s) used to locate the control.
        /// <para/>Normally you would just use the name identifier</param>
        public GUIGridObject(GUIForm parentForm, string descriptionOfControl, params Identifier[] identParams)
            : base(parentForm, descriptionOfControl, identParams)
        {
        }       

        /// <summary>
        /// Returns true if the specified column in the grid exists
        /// </summary>
        /// <param name="columnToFind">Column to check if hidden delimited by -> (or the user defined GridDelimiter property) for example Order -> Id</param>
        /// <returns>True or False</returns>
        public virtual bool ColumnExists(string columnToFind)
        {
            string[] columnHeader = columnToFind.Split(GUI.GridDelimiterAsArray, StringSplitOptions.None);

            if (FindColumnInternal(columnHeader) == -1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Returns the index of the specified column in the grid
        /// </summary>
        /// <param name="columnToFind">Column to check if hidden delimited by -> (or the user defined GridDelimiter property) for example Order -> Id</param>
        /// <returns>The index of the column</returns>
        public virtual int FindColumn(string columnToFind)
        {
            string[] columnHeader = columnToFind.Split(GUI.GridDelimiterAsArray, StringSplitOptions.None);

            return FindColumn(columnHeader);
        }

        /// <summary>
        /// Returns the index of the specified column in the grid
        /// </summary>
        /// <param name="columnHeader">The column to check</param>
        /// <returns>The index of the column</returns>
        public virtual int FindColumn(string[] columnHeader)
        {
            int column = -1;

            // Columns present may change so try twice
            try
            {
                column = FindColumnInternal(columnHeader);
            }
            catch
            {
                column = FindColumnInternal(columnHeader);
            }

            if (column == -1)
            {
                throw GUI.ApeException("Failed to find column " + string.Join(GUI.GridDelimiter, columnHeader) + " in the " + Description);
            }

            return column;
        }

        internal abstract int FindColumnInternal(string[] columnHeader);

        /// <summary>
        /// Returns the rows index of the specified value in the first visible column
        /// </summary>
        /// <param name="rowText">The value to look for in the first visible column or the treeview column if the grid is a tree</param>
        /// <returns>The index of the row or -1</returns>
        public int FindRow(string rowText)
        {
            int startAtRow = 0;
            return FindRow(rowText, -1, startAtRow);
        }

        /// <summary>
        /// Returns the rows index of the specified value in the specified column
        /// </summary>
        /// <param name="rowText">The value to look for in the specified column</param>
        /// <param name="columnText">The column to look for the value in delimited by -> (or the user defined GridDelimiter property) for example Order -> Id</param>
        /// <returns>The index of the row or -1</returns>
        public int FindRow(string rowText, string columnText)
        {
            int columnIndex = FindColumn(columnText);
            int startAtRow = 0;<commit_msg>Poll for columns existence for a short time<commit_after>﻿//
//Copyright 2016 <NAME>
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Threading;

namespace APE.Language
{
    /// <summary>
    /// Specifies what cell(s) property to get
    /// </summary>
    public enum CellProperty
    {
        /// <summary>
        /// Cell(s) value formatted as a string
        /// </summary>
        TextDisplay,
        /// <summary>
        /// Cells(s) back colour name as a string
        /// </summary>
        BackColourName,
        /// <summary>
        /// Cells(s) fore colour name as a string
        /// </summary>
        ForeColourName,
        /// <summary>
        /// The data type of the cell as a string
        /// Supported in GetCellValue only
        /// </summary>
        DataType,
        /// <summary>
        /// Cells(s) checkbox state as a string
        /// </summary>
        CheckBox,
        /// <summary>
        /// For GetCellValue this returns the actual image
        /// For GetCellRange this returns whether the cells contain a image
        /// </summary>
        Image,
        /// <summary>
        /// For GetCellValue this returns the actual background image
        /// For GetCellRange this returns whether the cells contain a background image
        /// </summary>
        BackgroundImage,
        /// <summary>
        /// The font style of the cell
        /// Supported in GetCellValue only
        /// </summary>
        FontStyle,
        /// <summary>
        /// The type of the user data associated with a cell (if any) as a string
        /// Supported in GetCellValue only
        /// </summary>
        UserDataType,
    }

    /// <summary>
    /// The location inside a grid cell where to click
    /// </summary>
    public enum CellClickLocation
    {
        /// <summary>
        /// Slightly in from the left side of the cell, centred vertically
        /// </summary>
        LeftSideOfCell = 0,
        /// <summary>
        /// Centre of the cell both horizontally and vertically
        /// </summary>
        CentreOfCell = 1,
        /// <summary>
        /// Slightly in from the right side of the cell, centred vertically
        /// </summary>
        RightSideOfCell = 2,
        /// <summary>
        /// The centre of the expand / collapse icon on a tree view cell
        /// </summary>
        ExpandCollapseIconOfCell = 3,
        /// <summary>
        /// Slightly to the right of the expand / collapse icon of a tree view cell, centred vertically
        /// </summary>
        LeftSideOfTreeItem = 4,
        /// <summary>
        /// The child column picker icon the elementstripgrid
        /// </summary>
        ChildColumnPicker = 5,
        /// <summary>
        /// Slightly in from the top right corner of the cell
        /// </summary>
        TopRightCorner = 6,
    }

    /// <summary>
    /// What checking to perform
    /// </summary>
    public enum ComparisonMethod
    {
        /// <summary>
        /// Compare the value before setting (to check if the value needs to be set) and after setting (to check it has been sucessfully set)
        /// </summary>
        CompareUsingDefaultEqualityComparer,
        /// <summary>
        /// Do not do any checking before or after setting a value
        /// </summary>
        DoNotCompare,
    }

    /// <summary>
    /// Automation class used to automate grid controls
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    public abstract class GUIGridObject : GUIFocusableObject
    {
        /// <summary>
        /// Initialises a new instance of the GUIElementStripGrid class
        /// </summary>
        /// <param name="parentForm">The top level form the control belongs to</param>
        /// <param name="descriptionOfControl">A description of the control which would make sense to a human.
        /// <para/>This text is used in the logging method.  For example: OK button</param>
        /// <param name="identParams">One or more identifier object(s) used to locate the control.
        /// <para/>Normally you would just use the name identifier</param>
        public GUIGridObject(GUIForm parentForm, string descriptionOfControl, params Identifier[] identParams)
            : base(parentForm, descriptionOfControl, identParams)
        {
        }       

        /// <summary>
        /// Returns true if the specified column in the grid exists
        /// </summary>
        /// <param name="columnToFind">Column to check if hidden delimited by -> (or the user defined GridDelimiter property) for example Order -> Id</param>
        /// <returns>True or False</returns>
        public virtual bool ColumnExists(string columnToFind)
        {
            string[] columnHeader = columnToFind.Split(GUI.GridDelimiterAsArray, StringSplitOptions.None);

            if (FindColumnInternal(columnHeader) == -1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Returns the index of the specified column in the grid
        /// </summary>
        /// <param name="columnToFind">Column to check if hidden delimited by -> (or the user defined GridDelimiter property) for example Order -> Id</param>
        /// <returns>The index of the column</returns>
        public virtual int FindColumn(string columnToFind)
        {
            string[] columnHeader = columnToFind.Split(GUI.GridDelimiterAsArray, StringSplitOptions.None);

            return FindColumn(columnHeader);
        }

        /// <summary>
        /// Returns the index of the specified column in the grid
        /// </summary>
        /// <param name="columnHeader">The column to check</param>
        /// <returns>The index of the column</returns>
        public virtual int FindColumn(string[] columnHeader)
        {
            int column = -1;
            Stopwatch timer = Stopwatch.StartNew();
            while (true)
            {
                try
                {
                    column = FindColumnInternal(columnHeader);
                    if (column != -1)
                    {
                        break;
                    }
                }
                catch
                {
                }

                if (timer.ElapsedMilliseconds > 2000)
                {
                    throw GUI.ApeException("Failed to find column " + string.Join(GUI.GridDelimiter, columnHeader) + " in the " + Description);
                }

                Thread.Sleep(50);
            }
            return column;
        }

        internal abstract int FindColumnInternal(string[] columnHeader);

        /// <summary>
        /// Returns the rows index of the specified value in the first visible column
        /// </summary>
        /// <param name="rowText">The value to look for in the first visible column or the treeview column if the grid is a tree</param>
        /// <returns>The index of the row or -1</returns>
        public int FindRow(string rowText)
        {
            int startAtRow = 0;
            return FindRow(rowText, -1, startAtRow);
        }

        /// <summary>
        /// Returns the rows index of the specified value in the specified column
        /// </summary>
        /// <param name="rowText">The value to look for in the specified column</param>
        /// <param name="columnText">The column to look for the value in delimited by -> (or the user defined GridDelimiter property) for example Order -> Id</param>
        /// <returns>The index of the row or -1</returns>
        public int FindRow(string rowText, string columnText)
        {
            int columnIndex = FindColumn(columnText);
            int startAtRow = 0;
{'fraction_non_alphanumeric': 0.10321170542165017, 'fraction_numerical': 0.0031570639305445935, 'mean_word_length': 1.738487115544472, 'pattern_counts': {'":': 0, '<': 177, '<?xml version=': 0, '>': 190, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5922457', 'n_tokens_mistral': 4136, 'n_tokens_neox': 4012, 'n_words': 1541}