Search in sources :

Example 1 with ValueHolder

use of javax.faces.component.ValueHolder in project acs-community-packaging by Alfresco.

the class DatePickerRenderer method encodeBegin.

/**
 * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
 *
 * All rendering logic for this component is implemented here. A renderer for an
 * input component must render the submitted value if it's set, and use the local
 * value only if there is no submitted value.
 */
@SuppressWarnings("deprecation")
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    // always check for this flag - as per the spec
    if (component.isRendered() == true) {
        Date date = null;
        String clientId = component.getClientId(context);
        ResponseWriter out = context.getResponseWriter();
        String cmdFieldName = clientId + FIELD_CMD;
        Boolean initIfNull = (Boolean) component.getAttributes().get("initialiseIfNull");
        // this is part of the spec:
        // first you attempt to build the date from the submitted value
        int[] submittedValue = (int[]) ((EditableValueHolder) component).getSubmittedValue();
        if (submittedValue != null) {
            date = (Date) getConvertedValue(context, component, submittedValue);
        } else {
            // second - if no submitted value is found, default to the current value
            Object value = ((ValueHolder) component).getValue();
            if (value instanceof Date) {
                date = (Date) value;
            }
            // is set to true set the date to today's date
            if (date == null && initIfNull != null && initIfNull.booleanValue()) {
                date = new Date();
            }
        }
        // create a flag to show if the component is disabled
        Boolean disabled = (Boolean) component.getAttributes().get("disabled");
        if (disabled == null) {
            disabled = Boolean.FALSE;
        }
        if (date != null) {
            // get the attributes from the component we need for rendering
            int nStartYear;
            Integer startYear = (Integer) component.getAttributes().get("startYear");
            if (startYear != null) {
                nStartYear = startYear.intValue();
            } else {
                // for "effectivity date" searches
                nStartYear = new Date().getYear() + 1900 + 2;
            }
            int nYearCount = 25;
            Integer yearCount = (Integer) component.getAttributes().get("yearCount");
            if (yearCount != null) {
                nYearCount = yearCount.intValue();
            }
            // now we render the output for our component
            // we create 3 drop-down menus for day, month and year and
            // two text fields for the hour and minute
            // note that we build a client id for our form elements that we are then
            // able to decode() as above.
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            renderMenu(out, component, getDays(), calendar.get(Calendar.DAY_OF_MONTH), clientId + FIELD_DAY);
            renderMenu(out, component, getMonths(), calendar.get(Calendar.MONTH), clientId + FIELD_MONTH);
            renderMenu(out, component, getYears(nStartYear, nYearCount), calendar.get(Calendar.YEAR), clientId + FIELD_YEAR);
            // make sure we have a flag to determine whether to show the time
            Boolean showTime = (Boolean) component.getAttributes().get("showTime");
            if (showTime == null) {
                showTime = Boolean.FALSE;
            }
            out.write(" ");
            renderTimeElement(out, component, calendar.get(Calendar.HOUR_OF_DAY), clientId + FIELD_HOUR, showTime.booleanValue());
            if (showTime.booleanValue()) {
                out.write(" : ");
            }
            renderTimeElement(out, component, calendar.get(Calendar.MINUTE), clientId + FIELD_MINUTE, showTime.booleanValue());
            out.write(" ");
            // date back to null (if initialiseIfNull is false) or to select today's date
            if (disabled.booleanValue() == false) {
                out.write("<input type=\"button\" onclick=\"");
                out.write(Utils.generateFormSubmit(context, component, cmdFieldName, Integer.toString(CMD_TODAY)));
                out.write("\" value=\"");
                out.write(Application.getMessage(context, "today"));
                out.write("\">&nbsp;");
                if (initIfNull != null && initIfNull.booleanValue() == false) {
                    out.write("<input type=\"button\" onclick=\"");
                    out.write(Utils.generateFormSubmit(context, component, cmdFieldName, Integer.toString(CMD_RESET)));
                    out.write("\" value=\"");
                    out.write(Application.getMessage(context, "none"));
                    out.write("\">");
                }
            }
        } else {
            // Render a link indicating there isn't a date set (unless the property is disabled)
            out.write("<div style=\"padding: 3px;");
            if (disabled.booleanValue() == false) {
                out.write("\"><a href=\"#\" title=\"");
                out.write(Application.getMessage(context, "click_to_set_date"));
                out.write("\" onclick=\"");
                out.write(Utils.generateFormSubmit(context, component, cmdFieldName, Integer.toString(CMD_SET)));
                out.write("\">");
            } else {
                out.write(" color: #666666; font-style: italic;\">");
            }
            // work out which label to use
            String noneLabel = (String) component.getAttributes().get("noneLabel");
            if (noneLabel == null || noneLabel.length() == 0) {
                noneLabel = Application.getMessage(context, "none");
            }
            out.write(noneLabel);
            if (disabled.booleanValue() == false) {
                out.write("</a>");
            }
            out.write("</div>");
        }
        // also output a hidden field containing the current value of the date, this will
        // allow JavaScript to determine if a value is set for validation purposes.
        out.write("<input type=\"hidden\" ");
        outputAttribute(out, clientId, "id");
        outputAttribute(out, clientId, "name");
        String strValue = "";
        if (date != null) {
            strValue = date.toString();
        }
        outputAttribute(out, strValue, "value");
        out.write("/>");
    }
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ValueHolder(javax.faces.component.ValueHolder) EditableValueHolder(javax.faces.component.EditableValueHolder) Date(java.util.Date)

Aggregations

Calendar (java.util.Calendar)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 EditableValueHolder (javax.faces.component.EditableValueHolder)1 ValueHolder (javax.faces.component.ValueHolder)1 ResponseWriter (javax.faces.context.ResponseWriter)1