Search in sources :

Example 61 with ResponseWriter

use of javax.faces.context.ResponseWriter in project acs-community-packaging by Alfresco.

the class UIShelfGroup method encodeChildren.

/**
 * @see javax.faces.component.UIComponentBase#encodeChildren(javax.faces.context.FacesContext)
 */
public void encodeChildren(FacesContext context) throws IOException {
    if (isRendered() == false) {
        return;
    }
    ResponseWriter out = context.getResponseWriter();
    // output each shelf group component in turn
    out.write("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">");
    for (Iterator i = this.getChildren().iterator(); i.hasNext(); ) /**/
    {
        UIComponent child = (UIComponent) i.next();
        if (child instanceof UIShelfItem) {
            // render child items
            out.write("<tr><td>");
            Utils.encodeRecursive(context, child);
            out.write("</td></tr>");
        }
    }
    out.write("</table>");
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) Iterator(java.util.Iterator) UIComponent(javax.faces.component.UIComponent)

Example 62 with ResponseWriter

use of javax.faces.context.ResponseWriter in project acs-community-packaging by Alfresco.

the class AbstractItemSelector method encodeBegin.

/**
 * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
 */
public void encodeBegin(FacesContext context) throws IOException {
    if (isRendered() == false) {
        return;
    }
    NodeService service = getNodeService(context);
    UserTransaction tx = null;
    try {
        tx = Repository.getUserTransaction(context, true);
        tx.begin();
        if (isDisabled()) {
            // render a read-only view of the selected category (if any)
            ResponseWriter out = context.getResponseWriter();
            // see if there is a current value for the category
            NodeRef nodeRef = (NodeRef) getSubmittedValue();
            if (nodeRef == null) {
                Object val = getValue();
                if (val instanceof NodeRef) {
                    nodeRef = (NodeRef) val;
                } else if (val instanceof String && ((String) val).length() != 0) {
                    nodeRef = new NodeRef((String) val);
                } else if (val instanceof List) {
                    // build a comma separated list of node names
                    List nodes = (List) val;
                    StringBuilder buffer = new StringBuilder(64);
                    for (Object obj : nodes) {
                        if (buffer.length() != 0) {
                            buffer.append(", ");
                        }
                        if (obj instanceof NodeRef) {
                            buffer.append(Utils.encode(Repository.getNameForNode(service, (NodeRef) obj)));
                        } else {
                            buffer.append(obj.toString());
                        }
                    }
                    // write out to response
                    out.write(buffer.toString());
                }
            }
            // if there is a value show it's name
            if (nodeRef != null) {
                out.write(Utils.encode(Repository.getNameForNode(service, nodeRef)));
            }
        } else {
            // render an editable control for selecting items
            String clientId = getClientId(context);
            StringBuilder buf = new StringBuilder(512);
            Map attrs = this.getAttributes();
            boolean showValueInHiddenField = false;
            NodeRef value = null;
            switch(this.mode) {
                case MODE_BEFORE_SELECTION:
                case MODE_CONFIRM_SELECTION:
                case MODE_CANCEL_SELECTION:
                    {
                        NodeRef submittedValue = (NodeRef) getSubmittedValue();
                        if (submittedValue != null) {
                            value = submittedValue;
                        } else {
                            Object val = getValue();
                            if (val instanceof NodeRef) {
                                value = (NodeRef) val;
                            } else if (val instanceof String && ((String) val).length() != 0) {
                                value = new NodeRef((String) val);
                            }
                        }
                        // show just the initial or current selection link
                        String label;
                        if (value == null) {
                            label = getLabel();
                            // if the label is still null get the default from the message bundle
                            if (label == null) {
                                label = getDefaultLabel();
                            }
                        } else {
                            label = Repository.getNameForNode(service, value);
                            showValueInHiddenField = true;
                        }
                        // output surrounding span for style purposes
                        buf.append("<span");
                        if (attrs.get("style") != null) {
                            buf.append(" style=\"").append(attrs.get("style")).append('"');
                        }
                        if (attrs.get("styleClass") != null) {
                            buf.append(" class=").append(attrs.get("styleClass"));
                        }
                        buf.append(">");
                        // rendering as initial selection mode means the sibilings of the selected
                        // item are shown instead of the children on first click in.
                        int theMode = MODE_INITIAL_SELECTION;
                        // if we have an initial selection and no value set the initial one up
                        if (value == null && this.getInitialSelection() != null) {
                            value = new NodeRef(Repository.getStoreRef(), this.getInitialSelection());
                        }
                        // field value is whether we are picking and the current or parent Id value
                        String fieldValue;
                        if (value != null) {
                            fieldValue = encodeFieldValues(theMode, value.getId());
                        } else {
                            fieldValue = encodeFieldValues(theMode, null);
                        }
                        buf.append("<a href='#' onclick=\"");
                        buf.append(Utils.generateFormSubmit(context, this, getHiddenFieldName(), fieldValue));
                        buf.append('"');
                        if (attrs.get("nodeStyle") != null) {
                            buf.append(" style=\"").append(attrs.get("nodeStyle")).append('"');
                        }
                        if (attrs.get("nodeStyleClass") != null) {
                            buf.append(" class=").append(attrs.get("nodeStyleClass"));
                        }
                        buf.append(">").append(Utils.encode(label)).append("</a></span>");
                        break;
                    }
                case MODE_DRILLDOWN_SELECTION:
                case MODE_INITIAL_SELECTION:
                    {
                        // show the picker list
                        // get the children of the node ref to show
                        buf.append("<table border=0 cellspacing=1 cellpadding=1");
                        if (attrs.get("style") != null) {
                            buf.append(" style=\"").append(attrs.get("style")).append('"');
                        }
                        if (attrs.get("styleClass") != null) {
                            buf.append(" class=").append(attrs.get("styleClass"));
                        }
                        buf.append(">");
                        // the item when the list is rendered
                        if (this.mode == MODE_INITIAL_SELECTION) {
                            this.navigationId = getParentNodeId(context);
                        }
                        // render "Go Up" link if not at the root level
                        if (this.navigationId != null) {
                            // get the id of the parent node of the current navigation node,
                            // null indicates we are at the root level
                            String id = getParentNodeId(context);
                            buf.append("<tr><td></td><td>");
                            String upImage = Utils.buildImageTag(context, WebResources.IMAGE_GO_UP, null, "absmiddle");
                            // render a link to the parent node
                            renderNodeLink(context, id, Application.getMessage(context, MSG_GO_UP), upImage, buf);
                            buf.append("</td></tr>");
                        }
                        String okButtonId = clientId + OK_BUTTON;
                        boolean okButtonEnabled = false;
                        // display the children of the specified navigation node ID
                        Collection<NodeRef> childRefs;
                        if (this.navigationId != null) {
                            // get a list of children for the current navigation node
                            childRefs = getChildrenForNode(context);
                        } else {
                            // no node set - special case to show the initial root items
                            childRefs = getRootChildren(context);
                        }
                        for (NodeRef childRef : childRefs) {
                            // render each child found
                            String childId = childRef.getId();
                            buf.append("<tr><td><input type='radio' name='").append(clientId).append(OPTION).append("' value='").append(childId).append("'");
                            if (childId.equals(this.initialSelectionId)) {
                                buf.append(" checked");
                                // if any radio buttons are checked, the OK button must start enabled
                                okButtonEnabled = true;
                                // now remove the initial selection as we only need it the first time
                                this.initialSelectionId = null;
                            }
                            buf.append(" onclick=\"javascript:document.getElementById('").append(okButtonId).append("').disabled=false;\"");
                            buf.append("/></td><td>");
                            // get the name for the child and output as link
                            String name = Repository.getNameForNode(service, childRef);
                            String prefixHtml = null;
                            String icon = getItemIcon(context, childRef);
                            if (icon != null) {
                                prefixHtml = "<span style='padding-right:4px'>" + Utils.buildImageTag(context, icon, null, "absmiddle") + "</span>";
                            }
                            renderNodeLink(context, childId, name, prefixHtml, buf);
                            buf.append("</td></tr>");
                        }
                        // render OK button
                        String fieldValue = encodeFieldValues(MODE_CONFIRM_SELECTION, null);
                        buf.append("<tr style='padding-top:4px'><td></td><td align=center>").append("<input type='button' ").append(okButtonEnabled == false ? "disabled" : "").append(" onclick=\"").append(Utils.generateFormSubmit(context, this, getHiddenFieldName(), fieldValue)).append("\" value='").append(Application.getMessage(context, MSG_OK)).append("' id='").append(okButtonId).append("'>&nbsp;");
                        // render Cancel button
                        fieldValue = encodeFieldValues(MODE_CANCEL_SELECTION, null);
                        buf.append("<input type='button' onclick=\"").append(Utils.generateFormSubmit(context, this, getHiddenFieldName(), fieldValue)).append("\" value='").append(Application.getMessage(context, MSG_CANCEL)).append("'></td></tr>");
                        buf.append("</table>");
                        break;
                    }
            }
            // output a hidden field containing the currently selected NodeRef so that JavaScript
            // can be used to check the state of the component
            buf.append("<input type='hidden' name='");
            buf.append(clientId);
            buf.append("_selected' id='");
            buf.append(clientId);
            buf.append("_selected' value='");
            if (showValueInHiddenField) {
                buf.append(value);
            }
            buf.append("'/>");
            context.getResponseWriter().write(buf.toString());
        }
        // commit the transaction
        tx.commit();
    } catch (Throwable err) {
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) NodeService(org.alfresco.service.cmr.repository.NodeService) AbortProcessingException(javax.faces.event.AbortProcessingException) IOException(java.io.IOException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ResponseWriter(javax.faces.context.ResponseWriter) Collection(java.util.Collection) List(java.util.List) Map(java.util.Map)

Example 63 with ResponseWriter

use of javax.faces.context.ResponseWriter in project acs-community-packaging by Alfresco.

the class InvokeCommand method setupResponseWriter.

/**
 * setup the JSF response writer.
 */
private ResponseWriter setupResponseWriter(final String mimetype, final HttpServletResponse response, final FacesContext facesContext) throws IOException {
    final OutputStream os = response.getOutputStream();
    final UIViewRoot viewRoot = facesContext.getViewRoot();
    final RenderKitFactory renderFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
    final RenderKit renderKit = renderFactory.getRenderKit(facesContext, viewRoot.getRenderKitId());
    final ResponseWriter writer = renderKit.createResponseWriter(new OutputStreamWriter(os, "UTF-8"), mimetype, "UTF-8");
    facesContext.setResponseWriter(writer);
    // must be text/xml otherwise IE doesn't parse the response properly into responseXML
    response.setContentType(mimetype);
    return writer;
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) OutputStream(java.io.OutputStream) RenderKit(javax.faces.render.RenderKit) OutputStreamWriter(java.io.OutputStreamWriter) UIViewRoot(javax.faces.component.UIViewRoot) RenderKitFactory(javax.faces.render.RenderKitFactory)

Example 64 with ResponseWriter

use of javax.faces.context.ResponseWriter 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("&nbsp;");
            renderTimeElement(out, component, calendar.get(Calendar.HOUR_OF_DAY), clientId + FIELD_HOUR, showTime.booleanValue());
            if (showTime.booleanValue()) {
                out.write("&nbsp;:&nbsp;");
            }
            renderTimeElement(out, component, calendar.get(Calendar.MINUTE), clientId + FIELD_MINUTE, showTime.booleanValue());
            out.write("&nbsp;");
            // 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)

Example 65 with ResponseWriter

use of javax.faces.context.ResponseWriter in project acs-community-packaging by Alfresco.

the class UIGenericPicker method encodeBegin.

/**
 * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
 */
public void encodeBegin(FacesContext context) throws IOException {
    if (isRendered() == false) {
        return;
    }
    ResponseWriter out = context.getResponseWriter();
    ResourceBundle bundle = Application.getBundle(context);
    String clientId = getClientId(context);
    // start outer table
    out.write("<table border=0 cellspacing=4 cellpadding=0>");
    // top row
    out.write("<tr valign=top><td>");
    // filter drop-down
    if (getShowFilter() == true) {
        out.write("<select name='");
        out.write(clientId + FIELD_FILTER);
        out.write("' size='1'");
        // apply onchange Form submit here if component attributes require it
        if (getFilterRefresh() == true) {
            out.write(" onchange=\"");
            out.write(generateFormSubmit(context, ACTION_FILTER));
            out.write("\"");
        }
        out.write(">");
        // output filter options
        SelectItem[] items = getFilterOptions();
        if (items != null) {
            for (int i = 0; i < items.length; i++) {
                out.write("<option value=\"");
                out.write(items[i].getValue().toString());
                if (this.filterIndex != i) {
                    out.write("\">");
                } else {
                    out.write("\" selected=\"true\">");
                }
                out.write(Utils.encode(items[i].getLabel()));
                out.write("</option>");
            }
        }
        out.write("</select>");
    }
    out.write("</td><td align=right>");
    // Contains textbox
    if (getShowContains() == true) {
        out.write("<input name='");
        out.write(clientId + FIELD_CONTAINS);
        out.write("' type='text' maxlength='256' style='width:");
        out.write(getShowFilter() ? "120" : "180");
        out.write("px' value=\"");
        out.write(Utils.encode(this.contains));
        out.write("\">&nbsp;");
    }
    // Search button
    out.write("<input type='submit' value='");
    String msg = getSearchButtonLabel();
    if (msg == null || msg.length() == 0) {
        msg = bundle.getString(MSG_SEARCH);
    }
    out.write(Utils.encode(msg));
    out.write("' onclick=\"");
    out.write(generateFormSubmit(context, ACTION_SEARCH));
    out.write("\">");
    out.write("</td></tr>");
    // information row
    if (this.currentResults != null && getShowContains() == true) {
        out.write("<tr><td colspan=3>");
        String resultsMsg;
        if (getShowFilter() == false) {
            resultsMsg = MessageFormat.format(bundle.getString(MSG_RESULTS1), new Object[] { Utils.encode(this.contains) });
        } else {
            String filterMsg = this.filters[this.filterIndex].getLabel();
            resultsMsg = MessageFormat.format(bundle.getString(MSG_RESULTS2), new Object[] { Utils.encode(this.contains), filterMsg });
        }
        out.write(resultsMsg);
        out.write("&nbsp;");
        out.write("<a href='#' onclick=\"");
        out.write(generateFormSubmit(context, ACTION_CLEAR));
        out.write("\">");
        out.write(Utils.encode(bundle.getString(MSG_CLEAR)));
        out.write("</a></td></tr>");
    }
    // results list row
    out.write("<tr><td colspan=2>");
    out.write("<select size='8' style='");
    out.write((currentResults != null && currentResults.length != 0) ? "min-width:" : "width:");
    out.write(Integer.toString(getWidth()));
    out.write("px;height:");
    out.write(Integer.toString(getHeight()));
    out.write("px' name='");
    out.write(clientId + FIELD_RESULTS);
    out.write("' id='");
    out.write(clientId + FIELD_RESULTS);
    out.write("'");
    if (getMultiSelect() == true) {
        out.write(" multiple");
    }
    out.write(">");
    // results
    if (currentResults != null) {
        // show each of the items in the results listbox
        for (int i = 0; i < currentResults.length; i++) {
            out.write("<option value=\"");
            out.write(currentResults[i].getValue().toString());
            out.write("\">");
            out.write(Utils.encode(currentResults[i].getLabel()));
            out.write("</option>");
        }
    }
    // end results list
    out.write("</select>");
    out.write("</td></tr>");
    // bottom row - add button
    if (getShowAddButton() == true) {
        out.write("<tr><td colspan=2>");
        out.write("<input type='submit' value='");
        msg = getAddButtonLabel();
        if (msg == null || msg.length() == 0) {
            msg = bundle.getString(MSG_ADD);
        }
        out.write(Utils.encode(msg));
        out.write("' onclick=\"");
        out.write(generateFormSubmit(context, ACTION_ADD));
        out.write("\">");
        out.write("</td></tr>");
    }
    // end outer table
    out.write("</table>");
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) SelectItem(javax.faces.model.SelectItem) ResourceBundle(java.util.ResourceBundle)

Aggregations

ResponseWriter (javax.faces.context.ResponseWriter)88 Map (java.util.Map)16 UIComponent (javax.faces.component.UIComponent)14 FacesContext (javax.faces.context.FacesContext)14 NodeRef (org.alfresco.service.cmr.repository.NodeRef)13 ResourceBundle (java.util.ResourceBundle)10 NodeService (org.alfresco.service.cmr.repository.NodeService)10 Iterator (java.util.Iterator)9 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 List (java.util.List)7 TreeNode (org.alfresco.web.ui.repo.component.UITree.TreeNode)7 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)5 UserTransaction (javax.transaction.UserTransaction)5 UIModeList (org.alfresco.web.ui.common.component.UIModeList)5 Serializable (java.io.Serializable)4 Node (org.alfresco.web.bean.repository.Node)4 QuickSort (org.alfresco.web.data.QuickSort)3 UIListItem (org.alfresco.web.ui.common.component.UIListItem)3