Search in sources :

Example 1 with Converter

use of javax.faces.convert.Converter in project ART-TIME by Artezio.

the class Sheet method getRenderValueForCell.

/**
 * Gets the render string for the value the given cell. Applys the available
 * converters to convert the value.
 * <p>
 * @param context
 * @param rowKey
 * @param col
 * @return
 */
public String getRenderValueForCell(FacesContext context, Object rowKey, int col) {
    // if we have a submitted value still, use it
    // note: can't check for null, as null may be the submitted value
    RowColIndex index = new RowColIndex(rowKey, col);
    if (submittedValues.containsKey(index))
        return submittedValues.get(index);
    Object value = getValueForCell(context, rowKey, col);
    if (value == null)
        return null;
    final Column column = getColumns().get(col);
    Converter converter = ComponentUtils.getConverter(context, column);
    if (converter == null)
        return value.toString();
    else
        return converter.getAsString(context, this, value);
}
Also used : Converter(javax.faces.convert.Converter)

Example 2 with Converter

use of javax.faces.convert.Converter in project ART-TIME by Artezio.

the class Sheet method validate.

/**
 * Converts each submitted value into a local value and stores it back in
 * the hash. If all values convert without error, then the component is
 * valid, and we can proceed to the processUpdates.
 */
@Override
public void validate(FacesContext context) {
    Iterator<Entry<RowColIndex, String>> entries = submittedValues.entrySet().iterator();
    boolean hadBadUpdates = !getBadUpdates().isEmpty();
    getBadUpdates().clear();
    while (entries.hasNext()) {
        final Entry<RowColIndex, String> entry = entries.next();
        final Column column = getColumns().get(entry.getKey().colIndex);
        final String newValue = entry.getValue();
        final Object rowKey = entry.getKey().getRowKey();
        final int col = entry.getKey().getColIndex();
        final RowMap map = rowMap.get(rowKey);
        this.setRowIndex(context, map.sortedIndex);
        // attempt to convert new value from string to correct object type
        // based on column converter. Use PF util as helper
        Converter converter = ComponentUtils.getConverter(context, column);
        // assume string value if converter not found
        Object newValueObj = newValue;
        if (converter != null)
            try {
                newValueObj = converter.getAsObject(context, this, newValue);
            } catch (ConverterException e) {
                // add offending cell to list of bad updates
                // and to a stringbuffer for error messages (so we have one
                // message for the component)
                setValid(false);
                FacesMessage message = e.getFacesMessage();
                if (message == null) {
                    message = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage());
                }
                context.addMessage(this.getClientId(context), message);
                String messageText = message.getDetail();
                this.getBadUpdates().add(new BadUpdate(getRowKeyValue(context), col, column, newValue, messageText));
                continue;
            }
        // value is fine, no further validations (again, not to be confused
        // with validators. until we have a "required" or something like
        // that, nothing else to do).
        setLocalValue(rowKey, col, newValueObj);
        // process validators on column
        column.setValue(newValueObj);
        try {
            column.validate(context);
        } finally {
            column.resetValue();
        }
        entries.remove();
    }
    this.setRowIndex(context, -1);
    final boolean newBadUpdates = !getBadUpdates().isEmpty();
    String errorMessage = this.getErrorMessage();
    if (hadBadUpdates || newBadUpdates) {
        // update the bad data var if partial request
        if (context.getPartialViewContext().isPartialRequest()) {
            this.sortAndFilter();
            this.renderBadUpdateScript(context);
        }
    }
    if (newBadUpdates && errorMessage != null) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, errorMessage);
        context.addMessage(null, message);
    }
}
Also used : ConverterException(javax.faces.convert.ConverterException) Entry(java.util.Map.Entry) Converter(javax.faces.convert.Converter) FacesMessage(javax.faces.application.FacesMessage)

Example 3 with Converter

use of javax.faces.convert.Converter in project acs-community-packaging by Alfresco.

the class BaseComponentGenerator method createAndSetConverter.

/**
 * Creates the converter with the given id and adds it to the component.
 *
 * @param context FacesContext
 * @param converterId The name of the converter to create
 * @param component The component to add the converter to
 */
protected void createAndSetConverter(FacesContext context, String converterId, UIComponent component) {
    if (converterId != null && component instanceof UIOutput) {
        try {
            Converter conv = context.getApplication().createConverter(converterId);
            ((UIOutput) component).setConverter(conv);
        } catch (NullPointerException npe) {
            // workaround a NPE bug in MyFaces
            logger.warn("Converter " + converterId + " could not be applied");
        } catch (FacesException fe) {
            logger.warn("Converter " + converterId + " could not be applied");
        }
    }
}
Also used : UIOutput(javax.faces.component.UIOutput) Converter(javax.faces.convert.Converter) FacesException(javax.faces.FacesException)

Example 4 with Converter

use of javax.faces.convert.Converter in project acs-community-packaging by Alfresco.

the class UIOutputText method encodeBegin.

/**
 * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
 */
public void encodeBegin(FacesContext context) throws IOException {
    if (isRendered() == false) {
        return;
    }
    Object value = getValue();
    if (value != null) {
        Converter converter = getConverter();
        if (converter != null) {
            value = converter.getAsString(context, this, value);
        }
        ResponseWriter out = context.getResponseWriter();
        if (isEncodeForJavaScript()) {
            out.write(URLEncoder.encode((String) getValue()));
        } else {
            String style = (String) getAttributes().get("style");
            String styleClass = (String) getAttributes().get("styleClass");
            if (style != null || styleClass != null) {
                out.write("<span");
                if (style != null) {
                    out.write(" style='");
                    out.write(style);
                    out.write('\'');
                }
                if (styleClass != null) {
                    out.write(" class=");
                    out.write(styleClass);
                }
                out.write('>');
                out.write(Utils.encode(value.toString()));
                out.write("</span>");
            } else {
                out.write(Utils.encode(value.toString()));
            }
        }
    }
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) Converter(javax.faces.convert.Converter)

Example 5 with Converter

use of javax.faces.convert.Converter in project acs-community-packaging by Alfresco.

the class BaseMultiValueRenderer method renderExistingItem.

/**
 * Renders an existing item with a remove button
 *
 * @param context FacesContext
 * @param component The UIComponent
 * @param out Writer to write output to
 * @param nodeService The NodeService
 * @param index The index of the item
 * @param value The item's value
 * @throws IOException
 */
protected void renderExistingItem(FacesContext context, UIComponent component, ResponseWriter out, NodeService nodeService, int index, Object value) throws IOException {
    out.write("<tr><td class='");
    if (this.highlightedRow) {
        out.write("selectedItemsRowAlt");
    } else {
        out.write("selectedItemsRow");
    }
    out.write("'>");
    if (value instanceof NodeRef) {
        String name;
        if (ContentModel.TYPE_CATEGORY.equals(nodeService.getType((NodeRef) value))) {
            name = Repository.getNameForCategoryNode(nodeService, (NodeRef) value);
        } else {
            name = Repository.getNameForNode(nodeService, (NodeRef) value);
        }
        out.write(Utils.encode(name));
    } else if (value instanceof Date) {
        XMLDateConverter converter = (XMLDateConverter) context.getApplication().createConverter(RepoConstants.ALFRESCO_FACES_XMLDATE_CONVERTER);
        UIComponent childComponent = (UIComponent) component.getChildren().get(0);
        Boolean showTime = (Boolean) childComponent.getAttributes().get("showTime");
        if (showTime != null && showTime.booleanValue()) {
            converter.setPattern(Application.getMessage(context, "date_time_pattern"));
        } else {
            converter.setPattern(Application.getMessage(context, "date_pattern"));
        }
        out.write(converter.getAsString(context, childComponent, value));
    } else if (value instanceof Boolean) {
        Converter converter = context.getApplication().createConverter(RepoConstants.ALFRESCO_FACES_BOOLEAN_CONVERTER);
        out.write(converter.getAsString(context, (UIComponent) component.getChildren().get(0), value));
    } else {
        out.write(Utils.encode(value.toString()));
    }
    out.write("&nbsp;&nbsp;");
    out.write("</td><td class='");
    if (this.highlightedRow) {
        out.write("selectedItemsRowAlt");
    } else {
        out.write("selectedItemsRow");
    }
    out.write("'><a href='#' title='");
    out.write(Application.getMessage(context, MSG_REMOVE));
    out.write("' onclick=\"");
    out.write(generateFormSubmit(context, component, UIMultiValueEditor.ACTION_REMOVE + UIMultiValueEditor.ACTION_SEPARATOR + index));
    out.write("\"><img src='");
    out.write(context.getExternalContext().getRequestContextPath());
    out.write("/images/icons/delete.gif' border='0' width='13' height='16'/></a>");
    this.highlightedRow = !this.highlightedRow;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) XMLDateConverter(org.alfresco.web.ui.common.converter.XMLDateConverter) UIComponent(javax.faces.component.UIComponent) XMLDateConverter(org.alfresco.web.ui.common.converter.XMLDateConverter) Converter(javax.faces.convert.Converter) Date(java.util.Date)

Aggregations

Converter (javax.faces.convert.Converter)5 Date (java.util.Date)1 Entry (java.util.Map.Entry)1 FacesException (javax.faces.FacesException)1 FacesMessage (javax.faces.application.FacesMessage)1 UIComponent (javax.faces.component.UIComponent)1 UIOutput (javax.faces.component.UIOutput)1 ResponseWriter (javax.faces.context.ResponseWriter)1 ConverterException (javax.faces.convert.ConverterException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 XMLDateConverter (org.alfresco.web.ui.common.converter.XMLDateConverter)1