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

import com.extjs.selenium.ExtJsComponent;
import com.extjs.selenium.Utils;
import com.sdl.selenium.web.WebLocator;
import junit.framework.Assert;
import org.apache.log4j.Logger;
import org.openqa.selenium.Keys;
import org.openqa.selenium.StaleElementReferenceException;

import java.awt.*;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

public class TextField extends ExtJsComponent {
    private static final Logger logger = Logger.getLogger(TextField.class);

    public TextField() {
        setClassName("TextField");
        setTag("input");
    }

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

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

    public TextField(WebLocator container, String label) {
        this(container);
        setLabel(label);
    }

    public TextField(String name, WebLocator container) {
        this(container);
        setName(name);
    }

    // methods

    public String itemToString() {
        String info;
        if (hasLabel()) {
            info = getLabel();
        } else {
            info = super.itemToString();
        }
        return info;
    }

    /**
     * Containing baseCls, class, name and style
     *
     * @return baseSelector
     */
    public String getBasePathSelector() {
        String selector = super.getBasePathSelector();

        selector += " and not (@type='hidden') ";
        // TODO use also if disabled some parents then can;t click/select some children
        // x-panel x-panel-noborder x-masked-relative x-masked  x-border-panel
        selector = Utils.fixPathSelector(selector);

        return selector;
    }

    public String getItemPath(boolean disabled) {
        String selector = getBasePathSelector();
        selector = "//" + getTag() + (selector.length() > 0 ? ("[" + selector + "]") : "");
        return selector;
    }

    public static void copyToClipboard(final String text) {
        final StringSelection stringSelection = new StringSelection(text);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection,
                new ClipboardOwner() {
                    @Override
                    public void lostOwnership(final java.awt.datatransfer.Clipboard clipboard, final Transferable contents) {
                        // do nothing
                    }
                });
    }

    public boolean pasteInValue(String value) {
        if (ready()) {
            if (value != null) {
                currentElement.clear();
                copyToClipboard(value);
                currentElement.sendKeys(Keys.CONTROL, "v");
                logger.info("Set value(" + this + "): " + value + "'");
                return true;
            }
        } else {
            logger.warn("setValue : field is not ready for use: " + toString());
        }
        return false;
    }

    public boolean setValue(String value) {
        if (value != null) {
            if (executor.isSamePath(this, this.getPath()) || ready()) {
                if (hasWebDriver()) {
                    try {
                        currentElement.clear();
                        currentElement.sendKeys(value);
                    } catch (StaleElementReferenceException exception){
                        logger.warn("StaleElementReferenceException" + exception);
                        logger.warn("Set value(" + this + ") second try:  '" + value + "'");
                        if(ready()){
                            currentElement.clear();
                            currentElement.sendKeys(value);
                        }
                    }<commit_msg>fix setValue for TextField.java<commit_after>package com.extjs.selenium.form;

import com.extjs.selenium.ExtJsComponent;
import com.extjs.selenium.Utils;
import com.sdl.selenium.web.WebLocator;
import junit.framework.Assert;
import org.apache.log4j.Logger;
import org.openqa.selenium.Keys;
import org.openqa.selenium.StaleElementReferenceException;

import java.awt.*;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

public class TextField extends ExtJsComponent {
    private static final Logger logger = Logger.getLogger(TextField.class);

    public TextField() {
        setClassName("TextField");
        setTag("input");
    }

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

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

    public TextField(WebLocator container, String label) {
        this(container);
        setLabel(label);
    }

    public TextField(String name, WebLocator container) {
        this(container);
        setName(name);
    }

    // methods

    public String itemToString() {
        String info;
        if (hasLabel()) {
            info = getLabel();
        } else {
            info = super.itemToString();
        }
        return info;
    }

    /**
     * Containing baseCls, class, name and style
     *
     * @return baseSelector
     */
    public String getBasePathSelector() {
        String selector = super.getBasePathSelector();

        selector += " and not (@type='hidden') ";
        // TODO use also if disabled some parents then can;t click/select some children
        // x-panel x-panel-noborder x-masked-relative x-masked  x-border-panel
        selector = Utils.fixPathSelector(selector);

        return selector;
    }

    public String getItemPath(boolean disabled) {
        String selector = getBasePathSelector();
        selector = "//" + getTag() + (selector.length() > 0 ? ("[" + selector + "]") : "");
        return selector;
    }

    public static void copyToClipboard(final String text) {
        final StringSelection stringSelection = new StringSelection(text);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection,
                new ClipboardOwner() {
                    @Override
                    public void lostOwnership(final java.awt.datatransfer.Clipboard clipboard, final Transferable contents) {
                        // do nothing
                    }
                });
    }

    public boolean pasteInValue(String value) {
        if (ready()) {
            if (value != null) {
                currentElement.clear();
                copyToClipboard(value);
                currentElement.sendKeys(Keys.CONTROL, "v");
                logger.info("Set value(" + this + "): " + value + "'");
                return true;
            }
        } else {
            logger.warn("setValue : field is not ready for use: " + toString());
        }
        return false;
    }

    public boolean setValue(String value) {
        if (value != null) {
//            if (executor.isSamePath(this, this.getPath()) || ready()) {  //TODO Sa gasim o solutie pentru cazurile cand elementul nu exista
// ca sa putem avea imbunatatire cand e elementul nu s-a modificat
            if (ready()) {
                if (hasWebDriver()) {
                    try {
                        currentElement.clear();
                        currentElement.sendKeys(value);
                    } catch (StaleElementReferenceException exception){
                        logger.warn("StaleElementReferenceException" + exception);
                        logger.warn("Set value(" + this + ") second try:  '" + value + "'");
                        if(ready()){
                            currentElement.clear();
                            currentElement.sendKeys(value);
                        }
                    }
{'fraction_non_alphanumeric': 0.10609163040696186, 'fraction_numerical': 0.0005119017148707448, 'mean_word_length': 1.908448083364347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5212243', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1983, 'n_words': 459}