Search in sources :

Example 1 with VisitCallback

use of javax.faces.component.visit.VisitCallback in project oxTrust by GluuFederation.

the class CustomAttributeAction method validateAttributeValues.

public void validateAttributeValues(ComponentSystemEvent event) {
    final FacesContext facesContext = FacesContext.getCurrentInstance();
    final List<Object> values = new ArrayList<Object>();
    event.getComponent().visitTree(VisitContext.createVisitContext(facesContext), new VisitCallback() {

        @Override
        public VisitResult visit(VisitContext context, UIComponent target) {
            if (target instanceof UIInput) {
                GluuAttribute attribute = (GluuAttribute) target.getAttributes().get("attribute");
                if (attribute != null) {
                    values.add(((UIInput) target).getValue());
                }
            }
            return VisitResult.ACCEPT;
        }
    });
    values.removeAll(Arrays.asList(null, ""));
    Set<Object> uniqValues = new HashSet<Object>(values);
    if (values.size() != uniqValues.size()) {
        event.getComponent().visitTree(VisitContext.createVisitContext(facesContext), new VisitCallback() {

            @Override
            public VisitResult visit(VisitContext context, UIComponent target) {
                if (target instanceof UIInput) {
                    GluuAttribute attribute = (GluuAttribute) target.getAttributes().get("attribute");
                    if (attribute != null) {
                        ((UIInput) target).setValid(false);
                        String message = "Please fill out an unique value for all of '" + attribute.getDisplayName() + "' fields";
                        facesMessages.add(target.getClientId(facesContext), FacesMessage.SEVERITY_ERROR, message);
                    }
                }
                return VisitResult.ACCEPT;
            }
        });
        facesContext.validationFailed();
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) VisitCallback(javax.faces.component.visit.VisitCallback) VisitContext(javax.faces.component.visit.VisitContext) ArrayList(java.util.ArrayList) UIComponent(javax.faces.component.UIComponent) UIInput(javax.faces.component.UIInput) GluuAttribute(org.gluu.model.GluuAttribute) VisitResult(javax.faces.component.visit.VisitResult) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 UIComponent (javax.faces.component.UIComponent)1 UIInput (javax.faces.component.UIInput)1 VisitCallback (javax.faces.component.visit.VisitCallback)1 VisitContext (javax.faces.component.visit.VisitContext)1 VisitResult (javax.faces.component.visit.VisitResult)1 FacesContext (javax.faces.context.FacesContext)1 GluuAttribute (org.gluu.model.GluuAttribute)1