001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/jsf/widgets/src/java/org/theospi/jsf/renderer/WizardStepsRenderer.java $
003: * $Id: WizardStepsRenderer.java 15703 2006-10-05 18:16:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.jsf.renderer;
021:
022: import java.io.IOException;
023: import java.text.MessageFormat;
024: import java.util.Iterator;
025:
026: import javax.faces.component.UIComponent;
027: import javax.faces.component.UIOutput;
028: import javax.faces.context.FacesContext;
029: import javax.faces.context.ResponseWriter;
030: import javax.faces.render.Renderer;
031:
032: import org.sakaiproject.jsf.util.RendererUtil;
033: import org.theospi.jsf.component.WizardStepComponent;
034: import org.theospi.jsf.util.ConfigurationResource;
035:
036: public class WizardStepsRenderer extends Renderer {
037: private static final String RESOURCE_PATH;
038: private static final String CSS_LOC;
039: private static final String STEP_MSG;
040:
041: static {
042: ConfigurationResource cr = new ConfigurationResource();
043: RESOURCE_PATH = "/" + cr.get("resources");
044: CSS_LOC = RESOURCE_PATH + "/" + cr.get("cssFile");
045: STEP_MSG = cr.get("wizard_step_message");
046:
047: }
048:
049: public boolean supportsComponentType(UIComponent component) {
050: return (component instanceof UIOutput);
051: }
052:
053: /**
054: * This renders html for the beginning of the tag.
055: *
056: * @param context
057: * @param component
058: * @throws IOException
059: */
060: public void encodeBegin(FacesContext context, UIComponent component)
061: throws IOException {
062: ResponseWriter writer = context.getResponseWriter();
063: RendererUtil.writeExternalCSSDependencies(context, writer,
064: "osp.jsf.css", CSS_LOC);
065:
066: writer.write("<div class=\"xheader\">");
067: }
068:
069: /**
070: * @param context FacesContext for the request we are processing
071: * @param component UIComponent to be rendered
072: * @exception IOException if an input/output error occurs while rendering
073: * @exception NullPointerException if <code>context</code> or <code>component</code> is null
074: */
075: public void encodeEnd(FacesContext context, UIComponent component)
076: throws IOException {
077: ResponseWriter writer = context.getResponseWriter();
078: writer.write("</div>");
079: }
080:
081: /**
082: * This class renders its own children and this is the function to do just that
083: * @param context FacesContext for the request we are processing
084: * @param component UIComponent to be rendered
085: * @exception IOException if an input/output error occurs while rendering
086: */
087: public void encodeChildren(FacesContext context,
088: UIComponent component) throws IOException {
089: Iterator iter = component.getChildren().iterator();
090: String currentStep = (String) RendererUtil.getAttribute(
091: context, component, "currentStep");
092: int totalSteps = countSteps(component);
093: int i = 0;
094: while (iter.hasNext()) {
095: UIComponent step = (UIComponent) iter.next();
096: if (!(step instanceof WizardStepComponent)
097: || !step.isRendered()
098: || !currentStep.equals(String.valueOf(i))) {
099: i++;
100: continue;
101: }
102:
103: String label = (String) RendererUtil.getAttribute(context,
104: step, "label");
105: ResponseWriter writer = context.getResponseWriter();
106:
107: String stepText = MessageFormat.format(STEP_MSG,
108: new Object[] {
109: String.valueOf(Integer
110: .parseInt(currentStep) + 1),
111: String.valueOf(totalSteps), label });
112:
113: writer.write(stepText);
114: break;
115: }
116: }
117:
118: protected int countSteps(UIComponent component) {
119: Iterator iter = component.getChildren().iterator();
120: int i = 0;
121: while (iter.hasNext()) {
122: UIComponent step = (UIComponent) iter.next();
123: if (!(step instanceof WizardStepComponent)
124: || !step.isRendered()) {
125: continue;
126: }
127:
128: i++;
129: }
130: return i;
131: }
132:
133: /**
134: * This class renders its own children
135: *
136: * @param context
137: * @param component
138: * @throws IOException
139: */
140: public boolean getRendersChildren() {
141: return true;
142: }
143: }
|