All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.webui.jsf.renderkit.html.ListRendererBase Maven / Gradle / Ivy

There is a newer version: 4.4.0.1
Show newest version
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at
 * https://woodstock.dev.java.net/public/CDDLv1.0.html.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at https://woodstock.dev.java.net/public/CDDLv1.0.html.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
 */
/*
 * $Id: ListRendererBase.java,v 1.1.4.1.2.1 2009-12-29 04:52:45 jyeary Exp $
 */
package com.sun.webui.jsf.renderkit.html;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.component.EditableValueHolder;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;
import com.sun.webui.jsf.component.ListManager;
import com.sun.webui.jsf.component.ListSelector;
import com.sun.webui.jsf.model.Separator;
import com.sun.webui.jsf.model.OptionTitle;
import com.sun.webui.jsf.model.list.ListItem;
import com.sun.webui.jsf.model.list.StartGroup;
import com.sun.webui.jsf.model.list.EndGroup;
import com.sun.webui.jsf.util.ConversionUtilities;
import com.sun.webui.jsf.util.RenderingUtilities;

/**
 * The ListRendererBase is the base class for the listbox renderers
 * (Drop-down Menu and Selectable List). These are both rendered using
 * the same HTML tag (select) so a lot of the renderering functionality
 * is shared.
 */
public abstract class ListRendererBase extends Renderer {

    /**
     *
     */
    private final static boolean DEBUG = false;
    /** 

The list of attribute names in the HTML 4.01 Specification that * correspond to the entity type %events;.

*/ public static final String[] STRING_ATTRIBUTES = {"onBlur", "onClick", "onDblClick", "onFocus", //NOI18N "onMouseDown", "onMouseUp", "onMouseOver", //NOI18N "onMouseMove", "onMouseOut", "onKeyPress", //NOI18N "onKeyDown", "onKeyUp", "onSelect" //NOI18N }; protected static final String SEPARATOR = "|"; //NOI18N /** *

This method determines whether the component should be * rendered as a standalone list, or laid out together with a * label that was defined as part of the component.

* *

A label will be rendered if either of the following is * true:

*
    *
  • The page author defined a label facet; or
  • *
  • The page author specified text in the label attribute.
  • *
*

If there is a label, the component will be laid out using a * HTML table. If not, the component will be rendered as a * standalone HTML select element.

* @param component The component associated with the * renderer. Must be a subclass of ListSelector. * @param context The FacesContext of the request * @param styles A String array of styles used to render the * component. The first item of the array is the name of the * JavaScript method that handles change event. The second item is * the style used when the list is enabled. The third style is the * one to use when the list is disabled. The fourth item is the * style to use for an item that is enabled, the fifth to use for * an item that is disabled, and the sixth to use when the item is * selected. * @throws java.io.IOException if the renderer fails to write to * the response */ void renderListComponent(ListSelector component, FacesContext context, String[] styles) throws IOException { if (DEBUG) { log("renderListComponent()"); } boolean readonly = component.isReadOnly(); if (readonly) { if (DEBUG) { log("\t component is readonly"); } // We don't want to accidentally mark any label as // required in this case... component.setRequired(false); } UIComponent label = component.getLabelComponent(); ResponseWriter writer = context.getResponseWriter(); String id = component.getClientId(context); boolean spanRendered = false; if (label != null) { renderOpenEncloser(component, context, "span", styles[8]); //NOI18N spanRendered = true; writer.writeText("\n", null); if (!component.isLabelOnTop() && component.getRows() > 1) { Map attributes = label.getAttributes(); Object styleClass = attributes.get("styleClass"); if (styleClass == null) { attributes.put("styleClass", styles[9]); } else if (styleClass.toString().indexOf(styles[9]) == -1) { attributes.put("styleClass", styleClass + " " + styles[9]); } } RenderingUtilities.renderComponent(label, context); writer.writeText("\n", null); if (component.isLabelOnTop()) { writer.startElement("br", component); //NOI18N writer.endElement("br"); //NOI18N writer.writeText("\n", null); } writer.writeText("\n", null); id = id.concat(ListSelector.LIST_ID); } if (readonly) { UIComponent value = component.getReadOnlyValueComponent(); if (label == null) { value.getAttributes().put("style", component.getStyle()); value.getAttributes().put("styleClass", component.getStyleClass()); } RenderingUtilities.renderComponent(value, context); } else { //renderHiddenValue(component, context, writer, styles[8]); //writer.writeText("\n", null); // Because renderHiddenValue is commented out this needs // to be called for supporting DB NULL values. // If it becomes uncommented remove this call. // recordRenderedValue(component); renderList(component, id, context, styles, label == null); } if (label != null) { context.getResponseWriter().endElement("span"); //NOI18N } } /** * Overrides encodeChildren of Renderer to do nothing. This * renderer renders its own children, but not through this * method. * @param context The FacesContext of the request * @param component The component associated with the * renderer. Must be a subclass of ListSelector. */ @Override public void encodeChildren(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component) throws java.io.IOException { return; } /** *

Renders the opening div tag.

* @param component The component associated with the * renderer. Must implement ListManager * @param context The FacesContext of the request * @param element One of "span" or "div" * @throws java.io.IOException if the renderer fails to write to * the response */ protected void renderOpenEncloser(ListManager component, FacesContext context, String element, String hiddenStyle) throws IOException { String id = component.getClientId(context); ResponseWriter writer = context.getResponseWriter(); writer.writeText("\n", null); writer.startElement(element, (UIComponent) component); writer.writeAttribute("id", id, "id"); //NOI18N String style = component.getStyle(); if (style != null && style.length() > 0) { writer.writeAttribute("style", style, "style"); //NOI18N } style = component.getStyleClass(); if (component.isVisible()) { if (style != null && style.length() > 0) { writer.writeAttribute("class", style, "class"); //NOI18N } } else { if (style == null) { style = hiddenStyle; } else { style = style + " " + hiddenStyle; //NOI18N } writer.writeAttribute("class", style, "class"); //NOI18N } writer.writeText("\n", null); //NOI18N } protected void renderHiddenValue(UIComponent component, FacesContext context, ResponseWriter writer, String hiddenStyle) throws IOException { ListManager listManager = (ListManager) component; recordRenderedValue(component); String hiddenID = component.getClientId(context).concat(ListSelector.VALUE_ID); String hiddenLabelID = component.getClientId(context).concat(ListSelector.VALUE_LABEL_ID); String[] values = listManager.getValueAsStringArray(context); if (DEBUG) { log("Values are: "); for (int i = 0; i < values.length; ++i) { log(String.valueOf(values[i])); } } // Write a hidden label to pacify a11y checkers. writer.startElement("label", component); //NOI18N writer.writeAttribute("id", hiddenLabelID, null); //NOI18N writer.writeAttribute("for", hiddenID, "for"); //NOI19N writer.writeAttribute("class", hiddenStyle, null); //NOI18N writer.endElement("label"); //NOI18N // Write the hidden