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.util.MessageUtil;
044: import java.beans.Beans;
045: import java.io.IOException;
046: import java.lang.NullPointerException;
047: import java.util.Iterator;
048:
049: import javax.faces.application.FacesMessage;
050: import javax.faces.component.UIComponent;
051: import javax.faces.component.UIMessages;
052: import javax.faces.context.FacesContext;
053: import javax.faces.context.ResponseWriter;
054:
055: import com.sun.rave.web.ui.component.Message;
056: import com.sun.rave.web.ui.theme.Theme;
057: import com.sun.rave.web.ui.theme.ThemeStyles;
058: import com.sun.rave.web.ui.util.FacesMessageUtils;
059: import com.sun.rave.web.ui.util.RenderingUtilities;
060: import com.sun.rave.web.ui.util.ThemeUtilities;
061:
062: /**
063: * <p>This class is responsible for rendering the Message component.</p>
064: */
065: public class MessageRenderer extends AbstractRenderer {
066:
067: /**
068: * Renders the Message component.
069: *
070: * @param context <code>FacesContext</code> for the current request
071: * @param component <code>UIComponent</code> to be rendered
072: * @param writer <code>ResponseWriter</code> to which the element
073: * end should be rendered
074: * @exception IOException if an input/output error occurs
075: */
076: protected void renderEnd(FacesContext context,
077: UIComponent component, ResponseWriter writer)
078: throws IOException {
079: // End the appropriate element
080: Message message = (Message) component;
081:
082: String forComponentId = message.getFor();
083: FacesMessage msg = null;
084: Iterator msgIt = null;
085:
086: if (Beans.isDesignTime()) {
087: // At design-time, prepare a default message
088: String summary = null;
089:
090: if (forComponentId == null || forComponentId.length() == 0) {
091: summary = MessageUtil.getMessage(context,
092: "com.sun.rave.web.ui.renderer.Bundle",
093: "Message.default.summary"); //NOI18N
094: // <RAVE>
095: renderMessage(context, component, writer,
096: new FacesMessage(summary));
097: // </RAVE>
098: } else {
099: summary = MessageUtil.getMessage(context,
100: "com.sun.rave.web.ui.renderer.Bundle",
101: "Message.for.summary", //NOI18N
102: new String[] { forComponentId });
103: // <RAVE>
104: String detail = MessageUtil.getMessage(context,
105: "com.sun.rave.web.ui.renderer.Bundle",
106: "Message.for.detail", //NOI18N
107: new String[] { forComponentId });
108:
109: renderMessage(context, component, writer,
110: new FacesMessage(summary, detail));
111: // </RAVE>
112: }
113: } else if (forComponentId != null) {
114: // Get the run-time messages for this component, if any
115: msgIt = FacesMessageUtils.getMessageIterator(context,
116: forComponentId, message);
117: if (msgIt.hasNext()) {
118: msg = (FacesMessage) msgIt.next();
119: renderMessage(context, component, writer, msg);
120: }
121: }
122: }
123:
124: /**
125: * Renders the Message text
126: *
127: * @param context The current FacesContext
128: * @param component The Message object to use
129: * @param writer The current ResponseWriter
130: * @param fMsg The FacesMessage message
131: *
132: * @exception IOException if an input/output error occurs
133: */
134: public void renderMessage(FacesContext context,
135: UIComponent component, ResponseWriter writer,
136: FacesMessage fMsg) throws IOException {
137:
138: Message message = (Message) component;
139: String summary = null;
140: String detail = null;
141:
142: // Check if there is both summary and detail messages to show
143: if (message.isShowSummary()) {
144: summary = fMsg.getSummary();
145: if ((summary != null) && (summary.length() <= 0)) {
146: summary = null;
147: }
148: }
149: if (message.isShowDetail()) {
150: detail = fMsg.getDetail();
151: if ((detail != null) && (detail.length() <= 0)) {
152: detail = null;
153: }
154: }
155:
156: if (summary == null && detail == null)
157: return;
158:
159: // Get the theme
160: Theme theme = ThemeUtilities.getTheme(context);
161: boolean wroteSpanId = false;
162: boolean wroteDivId = false;
163:
164: // <RAVE>
165: String style = message.getStyle();
166: String styleClass = message.getStyleClass();
167: // Render the style/styleClass attributes in a surrounding div
168: if (summary != null || detail != null) {
169: renderUserStyles(context, message, writer, style,
170: styleClass);
171: wroteDivId = true;
172: }
173:
174: // if user has defined severity based style, use that instead of
175: // default.
176: String severityStyleClass = null;
177: if (fMsg.getSeverity() == FacesMessage.SEVERITY_INFO) {
178: severityStyleClass = theme
179: .getStyleClass(ThemeStyles.MESSAGE_INFO);
180: } else if (fMsg.getSeverity() == FacesMessage.SEVERITY_WARN) {
181: severityStyleClass = theme
182: .getStyleClass(ThemeStyles.MESSAGE_WARN);
183: } else if (fMsg.getSeverity() == FacesMessage.SEVERITY_ERROR) {
184: severityStyleClass = theme
185: .getStyleClass(ThemeStyles.MESSAGE_ERROR);
186: } else if (fMsg.getSeverity() == FacesMessage.SEVERITY_FATAL) {
187: severityStyleClass = theme
188: .getStyleClass(ThemeStyles.MESSAGE_FATAL);
189: }
190:
191: if (summary != null) {
192: if (severityStyleClass == null
193: || severityStyleClass.length() == 0
194: || Beans.isDesignTime()) {
195: styleClass = theme
196: .getStyleClass(ThemeStyles.MESSAGE_FIELD_SUMMARY_TEXT);
197: } else {
198: styleClass = severityStyleClass;
199: }
200: renderMessageText(context, message, writer, summary,
201: styleClass, wroteSpanId);
202: wroteSpanId = true;
203: }
204:
205: if (detail != null) {
206: if (severityStyleClass == null
207: || severityStyleClass.length() == 0
208: || Beans.isDesignTime()) {
209: styleClass = theme
210: .getStyleClass(ThemeStyles.MESSAGE_FIELD_TEXT);
211: } else {
212: styleClass = severityStyleClass;
213: }
214: if (summary != null)
215: detail = " " + detail;
216: renderMessageText(context, message, writer, detail,
217: styleClass, wroteSpanId);
218: }
219:
220: if (wroteSpanId) {
221: writer.endElement("span"); // NOI18N
222: }
223:
224: if (wroteDivId) {
225: writer.endElement("div"); // NOI18N
226: }
227: //</RAVE>
228: }
229:
230: /**
231: * Helper method to write message text.
232: *
233: * @param context The current FacesContext
234: * @param message The Message object to use
235: * @param writer The current ResponseWriter
236: * @param msgText The message text
237: * @param textStyle The text style
238: * @param wroteOpeningSpanId Flag to indicate whether opening
239: * span and id were written
240: *
241: * @exception IOException if an input/output error occurs
242: */
243: private void renderMessageText(FacesContext context,
244: Message message, ResponseWriter writer, String msgText,
245: String textStyleClass, boolean wroteOpeningSpanId)
246: throws IOException {
247: //<RAVE>
248: //if (!wroteOpeningSpanId) {
249: // renderUserStyles(context, message, writer, textStyle, textStyleClass);
250: //} else {
251: //</RAVE>
252: if (!wroteOpeningSpanId) {
253: writer.startElement("span", message); //NOI18N
254: writer.writeAttribute("class", textStyleClass, "class"); //NOI18N
255: }
256: // Check if the message be HTML escaped (true by default).
257: /*
258: if (!message.isEscape()) {
259: writer.write(msgText);
260: } else {
261: writer.writeText(msgText, null);
262: }
263: */
264: writer.writeText(msgText, null);
265: //<RAVE>
266: //writer.endElement("span"); // NOI18N
267: //</RAVE>
268: }
269:
270: /**
271: * Helper method to write styles if specified.
272: * The span is written out even if no style/styleClass was specified.
273: *
274: * @param context The current FacesContext
275: * @param message The Message object to use
276: * @param writer The current ResponseWriter
277: * @param styleClass The message style class
278: *
279: * @exception IOException if an input/output error occurs
280: */
281: private void renderUserStyles(FacesContext context,
282: Message message, ResponseWriter writer, String userStyle,
283: String styleClass) throws IOException {
284: // <RAVE>
285: String id = message.getClientId(context);
286:
287: writer.startElement("div", message); //NO18N
288: writer.writeAttribute("id", id, "id"); //NOI18N
289:
290: if (userStyle != null && userStyle.length() > 0) {
291: writer.writeAttribute("style", userStyle, "style"); //NOI18N
292: }
293: // </RAVE>
294: RenderingUtilities.renderStyleClass(context, writer, message,
295: styleClass);
296: }
297:
298: }
|