/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.icefaces.support.example;
import com.sun.faces.renderkit.Attribute;
import com.sun.faces.renderkit.AttributeManager;
import com.sun.faces.renderkit.RenderKitUtils;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.render.FacesRenderer;
import com.sun.faces.renderkit.html_basic.MessagesRenderer;
import java.util.Iterator;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIMessages;
import javax.faces.context.ResponseWriter;
/**
*
* @author deepthip750
*/
@FacesRenderer(componentFamily="javax.faces.Messages", rendererType="javax.faces.Messages")
public class CustomMessagesRenderer extends MessagesRenderer {
private static final Attribute[] ATTRIBUTES =
AttributeManager.getAttributes(AttributeManager.Key.MESSAGESMESSAGES);
public CustomMessagesRenderer() {
System.out.println("CustomMessagesRenderer constructor called!");
System.out.println("Default Render Kit: " + FacesContext.getCurrentInstance().getApplication().getDefaultRenderKitId());
}
// ---------------------------------------------------------- Public Methods
//Override the encodeend method to display messages inside a rounded corner box and one message for each box.
// use it as
//Also if we want to display a message in a particular location other than top of the page use
//If we need the functionality of original h:messages use ice:message instead.
@Override
public void encodeEnd(FacesContext context, UIComponent component)
throws IOException {
System.out.println("CustomMessagesRenderer encodeEnd Called!");
rendererParamsNotNull(context, component);
if (!shouldEncode(component)) {
return;
}
// If id is user specified, we must render
boolean mustRender = shouldWriteIdAttribute(component);
UIMessages messages = (UIMessages) component;
ResponseWriter writer = context.getResponseWriter();
assert(writer != null);
String clientId = ((UIMessages) component).getFor();
// if no clientId was included
if (clientId == null) {
// and the author explicitly only wants global messages
if (messages.isGlobalOnly()) {
// make it so only global messages get displayed.
clientId = "";
}
}
//"for" attribute optional for Messages
Iterator messageIter = getMessageIter(context, clientId, component);
//--the following code is to display a particular message in some set location
String forClientId = (String) component.getAttributes().get("for");
if(!messageIter.hasNext()){
messageIter = context.getMessages(forClientId);
}
//--end
assert(messageIter != null);
if (!messageIter.hasNext()) {
return;
}
String layout = (String) component.getAttributes().get("layout");
boolean showSummary = messages.isShowSummary();
boolean showDetail = messages.isShowDetail();
String styleClass = (String) component.getAttributes().get(
"styleClass");
boolean wroteTable = false;
//For layout attribute of "table" render as HTML table.
//If layout attribute is not present, or layout attribute
//is "list", render as HTML list.
if ((layout != null) && (layout.equals("table"))) {
writer.startElement("table", component);
wroteTable = true;
} else {
writer.startElement("ul", component);
}
//Render "table" or "ul" level attributes.
writeIdAttributeIfNecessary(context, writer, component);
if (null != styleClass) {
writer.writeAttribute("class", styleClass, "styleClass");
}
// style is rendered as a passthru attribute
RenderKitUtils.renderPassThruAttributes(context,
writer,
component,
ATTRIBUTES);
while (messageIter.hasNext()) {
FacesMessage curMessage = (FacesMessage) messageIter.next();
if (curMessage.isRendered() && !messages.isRedisplay()) {
continue;
}
curMessage.rendered();
String severityStyle = null;
String severityStyleClass = null;
// make sure we have a non-null value for summary and
// detail.
String summary = (null != (summary = curMessage.getSummary())) ?
summary : "";
// Default to summary if we have no detail
String detail = (null != (detail = curMessage.getDetail())) ?
detail : summary;
String boxStyle="infoRoundedBox";
String boxContentStyle="infoRoundedBoxContent";
String boxTopStyle="infoRoundedBoxTop";
String boxBottomStyle="infoRoundedBoxBottom";
String iconPath =context.getExternalContext().getRequestContextPath()+"/resources/images/infoBlueCircle.gif";
if (curMessage.getSeverity() == FacesMessage.SEVERITY_INFO) {
severityStyle =
(String) component.getAttributes().get("infoStyle");
severityStyleClass = (String)
component.getAttributes().get("infoClass");
boxStyle="infoRoundedBox";
boxContentStyle="infoRoundedBoxContent";
boxTopStyle="infoRoundedBoxTop";
boxBottomStyle="infoRoundedBoxBottom";
iconPath= context.getExternalContext().getRequestContextPath()+"/resources/images/infoBlueCircle.gif";
} else if (curMessage.getSeverity() == FacesMessage.SEVERITY_WARN) {
severityStyle =
(String) component.getAttributes().get("warnStyle");
severityStyleClass = (String)
component.getAttributes().get("warnClass");
boxStyle="warningRoundedBox";
boxContentStyle="warningRoundedBoxContent";
boxTopStyle="warningRoundedBoxTop";
boxBottomStyle="warningRoundedBoxBottom";
iconPath= context.getExternalContext().getRequestContextPath()+"/resources/images/alertYellowTriangleLarge.gif";
} else
if (curMessage.getSeverity() == FacesMessage.SEVERITY_ERROR) {
severityStyle =
(String) component.getAttributes().get("errorStyle");
severityStyleClass = (String)
component.getAttributes().get("errorClass");
boxStyle="errorRoundedBox";
boxContentStyle="errorRoundedBoxContent";
boxTopStyle="errorRoundedBoxTop";
boxBottomStyle="errorRoundedBoxBottom";
iconPath= context.getExternalContext().getRequestContextPath()+"/resources/images/alertRedCircleLarge.gif";
} else
if (curMessage.getSeverity() == FacesMessage.SEVERITY_FATAL) {
severityStyle =
(String) component.getAttributes().get("fatalStyle");
severityStyleClass = (String)
component.getAttributes().get("fatalClass");
boxStyle="errorRoundedBox";
boxContentStyle="errorRoundedBoxContent";
boxTopStyle="errorRoundedBoxTop";
boxBottomStyle="errorRoundedBoxBottom";
iconPath= context.getExternalContext().getRequestContextPath()+"/resources/images/alertRedCircleLarge.gif";
}
//Done intializing local variables. Move on to rendering.
if (wroteTable) {
writer.startElement("div", component); // div class=boxstyle
writer.writeAttribute("class",boxStyle,"class");
} else {
writer.startElement("li", component);
}
if (severityStyle != null) {
writer.writeAttribute("style", severityStyle, "style");
}
if (severityStyleClass != null) {
styleClass = severityStyleClass;
writer.writeAttribute("class", styleClass, "styleClass");
}
if (wroteTable) {
// Begin Custom code to draw rounded corner boxes
writer.startElement("div", component); // div class=boxstyle
writer.writeAttribute("class",boxStyle,"class");
writer.startElement("div", component); //div class=content
writer.writeAttribute("class",boxContentStyle,"class");
writer.startElement("div", component); //div class=top
writer.writeAttribute("class",boxTopStyle,"class");
writer.endElement("div");
writer.startElement("div", component); //div class=icon
writer.writeAttribute("class","messageIcon","class");
writer.startElement("img", component);
writer.writeAttribute("src", iconPath, "src");
writer.endElement("img");
writer.endElement("div");
writer.startElement("div", component); //div class=text
writer.writeAttribute("class","messageText","class");
//end
}
Object val = component.getAttributes().get("tooltip");
boolean isTooltip = (val != null) && Boolean.valueOf(val.toString());
boolean wroteTooltip = false;
if (showSummary && showDetail && isTooltip) {
writer.startElement("span", component);
String title = (String) component.getAttributes().get("title");
if (title == null || title.length() == 0) {
writer.writeAttribute("class", title, "class");
writer.writeAttribute("title", summary, "title");
}
writer.flush();
writer.writeText("\t", component, null);
wroteTooltip = true;
}
if (!wroteTooltip && showSummary) {
writer.writeText("\t", component, null);
writer.writeText(summary, component, null);
writer.writeText(" ", component, null);
}
if (showDetail) {
writer.writeText(detail, component, null);
}
if (wroteTooltip) {
writer.endElement("span");
}
//close table row if present
if (wroteTable) {
//Begin custom code to close elements of rounded corner boxes
writer.endElement("div"); //div class=text
writer.startElement("div",component); //div class="messageend
writer.writeAttribute("class","messageEnd","class");
writer.endElement("div");
writer.endElement("div"); //div class=content
writer.startElement("div",component); //div class=bottom
writer.writeAttribute("class",boxBottomStyle,"class");
writer.startElement("div",component);
writer.endElement("div");
writer.endElement("div");
writer.endElement("div"); //div class=boxstyle
// end custom code
writer.endElement("td");
writer.endElement("tr");
} else {
writer.endElement("li");
}
} //messageIter
//close table if present
if (wroteTable) {
writer.endElement("div");
} else {
writer.endElement("ul");
}
//This is here to avoid display of unhandled messages on the page
Iterator unHandledMessagesIter = context.getMessages();
while (unHandledMessagesIter.hasNext()){
FacesMessage msg = (FacesMessage)unHandledMessagesIter.next();
if(!msg.isRendered()){
msg.rendered();
}
}
}
} // end of class MessagesRenderer