/** * Adds a field to the example's layout. * * @param input the input field to add. * @param labelText the label text for the field. * @param labelHint the optional label hint for the field. */ private void addField(final WComponent input, final String labelText, final String labelHint) { WLabel label = new WLabel(labelText, input); if (labelHint != null) { label.setHint(labelHint); } layout.addField(label, input); }
@Override public void executeOnValid(final ActionEvent event) { String regex = pattern.getText(); textField.setPattern(regex); textField.getLabel().setHint(regex); phoneNumberField.setPattern(regex); phoneNumberField.getLabel().setHint(regex); multiTextField.setPattern(regex); multiTextField.getLabel().setHint(regex); } });
/** * Adds a field to the example's layout. * * @param input the input field to add. * @param labelText the label text for the field. * @param labelHint the optional label hint for the field. */ private void addFieldToLayout(final WComponent input, final String labelText, final String labelHint) { WField field = layout.addField(labelText, input); if (labelHint != null) { field.getLabel().setHint(labelHint); } } }
/** * Adds a field to the example's layout. * * @param input the input field to add. * @param labelText the label text for the field. * @param labelHint the optional label hint for the field. */ private void addFieldToLayout(final WComponent input, final String labelText, final String labelHint) { WField field = layout.addField(labelText, input); if (labelHint != null) { field.getLabel().setHint(labelHint); } } }
@Override public void execute(final ActionEvent event) { field2.setMandatory(!field2.isMandatory()); field2.getLabel().setHint( "mandatory and must be at least two characters and may only contain alphabetic characters and must be different from field 1"); } });
@Test public void testHintAccessors() { String hint = "Test hint text"; WLabel lbl = new WLabel("test"); lbl.setHint(hint); Assert.assertEquals("hint accessors incorrect", hint, lbl.getHint()); }
/** * Creates a WDiagnosticComponent. */ public WDiagnosticComponent() { WFieldLayout layout = new WFieldLayout(); layout.setLabelWidth(30); layout.setMargin(new com.github.bordertech.wcomponents.Margin(0, 0, 12, 0)); //mandatory fields inputText1.setMandatory(true); layout.addField("Unique Input 1", inputText1).getLabel().setHint("required"); inputText2.setMandatory(true); layout.addField("Unique Input 2", inputText2).getLabel().setHint( "required and must be different from unique input 1"); add(layout); }
/** * Creates a RepeaterComponent. */ public RepeaterComponent() { fieldSet = new WFieldSet("Repeated Fields"); add(fieldSet); fieldSet.setMargin(new Margin(null, null, Size.LARGE, null)); WFieldLayout fields = new WFieldLayout(); fieldSet.add(fields); field1Text = new WTextField(); field1Text.setMinLength(2); field1Text.setMaxLength(5); WField field1 = fields.addField("Field 1", field1Text); field1Text.setMandatory(true); field1.getLabel().setHint("required"); field2Text = new WTextField(); WField field2 = fields.addField("Field 2", field2Text); field2Text.setMandatory(true); field2.getLabel().setHint("required and must only contain alphabetic characters"); field2.addValidator(new RegExFieldValidator("^[a-zA-Z]*$", "{0} must only contain alphabetic characters.")); }
/** * Creates a field layout with a couple of fields. * * @return a new field layout with content. */ private WFieldLayout createFieldLayout() { WFieldLayout fieldLayout = new WFieldLayout(); fieldLayout.addField("Field 1", new WTextField()); fieldLayout.addField("Field 2", new WTextArea()).getLabel().setHint("hint"); return fieldLayout; }
/** * Make the New user form. */ public NewUserCard() { WFieldLayout layout = new WFieldLayout(); add(layout); WTextField newUserName = new WTextField(); newUserName.setMandatory(true); newUserName.setMinLength(3); newUserName.setMaxLength(16); newUserName.setDefaultSubmitButton(submit); layout.addField("User name", newUserName).getLabel().setHint("User name must be between 3 and 16 characters."); newPassword.setMandatory(true); newPassword.setMinLength(3); newPassword.setMaxLength(16); newPassword.setDefaultSubmitButton(submit); layout.addField("Password", newPassword).getLabel().setHint("Password must be between 3 and 16 characters."); newPassword2.setMandatory(true); newPassword2.setMinLength(3); newPassword2.setMaxLength(16); newPassword2.setDefaultSubmitButton(submit); layout.addField("Repeat password", newPassword2); layout.addField((WLabel) null, submit); returnToLogInButton.setImage("/image/home.png"); returnToLogInButton.setRenderAsLink(true); }
/** * Creates a WDropdownSubmitOnChangeExample. */ public WDropdownSubmitOnChangeExample() { actMessage.setEncodeText(false); add(new ExplanatoryText("Any form control component which is not a WButton will show a visible warning in its label if its " + "submitOnChange property is set true.")); WFieldLayout flay = new WFieldLayout(); add(flay); flay.setLabelWidth(25); flay.setMargin(new Margin(null, null, Size.LARGE, null)); flay.addField("State", stateSelector).getLabel().setHint("Selecting a state will update the available regions."); flay.addField("Region", regionSelector); // Used to test control of visibility as part of submit on change. add(actMessage); actMessage.setVisible(false); stateSelector.setOptions(new String[]{null, STATE_ACT, STATE_NSW, STATE_VIC}); //This is the flag which causes accessibility problems. It may be removed completely in future versions of WComponents. //you should use AJAX instead stateSelector.setSubmitOnChange(true); stateSelector.setActionOnChange(new Action() { @Override public void execute(final ActionEvent event) { updateRegion(); regionSelector.resetData(); } }); regionSelector.setSubmitOnChange(true); }
@Test public void testXssEscaping() throws IOException, SAXException, XpathException { WLabel label = new WLabel(getMaliciousContent()); assertSafeContent(label); label.setHint(getMaliciousAttribute("ui:label")); assertSafeContent(label); label.setToolTip(getMaliciousAttribute("ui:label")); assertSafeContent(label); label.setAccessibleText(getMaliciousAttribute("ui:label")); assertSafeContent(label); }
/** * Creates a RepeaterFields. */ public RepeaterFields() { WFieldLayout fields = new WFieldLayout(); fields.setMargin(new Margin(null, null, Size.LARGE, null)); add(fields); nameText = new WTextField(); WField nameField = fields.addField("Name", nameText); nameText.setMandatory(true); nameField.getLabel().setHint("required"); repeater = new WRepeater(); repeater.setRepeatedComponent(new RepeaterComponent()); add(repeater); WPanel buttonPanel = new WPanel(WPanel.Type.FEATURE); buttonPanel.setMargin(new Margin(Size.LARGE, null, null, null)); add(buttonPanel); buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); submitBtn = new WButton("Submit", 'S'); buttonPanel.add(submitBtn); }
/** * This example creates the WCheckBoxSelect from an array of Strings. */ private void addExampleUsingStringArray() { add(new WHeading(HeadingLevel.H3, "WCheckBoxSelect created using a String array")); String[] options = new String[]{"Dog", "Cat", "Bird", "Turtle"}; final WCheckBoxSelect select = new WCheckBoxSelect(options); select.setToolTip("Animals"); select.setMandatory(true); final WTextField text = new WTextField(); text.setReadOnly(true); text.setText(NO_SELECTION); WButton update = new WButton("Select Animals"); update.setAction(new Action() { @Override public void execute(final ActionEvent event) { String output = select.getSelected().isEmpty() ? NO_SELECTION : "The selected animals are: " + select.getSelected(); text.setText(output); } }); select.setDefaultSubmitButton(update); WLabel animalLabel = new WLabel("A selection is required", select); animalLabel.setHint("mandatory"); add(animalLabel); add(select); add(update); add(text); add(new WAjaxControl(update, text)); }
layout.addField("Link AccessKey", tfAccesskey).getLabel().setHint( "A single upper case letter or digit."); layout.addField("Render as button", cbRenderAsButton);
/** * Creates a SubordinateControlSimpleWMultiSelectExample. */ public SubordinateControlSimpleWMultiSelectExample() { WMultiSelect groupSelect = getGroupSelect(); groupSelect.setOptions(new String[]{OPTION_A, OPTION_B, OPTION_C}); WFieldLayout layout = new WFieldLayout(); layout.setLabelWidth(25); layout.setMargin(new Margin(null, null, Size.LARGE, null)); add(layout); layout.addField("Select one or more options", groupSelect).getLabel().setHint( "Option c requires further information"); WField extraField = layout.addField("Extra information", new WTextField()); WSubordinateControl control = new WSubordinateControl(); add(control); Rule rule = new Rule(); rule.setCondition(new Equal(groupSelect, OPTION_C)); rule.addActionOnTrue(new Show(extraField)); rule.addActionOnFalse(new Hide(extraField)); control.addRule(rule); }
/** * Creates a SubordinateControlCrtWDropdownExample. */ public SubordinateControlCrtWDropdownExample() { WFieldLayout layout = new WFieldLayout(); add(layout); layout.setLabelWidth(25); layout.setMargin(new Margin(null, null, Size.LARGE, null)); WDropdown genderDropdown = new WDropdown(new TableWithNullOption("sex")); WField field = layout.addField("Gender", genderDropdown); field.getLabel().setHint("The 'Male' option requires more information"); WTextField extraField = new WTextField(); WField xtraWField = layout.addField("Extra information", extraField); WSubordinateControl control = new WSubordinateControl(); add(control); Rule rule = new Rule(); rule.setCondition(new Equal(genderDropdown, "M")); rule.addActionOnTrue(new Show(xtraWField)); rule.addActionOnFalse(new Hide(xtraWField)); control.addRule(rule); } }
/** * When a WCheckBoxSelect is added to a WFieldLayout the legend is moved. The first CheckBoxSelect has a frame, the * second doesn't */ private void addInsideAFieldLayoutExamples() { add(new WHeading(HeadingLevel.H3, "WCheckBoxSelect inside a WFieldLayout")); add(new ExplanatoryText("When a WCheckBoxSelect is inside a WField its label is exposed in a way which appears and behaves like a regular " + "HTML label. This allows WCheckBoxSelects to be used in a layout with simple form controls (such as WTextField) and produce a " + "consistent and predicatable interface. The third example in this set uses a null label and a toolTip to hide the labelling " + "element. This can lead to user confusion and is not recommended.")); WFieldLayout layout = new WFieldLayout(); layout.setLabelWidth(25); add(layout); String[] options = new String[]{"Dog", "Cat", "Bird", "Turtle"}; WCheckBoxSelect select = new WCheckBoxSelect(options); layout.addField("Select some animals", select); String[] options2 = new String[]{"Parrot", "Galah", "Cockatoo", "Lyre"}; select = new WCheckBoxSelect(options2); layout.addField("Select some birds", select); select.setFrameless(true); // a tooltip can be used as a label stand-in even in a WField String[] options3 = new String[]{"Carrot", "Beet", "Brocolli", "Bacon - the perfect vegetable"}; select = new WCheckBoxSelect(options3); layout.addField((WLabel) null, select); select.setToolTip("Veggies"); select = new WCheckBoxSelect("australian_state"); layout.addField("Select a state", select).getLabel().setHint("This is an ajax trigger"); add(new WAjaxControl(select, layout)); }
/** * adds a WRadioButtonSelect with setMandatory(true). */ private void addMandatorySelectExample() { add(new WHeading(HeadingLevel.H3, "Mandatory WRadioButtonSelect")); add(new ExplanatoryText("When a WRadioButtonSelect is mandatory it needs a visible labelling element, otherwise many users may not know that " + "the component requires an answer.")); final WRadioButtonSelect select = new WRadioButtonSelect("australian_state"); select.setButtonLayout(WRadioButtonSelect.LAYOUT_FLAT); select.setMandatory(true); add(new WLabel("Mandatory selection", select)); add(select); add(new WHeading(HeadingLevel.H3, "Mandatory WRadioButtonSelect in a WFieldLayout")); WRadioButtonSelect select2 = new WRadioButtonSelect("australian_state"); select2.setButtonLayout(WRadioButtonSelect.LAYOUT_FLAT); select2.setMandatory(true); final WFieldLayout layout = new WFieldLayout(); layout.setLabelWidth(25); add(layout); layout.addField("Required selection", select2).getLabel().setHint("Required"); select2 = new WRadioButtonSelect("australian_state"); select2.setButtonLayout(WRadioButtonSelect.LAYOUT_FLAT); select2.setMandatory(true); select2.setToolTip("Select a state"); layout.addField((WLabel) null, select2); }
@Test public void testDoPaintAllOptions() throws IOException, SAXException, XpathException { WTextArea text = new WTextArea(); text.setText("text1"); WLabel label = new WLabel(); label.setForComponent(text); label.setHint("hint1"); label.setAccessKey('A'); label.setText("label1"); WContainer root = new WContainer(); root.add(label); root.add(text); // Validate Schema assertSchemaMatch(root); // Check Attributes assertXpathEvaluatesTo(label.getId(), "//ui:label/@id", label); assertXpathEvaluatesTo(text.getId(), "//ui:label/@for", label); assertXpathEvaluatesTo("hint1", "//ui:label/@hint", label); assertXpathEvaluatesTo("A", "//ui:label/@accessKey", label); assertXpathEvaluatesTo("input", "//ui:label/@what", label); // Check Label assertXpathEvaluatesTo("label1", "//ui:label", label); // Add Children to Label WTextArea text2 = new WTextArea(); text2.setText("text2"); label.add(text2); assertSchemaMatch(root); assertXpathEvaluatesTo("text2", "//ui:label/ui:textarea", label); }