001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/widgets/src/java/org/sakaiproject/jsf/renderer/OutputDateRenderer.java $
003: * $Id: OutputDateRenderer.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 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.sakaiproject.jsf.renderer;
021:
022: import java.io.IOException;
023: import java.text.SimpleDateFormat;
024: import java.util.Date;
025:
026: import javax.faces.component.UIComponent;
027: import javax.faces.component.UIOutput;
028: import javax.faces.component.UIViewRoot;
029: import javax.faces.context.FacesContext;
030: import javax.faces.context.ResponseWriter;
031: import javax.faces.render.Renderer;
032:
033: import org.sakaiproject.jsf.util.ConfigurationResource;
034: import org.sakaiproject.jsf.util.RendererUtil;
035:
036: /**
037: * <p>OutputDateRenderer is an HTML renderer for Sakai 2.0 JSF outputDate.</p>
038: *
039: * @author esmiley@stanford.edu
040: * @author based partly on some previous code in DateOutput.java ggolden@umich.edu
041: * @version $Revision: 9278 $
042: *
043: *
044: */
045: public class OutputDateRenderer extends Renderer {
046: /**
047: * @todo add more configuration
048: */
049: private static final String OUTPUT_DATE;// for now we'll jsut use this one...
050: private static final String OUTPUT_DATE_TIME;
051: private static final String OUTPUT_DATE_SECS;
052: private static final String OUTPUT_TIME;
053: private static final String OUTPUT_SECS;
054:
055: // we have static resources for our date formats
056: static {
057: ConfigurationResource cr = new ConfigurationResource();
058: OUTPUT_DATE = cr.get("outputDate");
059: OUTPUT_DATE_TIME = cr.get("outputDateTime");
060: OUTPUT_DATE_SECS = cr.get("outputDateTimeSecs");
061: OUTPUT_TIME = cr.get("outputTime");
062: OUTPUT_SECS = cr.get("outputTimeSecs");
063: }
064:
065: public boolean supportsComponentType(UIComponent component) {
066: return (component instanceof UIOutput);
067: }
068:
069: /**
070: * <p>Faces render output method .</p>
071: * <p>Method Generator: org.sakaiproject.tool.assessment.devtoolsRenderMaker</p>
072: *
073: * @param context <code>FacesContext</code> for the current request
074: * @param component <code>UIComponent</code> being rendered
075: *
076: * @throws IOException if an input/output error occurs
077: */
078:
079: public void encodeBegin(FacesContext context, UIComponent component)
080: throws IOException {
081: if (!component.isRendered()) {
082: return;
083: }
084:
085: ResponseWriter writer = context.getResponseWriter();
086:
087: // this is a date object representing our date
088: // this is a debug line, we are losing our date
089: Date date = ((Date) RendererUtil.getDefaultedAttribute(context,
090: component, "value", new Date()));
091: // this is the formatted output representation of the date
092: String dateStr = "";
093: String formatStr = getFormatString(context, component);
094: dateStr = format(date, formatStr);
095:
096: String clientId = null;
097:
098: if (component.getId() != null
099: && !component.getId().startsWith(
100: UIViewRoot.UNIQUE_ID_PREFIX)) {
101: clientId = component.getClientId(context);
102: }
103:
104: if (clientId != null) {
105: writer.startElement("span", component);
106: writer.writeAttribute("id", clientId, "id");
107: }
108: writer.write(dateStr);
109: if (clientId != null) {
110: writer.endElement("span");
111: }
112:
113: }
114:
115: /**
116: * Obtain the format string from the
117: * @param context Faces context
118: * @param component the component
119: * @return a date format String
120: */
121: private String getFormatString(FacesContext context,
122: UIComponent component) {
123: String fmt = OUTPUT_DATE;
124:
125: String showTime = (String) RendererUtil.getAttribute(context,
126: component, "showTime");
127: String showDate = (String) RendererUtil.getAttribute(context,
128: component, "showDate");
129: String showSeconds = (String) RendererUtil.getAttribute(
130: context, component, "showSeconds");
131: // boolean hasTime = true; //"true".equals(showTime)? true: false;
132: // boolean hasDate = true; //"true".equals(showDate)? true: false;
133: // boolean hasSeconds = true; //"true".equals(showSeconds) && hasTime ? true: false;
134:
135: boolean hasTime = "true".equals(showTime) ? true : false;
136: boolean hasDate = "true".equals(showDate) ? true : false;
137: boolean hasSeconds = "true".equals(showSeconds) && hasTime ? true
138: : false;
139:
140: if (hasTime) {
141: if (hasDate) {
142: if (hasSeconds) {
143: fmt = OUTPUT_DATE_SECS;
144: } else {
145: fmt = OUTPUT_DATE_TIME;
146: }
147: } else {
148: if (hasSeconds) {
149: fmt = OUTPUT_SECS;
150: } else {
151: fmt = OUTPUT_TIME;
152: }
153: }
154: }
155:
156: return fmt;
157: }
158:
159: /**
160: * @param date
161: * @return the formatted date String
162: */
163: private String format(Date date, String format) {
164: SimpleDateFormat sdf = new SimpleDateFormat(format);
165: return sdf.format(date);
166: }
167:
168: }
|