Search in sources :

Example 1 with EditableValueHolder

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

the class AbstractItemSelector method decode.

/**
 * @see javax.faces.component.UIComponentBase#decode(javax.faces.context.FacesContext)
 */
public void decode(FacesContext context) {
    Map requestMap = context.getExternalContext().getRequestParameterMap();
    String fieldId = getHiddenFieldName();
    String value = (String) requestMap.get(fieldId);
    int mode = this.mode;
    if (value != null && value.length() != 0) {
        // break up the submitted value into it's parts
        // first part is the mode the component is in
        // followed by the id of the selection if we are drilling down
        String id = null;
        int sepIndex = value.indexOf(NamingContainer.SEPARATOR_CHAR);
        if (sepIndex != -1) {
            mode = Integer.parseInt(value.substring(0, sepIndex));
            if (value.length() > sepIndex + 1) {
                id = value.substring(sepIndex + 1);
            }
        } else {
            mode = Integer.parseInt(value);
        }
        // raise an event so we can pick the changed values up later
        ItemSelectorEvent event = new ItemSelectorEvent(this, mode, id);
        this.queueEvent(event);
    }
    if (mode == MODE_CONFIRM_SELECTION) {
        // only bother to check the selection if the mode is set to MODE_AFTER_SELECTION
        // see if a selection has been submitted
        String selection = (String) requestMap.get(getClientId(context) + OPTION);
        if (selection != null && selection.length() != 0) {
            NodeRef nodeRef = new NodeRef(Repository.getStoreRef(), selection);
            if (!getNodeService(context).exists(nodeRef)) {
                throw new RuntimeException("NodeRef not found.");
            } else {
                ((EditableValueHolder) this).setSubmittedValue(nodeRef);
            }
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Map(java.util.Map) EditableValueHolder(javax.faces.component.EditableValueHolder)

Example 2 with EditableValueHolder

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

the class DatePickerRenderer method decode.

/**
 * @see javax.faces.render.Renderer#decode(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
 *
 * The decode method takes the parameters from the external requests, finds the
 * ones revelant to this component and decodes the results into an object known
 * as the "submitted value".
 */
public void decode(FacesContext context, UIComponent component) {
    try {
        // TODO: should check for disabled/readonly here - no need to decode
        String clientId = component.getClientId(context);
        Map params = context.getExternalContext().getRequestParameterMap();
        // see if a command was invoked
        String cmd = (String) params.get(clientId + FIELD_CMD);
        if (cmd != null && cmd.length() > 0) {
            int action = Integer.parseInt(cmd);
            switch(action) {
                case CMD_RESET:
                    {
                        // set the submitted value to be null
                        ((EditableValueHolder) component).setSubmittedValue(null);
                        // set the component value to be null too
                        ((EditableValueHolder) component).setValue(null);
                        break;
                    }
                default:
                    {
                        // the user is either trying to set the date for the first
                        // time or set it back to today's date, create the parts array
                        // to represent this and set as the submitted value
                        int[] parts = new int[5];
                        Calendar date = Calendar.getInstance();
                        parts[0] = date.get(Calendar.YEAR);
                        parts[1] = date.get(Calendar.MONTH);
                        parts[2] = date.get(Calendar.DAY_OF_MONTH);
                        parts[3] = date.get(Calendar.HOUR_OF_DAY);
                        parts[4] = date.get(Calendar.MINUTE);
                        ((EditableValueHolder) component).setSubmittedValue(parts);
                    }
            }
        } else {
            // a command was not invoked so decode the date the user set (if present)
            String year = (String) params.get(clientId + FIELD_YEAR);
            if (year != null) {
                // found data for our component
                String month = (String) params.get(clientId + FIELD_MONTH);
                String day = (String) params.get(clientId + FIELD_DAY);
                String hour = (String) params.get(clientId + FIELD_HOUR);
                String minute = (String) params.get(clientId + FIELD_MINUTE);
                // we encode the values needed for the component as we see fit
                int[] parts = new int[5];
                parts[0] = Integer.parseInt(year);
                parts[1] = Integer.parseInt(month);
                parts[2] = Integer.parseInt(day);
                parts[3] = Integer.parseInt(hour);
                parts[4] = Integer.parseInt(minute);
                // save the data in an object for our component as the "EditableValueHolder"
                // all UI Input Components support this interface for the submitted value
                ((EditableValueHolder) component).setSubmittedValue(parts);
            }
        }
    } catch (NumberFormatException nfe) {
    // just ignore the error and skip the update of the property
    }
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) Map(java.util.Map) EditableValueHolder(javax.faces.component.EditableValueHolder)

Example 3 with EditableValueHolder

use of javax.faces.component.EditableValueHolder 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

EditableValueHolder (javax.faces.component.EditableValueHolder)3 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 Map (java.util.Map)2 Date (java.util.Date)1 ValueHolder (javax.faces.component.ValueHolder)1 ResponseWriter (javax.faces.context.ResponseWriter)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1