Search in sources :

Example 1 with SelectableTextInput

use of com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput in project htmlunit by HtmlUnit.

the class HtmlPage method setFocusedElement.

/**
 * Moves the focus to the specified element. This will trigger any relevant JavaScript
 * event handlers.
 *
 * @param newElement the element that will receive the focus, use {@code null} to remove focus from any element
 * @param windowActivated - whether the enclosing window got focus resulting in specified element getting focus
 * @return true if the specified element now has the focus
 * @see #getFocusedElement()
 */
public boolean setFocusedElement(final DomElement newElement, final boolean windowActivated) {
    if (elementWithFocus_ == newElement && !windowActivated) {
        // nothing to do
        return true;
    }
    final DomElement oldFocusedElement = elementWithFocus_;
    elementWithFocus_ = null;
    if (getWebClient().isJavaScriptEnabled()) {
        final Object o = getScriptableObject();
        if (o instanceof HTMLDocument) {
            ((HTMLDocument) o).setActiveElement(null);
        }
    }
    if (!windowActivated) {
        if (hasFeature(EVENT_FOCUS_IN_FOCUS_OUT_BLUR)) {
            if (oldFocusedElement != null) {
                oldFocusedElement.fireEvent(Event.TYPE_FOCUS_OUT);
            }
            if (newElement != null) {
                newElement.fireEvent(Event.TYPE_FOCUS_IN);
            }
        }
        if (oldFocusedElement != null) {
            oldFocusedElement.removeFocus();
            oldFocusedElement.fireEvent(Event.TYPE_BLUR);
            if (hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
                oldFocusedElement.fireEvent(Event.TYPE_FOCUS_OUT);
            }
        }
    }
    elementWithFocus_ = newElement;
    // might be changed by another thread
    if (newElement instanceof SelectableTextInput && hasFeature(PAGE_SELECTION_RANGE_FROM_SELECTABLE_TEXT_INPUT)) {
        final SelectableTextInput sti = (SelectableTextInput) newElement;
        setSelectionRange(new SimpleRange(sti, sti.getSelectionStart(), sti, sti.getSelectionEnd()));
    }
    if (newElement != null) {
        if (getWebClient().isJavaScriptEnabled()) {
            final Object o = getScriptableObject();
            if (o instanceof HTMLDocument) {
                final Object e = newElement.getScriptableObject();
                if (e instanceof HTMLElement) {
                    ((HTMLDocument) o).setActiveElement((HTMLElement) e);
                }
            }
        }
        newElement.focus();
        newElement.fireEvent(Event.TYPE_FOCUS);
        if (hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
            newElement.fireEvent(Event.TYPE_FOCUS_IN);
        }
    }
    // element will not have the focus because its page has gone away.
    return this == getEnclosingWindow().getEnclosedPage();
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange) SelectableTextInput(com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput)

Example 2 with SelectableTextInput

use of com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput in project htmlunit by HtmlUnit.

the class HtmlElement method type.

/**
 * Simulates typing the specified {@link Keyboard} while this element has focus, returning the page contained
 * by this element's window after typing. Note that it may or may not be the same as the original page,
 * depending on the JavaScript event handlers, etc. Note also that for some elements, typing <tt>XXXXXXXXXXX</tt>
 * submits the enclosed form.
 *
 * @param keyboard the keyboard
 * @return the page that occupies this window after typing
 * @exception IOException if an IO error occurs
 */
public Page type(final Keyboard keyboard) throws IOException {
    Page page = null;
    final List<Object[]> keys = keyboard.getKeys();
    if (keyboard.isStartAtEnd()) {
        if (this instanceof SelectableTextInput) {
            final SelectableTextInput textInput = (SelectableTextInput) this;
            textInput.setSelectionStart(textInput.getText().length());
        } else {
            final DomText domText = getDoTypeNode();
            if (domText != null) {
                domText.moveSelectionToEnd();
            }
        }
    }
    for (int i = 0; i < keys.size(); i++) {
        final Object[] entry = keys.get(i);
        if (entry.length == 1) {
            type((char) entry[0], i == keys.size() - 1);
        } else {
            final int key = (int) entry[0];
            final boolean pressed = (boolean) entry[1];
            switch(key) {
                case KeyboardEvent.DOM_VK_SHIFT:
                    shiftPressed_ = pressed;
                    break;
                case KeyboardEvent.DOM_VK_CONTROL:
                    ctrlPressed_ = pressed;
                    break;
                case KeyboardEvent.DOM_VK_ALT:
                    altPressed_ = pressed;
                    break;
                default:
            }
            if (pressed) {
                boolean keyPress = true;
                boolean keyUp = true;
                switch(key) {
                    case KeyboardEvent.DOM_VK_SHIFT:
                    case KeyboardEvent.DOM_VK_CONTROL:
                    case KeyboardEvent.DOM_VK_ALT:
                        keyPress = false;
                        keyUp = false;
                        break;
                    default:
                }
                page = type(key, true, keyPress, keyUp, i == keys.size() - 1);
            } else {
                page = type(key, false, false, true, i == keys.size() - 1);
            }
        }
    }
    return page;
}
Also used : SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) Page(com.gargoylesoftware.htmlunit.Page) SelectableTextInput(com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput)

Example 3 with SelectableTextInput

use of com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput in project htmlunit by HtmlUnit.

the class TextRange method setText.

/**
 * Sets the text contained within the range.
 * @param text the text contained within the range
 */
@JsxSetter
public void setText(final String text) {
    if (range_.getStartContainer() == range_.getEndContainer() && range_.getStartContainer() instanceof SelectableTextInput) {
        final SelectableTextInput input = (SelectableTextInput) range_.getStartContainer();
        final String oldValue = input.getText();
        input.setText(oldValue.substring(0, input.getSelectionStart()) + text + oldValue.substring(input.getSelectionEnd()));
    }
}
Also used : SelectableTextInput(com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 4 with SelectableTextInput

use of com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput in project htmlunit by HtmlUnit.

the class TextRange method moveEnd.

/**
 * Changes the end position of the range.
 * @param unit specifies the units to move
 * @param count the number of units to move
 * @return the number of units moved
 */
@JsxFunction
public int moveEnd(final String unit, final Object count) {
    if (!"character".equals(unit)) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("moveEnd('" + unit + "') is not yet supported");
        }
        return 0;
    }
    int c = 1;
    if (!Undefined.isUndefined(count)) {
        c = (int) Context.toNumber(count);
    }
    if (range_.getStartContainer() == range_.getEndContainer() && range_.getStartContainer() instanceof SelectableTextInput) {
        final SelectableTextInput input = (SelectableTextInput) range_.getStartContainer();
        c = constrainMoveBy(c, range_.getEndOffset(), input.getText().length());
        range_.setEnd(input, range_.getEndOffset() + c);
    }
    return c;
}
Also used : SelectableTextInput(com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 5 with SelectableTextInput

use of com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput in project htmlunit by HtmlUnit.

the class TextRange method moveStart.

/**
 * Changes the start position of the range.
 * @param unit specifies the units to move
 * @param count the number of units to move
 * @return the number of units moved
 */
@JsxFunction
public int moveStart(final String unit, final Object count) {
    if (!"character".equals(unit)) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("moveStart('" + unit + "') is not yet supported");
        }
        return 0;
    }
    int c = 1;
    if (!Undefined.isUndefined(count)) {
        c = (int) Context.toNumber(count);
    }
    if (range_.getStartContainer() == range_.getEndContainer() && range_.getStartContainer() instanceof SelectableTextInput) {
        final SelectableTextInput input = (SelectableTextInput) range_.getStartContainer();
        c = constrainMoveBy(c, range_.getStartOffset(), input.getText().length());
        range_.setStart(input, range_.getStartOffset() + c);
    }
    return c;
}
Also used : SelectableTextInput(com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

SelectableTextInput (com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput)5 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 Page (com.gargoylesoftware.htmlunit.Page)1 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)1 SimpleRange (com.gargoylesoftware.htmlunit.html.impl.SimpleRange)1 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)1 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)1 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)1 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)1