From: Guillermo Alvarez Date: Tue, 5 Aug 2014 15:45:14 +0000 (+0300) Subject: Correctly display an item which is too long for the textfield. (#13477) X-Git-Tag: 7.3.0.rc1~11^2~44 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=409a41ce44f577170f4c84211515dbb5815f5642;p=vaadin-framework.git 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 --- 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