001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.renderer;
042:
043: import com.sun.rave.web.ui.component.PropertySheetSection;
044: import com.sun.rave.web.ui.theme.Theme;
045: import com.sun.rave.web.ui.theme.ThemeStyles;
046: import com.sun.rave.web.ui.util.RenderingUtilities;
047: import com.sun.rave.web.ui.util.ThemeUtilities;
048: import java.io.IOException;
049: import java.util.Iterator;
050: import java.util.List;
051:
052: import javax.faces.component.UIComponent;
053: import javax.faces.context.FacesContext;
054: import javax.faces.context.ResponseWriter;
055: import javax.faces.render.Renderer;
056:
057: public class PropertySheetSectionRenderer extends Renderer {
058:
059: /**
060: * Creates a new instance of PropertySheetSectionRenderer.
061: */
062: public PropertySheetSectionRenderer() {
063: }
064:
065: /**
066: * This renderer renders the component's children.
067: */
068: public boolean getRendersChildren() {
069: return true;
070: }
071:
072: /**
073: * Render a property sheet.
074: *
075: * @param context The current FacesContext
076: * @param component The PropertySheet object to render
077: * @param writer The current ResponseWriter
078: *
079: * @exception IOException if an input/output error occurs
080: */
081: public void encodeEnd(FacesContext context, UIComponent component)
082: throws IOException {
083: if (context == null || component == null) {
084: throw new NullPointerException();
085: }
086:
087: if (!component.isRendered()) {
088: return;
089: }
090:
091: ResponseWriter writer = context.getResponseWriter();
092:
093: PropertySheetSection propertySheetSection = (PropertySheetSection) component;
094:
095: // Get the theme
096: //
097: Theme theme = ThemeUtilities.getTheme(context);
098:
099: renderPropertySheetSection(context, propertySheetSection,
100: theme, writer);
101: }
102:
103: // There is an extensive use of the request map by the
104: // template renderer.
105: // The setAttribute handler places the key/value pair in the
106: // request map.
107: //
108: /**
109: * Render the property sheet sections.
110: *
111: * @param context The current FacesContext
112: * @param propertySheet The PropertySheet object to render
113: * @param theme The Theme to reference.
114: * @param writer The current ResponseWriter
115: *
116: * @exception IOException if an input/output error occurs
117: */
118: protected void renderPropertySheetSection(FacesContext context,
119: PropertySheetSection propertySheetSection, Theme theme,
120: ResponseWriter writer) throws IOException {
121:
122: int numChildren = propertySheetSection
123: .getSectionChildrenCount();
124: if (numChildren <= 0) {
125: return;
126: }
127:
128: writer.startElement("div", propertySheetSection);
129: writer.writeAttribute("id", propertySheetSection
130: .getClientId(context), "id");//NOI18N
131: String propValue = RenderingUtilities.getStyleClasses(context,
132: propertySheetSection, theme
133: .getStyleClass(ThemeStyles.CONTENT_FIELDSET));
134: writer.writeAttribute("class", propValue, null);
135:
136: // There was a distinction made between ie and other browsers.
137: // If the browser was ie, fieldsets were used, and if not
138: // divs were used. Why ? Just use divs here.
139: //
140: writer.startElement("div", propertySheetSection);
141: writer.writeAttribute("class", theme
142: .getStyleClass(ThemeStyles.CONTENT_FIELDSET_DIV), null);
143:
144: // Render the section label
145: // Why isn't this a label facet on PropertySheetSection, too ?
146: //
147: propValue = propertySheetSection.getLabel();
148: if (propValue != null) {
149: writer.startElement("div", propertySheetSection);
150: writer
151: .writeAttribute(
152: "class",
153: theme
154: .getStyleClass(ThemeStyles.CONTENT_FIELDSET_LEGEND_DIV),
155: null);
156: writer.writeText(propValue, null);
157: writer.endElement("div");
158: }
159:
160: renderProperties(context, propertySheetSection, theme, writer);
161:
162: writer.endElement("div");
163: writer.endElement("div");
164: }
165:
166: /**
167: * Render a required fields legend.
168: * If <code>propertySheet.getRequiredFields</code> returns null
169: * a spacer is rendered.
170: *
171: * @param context The current FacesContext
172: * @param propertySheet The PropertySheet object to render
173: * @param theme The Theme to reference.
174: * @param writer The current ResponseWriter
175: *
176: * @exception IOException if an input/output error occurs
177: */
178: protected void renderProperties(FacesContext context,
179: PropertySheetSection propertySheetSection, Theme theme,
180: ResponseWriter writer) throws IOException {
181:
182: List properties = propertySheetSection
183: .getVisibleSectionChildren();
184:
185: writer.startElement("table", propertySheetSection);
186: writer.writeAttribute("border", "0", null);
187: writer.writeAttribute("cellspacing", "0", null);
188: writer.writeAttribute("cellpadding", "0", null);
189: writer.writeAttribute("title", "", null); //NOI18N
190:
191: // Unfortunately the PropertyRenderer needs to render
192: // a TR and TD since we are opening a table context here.
193: // This can't be changed easily unless a strategy like the
194: // radio button and checkbox group renderer is used, where there is
195: // a table layout renderer. I'm not sure if that is sufficiently
196: // robust to handle "properties".
197: //
198:
199: Iterator propertiesIterator = properties.iterator();
200: while (propertiesIterator.hasNext()) {
201: UIComponent property = (UIComponent) propertiesIterator
202: .next();
203: RenderingUtilities.renderComponent(property, context);
204: }
205:
206: writer.endElement("table");
207: }
208:
209: /**
210: * Render a property sheet.
211: *
212: * @param context The current FacesContext
213: * @param component The PropertySheet object to render
214: * @param writer The current ResponseWriter
215: *
216: * @exception IOException if an input/output error occurs
217: */
218: public void encodeChildren(FacesContext context,
219: UIComponent component) throws IOException {
220: if (context == null || component == null) {
221: throw new NullPointerException();
222: }
223: }
224: }
|