001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.bridges.jsf.common;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024:
025: import java.util.Locale;
026:
027: import javax.faces.application.FacesMessage.Severity;
028: import javax.faces.application.FacesMessage;
029: import javax.faces.context.FacesContext;
030:
031: /**
032: * <a href="FacesMessageUtil.java.html"><b><i>View Source</i></b></a>
033: *
034: * <p>
035: * This class provides static convenience methods for creating FacesMessage
036: * objects from locale-specific values in the Liferay portal.properties file,
037: * and adding them to the FacesContext either globally, or to individual
038: * components.
039: * </p>
040: *
041: * @author Neil Griffin
042: *
043: */
044: public class FacesMessageUtil {
045:
046: public static void error(FacesContext facesContext, String key) {
047: error(null, facesContext, key);
048: }
049:
050: public static void error(FacesContext facesContext, String key,
051: Object argument) {
052:
053: error(null, facesContext, key, argument);
054: }
055:
056: public static void error(FacesContext facesContext, String key,
057: Object[] arguments) {
058:
059: error(null, facesContext, key, arguments);
060: }
061:
062: public static void error(String clientId,
063: FacesContext facesContext, String key) {
064:
065: _addMessage(clientId, facesContext,
066: FacesMessage.SEVERITY_ERROR, key);
067: }
068:
069: public static void error(String clientId,
070: FacesContext facesContext, String key, Object argument) {
071:
072: _addMessage(clientId, facesContext,
073: FacesMessage.SEVERITY_ERROR, key, argument);
074: }
075:
076: public static void error(String clientId,
077: FacesContext facesContext, String key, Object[] arguments) {
078:
079: _addMessage(clientId, facesContext,
080: FacesMessage.SEVERITY_ERROR, key, arguments);
081: }
082:
083: public static void info(FacesContext facesContext, String key) {
084: info(null, facesContext, key);
085: }
086:
087: public static void info(FacesContext facesContext, String key,
088: Object argument) {
089:
090: info(null, facesContext, key, argument);
091: }
092:
093: public static void info(FacesContext facesContext, String key,
094: Object[] arguments) {
095:
096: info(null, facesContext, key, arguments);
097: }
098:
099: public static void info(String clientId, FacesContext facesContext,
100: String key) {
101:
102: _addMessage(clientId, facesContext, FacesMessage.SEVERITY_INFO,
103: key);
104: }
105:
106: public static void info(String clientId, FacesContext facesContext,
107: String key, Object argument) {
108:
109: _addMessage(clientId, facesContext, FacesMessage.SEVERITY_INFO,
110: key, argument);
111: }
112:
113: public static void info(String clientId, FacesContext facesContext,
114: String key, Object[] arguments) {
115:
116: _addMessage(clientId, facesContext, FacesMessage.SEVERITY_INFO,
117: key, arguments);
118: }
119:
120: private static void _addMessage(String clientId,
121: FacesContext facesContext, Severity severity, String key) {
122:
123: Locale locale = JSFPortletUtil.getLocale(facesContext);
124:
125: String message = LanguageUtil.get(locale, key);
126:
127: FacesMessage facesMessage = new FacesMessage(severity, message,
128: null);
129:
130: facesContext.addMessage(clientId, facesMessage);
131: }
132:
133: private static void _addMessage(String clientId,
134: FacesContext facesContext, Severity severity, String key,
135: Object argument) {
136:
137: Locale locale = JSFPortletUtil.getLocale(facesContext);
138:
139: String message = LanguageUtil.format(locale, key, argument);
140:
141: FacesMessage facesMessage = new FacesMessage(severity, message,
142: null);
143:
144: facesContext.addMessage(clientId, facesMessage);
145: }
146:
147: private static void _addMessage(String clientId,
148: FacesContext facesContext, Severity severity, String key,
149: Object[] arguments) {
150:
151: Locale locale = JSFPortletUtil.getLocale(facesContext);
152:
153: String message = LanguageUtil.format(locale, key, arguments);
154:
155: FacesMessage facesMessage = new FacesMessage(severity, message,
156: null);
157:
158: facesContext.addMessage(clientId, facesMessage);
159: }
160:
161: }
|