Search in sources :

Example 1 with Rule

use of com.github.bordertech.wcomponents.subordinate.Rule in project wcomponents by BorderTech.

the class WDropdownOptionsExample method applySettings.

/**
 * Apply the settings from the control table to the drop down.
 */
private void applySettings() {
    container.reset();
    infoPanel.reset();
    // create the list of options.
    List<String> options = new ArrayList<>(Arrays.asList(OPTIONS_ARRAY));
    if (cbNullOption.isSelected()) {
        options.add(0, "");
    }
    // create the dropdown.
    final WDropdown dropdown = new WDropdown(options);
    // set the dropdown type.
    dropdown.setType((DropdownType) rbsDDType.getSelected());
    // set the selected option if applicable.
    String selected = (String) rgDefaultOption.getSelected();
    if (selected != null && !NONE.equals(selected)) {
        dropdown.setSelected(selected);
    }
    // set the width.
    if (nfWidth.getValue() != null) {
        dropdown.setOptionWidth(nfWidth.getValue().intValue());
    }
    // set the tool tip.
    if (tfToolTip.getText() != null && tfToolTip.getText().length() > 0) {
        dropdown.setToolTip(tfToolTip.getText());
    }
    // set misc options.
    dropdown.setVisible(cbVisible.isSelected());
    dropdown.setDisabled(cbDisabled.isSelected());
    // add the action for action on change, ajax and subordinate.
    if (cbActionOnChange.isSelected() || cbAjax.isSelected() || cbSubmitOnChange.isSelected()) {
        final WStyledText info = new WStyledText();
        info.setWhitespaceMode(WhitespaceMode.PRESERVE);
        infoPanel.add(info);
        dropdown.setActionOnChange(new Action() {

            @Override
            public void execute(final ActionEvent event) {
                String selectedOption = (String) dropdown.getSelected();
                info.setText(selectedOption);
            }
        });
    }
    // this has to be below the set action on change so it is
    // not over written.
    dropdown.setSubmitOnChange(cbSubmitOnChange.isSelected());
    // add the ajax target.
    if (cbAjax.isSelected()) {
        WAjaxControl update = new WAjaxControl(dropdown);
        update.addTarget(infoPanel);
        container.add(update);
    }
    // add the subordinate stuff.
    if (rbsDDType.getValue() == WDropdown.DropdownType.COMBO) {
        // This is to work around a WComponent Subordinate logic flaw.
        cbSubordinate.setSelected(false);
    }
    if (cbSubordinate.isSelected()) {
        WComponentGroup<SubordinateTarget> group = new WComponentGroup<>();
        container.add(group);
        WSubordinateControl control = new WSubordinateControl();
        container.add(control);
        for (String option : OPTIONS_ARRAY) {
            buildSubordinatePanel(dropdown, option, group, control);
        }
        // add a rule for none selected.
        Rule rule = new Rule();
        control.addRule(rule);
        rule.setCondition(new Equal(dropdown, ""));
        rule.addActionOnTrue(new Hide(group));
    }
    WFieldLayout flay = new WFieldLayout();
    flay.setLabelWidth(25);
    container.add(flay);
    flay.addField("Configured dropdown", dropdown);
    flay.addField((WLabel) null, new WButton("Submit"));
}
Also used : Hide(com.github.bordertech.wcomponents.subordinate.Hide) Action(com.github.bordertech.wcomponents.Action) WAjaxControl(com.github.bordertech.wcomponents.WAjaxControl) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) ArrayList(java.util.ArrayList) WSubordinateControl(com.github.bordertech.wcomponents.subordinate.WSubordinateControl) WStyledText(com.github.bordertech.wcomponents.WStyledText) WComponentGroup(com.github.bordertech.wcomponents.WComponentGroup) WButton(com.github.bordertech.wcomponents.WButton) SubordinateTarget(com.github.bordertech.wcomponents.SubordinateTarget) WDropdown(com.github.bordertech.wcomponents.WDropdown) Equal(com.github.bordertech.wcomponents.subordinate.Equal) WFieldLayout(com.github.bordertech.wcomponents.WFieldLayout) Rule(com.github.bordertech.wcomponents.subordinate.Rule) WHorizontalRule(com.github.bordertech.wcomponents.WHorizontalRule)

Example 2 with Rule

use of com.github.bordertech.wcomponents.subordinate.Rule in project wcomponents by BorderTech.

the class WDropdownOptionsExample method getDropDownControls.

/**
 * build the drop down controls.
 *
 * @return a field set containing the dropdown controls.
 */
private WFieldSet getDropDownControls() {
    WFieldSet fieldSet = new WFieldSet("Drop down configuration");
    WFieldLayout layout = new WFieldLayout();
    layout.setLabelWidth(25);
    fieldSet.add(layout);
    rbsDDType.setButtonLayout(WRadioButtonSelect.LAYOUT_FLAT);
    rbsDDType.setSelected(WDropdown.DropdownType.NATIVE);
    rbsDDType.setFrameless(true);
    layout.addField("Dropdown Type", rbsDDType);
    nfWidth.setMinValue(0);
    nfWidth.setDecimalPlaces(0);
    layout.addField("Width", nfWidth);
    layout.addField("ToolTip", tfToolTip);
    layout.addField("Include null option", cbNullOption);
    rgDefaultOption.setButtonLayout(WRadioButtonSelect.LAYOUT_COLUMNS);
    rgDefaultOption.setButtonColumns(2);
    rgDefaultOption.setSelected(NONE);
    rgDefaultOption.setFrameless(true);
    layout.addField("Default Option", rgDefaultOption);
    layout.addField("Action on change", cbActionOnChange);
    layout.addField("Ajax", cbAjax);
    WField subField = layout.addField("Subordinate", cbSubordinate);
    // .getLabel().setHint("Does not work with Dropdown Type COMBO");
    layout.addField("Submit on change", cbSubmitOnChange);
    layout.addField("Visible", cbVisible);
    layout.addField("Disabled", cbDisabled);
    // Apply Button
    WButton apply = new WButton("Apply");
    fieldSet.add(apply);
    WSubordinateControl subSubControl = new WSubordinateControl();
    Rule rule = new Rule();
    subSubControl.addRule(rule);
    rule.setCondition(new Equal(rbsDDType, WDropdown.DropdownType.COMBO));
    rule.addActionOnTrue(new Disable(subField));
    rule.addActionOnFalse(new Enable(subField));
    fieldSet.add(subSubControl);
    apply.setAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
            applySettings();
        }
    });
    return fieldSet;
}
Also used : WField(com.github.bordertech.wcomponents.WField) Action(com.github.bordertech.wcomponents.Action) WFieldSet(com.github.bordertech.wcomponents.WFieldSet) Equal(com.github.bordertech.wcomponents.subordinate.Equal) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) WFieldLayout(com.github.bordertech.wcomponents.WFieldLayout) WSubordinateControl(com.github.bordertech.wcomponents.subordinate.WSubordinateControl) Enable(com.github.bordertech.wcomponents.subordinate.Enable) Rule(com.github.bordertech.wcomponents.subordinate.Rule) WHorizontalRule(com.github.bordertech.wcomponents.WHorizontalRule) WButton(com.github.bordertech.wcomponents.WButton) Disable(com.github.bordertech.wcomponents.subordinate.Disable)

Example 3 with Rule

use of com.github.bordertech.wcomponents.subordinate.Rule in project wcomponents by BorderTech.

the class WVideoExample method buildUI.

/**
 * Build the UI for this example.
 */
private void buildUI() {
    // build the configuration options UI.
    WFieldLayout layout = new WFieldLayout(WFieldLayout.LAYOUT_STACKED);
    layout.setMargin(new Margin(null, null, Size.LARGE, null));
    add(layout);
    layout.addField("Autoplay", cbAutoPlay);
    layout.addField("Loop", cbLoop);
    layout.addField("Mute", cbMute);
    layout.addField("Disable", cbDisable);
    layout.addField("Show separate play/pause", cbControls);
    layout.addField((WLabel) null, btnApply);
    // add the video to the UI
    add(video);
    // disable mute and enable disable if PLAY_PAUSE is used
    WSubordinateControl control = new WSubordinateControl();
    add(control);
    Rule rule = new Rule();
    rule.setCondition(new Equal(cbControls, Boolean.TRUE.toString()));
    rule.addActionOnTrue(new Disable(cbMute));
    rule.addActionOnTrue(new Enable(cbDisable));
    rule.addActionOnFalse(new Enable(cbMute));
    rule.addActionOnFalse(new Disable(cbDisable));
    control.addRule(rule);
    // Allow the config to be updated without reloading the whole UI.
    add(new WAjaxControl(btnApply, video));
}
Also used : WAjaxControl(com.github.bordertech.wcomponents.WAjaxControl) Equal(com.github.bordertech.wcomponents.subordinate.Equal) WFieldLayout(com.github.bordertech.wcomponents.WFieldLayout) WSubordinateControl(com.github.bordertech.wcomponents.subordinate.WSubordinateControl) Enable(com.github.bordertech.wcomponents.subordinate.Enable) Rule(com.github.bordertech.wcomponents.subordinate.Rule) Disable(com.github.bordertech.wcomponents.subordinate.Disable) Margin(com.github.bordertech.wcomponents.Margin)

Example 4 with Rule

use of com.github.bordertech.wcomponents.subordinate.Rule in project wcomponents by BorderTech.

the class WSubordinateControlRenderer method doRender.

/**
 * Paints the given SubordinateControl.
 *
 * @param component the SubordinateControl to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WSubordinateControl subordinate = (WSubordinateControl) component;
    XmlStringBuilder xml = renderContext.getWriter();
    if (!subordinate.getRules().isEmpty()) {
        int seq = 0;
        for (Rule rule : subordinate.getRules()) {
            xml.appendTagOpen("ui:subordinate");
            xml.appendAttribute("id", subordinate.getId() + "-c" + seq++);
            xml.appendClose();
            paintRule(rule, xml);
            xml.appendEndTag("ui:subordinate");
        }
    }
}
Also used : WSubordinateControl(com.github.bordertech.wcomponents.subordinate.WSubordinateControl) Rule(com.github.bordertech.wcomponents.subordinate.Rule) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 5 with Rule

use of com.github.bordertech.wcomponents.subordinate.Rule in project wcomponents by BorderTech.

the class SubordinateControlInterceptor_Test method testServiceRequestApplyControls.

@Test
public void testServiceRequestApplyControls() {
    // Create Target
    WButton target = new WButton();
    target.setAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
            buttonClicked = true;
        }
    });
    // Create Control - Enable/Disable Button
    WCheckBox box = new WCheckBox();
    Rule rule = new Rule();
    rule.setCondition(new Equal(box, Boolean.TRUE));
    rule.addActionOnTrue(new Enable(target));
    rule.addActionOnFalse(new Disable(target));
    WSubordinateControl control = new WSubordinateControl();
    control.addRule(rule);
    // Create component tree
    WContainer root = new WContainer();
    root.add(control);
    root.add(box);
    root.add(target);
    // Setup Intercepter
    SubordinateControlInterceptor interceptor = new SubordinateControlInterceptor();
    interceptor.setBackingComponent(root);
    UIContext uic = createUIContext();
    uic.setUI(root);
    setActiveContext(uic);
    buttonClicked = false;
    // Test Service Request - Empty Request and no control registered, so the control should not be applied
    MockRequest request = new MockRequest();
    interceptor.serviceRequest(request);
    // Target should still be enabled (control not applied)
    Assert.assertFalse("After service request target should be enabled", target.isDisabled());
    // Button not clicked
    Assert.assertFalse("Button should not have been clicked", buttonClicked);
    // Test Service Request - Try to click button while it is disabled and should not be clicked
    target.setDisabled(true);
    request.setParameter(target.getId(), "x");
    interceptor.serviceRequest(request);
    // Target should still be disabled (control not applied, as still not registered)
    Assert.assertTrue("After service request target should be disabled", target.isDisabled());
    // Button not clicked
    Assert.assertFalse("Button should not have been clicked while disabled", buttonClicked);
    // Test Prepare Paint - Should register and apply the subordinate control
    target.setDisabled(false);
    request = new MockRequest();
    interceptor.preparePaint(request);
    // Target should be disabled (Disabled by control as box is not selected)
    Assert.assertTrue("After service request target should be disabled", target.isDisabled());
    // Test Service Request - Simulate button click as it was enabled on the client by the check box being selected.
    // As the controls have been registered from the Prepare Paint, they will be applied in the Service Request and
    // this will enable the button and allow it to be clicked.
    buttonClicked = false;
    request.setParameter(target.getId(), "x");
    setupCheckBoxRequest(box, request, true);
    interceptor.serviceRequest(request);
    // Target should be enabled (enabled by control as box is selected)
    Assert.assertFalse("After service request target should be enabled", target.isDisabled());
    // Button should have been clicked
    Assert.assertTrue("Button should have been clicked", buttonClicked);
// // Check Subordinate Controls have not been cleared from session
// Assert.assertNotNull("Registered Controls should not have been cleared on the session",
// request.getSessionAttribute(SubordinateControlHelper.SUBORDINATE_CONTROL_SESSION_KEY));
// 
// interceptor.preparePaint(request);
// 
// // Check Subordinate Controls have been cleared from session
// Assert.assertNull("Registered Controls should have been cleared on the session",
// request.getSessionAttribute(SubordinateControlHelper.SUBORDINATE_CONTROL_SESSION_KEY));
}
Also used : Action(com.github.bordertech.wcomponents.Action) WContainer(com.github.bordertech.wcomponents.WContainer) UIContext(com.github.bordertech.wcomponents.UIContext) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) WSubordinateControl(com.github.bordertech.wcomponents.subordinate.WSubordinateControl) WButton(com.github.bordertech.wcomponents.WButton) WCheckBox(com.github.bordertech.wcomponents.WCheckBox) Equal(com.github.bordertech.wcomponents.subordinate.Equal) Enable(com.github.bordertech.wcomponents.subordinate.Enable) Rule(com.github.bordertech.wcomponents.subordinate.Rule) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Disable(com.github.bordertech.wcomponents.subordinate.Disable) Test(org.junit.Test)

Aggregations

Rule (com.github.bordertech.wcomponents.subordinate.Rule)27 WSubordinateControl (com.github.bordertech.wcomponents.subordinate.WSubordinateControl)26 Equal (com.github.bordertech.wcomponents.subordinate.Equal)25 SubordinateTarget (com.github.bordertech.wcomponents.SubordinateTarget)17 WContainer (com.github.bordertech.wcomponents.WContainer)16 WTextField (com.github.bordertech.wcomponents.WTextField)15 GreaterThanOrEqual (com.github.bordertech.wcomponents.subordinate.GreaterThanOrEqual)15 LessThanOrEqual (com.github.bordertech.wcomponents.subordinate.LessThanOrEqual)15 NotEqual (com.github.bordertech.wcomponents.subordinate.NotEqual)15 Test (org.junit.Test)15 WCheckBox (com.github.bordertech.wcomponents.WCheckBox)13 Hide (com.github.bordertech.wcomponents.subordinate.Hide)13 Show (com.github.bordertech.wcomponents.subordinate.Show)12 SubordinateTrigger (com.github.bordertech.wcomponents.SubordinateTrigger)11 WComponentGroup (com.github.bordertech.wcomponents.WComponentGroup)9 Disable (com.github.bordertech.wcomponents.subordinate.Disable)8 Enable (com.github.bordertech.wcomponents.subordinate.Enable)8 WHorizontalRule (com.github.bordertech.wcomponents.WHorizontalRule)6 WFieldLayout (com.github.bordertech.wcomponents.WFieldLayout)5 Condition (com.github.bordertech.wcomponents.subordinate.Condition)5