Search in sources :

Example 16 with Action

use of com.github.bordertech.wcomponents.Action 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)

Example 17 with Action

use of com.github.bordertech.wcomponents.Action in project wcomponents by BorderTech.

the class WSuggestionsRenderer_Test method testDoPaintLookupTableOptions.

@Test
public void testDoPaintLookupTableOptions() throws IOException, SAXException, XpathException {
    WSuggestions field = new WSuggestions(TestLookupTable.CACHEABLE_DAY_OF_WEEK_TABLE);
    assertSchemaMatch(field);
    assertXpathEvaluatesTo(field.getId(), "//ui:suggestions/@id", field);
    assertXpathNotExists("//ui:suggestions/@ajax", field);
    assertXpathNotExists("//ui:suggestions/@min", field);
    assertXpathNotExists("//ui:suggestions/suggestion", field);
    assertXpathEvaluatesTo(field.getListCacheKey(), "//ui:suggestions/@data", field);
    // If action set with a lookup table, AJAX flag should still be not set
    field.setRefreshAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
        // Do nothing
        }
    });
    assertSchemaMatch(field);
    assertXpathNotExists("//ui:suggestions/@ajax", field);
}
Also used : Action(com.github.bordertech.wcomponents.Action) WSuggestions(com.github.bordertech.wcomponents.WSuggestions) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) Test(org.junit.Test)

Example 18 with Action

use of com.github.bordertech.wcomponents.Action in project wcomponents by BorderTech.

the class WSuggestionsRenderer_Test method testDoPaintAjaxOptions.

@Test
public void testDoPaintAjaxOptions() throws IOException, SAXException, XpathException {
    List<String> options = Arrays.asList("A", "B", "C");
    WSuggestions field = new WSuggestions(options);
    // Set action for AJAX refresh
    field.setRefreshAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
        // Do nothing
        }
    });
    assertSchemaMatch(field);
    assertXpathEvaluatesTo(field.getId(), "//ui:suggestions/@id", field);
    assertXpathNotExists("//ui:suggestions/@min", field);
    assertXpathNotExists("//ui:suggestions/@data", field);
    // AJAX flag should be true
    assertXpathEvaluatesTo("true", "//ui:suggestions/@ajax", field);
    // Suggestions should only be rendered when refreshed via AJAX
    assertXpathNotExists("//ui:suggestions/ui:suggestion", field);
    // Setup suggestions as the current AJAX trigger
    UIContext uic = createUIContext();
    uic.setUI(new DefaultWComponent());
    setActiveContext(uic);
    try {
        AjaxOperation operation = new AjaxOperation(field.getId(), field.getId());
        AjaxHelper.setCurrentOperationDetails(operation, null);
        assertSchemaMatch(field);
        assertXpathExists("//ui:suggestions/ui:suggestion", field);
        assertXpathEvaluatesTo(options.get(0), "//ui:suggestions/ui:suggestion[1]/@value", field);
        assertXpathEvaluatesTo(options.get(1), "//ui:suggestions/ui:suggestion[2]/@value", field);
        assertXpathEvaluatesTo(options.get(2), "//ui:suggestions/ui:suggestion[3]/@value", field);
    } finally {
        AjaxHelper.clearCurrentOperationDetails();
    }
}
Also used : Action(com.github.bordertech.wcomponents.Action) AjaxOperation(com.github.bordertech.wcomponents.AjaxOperation) UIContext(com.github.bordertech.wcomponents.UIContext) WSuggestions(com.github.bordertech.wcomponents.WSuggestions) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) DefaultWComponent(com.github.bordertech.wcomponents.DefaultWComponent) Test(org.junit.Test)

Example 19 with Action

use of com.github.bordertech.wcomponents.Action in project wcomponents by BorderTech.

the class SimpleTabs method addTab.

/**
 * Adds a tab.
 *
 * @param card the tab content.
 * @param name the tab name.
 */
public void addTab(final WComponent card, final String name) {
    WContainer titledCard = new WContainer();
    WText title = new WText("<b>[" + name + "]:</b><br/>");
    title.setEncodeText(false);
    titledCard.add(title);
    titledCard.add(card);
    deck.add(titledCard);
    final TabButton button = new TabButton(name, titledCard);
    button.setAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
            deck.makeVisible(button.getAssociatedCard());
        }
    });
    btnPanel.add(button);
}
Also used : WContainer(com.github.bordertech.wcomponents.WContainer) Action(com.github.bordertech.wcomponents.Action) WText(com.github.bordertech.wcomponents.WText) ActionEvent(com.github.bordertech.wcomponents.ActionEvent)

Example 20 with Action

use of com.github.bordertech.wcomponents.Action in project wcomponents by BorderTech.

the class WRadioButtonTriggerActionExample method setup.

/**
 * Add controls to the UI.
 */
private void setup() {
    setLayout(new FlowLayout(FlowLayout.VERTICAL));
    WFieldSet fset = new WFieldSet("Select a meal");
    add(fset);
    fset.setMargin(new Margin(null, null, Size.LARGE, null));
    WFieldLayout flay = new WFieldLayout(WFieldLayout.LAYOUT_STACKED);
    fset.add(flay);
    flay.setLabelWidth(0);
    flay.addField("Breakfast", rb1);
    flay.addField("Lunch", rb2);
    flay.addField("Dinner", rb3);
    /*
		 * NOTE: you should never use submitOnChange with a WRadioButton
		 */
    fset.add(new WAjaxControl(mealSelections, textBox));
    mealSelections.setActionOnChange(new Action() {

        @Override
        public void execute(final ActionEvent event) {
            String selection = null;
            if (rb1.isSelected()) {
                selection = "Breakfast selected";
            }
            if (rb2.isSelected()) {
                selection = "Lunch selected";
            }
            if (rb3.isSelected()) {
                selection = "Dinner selected";
            }
            text1.setText(selection + " : " + (new Date()).toString());
        }
    });
    textBox.add(text1);
    add(textBox);
    add(mealSelections);
}
Also used : WAjaxControl(com.github.bordertech.wcomponents.WAjaxControl) Action(com.github.bordertech.wcomponents.Action) FlowLayout(com.github.bordertech.wcomponents.layout.FlowLayout) WFieldSet(com.github.bordertech.wcomponents.WFieldSet) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) WFieldLayout(com.github.bordertech.wcomponents.WFieldLayout) Date(java.util.Date) Margin(com.github.bordertech.wcomponents.Margin)

Aggregations

Action (com.github.bordertech.wcomponents.Action)28 ActionEvent (com.github.bordertech.wcomponents.ActionEvent)27 WButton (com.github.bordertech.wcomponents.WButton)20 WAjaxControl (com.github.bordertech.wcomponents.WAjaxControl)15 WHeading (com.github.bordertech.wcomponents.WHeading)12 WFieldLayout (com.github.bordertech.wcomponents.WFieldLayout)8 WTextField (com.github.bordertech.wcomponents.WTextField)7 WPanel (com.github.bordertech.wcomponents.WPanel)6 ValidatingAction (com.github.bordertech.wcomponents.validation.ValidatingAction)6 WCheckBoxSelect (com.github.bordertech.wcomponents.WCheckBoxSelect)4 WContainer (com.github.bordertech.wcomponents.WContainer)4 WLabel (com.github.bordertech.wcomponents.WLabel)4 WMenu (com.github.bordertech.wcomponents.WMenu)4 WMenuItem (com.github.bordertech.wcomponents.WMenuItem)4 WRadioButtonSelect (com.github.bordertech.wcomponents.WRadioButtonSelect)4 ExplanatoryText (com.github.bordertech.wcomponents.examples.common.ExplanatoryText)4 Margin (com.github.bordertech.wcomponents.Margin)3 WText (com.github.bordertech.wcomponents.WText)3 Equal (com.github.bordertech.wcomponents.subordinate.Equal)3 Rule (com.github.bordertech.wcomponents.subordinate.Rule)3