diff options
author | Guillermo Alvarez <guillermo@vaadin.com> | 2014-08-05 18:45:14 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-08-07 10:50:33 +0000 |
commit | 409a41ce44f577170f4c84211515dbb5815f5642 (patch) | |
tree | 5549f4ce7eccf637d3967d82bb84eaef2fe2d279 | |
parent | 80d72d34d9bc2f6686b607177ff6c89ff845728e (diff) | |
download | vaadin-framework-409a41ce44f577170f4c84211515dbb5815f5642.tar.gz vaadin-framework-409a41ce44f577170f4c84211515dbb5815f5642.zip |
Correctly display an item which is too long for the textfield. (#13477)
As setSelectionRange is not working correctly in IE the current approach
sets the direction before setting the text and resets it to the original
immediately after that.
Change-Id: I33f40f9ae436122092d995fa17c35a9cbe38aedb
3 files changed, 150 insertions, 6 deletions
diff --git a/client/src/com/vaadin/client/ui/VFilterSelect.java b/client/src/com/vaadin/client/ui/VFilterSelect.java index 46f90c07fa..6ba0785acc 100644 --- a/client/src/com/vaadin/client/ui/VFilterSelect.java +++ b/client/src/com/vaadin/client/ui/VFilterSelect.java @@ -46,6 +46,7 @@ import com.google.gwt.event.dom.client.LoadEvent; import com.google.gwt.event.dom.client.LoadHandler; import com.google.gwt.event.logical.shared.CloseEvent; import com.google.gwt.event.logical.shared.CloseHandler; +import com.google.gwt.i18n.client.HasDirection.Direction; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; @@ -452,9 +453,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, setSelectedItemIcon(suggestion.getIconUri()); // Set the text. - tb.setText(text); - tb.setSelectionRange(lastFilter.length(), text.length() - - lastFilter.length()); + setText(text); menu.updateKeyboardSelectedItem(); } @@ -916,11 +915,11 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, && !currentSuggestion.key.equals("")) { // An item (not null) selected String text = currentSuggestion.getReplacementString(); - tb.setText(text); + setText(text); selectedOptionKey = currentSuggestion.key; } else { // Null selected - tb.setText(""); + setText(""); selectedOptionKey = null; } } @@ -1060,7 +1059,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, if (textInputEnabled) { super.setSelectionRange(pos, length); } else { - super.setSelectionRange(getValue().length(), 0); + super.setSelectionRange(0, getValue().length()); } } } @@ -1452,7 +1451,19 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, if (enableDebug) { debug("VFS: setTextboxText(" + text + ")"); } + setText(text); + } + + private void setText(final String text) { + /** + * To leave caret in the beginning of the line. + * SetSelectionRange wouldn't work on IE + * (see #13477) + */ + Direction previousDirection = tb.getDirection(); + tb.setDirection(Direction.RTL); tb.setText(text); + tb.setDirection(previousDirection); } /** diff --git a/uitest/src/com/vaadin/tests/components/ui/ComboboxSelectedItemText.java b/uitest/src/com/vaadin/tests/components/ui/ComboboxSelectedItemText.java new file mode 100644 index 0000000000..d4599fc1a3 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/ui/ComboboxSelectedItemText.java @@ -0,0 +1,43 @@ +package com.vaadin.tests.components.ui; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.ComboBox; +import com.vaadin.ui.Label; + +public class ComboboxSelectedItemText extends AbstractTestUIWithLog { + @Override + protected void setup(VaadinRequest request) { + getLayout() + .addComponent( + new Label( + "Select first ANTIGUA AND BARBUDA from the first combobox. Then select ANTIGUA AND BARBUDA from the second combobox. Finally, click the popup button on the first combobox. Before fix you would see UA AND BAR in the field.")); + + ComboBox combobox = new ComboBox("Text input enabled:"); + combobox.setWidth("100px"); + + combobox.addItem("AMERICAN SAMOA"); + combobox.addItem("ANTIGUA AND BARBUDA"); + + ComboBox combobox2 = new ComboBox("Text input disabled:"); + combobox2.setWidth("100px"); + combobox2.setTextInputAllowed(false); + + combobox2.addItem("AMERICAN SAMOA"); + combobox2.addItem("ANTIGUA AND BARBUDA"); + + getLayout().addComponent(combobox); + getLayout().addComponent(combobox2); + } + + @Override + protected String getTestDescription() { + return "Tests selected item is displayed from the beginning"; + } + + @Override + protected Integer getTicketNumber() { + return 13477; + } + +}
\ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/components/ui/ComboboxSelectedItemTextTest.java b/uitest/src/com/vaadin/tests/components/ui/ComboboxSelectedItemTextTest.java new file mode 100644 index 0000000000..f826654022 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/ui/ComboboxSelectedItemTextTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2000-2013 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.ui; + +import java.io.IOException; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Test class for issue #13477, where selecting a combobox item that is too long + * would render the ending of an item instead of the beginning, which was + * considered less than informative. + * + * @author Vaadin Ltd + */ + +public class ComboboxSelectedItemTextTest extends MultiBrowserTest { + + public final String SCREENSHOT_NAME_EDITABLE = "LongComboboxItemSelectedEditable"; + public final String SCREENSHOT_NAME_NON_EDITABLE = "LongComboboxItemSelectedNonEditable"; + public final int INDEX_EDITABLE_COMBOBOX = 1; + public final int INDEX_NON_EDITABLE_COMBOBOX = 2; + + @Test + public void testCombobox() throws IOException { + testCombobox(INDEX_EDITABLE_COMBOBOX, INDEX_NON_EDITABLE_COMBOBOX, + SCREENSHOT_NAME_EDITABLE); + } + + @Test + public void testComboboxNonEditable() throws IOException { + testCombobox(INDEX_NON_EDITABLE_COMBOBOX, INDEX_EDITABLE_COMBOBOX, + SCREENSHOT_NAME_NON_EDITABLE); + } + + private void testCombobox(int indexToTest, int indexToFocus, + String screenshotIdentifier) throws IOException { + openTestURL(); + + WebElement comboBox = vaadinElement("/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[" + + indexToTest + "]/VFilterSelect[0]"); + WebElement comboBoxFocus = vaadinElement("/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[" + + indexToFocus + "]/VFilterSelect[0]"); + + // Select an element from the first (editable) combobox. + + comboBox.findElement(By.className("v-filterselect-button")).click(); + WebElement comboBoxPopup = vaadinElement("/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[" + + indexToTest + "]/VFilterSelect[0]#popup"); + comboBoxPopup.findElements(By.tagName("td")).get(2).click(); + + // Select an element from the second (non-editable combobox) to remove + // focus from the first combobox + + comboBoxFocus.findElement(By.className("v-filterselect-button")) + .click(); + comboBoxPopup = vaadinElement("/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[" + + indexToFocus + "]/VFilterSelect[0]#popup"); + comboBoxPopup.findElements(By.tagName("td")).get(2).click(); + + // click the popup on the first combobox. This would reveal the unwanted + // behaviour. + + comboBox.findElement(By.className("v-filterselect-button")).click(); + + // sadly, screenshot comparison is the only reasonable way to test a + // rendering issue. + + compareScreen(screenshotIdentifier); + + } + +}
\ No newline at end of file |