001: /*
002: * Copyright ? 2006 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Sun Microsystems, Inc. has intellectual property rights relating to
005: * technology embodied in the product that is described in this document.
006: * In particular, and without limitation, these intellectual property
007: * rights may include one or more of the U.S. patents listed at
008: * http://www.sun.com/patents and one or more additional patents or
009: * pending patent applications in the U.S. and in other countries.
010: *
011: * U.S. Government Rights - Commercial software. Government users are subject
012: * to the Sun Microsystems, Inc. standard license agreement and applicable
013: * provisions of the FAR and its supplements. Use is subject to license terms.
014: * This distribution may include materials developed by third parties.
015: * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
016: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
017: */
018: package com.sun.portal.app.blog.handler;
019:
020: import com.sun.portal.app.blog.Resources;
021: import java.util.Iterator;
022: import javax.faces.application.FacesMessage;
023: import javax.faces.context.FacesContext;
024:
025: public class MessageHandler {
026: private static Resources resources = new Resources(
027: "com.sun.portal.app.blog.handler.MessageHandler");
028:
029: public void addInfo(String info) {
030: add(null, FacesMessage.SEVERITY_INFO, info, null);
031: }
032:
033: public void addError(String err) {
034: addError(err, (Throwable) null);
035: }
036:
037: public void addError(String err, Throwable t) {
038: addError(null, err, t);
039: }
040:
041: public void addError(String err, String detail) {
042: addError(null, err, detail);
043: }
044:
045: public void addError(String id, String err, Throwable t) {
046: String exceptionMsg = (t != null) ? t.getMessage() : null;
047: //add(id, FacesMessage.SEVERITY_ERROR, err, exceptionMsg);
048: // HACK: seems that JSF reverses the summary and detail
049: // so call with arguments reversed
050: add(id, FacesMessage.SEVERITY_ERROR, exceptionMsg, err);
051: }
052:
053: public void addError(String id, String err, String detail) {
054: //add(id, FacesMessage.SEVERITY_ERROR, err, detail);
055: // HACK: seems that JSF reverses the summary and detail
056: // so call with arguments reversed
057: add(id, FacesMessage.SEVERITY_ERROR, detail, err);
058: }
059:
060: private void add(String id, FacesMessage.Severity severity,
061: String err, String detail) {
062: add(id, new FacesMessage(severity, err, detail));
063: }
064:
065: private void add(String id, FacesMessage fm) {
066: if (!containsMessage(fm)) {
067: FacesContext.getCurrentInstance().addMessage(id, fm);
068: }
069: }
070:
071: private static boolean containsMessage(FacesMessage fm) {
072: for (Iterator i = FacesContext.getCurrentInstance()
073: .getMessages(null); i.hasNext();) {
074: FacesMessage msg = (FacesMessage) i.next();
075: if (messageEquals(fm, msg)) {
076: return true;
077: }
078: }
079: return false;
080: }
081:
082: private static boolean messageEquals(FacesMessage fm1,
083: FacesMessage fm2) {
084: if (!fm1.getSummary().equals(fm2.getSummary())) {
085: return false;
086: }
087: if (!fm1.getDetail().equals(fm2.getDetail())) {
088: return false;
089: }
090: if (fm1.getSeverity().compareTo(fm2.getSeverity()) != 0) {
091: return false;
092: }
093:
094: return true;
095:
096: }
097:
098: public boolean isGlobalMessagesPresent() {
099: // test if there are any GLOBAL messages
100: if (!FacesContext.getCurrentInstance().getMessages(null)
101: .hasNext()) {
102: return false;
103: }
104: return true;
105: }
106:
107: private boolean isErrorPresent() {
108: FacesMessage.Severity severity = FacesContext
109: .getCurrentInstance().getMaximumSeverity();
110: if (severity == FacesMessage.SEVERITY_ERROR) {
111: return true;
112: }
113: return false;
114: }
115:
116: public String getIcon() {
117: if (isErrorPresent()) {
118: return resources.get("getIcon.errorIcon");
119: } else {
120: return resources.get("getIcon.infoIcon");
121: }
122: }
123: }
|