Created
August 22, 2012 12:21
-
-
Save pauldijou/3425047 to your computer and use it in GitHub Desktop.
Override RichFaces Message renderer with Bootstrap alert design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<render-kit> | |
<renderer> | |
<component-family>javax.faces.Messages</component-family> | |
<renderer-type>org.richfaces.MessagesRenderer</renderer-type> | |
<renderer-class>your.package.RichMessagesRenderer</renderer-class> | |
</renderer> | |
</render-kit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import javax.faces.application.FacesMessage; | |
import javax.faces.application.FacesMessage.Severity; | |
import javax.faces.component.UIComponent; | |
import javax.faces.component.UIMessages; | |
import javax.faces.context.FacesContext; | |
import javax.faces.context.ResponseWriter; | |
import javax.faces.render.FacesRenderer; | |
import javax.faces.render.Renderer; | |
@FacesRenderer(componentFamily="javax.faces.Messages", rendererType="org.richfaces.MessagesRenderer") | |
public class RichMessagesRenderer extends Renderer { | |
@Override | |
public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException{ | |
if(!component.isRendered()) { | |
return; | |
} | |
UIMessages uiMessages = (UIMessages) component; | |
ResponseWriter writer = facesContext.getResponseWriter(); | |
assert(writer != null); | |
String clientId = uiMessages.getClientId(facesContext); | |
Iterator<FacesMessage> allMessages = uiMessages.isGlobalOnly() ? facesContext.getMessages(null) : facesContext.getMessages(); | |
Map<Severity, List<FacesMessage>> messages = new HashMap<Severity, List<FacesMessage>>(); | |
messages.put(FacesMessage.SEVERITY_INFO, new ArrayList<FacesMessage>()); // Bootstrap info | |
messages.put(FacesMessage.SEVERITY_WARN, new ArrayList<FacesMessage>()); // Bootstrap warning | |
messages.put(FacesMessage.SEVERITY_ERROR, new ArrayList<FacesMessage>()); // Bootstrap error | |
messages.put(FacesMessage.SEVERITY_FATAL, new ArrayList<FacesMessage>()); // Bootstrap error | |
while (allMessages.hasNext()){ | |
FacesMessage message = allMessages.next(); | |
if(message.isRendered() && !uiMessages.isRedisplay()) { | |
continue; | |
} | |
messages.get(message.getSeverity()).add(message); | |
} | |
writer.startElement("div", uiMessages); | |
writer.writeAttribute("id", clientId, "id"); | |
List<FacesMessage> severityMessages = messages.get(FacesMessage.SEVERITY_FATAL); | |
if (severityMessages.size() > 0){ | |
encodeSeverityMessages(facesContext, uiMessages, FacesMessage.SEVERITY_FATAL, severityMessages); | |
} | |
severityMessages = messages.get(FacesMessage.SEVERITY_ERROR); | |
if (severityMessages.size() > 0){ | |
encodeSeverityMessages(facesContext, uiMessages, FacesMessage.SEVERITY_ERROR, severityMessages); | |
} | |
severityMessages = messages.get(FacesMessage.SEVERITY_WARN); | |
if (severityMessages.size() > 0){ | |
encodeSeverityMessages(facesContext, uiMessages, FacesMessage.SEVERITY_WARN, severityMessages); | |
} | |
severityMessages = messages.get(FacesMessage.SEVERITY_INFO); | |
if (severityMessages.size() > 0){ | |
encodeSeverityMessages(facesContext, uiMessages, FacesMessage.SEVERITY_INFO, severityMessages); | |
} | |
writer.endElement("div"); | |
} | |
private void encodeSeverityMessages(FacesContext facesContext, UIMessages uiMessages, Severity severity, List<FacesMessage> messages) throws IOException { | |
ResponseWriter writer = facesContext.getResponseWriter(); | |
String alertSeverityClass = ""; | |
if(FacesMessage.SEVERITY_INFO.equals(severity)) { | |
alertSeverityClass = "alert-info"; | |
} | |
else if(FacesMessage.SEVERITY_WARN.equals(severity)) { | |
alertSeverityClass = ""; // Default alert is a warning | |
} | |
else if(FacesMessage.SEVERITY_ERROR.equals(severity)) { | |
alertSeverityClass = "alert-error"; | |
} | |
else if(FacesMessage.SEVERITY_FATAL.equals(severity)) { | |
alertSeverityClass = "alert-error"; | |
} | |
writer.startElement("div", null); | |
writer.writeAttribute("class", "rf alert fade in alert-block " + alertSeverityClass, null); | |
writer.startElement("a", null); | |
writer.writeAttribute("class", "close", null); | |
writer.writeAttribute("data-dismiss","alert", null); | |
writer.writeAttribute("href", "#", null); | |
writer.write("×"); | |
writer.endElement("a"); | |
writer.startElement("ul", null); | |
for (FacesMessage msg : messages){ | |
String summary = msg.getSummary() != null ? msg.getSummary() : ""; | |
String detail = msg.getDetail() != null ? msg.getDetail() : summary; | |
writer.startElement("li", null); | |
if (uiMessages.isShowSummary()){ | |
writer.startElement("strong", null); | |
writer.writeText(summary, null); | |
writer.endElement("strong"); | |
} | |
if (uiMessages.isShowDetail()){ | |
writer.writeText(" "+detail, null); | |
} | |
writer.endElement("li"); | |
msg.rendered(); | |
} | |
writer.endElement("ul"); | |
writer.endElement("div"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see https://gist.github.com/4151211 for JSF version