/** * Creates a WField with the specified label text and field. * * @param labelText contains the textual label to be displayed next to the input field. * @param field is the component to be layed out (normally in the right hand column). In order to support * validation, the given field must be a WInput or a component that contains one WInput as a descendant. */ WField(final String labelText, final WComponent field) { this(new WLabel(labelText), field); }
/** * Gets the label text (if label exists) else returns <code>null</code>. * * @return the text of the label component of this field. */ public String getLabelText() { String text = null; if (label != null) { text = label.getText(); } return text; }
/** * {@inheritDoc} */ @Override public String getId() { if (getIdName() == null && getForComponent() != null && getForComponent().getIdName() != null) { return getForComponent().getId() + ID_FRAMEWORK_ASSIGNED_SEPERATOR + "lbl"; } return super.getId(); }
@Test public void testSanitizeOnOutput() { WLabel label = new WLabel("<form>content</form>"); label.setSanitizeOnOutput(true); label.setEncodeText(false); Assert.assertEquals("Expect output to be sanitized", "content", label.getText()); }
@Test public void testToString() { MyTarget target = new MyTarget(); Disable action = new Disable(target); Assert.assertEquals("Incorrect toString for action", "disable MyTarget", action.toString()); WLabel label = new WLabel("test label", target); Assert.assertEquals("Incorrect toString for action with a label", "disable " + label. getText(), action.toString()); }
@Test public void testSpecialCharacters() { WLabel lbl = new WLabel(); Assert.assertNull("Default text should be null by default", lbl.getText()); lbl.setLocked(true); setActiveContext(createUIContext()); lbl.setText(null); Assert.assertNull("Session text should be null after set to null", lbl.getText()); String myText = "a!@#$%^&()-=_+\\[]~{}|<>:;',.?/`~z"; lbl.setText(myText); Assert.assertEquals("getText returned incorrect session text", myText, lbl.getText()); resetContext(); lbl.setText(myText); Assert.assertEquals("getText returned incorrect default text", myText, lbl.getText()); }
@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); }
xml.appendOptionalAttribute("class", component.getHtmlClass()); xml.appendOptionalAttribute("track", component.isTracking(), "true"); xml.appendOptionalAttribute("for", label.getLabelFor()); WComponent what = label.getForComponent(); String whatFor = null; if (what instanceof MultiInputComponent) { xml.appendOptionalAttribute("required", isMandatory, "true"); xml.appendOptionalAttribute("hiddencomponent", (what != null && what.isHidden()), "true"); xml.appendOptionalAttribute("hint", label.getHint()); xml.appendOptionalAttribute("accessKey", Util.upperCase(label.getAccessKeyAsString())); xml.appendOptionalAttribute("hidden", label.isHidden(), "true"); xml.appendOptionalAttribute("toolTip", label.getToolTip()); xml.appendOptionalAttribute("accessibleText", label.getAccessibleText()); xml.appendClose(); xml.append(label.getText(), label.isEncodeText());
WLabel hiddenLabel = new WLabel("Address line two"); hiddenLabel.setHidden(true); fieldsFlat.addField(hiddenLabel, new WTextField()); hiddenLabel = new WLabel("A hidden label for a read only field"); hiddenLabel.setHidden(true); WTextField roField = new WTextField(); roField.setReadOnly(true); roField.setText("This is a read only field with a hidden label"); fieldsFlat.addField(hiddenLabel, roField); hiddenLabel = new WLabel("Hidden label for a compound control"); hiddenLabel.setHidden(true); WCheckBoxSelect cbSelect = new WCheckBoxSelect(new String[]{"Apple", "Cherry", "Orange", "Pineapple", "control label is hidden"}); fieldsFlat.addField(hiddenLabel, cbSelect); WLabel theLabel = new WLabel("WDropdown"); theLabel.setHint("required"); WDropdown theDropdown = new WDropdown(new String[]{null, "option one", "option two", "option three"}); theDropdown.setMandatory(true); WLabel pBarLabel = new WLabel("WProgressBar", pBar); fieldsFlat.addField(pBarLabel, pBar); theLabel = new WLabel() { @Override protected void preparePaintComponent(final Request request) { theLabel = new WLabel("WTextArea"); WStyledText msg = new WStyledText(" these notes are important");
WLabel wLabel = new WLabel(); wLabel.setText("WLABEL"); label.setBody(wLabel); wLabel.setText("WLABEL - UIC"); Assert.assertEquals("Incorrect text value for WLabel Component with uic", wLabel.getText(), label.getText()); Assert.assertEquals("Incorrect text value for WLabel Component", wLabel.getText(), label. getText()); Assert.assertEquals("Incorrect text value set for WLabel Component", wLabel.getText(), label.getText()); getText(), label.getText()); label.setText("WLABEL2 - UIC"); Assert.assertEquals("Incorrect text value set for WLabel Component with uic", wLabel. getText(), label.getText()); Assert.assertEquals("Incorrect text value set for WLabel Component", wLabel.getText(), label.getText());
@Test public void testNoSanitizeOnOutput() { String input = "<form>content</form>"; WLabel label = new WLabel(input); label.setEncodeText(false); Assert.assertEquals("Expect output to not be sanitized", input, label.getText()); }
@Test public void testToXml() { WContainer component = new WContainer(); setActiveContext(createUIContext()); WLabel label = new WLabel(TEST_LABEL); component.add(label); final int nodeId = component.getIndexOfChild(label); final String fieldName = label.getId(); ObjectGraphNode node = new ObjectGraphNode(nodeId, fieldName, label.getClass().getName(), label); String xmlSummary = node.toXml(); Assert.assertTrue("should report correct ID", xmlSummary.indexOf("object id=\"0\"") != -1); Assert.assertTrue("should report correct field name", xmlSummary.indexOf( "field=\"" + fieldName + "\"") != -1); Assert.assertTrue("should report correct class name", xmlSummary.indexOf("type=\"com.github.bordertech.wcomponents.WLabel\"") != -1); Assert.assertTrue("should report correct size", xmlSummary.indexOf("size=\"12\"") != -1); } }
/** * Examples showing WLabel with a nested input control WComponent. * This is VERY dangerous as only a very few WComponents are valid for this scenario. If you go down this route: stop!! * These are really here for framework testing, not as examples as to how to do things. */ private void addNestedFieldExamples() { add(new WHeading(HeadingLevel.H2, "Label nesting which is technically OK")); /* Just because it is OK to do this does not mean you should! So these "examples" have far fewer comments. */ WPanel errorLayoutPanel = new WPanel(); errorLayoutPanel.setLayout(new FlowLayout(FlowLayout.VERTICAL, Size.LARGE)); errorLayoutPanel.setMargin(new Margin(null, null, Size.XL, null)); add(errorLayoutPanel); errorLayoutPanel.add(new ExplanatoryText("This example shows WLabels with a single nested simple form control WTextField." + " This is not a contravention of the HTML specification but you should not do it.")); WLabel outerLabel = new WLabel("Label with nested WTextField and not 'for' anything"); errorLayoutPanel.add(outerLabel); outerLabel.add(new WTextField()); WTextField innerField = new WTextField(); outerLabel = new WLabel("Label 'for' nested WTextField", innerField); errorLayoutPanel.add(outerLabel); outerLabel.add(innerField); }
@Test public void testForComponentAccessors() { WText forComponent1 = new WText(); WText forComponent2 = new WText(); WLabel lbl = new WLabel("test", forComponent1); Assert.assertSame("forComponent accessor incorrect", forComponent1, lbl.getForComponent()); lbl.setForComponent(forComponent2); Assert.assertSame("forComponent accessors incorrect", forComponent2, lbl.getForComponent()); }
@Test public void testHidden() throws IOException, SAXException, XpathException { MyInput comp = new MyInput(); WLabel label = new WLabel("label", comp); label.setHidden(true); assertSchemaMatch(label); assertXpathEvaluatesTo("", "//ui:label/@hiddencomponent", label); assertXpathEvaluatesTo("true", "//ui:label/@hidden", label); }
WLabel checkBoxLabel = new WLabel("Check box", checkBox); checkBoxLabel.setAccessKey('b'); add(checkBoxLabel); add(checkBox); add(group); WRadioButton radioButton = group.addRadioButton(1); WLabel radioButtonLabel = new WLabel("Radio Button", 'x', radioButton); radioButtonLabel.setToolTip("My tool tip"); add(radioButtonLabel); add(radioButton); WLabel textFieldLabel = new WLabel("Text Field", 'f', textField); add(textFieldLabel); add(textField); WLabel dropdownLabel = new WLabel("Dropdown", 'o', dropdown); add(dropdownLabel); add(dropdown); final WCheckBoxSelect select = new WCheckBoxSelect(options); select.setFrameless(true); WLabel label = new WLabel("Select some animals"); label.setAccessKey('O'); layout.addField(label, select); WRadioButtonSelect rbs = new WRadioButtonSelect(options); rbs.setFrameless(true); label = new WLabel("Select an animal"); label.setAccessKey('A');
@Test public void testAccessKeyAccessors() { char accessKey1 = 'B'; char accessKey2 = 'C'; WLabel lbl = new WLabel("test", accessKey1); Assert.assertEquals("accessKey accessor incorrect", accessKey1, lbl.getAccessKey()); lbl.setAccessKey(accessKey2); Assert.assertEquals("accessKey accessors incorrect", accessKey2, lbl.getAccessKey()); }
Assert.assertEquals("Incorrect description text", "The field 'a tooltip' is required", diag.getDescription()); WLabel label = new WLabel("bc", input); Assert.assertEquals("Incorrect description text", "The field 'bc' is required", diag. getDescription()); label.setText("def:"); Assert.assertEquals("Incorrect description text", "The field 'def' is required", diag. getDescription());