<commit_before>package com.extjs.selenium.grid;

import com.extjs.selenium.ExtJsComponent;
import com.extjs.selenium.Utils;
import com.extjs.selenium.panel.Panel;
import com.extjs.selenium.tab.TabPanel;
import com.sdl.selenium.conditions.Condition;
import com.sdl.selenium.conditions.ConditionManager;
import com.sdl.selenium.conditions.ElementRemovedSuccessCondition;
import com.sdl.selenium.web.WebLocator;
import org.apache.log4j.Logger;
import org.openqa.selenium.Keys;

import java.util.*;

public class GridPanel extends Panel {
    private static final Logger logger = Logger.getLogger(GridPanel.class);

    private String searchColumnId = "0";

    // TODO temporary solution for verification if loaded
    private boolean isLoaded = false;
    private int timeout = 60;

    public GridPanel() {
        setClassName("GridPanel");
        setBaseCls("x-grid-panel");
        setHeaderBaseCls("x-panel");
        //logger.debug(getClassName() + "() constructor");
    }

    public GridPanel(String cls) {
        this();
        setCls(cls);
    }

    public GridPanel(WebLocator container) {
        this();
        setContainer(container);
    }

    public GridPanel(String cls, String searchColumnId) {
        this();
        setCls(cls);
        this.searchColumnId = searchColumnId;
    }

    public GridPanel(WebLocator container, String searchColumnId) {
        this(container);
        setSearchColumnId(searchColumnId);
    }

    public GridPanel(WebLocator container, String cls, String searchColumnId) {
        this(container);
        setCls(cls);
        setSearchColumnId(searchColumnId);
    }

    public static GridPanel getInstance(WebLocator container, String searchColumnId) {
        GridPanel gridPanel = new GridPanel(container, searchColumnId);
        return gridPanel;
    }

    // TODO find better solution for GridPanel that is used in TabPanel
    public static GridPanel getInstanceByTabPanel(TabPanel tabPanel, String searchColumnId) {
        GridPanel gridPanel = new GridPanel();
        gridPanel.setContainer(tabPanel.getContainer());
        gridPanel.setElPath(tabPanel.getItemPath(false));
        gridPanel.setSearchColumnId(searchColumnId);
        return gridPanel;
    }

    public String getSearchColumnId() {
        return searchColumnId;
    }

    public void setSearchColumnId(String searchColumnId) {
        this.searchColumnId = searchColumnId;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(final int timeout) {
        this.timeout = timeout;
    }

    // Methods

    /**
     * TODO find better solution
     * (for example when grid has been loaded but has no records or when grid is loading for second time)
     */
    public boolean waitToLoad() {
        return waitToLoad(timeout);
    }

    public boolean waitToLoad(int seconds) {
        ExtJsComponent mask = new ExtJsComponent("x-mask-loading", this);
        Condition condition = new ConditionManager(seconds * 1000).add(new ElementRemovedSuccessCondition(mask)).execute();
        isLoaded = condition.isSuccess();
        if (!isLoaded) {
            logger.warn(this + " still has x-mask-loading");
        }
        return isLoaded;
    }

    public boolean waitToLoad(int seconds, boolean waitRows) {
        waitToLoad(seconds);
        return waitRows ? waitToPopulate(seconds) : isLoaded;
    }

    public boolean waitToLoad(boolean waitRows) {
        return waitToLoad() && (!waitRows || waitToPopulate());
    }

    public boolean executeScrollScript(String info, String script) {
        Boolean scrolled;
        //logger.info(this + " - " + info);
        if (hasWebDriver()) {
            scrolled = (Boolean) executeScript(script);
        } else {
            scrolled = Boolean.parseBoolean((String) executeScript(script));
        }
        logger.info(this + " - " + info + " > " + scrolled);
        Utils.sleep(200); // because of Buffer view that can have scroll delay
        return scrolled;
    }

    /**
     * Scroll on the top in Grid
     *
     * @return true if scrolled
     */
    public boolean scrollTop() {
        String id = getAttributeId();
        String script = "(function(g){var a=g.view.scroller;if(a.dom.scrollTop!=0){a.dom.scrollTop=0;return true}return false})(window.Ext.getCmp('" + id + "'))";
        if (hasWebDriver()) {
            script = "return " + script;
        }
        return executeScrollScript("scrollTop", script);
    }

    public boolean scrollBottom() {
        String id = getAttributeId();
        String script = "(function(g){var a=g.view.scroller;a.dom.scrollTop=g.view.mainBody.getHeight();return true})(window.Ext.getCmp('" + id + "'))";
        if (hasWebDriver()) {
            script = "return " + script;
        }
        return executeScrollScript("scrollButtom", script);
    }

    /**
     * Scroll Up one visible page in Grid
     *
     * @return true if scrolled
     */
    public boolean scrollPageUp() {
        String id = getAttributeId();
        String script = "(function(c){var a=c.view,b=a.scroller;if(b.dom.scrollTop>0){b.dom.scrollTop-=b.getHeight()-10;return true}return false})(window.Ext.getCmp('" + id + "'))";
        if (hasWebDriver()) {
            script = "return " + script;
        }
        return executeScrollScript("scrollPageUp", script);
    }

    /**
     * Scroll Down one visible page in Grid
     *
     * @return true if scrolled
     */
    public boolean scrollPageDown() {
        String id = getAttributeId();
        String script = "(function(c){var a=c.view,b=a.scroller;if(b.dom.scrollTop<(a.mainBody.getHeight()-b.getHeight())){b.dom.scrollTop+=b.getHeight()-10;return true}return false})(window.Ext.getCmp('" + id + "'))";
        if (hasWebDriver()) {
            return executeScrollScript("scrollPageDown", "return " + script);
        } else {
            return executeScrollScript("scrollPageDown", script);
        }
    }

    /**
     * selects (clicks) on a grid which contains a certain element
     *
     * @param searchElement the searchElement of the grid element on which the search is done
     * @return true if selected
     */
    public boolean rowSelect(String searchElement) {
        return rowSelect(searchElement, false);
    }

    /**
     * Use this method when really need to select some records not for verification if row is in grid
     *
     * @param searchElement
     * @param startWith
     * @return
     */
    public boolean rowSelect(String searchElement, Boolean startWith) {
        ready();
        GridCell cell = getGridCell(searchElement, startWith);
        return doCellSelect(cell);
    }

    /**
     * @param searchElement
     * @param columnId
     * @param searchType    accepted values are: {"equals", "starts-with", "contains"}
     * @return
     */

    public boolean rowSelect(String searchElement, int columnId, String searchType) {<commit_msg>add rowSelect(String searchElement, String searchType) in GridPanel<commit_after>package com.extjs.selenium.grid;

import com.extjs.selenium.ExtJsComponent;
import com.extjs.selenium.Utils;
import com.extjs.selenium.panel.Panel;
import com.extjs.selenium.tab.TabPanel;
import com.sdl.selenium.conditions.Condition;
import com.sdl.selenium.conditions.ConditionManager;
import com.sdl.selenium.conditions.ElementRemovedSuccessCondition;
import com.sdl.selenium.web.WebLocator;
import org.apache.log4j.Logger;
import org.openqa.selenium.Keys;

import java.util.*;

public class GridPanel extends Panel {
    private static final Logger logger = Logger.getLogger(GridPanel.class);

    private String searchColumnId = "0";

    // TODO temporary solution for verification if loaded
    private boolean isLoaded = false;
    private int timeout = 60;

    public GridPanel() {
        setClassName("GridPanel");
        setBaseCls("x-grid-panel");
        setHeaderBaseCls("x-panel");
        //logger.debug(getClassName() + "() constructor");
    }

    public GridPanel(String cls) {
        this();
        setCls(cls);
    }

    public GridPanel(WebLocator container) {
        this();
        setContainer(container);
    }

    public GridPanel(String cls, String searchColumnId) {
        this();
        setCls(cls);
        this.searchColumnId = searchColumnId;
    }

    public GridPanel(WebLocator container, String searchColumnId) {
        this(container);
        setSearchColumnId(searchColumnId);
    }

    public GridPanel(WebLocator container, String cls, String searchColumnId) {
        this(container);
        setCls(cls);
        setSearchColumnId(searchColumnId);
    }

    public static GridPanel getInstance(WebLocator container, String searchColumnId) {
        GridPanel gridPanel = new GridPanel(container, searchColumnId);
        return gridPanel;
    }

    // TODO find better solution for GridPanel that is used in TabPanel
    public static GridPanel getInstanceByTabPanel(TabPanel tabPanel, String searchColumnId) {
        GridPanel gridPanel = new GridPanel();
        gridPanel.setContainer(tabPanel.getContainer());
        gridPanel.setElPath(tabPanel.getItemPath(false));
        gridPanel.setSearchColumnId(searchColumnId);
        return gridPanel;
    }

    public String getSearchColumnId() {
        return searchColumnId;
    }

    public void setSearchColumnId(String searchColumnId) {
        this.searchColumnId = searchColumnId;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(final int timeout) {
        this.timeout = timeout;
    }

    // Methods

    /**
     * TODO find better solution
     * (for example when grid has been loaded but has no records or when grid is loading for second time)
     */
    public boolean waitToLoad() {
        return waitToLoad(timeout);
    }

    public boolean waitToLoad(int seconds) {
        ExtJsComponent mask = new ExtJsComponent("x-mask-loading", this);
        Condition condition = new ConditionManager(seconds * 1000).add(new ElementRemovedSuccessCondition(mask)).execute();
        isLoaded = condition.isSuccess();
        if (!isLoaded) {
            logger.warn(this + " still has x-mask-loading");
        }
        return isLoaded;
    }

    public boolean waitToLoad(int seconds, boolean waitRows) {
        waitToLoad(seconds);
        return waitRows ? waitToPopulate(seconds) : isLoaded;
    }

    public boolean waitToLoad(boolean waitRows) {
        return waitToLoad() && (!waitRows || waitToPopulate());
    }

    public boolean executeScrollScript(String info, String script) {
        Boolean scrolled;
        //logger.info(this + " - " + info);
        if (hasWebDriver()) {
            scrolled = (Boolean) executeScript(script);
        } else {
            scrolled = Boolean.parseBoolean((String) executeScript(script));
        }
        logger.info(this + " - " + info + " > " + scrolled);
        Utils.sleep(200); // because of Buffer view that can have scroll delay
        return scrolled;
    }

    /**
     * Scroll on the top in Grid
     *
     * @return true if scrolled
     */
    public boolean scrollTop() {
        String id = getAttributeId();
        String script = "(function(g){var a=g.view.scroller;if(a.dom.scrollTop!=0){a.dom.scrollTop=0;return true}return false})(window.Ext.getCmp('" + id + "'))";
        if (hasWebDriver()) {
            script = "return " + script;
        }
        return executeScrollScript("scrollTop", script);
    }

    public boolean scrollBottom() {
        String id = getAttributeId();
        String script = "(function(g){var a=g.view.scroller;a.dom.scrollTop=g.view.mainBody.getHeight();return true})(window.Ext.getCmp('" + id + "'))";
        if (hasWebDriver()) {
            script = "return " + script;
        }
        return executeScrollScript("scrollButtom", script);
    }

    /**
     * Scroll Up one visible page in Grid
     *
     * @return true if scrolled
     */
    public boolean scrollPageUp() {
        String id = getAttributeId();
        String script = "(function(c){var a=c.view,b=a.scroller;if(b.dom.scrollTop>0){b.dom.scrollTop-=b.getHeight()-10;return true}return false})(window.Ext.getCmp('" + id + "'))";
        if (hasWebDriver()) {
            script = "return " + script;
        }
        return executeScrollScript("scrollPageUp", script);
    }

    /**
     * Scroll Down one visible page in Grid
     *
     * @return true if scrolled
     */
    public boolean scrollPageDown() {
        String id = getAttributeId();
        String script = "(function(c){var a=c.view,b=a.scroller;if(b.dom.scrollTop<(a.mainBody.getHeight()-b.getHeight())){b.dom.scrollTop+=b.getHeight()-10;return true}return false})(window.Ext.getCmp('" + id + "'))";
        if (hasWebDriver()) {
            return executeScrollScript("scrollPageDown", "return " + script);
        } else {
            return executeScrollScript("scrollPageDown", script);
        }
    }

    /**
     * selects (clicks) on a grid which contains a certain element
     *
     * @param searchElement the searchElement of the grid element on which the search is done
     * @return true if selected
     */
    public boolean rowSelect(String searchElement) {
        return rowSelect(searchElement, false);
    }

    /**
     * Use this method when really need to select some records not for verification if row is in grid
     *
     * @param searchElement
     * @param startWith
     * @return
     */
    public boolean rowSelect(String searchElement, Boolean startWith) {
        ready();
        GridCell cell = getGridCell(searchElement, startWith);
        return doCellSelect(cell);
    }

    /**
     *
     * @param searchElement
     * @param searchType accepted values are: {"equals"/"eq", "starts-with", "contains"}
     * @return
     */
    public boolean rowSelect(String searchElement, String searchType) {
        ready(true);
        GridCell cell = getGridCell(searchElement, searchType);
        return doCellSelect(cell);
    }

    /**
     * @param searchElement
     * @param columnId
     * @param searchType accepted values are: {"equals", "starts-with", "contains"}
     * @return
     */

    public boolean rowSelect(String searchElement, int columnId, String searchType) {
{'fraction_non_alphanumeric': 0.11135233559773093, 'fraction_numerical': 0.002521184956929757, 'mean_word_length': 2.7139141742522757, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '367669', 'n_tokens_mistral': 4054, 'n_tokens_neox': 3947, 'n_words': 984}