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.Collections;
048: import java.util.Iterator;
049: import java.util.List;
050:
051: import javax.faces.application.FacesMessage;
052: import javax.faces.component.UIComponent;
053: import javax.faces.component.UIMessages;
054: import javax.faces.context.FacesContext;
055: import javax.faces.context.ResponseWriter;
056:
057: import com.sun.rave.web.ui.component.MessageGroup;
058: import com.sun.rave.web.ui.theme.Theme;
059: import com.sun.rave.web.ui.theme.ThemeStyles;
060: import com.sun.rave.web.ui.util.FacesMessageUtils;
061: import com.sun.rave.web.ui.util.RenderingUtilities;
062: import com.sun.rave.web.ui.util.ThemeUtilities;
063:
064: /**
065: * <p>This class is responsible for rendering the Message component.</p>
066: */
067: public class MessageGroupRenderer extends AbstractRenderer {
068:
069: /**
070: * Renders the Message component.
071: *
072: * @param context <code>FacesContext</code> for the current request
073: * @param component <code>UIComponent</code> to be rendered
074: * @param writer <code>ResponseWriter</code> to which the element
075: * end should be rendered
076: * @exception IOException if an input/output error occurs
077: */
078: protected void renderEnd(FacesContext context,
079: UIComponent component, ResponseWriter writer)
080: throws IOException {
081: // End the appropriate element
082: MessageGroup msgGrp = (MessageGroup) component;
083: Iterator msgIt = null;
084: String forComponentId = null;
085:
086: if (Beans.isDesignTime()
087: && (msgGrp.isShowDetail() || msgGrp.isShowSummary())) {
088: StringBuffer resourceNameBuffer = new StringBuffer();
089: resourceNameBuffer.append("MessageGroup."); //NOI18N
090: if (msgGrp.isShowGlobalOnly())
091: resourceNameBuffer.append("global."); //NOI18N
092: else
093: resourceNameBuffer.append("default."); //NOI18N
094: if (msgGrp.isShowDetail() && msgGrp.isShowSummary())
095: resourceNameBuffer.append("both"); //NOI18N
096: else if (msgGrp.isShowDetail())
097: resourceNameBuffer.append("detail"); //NOI18N
098: else if (msgGrp.isShowSummary())
099: resourceNameBuffer.append("summary"); //NOI18N
100: String summary = MessageUtil.getMessage(context,
101: "com.sun.rave.web.ui.renderer.Bundle", //NOI18N
102: resourceNameBuffer.toString());
103: FacesMessage defaultMessage = new FacesMessage();
104: defaultMessage.setSummary(summary);
105: msgIt = Collections.singletonList(defaultMessage)
106: .iterator();
107: } else {
108: if (msgGrp.isShowGlobalOnly()) {
109: forComponentId = ""; // for only global messages
110: }
111: msgIt = FacesMessageUtils.getMessageIterator(context,
112: forComponentId, msgGrp);
113: }
114: if (msgIt.hasNext()) {
115: renderMessageGroup(context, msgGrp, writer, msgIt);
116: }
117: }
118:
119: /**
120: * Renders the Message text
121: *
122: * @param context The current FacesContext
123: * @param component The VersionPage object to use
124: * @param writer The current ResponseWriter
125: * @param msg The message
126: *
127: * @exception IOException if an input/output error occurs
128: */
129: public void renderMessageGroup(FacesContext context,
130: UIComponent component, ResponseWriter writer, Iterator msgIt)
131: throws IOException {
132:
133: MessageGroup msgGrp = (MessageGroup) component;
134:
135: // Get the theme
136: Theme theme = ThemeUtilities.getTheme(context);
137:
138: // Render the style/styleClass attributes in a surrounding div
139: renderUserStyles(context, msgGrp, writer);
140:
141: // Render the opening table
142: renderOpeningTable(msgGrp, writer, theme);
143:
144: FacesMessage fMsg = null;
145: boolean showSummary = msgGrp.isShowSummary();
146: boolean showDetail = msgGrp.isShowDetail();
147: String summaryStyle = ThemeStyles.MESSAGE_GROUP_SUMMARY_TEXT;
148: String detailStyle = ThemeStyles.MESSAGE_GROUP_TEXT;
149: String summary = null;
150: String detail = null;
151:
152: while (msgIt.hasNext()) {
153: fMsg = (FacesMessage) msgIt.next();
154: // Check if we should show detail or summary
155: if (showSummary) {
156: summary = fMsg.getSummary();
157: if ((summary != null) && (summary.length() <= 0)) {
158: summary = null;
159: }
160: }
161: if (showDetail) {
162: detail = fMsg.getDetail();
163: if ((detail != null) && (detail.length() <= 0)) {
164: detail = null;
165: }
166: }
167:
168: if (summary == null && detail == null)
169: continue;
170:
171: writer.startElement("div", msgGrp); //NOI18N
172: writer.writeAttribute(
173: "class", //NOI18N
174: theme.getStyleClass(ThemeStyles.MESSAGE_GROUP_DIV),
175: null); //NOI18N
176: writer.startElement("ul", msgGrp); //NOI18N
177: writer.startElement("li", msgGrp); //NOI18N
178:
179: // <RAVE>
180: // render theme based style based on severity.
181: String severityStyleClass = null;
182: if (fMsg.getSeverity() == FacesMessage.SEVERITY_INFO) {
183: severityStyleClass = theme
184: .getStyleClass(ThemeStyles.MESSAGE_GROUP_INFO);
185: } else if (fMsg.getSeverity() == FacesMessage.SEVERITY_WARN) {
186: severityStyleClass = theme
187: .getStyleClass(ThemeStyles.MESSAGE_GROUP_WARN);
188: } else if (fMsg.getSeverity() == FacesMessage.SEVERITY_ERROR) {
189: severityStyleClass = theme
190: .getStyleClass(ThemeStyles.MESSAGE_GROUP_ERROR);
191: } else if (fMsg.getSeverity() == FacesMessage.SEVERITY_FATAL) {
192: severityStyleClass = theme
193: .getStyleClass(ThemeStyles.MESSAGE_GROUP_FATAL);
194: }
195:
196: if (severityStyleClass != null
197: && severityStyleClass.length() > 0) {
198: writer.writeAttribute("class", severityStyleClass,
199: "styleClass");
200: }
201:
202: if (summary != null) {
203: // renderMessageText(msgGrp, writer, summary, summaryStyle);
204: // if severity based style is set, don't use theme based
205: // default styles.
206: if (severityStyleClass == null
207: || severityStyleClass == "") {
208: renderMessageText(msgGrp, writer, summary,
209: summaryStyle);
210: } else {
211: renderMessageText(msgGrp, writer, summary, null);
212: }
213: }
214:
215: if (detail != null) {
216: // renderMessageText(msgGrp, writer, detail, detailStyle);
217: // if severity based style is set, don't use theme based
218: // default styles.
219: if (summary != null) {
220: detail = " " + detail;
221: }
222: if (severityStyleClass == null
223: || severityStyleClass == "") {
224: renderMessageText(msgGrp, writer, detail,
225: detailStyle);
226: } else {
227: renderMessageText(msgGrp, writer, detail, null);
228: }
229: }
230: //</RAVE>
231:
232: writer.endElement("li"); //NOI18N
233: writer.endElement("ul"); //NOI18N
234: writer.endElement("div"); //NOI18N
235: }
236:
237: // Close tags
238: renderClosingTable(writer);
239: writer.endElement("div"); // NOI18N
240: }
241:
242: /**
243: * Helper method to render opening tags for the layout table.
244: *
245: * @param msgGrp The MessageGroup object to use
246: * @param writer The current ResponseWriter
247: * @param theme The theme to use
248: *
249: * @exception IOException if an input/output error occurs
250: */
251: public void renderOpeningTable(MessageGroup msgGrp,
252: ResponseWriter writer, Theme theme) throws IOException {
253: writer.startElement("table", msgGrp); //NOI18N
254: writer.writeAttribute("border", "0", null); //NOI18N
255: writer.writeAttribute("cellspacing", "0", null); //NOI18N
256: writer.writeAttribute("cellpadding", "0", null); //NTOI18N
257: // <RAVE>
258: if (msgGrp.getToolTip() != null) {
259: writer.writeAttribute("title", msgGrp.getToolTip(), null); //NOI18N
260: }
261: // </RAVE>
262: writer.writeAttribute(
263: "class", //NOI18N
264: theme.getStyleClass(ThemeStyles.MESSAGE_GROUP_TABLE),
265: null); //NOI18N
266: writer.writeText("\n", null); //NOI18N
267:
268: writer.startElement("caption", msgGrp); //NOI18N
269: writer.writeAttribute("class", theme
270: .getStyleClass(ThemeStyles.MESSAGE_GROUP_TABLE_TITLE),
271: null); //NOI18N
272: writer
273: .writeText(theme.getMessage("messageGroup.heading"),
274: null);
275: writer.endElement("caption");
276: writer.writeText("\n", null); //NOI18N
277: writer.startElement("tr", msgGrp); //NOI18N
278: writer.startElement("td", msgGrp); //NOI18N
279: }
280:
281: /**
282: * Helper method to render closing tags for the layout table.
283: *
284: * @param writer The current ResponseWriter
285: *
286: * @exception IOException if an input/output error occurs
287: */
288: public void renderClosingTable(ResponseWriter writer)
289: throws IOException {
290: writer.endElement("td"); //NOI18N
291: writer.endElement("tr"); //NOI18N
292: writer.endElement("table"); //NOI18N
293: }
294:
295: /**
296: * Helper method to write message text.
297: *
298: * @param msgGrp The MessageGroup object to use
299: * @param writer The current ResponseWriter
300: * @param msgText The message text
301: * @param textStyle The text style
302: *
303: * @exception IOException if an input/output error occurs
304: */
305: public void renderMessageText(MessageGroup msgGrp,
306: ResponseWriter writer, String msgText, String textStyle)
307: throws IOException {
308: writer.startElement("span", msgGrp); //NOI18N
309: // <RAVE>
310: if (textStyle != null && textStyle.length() > 0) {
311: writer.writeAttribute("class", textStyle, "class"); //
312: }
313: //</RAVE>
314: // Check if the message be HTML escaped (true by default).
315: /*
316: if (!msgGrp.isEscape()) {
317: writer.write(msgText);
318: } else {
319: writer.writeText(msgText, null);
320: }
321: */
322: writer.writeText(msgText, null);
323: writer.endElement("span"); // NOI18N
324: }
325:
326: /**
327: * Helper method to write styles if specified.
328: * The div is always written out. (okay?)
329: *
330: * @param context The current FacesContext
331: * @param msgGrp The MessageGroup object to use
332: * @param writer The current ResponseWriter
333: *
334: * @exception IOException if an input/output error occurs
335: */
336: private void renderUserStyles(FacesContext context,
337: MessageGroup msgGrp, ResponseWriter writer)
338: throws IOException {
339:
340: String userStyle = msgGrp.getStyle();
341: String userStyleClass = msgGrp.getStyleClass();
342: String id = msgGrp.getClientId(context);
343:
344: writer.startElement("div", msgGrp); //NO18N
345: writer.writeAttribute("id", id, "id"); //NOI18N
346:
347: if (userStyle != null && userStyle.length() > 0) {
348: writer.writeAttribute("style", userStyle, "style"); //NOI18N
349: }
350: RenderingUtilities.renderStyleClass(context, writer, msgGrp,
351: null);
352: }
353: }
|