use of com.github.bordertech.wcomponents.WContainer in project wcomponents by BorderTech.
the class WSubordinateControl_Test method testApplyControls.
@Test
public void testApplyControls() {
WCheckBox box = new WCheckBox();
SubordinateTarget target = new WTextField();
// Create Rule
Rule rule = new Rule();
rule.setCondition(new Equal(box, Boolean.TRUE));
rule.addActionOnTrue(new Hide(target));
rule.addActionOnFalse(new Show(target));
WSubordinateControl control = new WSubordinateControl();
control.addRule(rule);
WContainer root = new WContainer();
root.add(control);
root.add(box);
root.add(target);
setActiveContext(createUIContext());
// Setup true condition - Selected
box.setSelected(true);
// Apply the controls
setFlag(box, ComponentModel.HIDE_FLAG, false);
control.applyTheControls();
Assert.assertTrue("After applyControls target should be hidden", target.isHidden());
setFlag(box, ComponentModel.HIDE_FLAG, false);
// Setup true condition - CheckBox selected in Request
MockRequest request = new MockRequest();
setupCheckBoxRequest(box, request, true);
// ApplyControls with Request
control.applyTheControls(request);
Assert.assertTrue("Request - After applyControls target should be hidden", target.isHidden());
}
use of com.github.bordertech.wcomponents.WContainer in project wcomponents by BorderTech.
the class WValidationErrorsRenderer method doRender.
/**
* Paints the given {@link WValidationErrors} component.
*
* @param component The {@link WValidationErrors} component to paint.
* @param renderContext the RenderContext to paint to.
*/
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
WValidationErrors errors = (WValidationErrors) component;
XmlStringBuilder xml = renderContext.getWriter();
if (errors.hasErrors()) {
xml.appendTagOpen("ui:validationerrors");
xml.appendAttribute("id", component.getId());
xml.appendOptionalAttribute("class", component.getHtmlClass());
xml.appendOptionalAttribute("track", component.isTracking(), "true");
xml.appendOptionalAttribute("title", errors.getTitleText());
xml.appendClose();
for (GroupedDiagnositcs nextGroup : errors.getGroupedErrors()) {
// Render each diagnostic message in this group.
for (Diagnostic nextMessage : nextGroup.getDiagnostics()) {
xml.appendTagOpen("ui:error");
WComponent forComponent = nextMessage.getComponent();
if (forComponent != null) {
UIContextHolder.pushContext(nextMessage.getContext());
try {
xml.appendAttribute("for", forComponent.getId());
} finally {
UIContextHolder.popContext();
}
}
xml.appendClose();
// of a WComponent as the message.
if (nextMessage instanceof DiagnosticImpl) {
WComponent messageComponent = ((DiagnosticImpl) nextMessage).createDiagnosticErrorComponent();
// We add the component to a throw-away container so that it renders with the correct ID.
WContainer container = new WContainer() {
@Override
public String getId() {
return component.getId();
}
};
container.add(messageComponent);
messageComponent.paint(renderContext);
container.remove(messageComponent);
container.reset();
} else {
xml.append(nextMessage.getDescription());
}
xml.appendEndTag("ui:error");
}
}
xml.appendEndTag("ui:validationerrors");
}
}
use of com.github.bordertech.wcomponents.WContainer 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));
}
use of com.github.bordertech.wcomponents.WContainer in project wcomponents by BorderTech.
the class WAjaxControlRenderer_Test method testDoPaint.
@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
WContainer root = new WContainer();
WButton trigger = new WButton("x");
WPanel target1 = new WPanel();
WPanel target2 = new WPanel();
WPanel target3 = new WPanel();
WAjaxControl control = new WAjaxControl(trigger);
root.add(trigger);
root.add(target1);
root.add(target2);
root.add(target3);
root.add(control);
// No Targets
assertSchemaMatch(root);
assertXpathEvaluatesTo("0", "count(//ui:ajaxtrigger)", root);
// With Targets
control.addTargets(new AjaxTarget[] { target1, target2, target3 });
setActiveContext(createUIContext());
assertSchemaMatch(root);
assertXpathEvaluatesTo(trigger.getId(), "//ui:ajaxtrigger/@triggerId", root);
assertXpathNotExists("//ui:ajaxtrigger/@loadOnce", root);
assertXpathNotExists("//ui:ajaxtrigger/@delay", root);
assertXpathEvaluatesTo("3", "count(//ui:ajaxtrigger/ui:ajaxtargetid)", root);
assertXpathEvaluatesTo(target1.getId(), "//ui:ajaxtrigger/ui:ajaxtargetid[1]/@targetId", root);
assertXpathEvaluatesTo(target2.getId(), "//ui:ajaxtrigger/ui:ajaxtargetid[2]/@targetId", root);
assertXpathEvaluatesTo(target3.getId(), "//ui:ajaxtrigger/ui:ajaxtargetid[3]/@targetId", root);
control.setLoadOnce(true);
assertSchemaMatch(root);
assertXpathEvaluatesTo("true", "//ui:ajaxtrigger/@loadOnce", root);
// remove loadOnce then reset it using loadCount
control.setLoadOnce(false);
assertSchemaMatch(root);
assertXpathNotExists("//ui:ajaxtrigger/@loadOnce", root);
// With Targets and optional attributes
// any number greateer than 0...
control.setLoadCount(6);
control.setDelay(1000);
assertSchemaMatch(root);
assertXpathEvaluatesTo(trigger.getId(), "//ui:ajaxtrigger/@triggerId", root);
assertXpathEvaluatesTo("true", "//ui:ajaxtrigger/@loadOnce", root);
assertXpathEvaluatesTo("1000", "//ui:ajaxtrigger/@delay", root);
assertXpathEvaluatesTo("3", "count(//ui:ajaxtrigger/ui:ajaxtargetid)", root);
}
use of com.github.bordertech.wcomponents.WContainer in project wcomponents by BorderTech.
the class WSubordinateControlRenderer_Test method testMultipleControlsAndGroups.
@Test
public void testMultipleControlsAndGroups() throws IOException, SAXException, XpathException {
SubordinateTrigger condTrigger = new WCheckBox();
// Setup Groups
SubordinateTarget actionTarget1 = new WTextField();
SubordinateTarget actionTarget2 = new WTextField();
SubordinateTarget actionTarget3 = new WTextField();
SubordinateTarget actionTarget4 = new WTextField();
// Multiple Groups
WComponentGroup<SubordinateTarget> group1 = new WComponentGroup<>();
group1.addToGroup(actionTarget1);
group1.addToGroup(actionTarget2);
WComponentGroup<SubordinateTarget> group2 = new WComponentGroup<>();
group2.addToGroup(actionTarget3);
group2.addToGroup(actionTarget4);
// Multiple Rules
Rule rule1 = new Rule();
rule1.setCondition(new Equal(condTrigger, Boolean.TRUE));
rule1.addActionOnTrue(new Show(group1));
rule1.addActionOnFalse(new Hide(group1));
Rule rule2 = new Rule();
rule2.setCondition(new Equal(condTrigger, Boolean.FALSE));
rule2.addActionOnTrue(new Show(group2));
rule2.addActionOnFalse(new Hide(group2));
// Setup Subordinate
WSubordinateControl control = new WSubordinateControl();
control.addRule(rule1);
control.addRule(rule2);
WContainer root = new WContainer();
root.add(condTrigger);
root.add(actionTarget1);
root.add(actionTarget2);
root.add(actionTarget3);
root.add(actionTarget4);
root.add(control);
root.add(group1);
root.add(group2);
setActiveContext(createUIContext());
// Apply the controls
control.applyTheControls();
// Validate Schema
assertSchemaMatch(root);
// Check for basic elements
assertXpathEvaluatesTo("2", "count(//ui:subordinate)", root);
assertXpathEvaluatesTo("2", "count(//ui:subordinate/ui:condition)", root);
assertXpathEvaluatesTo("2", "count(//ui:subordinate/ui:onTrue)", root);
assertXpathEvaluatesTo("2", "count(//ui:subordinate/ui:onFalse)", root);
assertXpathEvaluatesTo("2", "count(//ui:componentGroup)", root);
assertXpathEvaluatesTo("2", "count(//ui:componentGroup[position()=1]/ui:component)", root);
assertXpathEvaluatesTo("2", "count(//ui:componentGroup[position()=2]/ui:component)", root);
// Check ids
assertXpathEvaluatesTo(control.getId() + "-c0", "//ui:subordinate[position()=1]/@id", root);
assertXpathEvaluatesTo(control.getId() + "-c1", "//ui:subordinate[position()=2]/@id", root);
}
Aggregations