Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package de.openknowledge.component;
import java.io.IOException;
import java.util.Iterator;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;
import org.primefaces.component.growl.Growl;
import org.primefaces.component.growl.GrowlRenderer;
/**
* Bartug Bölükemini - open knowledge GmbH
*/
@FacesRenderer(
componentFamily = "org.primefaces.component",
rendererType = "org.primefaces.component.growl.GrowlRenderer"
)
public class ClickableGrowlRenderer extends GrowlRenderer {
@Override
protected void encodeMessages(FacesContext context, Growl growl) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String forGrowl = growl.getFor();
Iterator<FacesMessage> messages;
if (forGrowl != null) {
messages = context.getMessages(forGrowl);
} else {
messages = growl.isGlobalOnly() ? context.getMessages(null) : context.getMessages();
}
writer.write("[");
while (messages.hasNext()) {
FacesMessage message = messages.next();
String severityName = getSeverityName(message);
if (shouldRender(growl, message, severityName)) {
String summary = escapeText(message.getSummary());
String detail = escapeText(message.getDetail());
writer.write("{");
extendMessages(context, growl, writer, message, summary, detail);
writer.write(",severity:'" + severityName + "'");
writer.write("}");
if (messages.hasNext()) {
writer.write(",");
}
message.rendered();
}
}
writer.write("]");
}
private void extendMessages(FacesContext context, Growl growl, ResponseWriter writer, FacesMessage message,
String summary, String detail) throws IOException {
if (growl.isShowSummary() && growl.isShowDetail()) {
writer.writeText(
"id:\"" + getClientId(message, context) + "\",summary:\"" + summary + "\",detail:\"" + detail + "\"",
null);
} else if (growl.isShowSummary() && !growl.isShowDetail()) {
writer.writeText("id:\"" + getClientId(message, context) + "\",summary:\"" + summary + "\",detail:\"\"",
null);
} else if (!growl.isShowSummary() && growl.isShowDetail()) {
writer.writeText("id:\"" + getClientId(message, context) + "\",summary:\"\",detail:\"" + detail + "\"",
null);
}
}
private String getClientId(FacesMessage message, FacesContext context) {
for (Iterator<String> i = context.getClientIdsWithMessages(); i.hasNext();) {
String clientId = i.next();
if (context.getMessageList(clientId).contains(message)) {
return clientId;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment