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 java.io.IOException;
044:
045: import javax.faces.FacesException;
046: import javax.faces.component.UIComponent;
047: import javax.faces.context.FacesContext;
048: import javax.faces.context.ResponseWriter;
049: import javax.faces.context.FacesContext;
050: import javax.faces.render.Renderer;
051:
052: import com.sun.rave.web.ui.component.DropDown;
053: import com.sun.rave.web.ui.component.Time;
054: import com.sun.rave.web.ui.model.ClockTime;
055: import com.sun.rave.web.ui.theme.Theme;
056: import com.sun.rave.web.ui.theme.ThemeStyles;
057: import com.sun.rave.web.ui.util.MessageUtil;
058: import com.sun.rave.web.ui.util.RenderingUtilities;
059: import com.sun.rave.web.ui.util.ThemeUtilities;
060:
061: /**
062: *
063: * @author avk
064: */
065: public class TimeRenderer extends Renderer {
066:
067: private static final boolean DEBUG = false;
068:
069: public void encodeEnd(FacesContext context, UIComponent component)
070: throws IOException {
071:
072: if (DEBUG)
073: log("encodeEnd() START");
074:
075: if (!(component instanceof Time)) {
076: Object[] params = { component.toString(),
077: this .getClass().getName(), Time.class.getName() };
078: String message = MessageUtil.getMessage(
079: "com.sun.rave.web.ui.resources.LogMessages", //NOI18N
080: "Renderer.component", params); //NOI18N
081: throw new FacesException(message);
082: }
083: Theme theme = ThemeUtilities.getTheme(context);
084: Time time = (Time) component;
085: DropDown hourMenu = time.getHourMenu();
086: DropDown minuteMenu = time.getMinutesMenu();
087:
088: // If there is no submitted value, set the values of
089: // the DropDowns to the actual value... If we have
090: // a submitted value, the DropDown will remember it
091: // so we do nothing in that case.
092: // FIXME: have to round this to the nearest five minutes!
093: if (time.getSubmittedValue() == null) {
094:
095: if (DEBUG)
096: log("No submitted value");
097: Object object = time.getValue();
098: if (DEBUG)
099: log("Got the ClockTime");
100:
101: ClockTime value = null;
102: if (object != null && object instanceof ClockTime) {
103: value = (ClockTime) object;
104: if (DEBUG)
105: log("\tValue is " + String.valueOf(value));
106: }
107: if (value != null) {
108: hourMenu.setValue(value.getHour());
109: } else {
110: hourMenu.setValue(new Integer(-1));
111: }
112:
113: if (value != null) {
114: minuteMenu.setValue(value.getMinute());
115: } else {
116: minuteMenu.setValue(new Integer(-1));
117: }
118: } else if (DEBUG)
119: log("Found submitted value");
120:
121: String key = time.getHourTooltipKey();
122: if (key != null) {
123: hourMenu.setToolTip(theme.getMessage(key));
124: }
125:
126: key = time.getMinutesTooltipKey();
127: if (key != null) {
128: minuteMenu.setToolTip(theme.getMessage(key));
129: }
130:
131: ResponseWriter writer = context.getResponseWriter();
132:
133: writer.startElement("table", time); //NOI18N
134: writer.writeAttribute("cellspacing", "0", null); // NOI18N
135: writer.writeAttribute("cellpadding", "0", null); // NOI18N
136: writer.writeText("\n", null); //NOI18N
137: writer.startElement("tr", time); //NOI18N
138: writer.writeText("\n", null); //NOI18N
139:
140: // hour menu
141: writer.startElement("td", time); //NOI18N
142: writer.writeText("\n", null); //NOI18N
143: RenderingUtilities.renderComponent(hourMenu, context);
144: writer.writeText("\n", null); //NOI18N
145: writer.endElement("td"); //NOI18N
146: writer.writeText("\n", null); //NOI18N
147:
148: // colon
149: writer.startElement("td", time);
150: writer.write(":");
151: writer.endElement("td");
152: writer.writeText("\n", null); //NOI18N
153:
154: // minutes menu
155: writer.startElement("td", time); //NOI18N
156: writer.writeText("\n", null); //NOI18N
157: RenderingUtilities.renderComponent(minuteMenu, context);
158: writer.writeText("\n", null); //NOI18N
159:
160: // Should use another cell for this, and a width attribute instad
161: //writer.write(" ");
162:
163: writer.startElement("span", time); //NOI18N
164:
165: String string = theme
166: .getStyleClass(ThemeStyles.DATE_TIME_ZONE_TEXT);
167: writer.writeAttribute("class", string, null); // NOI18N
168: writer.writeText(theme.getMessage("Time.gmt"), null);
169: writer.writeText(time.getOffset(), null);
170: writer.endElement("span"); //NOI18N
171: writer.writeText("\n", null); //NOI18N
172:
173: writer.endElement("td"); //NOI18N
174: writer.writeText("\n", null); //NOI18N
175: writer.endElement("tr"); //NOI18N
176: writer.writeText("\n", null); //NOI18N
177: writer.endElement("table"); //NOI18N
178: writer.writeText("\n", null); //NOI18N
179:
180: if (DEBUG)
181: log("encodeEnd() END");
182: }
183:
184: public void encodeChildren(FacesContext context,
185: UIComponent component) {
186: return;
187: }
188:
189: public void encodeBegin(FacesContext context, UIComponent component) {
190: return;
191: }
192:
193: public String toString() {
194: return this .getClass().getName();
195: }
196:
197: public boolean getRendersChildren() {
198: return true;
199: }
200:
201: private void log(String s) {
202: System.out.println(this .getClass().getName() + "::" + s);
203: }
204: }
|