Java Code Examples for javax.faces.component.UIViewRoot#findComponent()

The following examples show how to use javax.faces.component.UIViewRoot#findComponent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageListener.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Ids of fields inside tables look like <code>tableId:rowNr:fieldId</code>.
 * So we try to cut the fieldId and the rowNr parts to get the table id and
 * then check if it is inside a section and return the section id.
 * 
 * @param clientId
 *            the id of the field inside of a table
 * @param root
 *            the {@link UIViewRoot}
 * @return the section id or <code>null</code>
 */
private String getTableSectionId(String clientId, UIViewRoot root) {
    if (!clientId.contains(":")) {
        return null;
    }
    // cut the field id
    clientId = clientId.substring(0, clientId.lastIndexOf(':'));
    if (!clientId.contains(":")) {
        return null;
    }
    // cut the row number - we should have the table id
    clientId = clientId.substring(0, clientId.lastIndexOf(':'));
    UIComponent comp = root.findComponent(clientId);
    if (comp != null) {
        return getSectionId(comp);
    }
    return null;
}
 
Example 2
Source File: MessageListener.java    From development with Apache License 2.0 4 votes vote down vote up
public void beforePhase(PhaseEvent event) {
    FacesContext fc = event.getFacesContext();
    ServletRequest request = (ServletRequest) fc.getExternalContext()
            .getRequest();
    UIViewRoot root = fc.getViewRoot();
    resetColor(root.getChildren());
    List<String> additionalErrorElements = new ArrayList<String>();
    Set<String> sectionsToExpand = new HashSet<String>();
    Iterator<String> clientIds = fc.getClientIdsWithMessages();
    while (clientIds.hasNext()) {
        String clientId = clientIds.next();
        if (canIgnoreClientId(clientId)) {
            // skip, since it is not associated with a specific field
            continue;
        }
        if (request.getAttribute(ATTRIBUTE_FIRST_ERROR_ELEMENT) == null) {
            // set focus to first component only
            request.setAttribute(ATTRIBUTE_FIRST_ERROR_ELEMENT, clientId);
        }
        UIComponent uiComponent = root.findComponent(clientId);
        String sectionId = null;
        if (uiComponent != null) {
            sectionId = getSectionId(uiComponent);
            uiComponent.getAttributes().put(ATTRIBUTE_STYLE, STYLE_ERROR);
            Object object = uiComponent.getAttributes().get("connectedTo");
            if (object != null) {
                UIComponent connected = root.findComponent(object
                        .toString());
                if (connected != null) {
                    connected.getAttributes().put(ATTRIBUTE_STYLE,
                            STYLE_ERROR);
                }
            }
            if (uiComponent instanceof UISelectBoolean) {
                uiComponent.getAttributes().put(ATTRIBUTE_STYLE,
                        STYLE_CHECKBOX_ERROR);
            } else if (uiComponent instanceof UICalendar) {
                additionalErrorElements.add(clientId + "InputDate");
            } else {
                uiComponent.getAttributes().put(ATTRIBUTE_STYLE,
                        STYLE_ERROR);
            }
        } else {
            sectionId = getTableSectionId(clientId, root);
            // if the element is part of a data table we must change the
            // style from javascript
            additionalErrorElements.add(clientId);
        }
        if (sectionId != null) {
            sectionsToExpand.add(sectionId);
        }
    }
    request.setAttribute(ATTRIBUTE_ADDITIONAL_ERROR_ELEMENTS,
            additionalErrorElements);
    if (!sectionsToExpand.isEmpty()) {
        request.setAttribute(OpenStateBean.SECTIONS_TO_EXPAND,
                sectionsToExpand);
    }
    if (request.getAttribute(ATTRIBUTE_FIRST_ERROR_ELEMENT) != null) {
        JSFUtils.addMessage(null, FacesMessage.SEVERITY_ERROR,
                BaseBean.ERROR_TEXT_FIELDS, null);
    }

    if (request.getAttribute(FILEUPLOAD_EXCEPTION) != null) {
        // The upload of one or a bunch of files failed because either one
        // of the files exceeds the value of "uploadMaxFileSize" or the size
        // of all files exceeds the value of "uploadMaxSize". If
        // "uploadMaxSize" if not set => uploadMaxSize = uploadMaxFileSize

        Integer maxSize = (Integer) request
                .getAttribute(FILEUPLOAD_MAX_SIZE);

        if (maxSize == null) {
            // The attribute indicating the maximum allowed size was not set
            JSFUtils.addMessage(null, FacesMessage.SEVERITY_ERROR,
                    BaseBean.ERROR_UPLOAD_SIZE_LIMIT_EXCEEDED, null);
        } else {
            // Calculate a more handy megabyte value and bring it to a nice
            // format
            Locale viewLocale = JSFUtils.getViewLocale();
            DecimalFormat df = (DecimalFormat) DecimalFormat
                    .getInstance(viewLocale);
            df.applyPattern("#0.00");

            double maxValueMb = maxSize.intValue() / (1024.0 * 1024.0);

            JSFUtils.addMessage(null, FacesMessage.SEVERITY_ERROR,
                    BaseBean.ERROR_UPLOAD_SIZE_LIMIT_EXCEEDED_KNOWNMAX,
                    new Object[] { df.format(maxValueMb) });
        }
    }
}