Fixes #9653tags/8.1.1
*/ | */ | ||||
package com.vaadin.client.ui.textfield; | package com.vaadin.client.ui.textfield; | ||||
import com.google.gwt.event.dom.client.HasBlurHandlers; | |||||
import com.google.gwt.user.client.ui.Widget; | import com.google.gwt.user.client.ui.Widget; | ||||
import com.vaadin.client.DeferredWorker; | import com.vaadin.client.DeferredWorker; | ||||
import com.vaadin.client.annotations.OnStateChange; | import com.vaadin.client.annotations.OnStateChange; | ||||
/** | /** | ||||
* Connector class for AbstractTextField. | * Connector class for AbstractTextField. | ||||
* | |||||
* | |||||
* @since 8.0 | * @since 8.0 | ||||
*/ | */ | ||||
public abstract class AbstractTextFieldConnector extends AbstractFieldConnector | public abstract class AbstractTextFieldConnector extends AbstractFieldConnector | ||||
new AbstractTextFieldClientRpcImpl()); | new AbstractTextFieldClientRpcImpl()); | ||||
ConnectorFocusAndBlurHandler.addHandlers(this); | ConnectorFocusAndBlurHandler.addHandlers(this); | ||||
valueChangeHandler = new ValueChangeHandler(this); | valueChangeHandler = new ValueChangeHandler(this); | ||||
// Ensures that the cursor position is sent when leaving the field | |||||
// (if it has changed) | |||||
if (getWidget() instanceof HasBlurHandlers) { | |||||
((HasBlurHandlers) getWidget()) | |||||
.addBlurHandler(event -> sendValueChange()); | |||||
} | |||||
} | } | ||||
protected ValueChangeHandler getValueChangeHandler() { | protected ValueChangeHandler getValueChangeHandler() { |
package com.vaadin.tests.components.textarea; | |||||
import com.vaadin.annotations.Widgetset; | |||||
import com.vaadin.server.VaadinRequest; | |||||
import com.vaadin.tests.components.AbstractTestUIWithLog; | |||||
import com.vaadin.ui.AbstractTextField; | |||||
import com.vaadin.ui.Button; | |||||
import com.vaadin.ui.TextArea; | |||||
import com.vaadin.ui.TextField; | |||||
@Widgetset("com.vaadin.DefaultWidgetSet") | |||||
public class TextAreaTextFieldCursorPosition extends AbstractTestUIWithLog { | |||||
public static final String GET_POSITION = "getposition"; | |||||
public static final String INSERT = "insert"; | |||||
@Override | |||||
protected void setup(VaadinRequest request) { | |||||
TextArea textArea = new TextArea("Simple Text Area"); | |||||
textArea.setValue("I am just a piece of random text"); | |||||
textArea.setWidth("500px"); | |||||
addComponent(textArea); | |||||
TextField textField = new TextField("Simple Text field"); | |||||
textField.setValue("I am just a piece of random text"); | |||||
textField.setWidth("500px"); | |||||
addComponent(textField); | |||||
Button posButton = new Button("Get Position"); | |||||
posButton.setId(GET_POSITION); | |||||
posButton.addClickListener(c -> { | |||||
log("TextArea position: " + textArea.getCursorPosition()); | |||||
log("TextField position: " + textField.getCursorPosition()); | |||||
}); | |||||
addComponent(posButton); | |||||
Button insertButton = new Button("Insert"); | |||||
insertButton.setId(INSERT); | |||||
insertButton.addClickListener(c -> { | |||||
insert(textArea); | |||||
insert(textField); | |||||
}); | |||||
addComponent(insertButton); | |||||
} | |||||
private void insert(AbstractTextField field) { | |||||
String value = field.getValue(); | |||||
if (field.getCursorPosition() != -1) { | |||||
int pos = field.getCursorPosition(); | |||||
log("Insert position: " + field.getCursorPosition()); | |||||
value = value.substring(0, pos) + "-insertedtext-" | |||||
+ value.substring(pos, value.length()); | |||||
} else { | |||||
value += "-appendedtext"; | |||||
} | |||||
field.setValue(value); | |||||
} | |||||
} |
/* | |||||
* Copyright 2000-2016 Vaadin Ltd. | |||||
* | |||||
* 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. | |||||
*/ | |||||
package com.vaadin.tests.components.textarea; | |||||
import org.junit.Assert; | |||||
import org.junit.Test; | |||||
import com.vaadin.testbench.elements.ButtonElement; | |||||
import com.vaadin.testbench.elements.TextAreaElement; | |||||
import com.vaadin.testbench.elements.TextFieldElement; | |||||
import com.vaadin.tests.tb3.SingleBrowserTest; | |||||
public class TextAreaTextFieldCursorPositionTest extends SingleBrowserTest { | |||||
@Test | |||||
public void positionUpdatedWithoutTextChanges() { | |||||
openTestURL(); | |||||
$(ButtonElement.class).id(TextAreaTextFieldCursorPosition.GET_POSITION) | |||||
.click(); | |||||
Assert.assertEquals("2. TextField position: -1", getLogRow(0)); | |||||
Assert.assertEquals("1. TextArea position: -1", getLogRow(1)); | |||||
$(TextFieldElement.class).first().focus(); | |||||
$(TextAreaElement.class).first().focus(); | |||||
$(ButtonElement.class).id(TextAreaTextFieldCursorPosition.GET_POSITION) | |||||
.click(); | |||||
Assert.assertTrue(getLogRow(0).startsWith("4. TextField position:")); | |||||
Assert.assertNotEquals("4. TextField position: -1", getLogRow(0)); | |||||
Assert.assertNotEquals("3. TextArea position: -1", getLogRow(1)); | |||||
} | |||||
} |