From b3b5c062b02ca5edfc5527090c027145704ee51a Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Sun, 7 Feb 2016 18:30:54 +0200 Subject: Use error styles for NativeSelect/ListSelect/TwinColSelect in Valo (#19550) Change-Id: Ifc87f48b8145b56c7b83b9ce4aa1db8a698d7d49 --- .../tests/validation/FieldErrorIndication.java | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 uitest/src/main/java/com/vaadin/tests/validation/FieldErrorIndication.java (limited to 'uitest') diff --git a/uitest/src/main/java/com/vaadin/tests/validation/FieldErrorIndication.java b/uitest/src/main/java/com/vaadin/tests/validation/FieldErrorIndication.java new file mode 100644 index 0000000000..3135ce0592 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/validation/FieldErrorIndication.java @@ -0,0 +1,115 @@ +/* + * Copyright 2000-2014 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.validation; + +import java.util.Set; + +import com.vaadin.data.Validator; +import com.vaadin.data.validator.StringLengthValidator; +import com.vaadin.server.UserError; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.AbstractField; +import com.vaadin.ui.ComboBox; +import com.vaadin.ui.Component; +import com.vaadin.ui.Field; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.ListSelect; +import com.vaadin.ui.NativeSelect; +import com.vaadin.ui.PasswordField; +import com.vaadin.ui.RichTextArea; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.TextField; +import com.vaadin.ui.TwinColSelect; +import com.vaadin.ui.VerticalLayout; + +public class FieldErrorIndication extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + HorizontalLayout hl = new HorizontalLayout(); + addComponent(hl); + + VerticalLayout vl = new VerticalLayout(); + hl.addComponent(vl); + + ComboBox comboBox = new ComboBox("ComboBox"); + comboBox.addItem("ok"); + comboBox.addItem("error"); + comboBox.addValidator(new StringLengthValidator("fail", 0, 2, false)); + comboBox.setValue("error"); + + ListSelect listSelect = new ListSelect("ListSelect"); + listSelect.addItem("ok"); + listSelect.addItem("error"); + listSelect.addValidator(new StringLengthValidator("fail", 0, 2, false)); + listSelect.setValue("error"); + + NativeSelect nativeSelect = new NativeSelect("NativeSelect"); + nativeSelect.addItem("ok"); + nativeSelect.addItem("error"); + nativeSelect + .addValidator(new StringLengthValidator("fail", 0, 2, false)); + nativeSelect.setValue("error"); + TwinColSelect twinColSelect = new TwinColSelect("TwinColSelect"); + twinColSelect.addItem("ok"); + twinColSelect.addItem("error"); + twinColSelect.addValidator(new Validator() { + + @Override + public void validate(Object value) throws InvalidValueException { + if (value instanceof Set && ((Set) value).size() == 1 + && ((Set) value).contains("ok")) { + return; + } + + throw new InvalidValueException("fail"); + } + + }); + twinColSelect.setValue("error"); + + vl.addComponents(comboBox, listSelect, nativeSelect, twinColSelect); + + Class[] textFields = new Class[] { + TextField.class, TextArea.class, RichTextArea.class, + PasswordField.class }; + vl = new VerticalLayout(); + hl.addComponent(vl); + for (Class fieldClass : textFields) { + vl.addComponent(getField(fieldClass)); + } + + } + + /** + * @since + * @param fieldClass + * @return + */ + private Component getField(Class fieldClass) { + AbstractField f; + try { + f = (AbstractField) fieldClass.newInstance(); + f.setCaption(fieldClass.getSimpleName()); + f.setComponentError(new UserError("fail")); + return f; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } +} -- cgit v1.2.3