[seam-commits] Seam SVN: r8177 - in trunk/src/main: org/jboss/seam/faces and 1 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Tue May 13 08:45:40 EDT 2008
Author: pete.muir at jboss.org
Date: 2008-05-13 08:45:40 -0400 (Tue, 13 May 2008)
New Revision: 8177
Added:
trunk/src/main/org/jboss/seam/faces/FacesMessages.java
trunk/src/main/org/jboss/seam/international/StatusMessage.java
trunk/src/main/org/jboss/seam/international/StatusMessages.java
Removed:
trunk/src/main/org/jboss/seam/faces/FacesMessages.java
Modified:
trunk/src/main/META-INF/components.xml
Log:
JBSEAM-2515
Modified: trunk/src/main/META-INF/components.xml
===================================================================
--- trunk/src/main/META-INF/components.xml 2008-05-13 12:44:38 UTC (rev 8176)
+++ trunk/src/main/META-INF/components.xml 2008-05-13 12:45:40 UTC (rev 8177)
@@ -20,5 +20,9 @@
<import>org.jboss.seam.captcha</import>
<factory name="org.jboss.seam.web.webSession" auto-create="true" scope="stateless" value="#{org.jboss.seam.web.session}" />
+
+ <factory name="facesMessages" auto-create="true" scope="stateless" value="#{org.jboss.seam.international.statusMessages}" />
+
+ <factory name="org.jboss.seam.faces.facesMessages" auto-create="true" scope="stateless" value="#{org.jboss.seam.international.statusMessages}" />
</components>
Deleted: trunk/src/main/org/jboss/seam/faces/FacesMessages.java
===================================================================
--- trunk/src/main/org/jboss/seam/faces/FacesMessages.java 2008-05-13 12:44:38 UTC (rev 8176)
+++ trunk/src/main/org/jboss/seam/faces/FacesMessages.java 2008-05-13 12:45:40 UTC (rev 8177)
@@ -1,445 +0,0 @@
-package org.jboss.seam.faces;
-
-import static org.jboss.seam.annotations.Install.BUILT_IN;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.MissingResourceException;
-import java.util.ResourceBundle;
-
-import javax.faces.application.FacesMessage;
-import javax.faces.application.FacesMessage.Severity;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
-import org.hibernate.validator.InvalidValue;
-import org.jboss.seam.Component;
-import org.jboss.seam.ScopeType;
-import org.jboss.seam.annotations.Install;
-import org.jboss.seam.annotations.Name;
-import org.jboss.seam.annotations.Scope;
-import org.jboss.seam.annotations.intercept.BypassInterceptors;
-import org.jboss.seam.contexts.Contexts;
-import org.jboss.seam.core.Interpolator;
-import org.jboss.seam.core.SeamResourceBundle;
-import org.jboss.seam.util.Strings;
-
-/**
- * A Seam component that propagates FacesMessages across redirects
- * and interpolates EL expressions in the message string.
- *
- * @author Gavin King
- */
- at Scope(ScopeType.CONVERSATION)
- at Name("org.jboss.seam.faces.facesMessages")
- at Install(precedence=BUILT_IN, classDependencies="javax.faces.context.FacesContext")
- at BypassInterceptors
-public class FacesMessages implements Serializable
-{
- private static final long serialVersionUID = -5395975397632138270L;
- private transient List<Runnable> tasks;
-
- private List<Message> facesMessages = new ArrayList<Message>();
- private Map<String, List<Message>> keyedFacesMessages = new HashMap<String, List<Message>>();
-
- /**
- * Workaround for non-serializability of
- * JSF FacesMessage.Severity class.
- *
- * @author Gavin King
- *
- */
- class Message implements Serializable
- {
- private String summary;
- private String detail;
- private int severityOrdinal;
-
- Message(FacesMessage fm)
- {
- summary = fm.getSummary();
- detail = fm.getDetail();
- severityOrdinal = fm.getSeverity().getOrdinal();
- }
-
- FacesMessage toFacesMessage()
- {
- Severity severity = null;
- for (Object o : FacesMessage.VALUES)
- {
- severity = (Severity) o;
- if (severity.getOrdinal() == severityOrdinal)
- {
- break;
- }
- }
- return new FacesMessage(severity, summary, detail );
- }
- }
-
- public void beforeRenderResponse()
- {
- for (Message message: facesMessages)
- {
- FacesContext.getCurrentInstance().addMessage( null,message.toFacesMessage() );
- }
- for ( Map.Entry<String, List<Message>> entry: keyedFacesMessages.entrySet() )
- {
- for ( Message msg: entry.getValue() )
- {
- FacesContext.getCurrentInstance().addMessage( entry.getKey(), msg.toFacesMessage() );
- }
- }
- clear();
- }
-
- /**
- * Get all faces messages that have already been added
- * to the context.
- *
- * @return a list of messages
- */
- public List<FacesMessage> getCurrentMessages()
- {
- List<FacesMessage> result = new ArrayList<FacesMessage>();
- Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages();
- while ( iter.hasNext() )
- {
- result.add( iter.next() );
- }
- return result;
- }
-
- /**
- * Get all faces global messages that have already been added
- * to the context.
- *
- * @return a list of global messages
- */
- public List<FacesMessage> getCurrentGlobalMessages()
- {
- List<FacesMessage> result = new ArrayList<FacesMessage>();
- Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(null);
- while ( iter.hasNext() )
- {
- result.add( iter.next() );
- }
- return result;
- }
-
- /**
- * Get all faces messages that have already been added
- * to the control.
- *
- * @return a list of messages
- */
- public List<FacesMessage> getCurrentMessagesForControl(String id)
- {
- String clientId = getClientId(id);
- List<FacesMessage> result = new ArrayList<FacesMessage>();
- Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(clientId);
- while ( iter.hasNext() )
- {
- result.add( iter.next() );
- }
- return result;
- }
-
- private void runTasks()
- {
- if (tasks!=null)
- {
- for (Runnable task: tasks) task.run();
- tasks.clear();
- }
- }
-
- public static void afterPhase()
- {
- if ( Contexts.isConversationContextActive() )
- {
- FacesMessages instance = (FacesMessages) Component.getInstance(FacesMessages.class, ScopeType.CONVERSATION, false);
- if (instance!=null) instance.runTasks();
- }
- }
-
- public void clear()
- {
- facesMessages.clear();
- keyedFacesMessages.clear();
- }
-
- /**
- * Add a FacesMessage that will be used
- * the next time a page is rendered.
- */
- public void add(FacesMessage facesMessage)
- {
- if (facesMessage!=null)
- {
- facesMessages.add( new Message(facesMessage) );
- }
- }
-
- /**
- * Add a FacesMessage instance to a particular component id
- * @param id a JSF component id
- */
- public void addToControl(String id, FacesMessage facesMessage)
- {
- if (facesMessage!=null)
- {
- String clientId = getClientId(id);
- List<Message> list = keyedFacesMessages.get(clientId);
- if (list==null)
- {
- list = new ArrayList<Message>();
- keyedFacesMessages.put(clientId, list);
- }
- list.add( new Message(facesMessage) );
- }
- }
-
- /**
- * Add a templated FacesMessage that will be used
- * the next time a page is rendered.
- */
- public void add(String messageTemplate, Object... params)
- {
- addToTasks(FacesMessage.SEVERITY_INFO, null, messageTemplate, params);
- }
-
- /**
- * Add a templated FacesMessage that will be used
- * the next time a page is rendered.
- */
- public void add(Severity severity, String messageTemplate, Object... params)
- {
- addToTasks(severity, null, messageTemplate, params);
- }
-
- /**
- * Add a templated FacesMessage to a particular JSF control
- * @param id a JSF component id
- */
- public void addToControl(String id, String messageTemplate, Object... params)
- {
- addToControl(id, FacesMessage.SEVERITY_INFO, messageTemplate, params);
- }
-
- /**
- * Add a templated FacesMessage to a particular JSF control
- * @param id a JSF component id
- */
- public void addToControl(String id, Severity severity, String messageTemplate, Object... params)
- {
- addToTasks(id, severity, null, messageTemplate, params);
- }
-
- /**
- * Add a templated FacesMessage by looking for the message
- * template in the resource bundle.
- */
- public void addFromResourceBundle(String key, Object... params)
- {
- addFromResourceBundle(FacesMessage.SEVERITY_INFO, key, params);
- }
-
- /**
- * Add a templated FacesMessage by looking for the message
- * template in the resource bundle.
- */
- public void addFromResourceBundle(Severity severity, String key, Object... params)
- {
- addFromResourceBundleOrDefault(severity, key, key, params);
- }
-
- /**
- * Add a templated FacesMessage to a particular component id by looking
- * for the message template in the resource bundle. If it is missing, use
- * the given message template.
- */
- public void addFromResourceBundleOrDefault(String key, String defaultMessageTemplate, Object... params)
- {
- addFromResourceBundleOrDefault(FacesMessage.SEVERITY_INFO, key, defaultMessageTemplate, params);
- }
-
- /**
- * Add a templated FacesMessage to a particular component id by looking
- * for the message template in the resource bundle. If it is missing, use
- * the given message template.
- */
- public void addFromResourceBundleOrDefault(Severity severity, String key, String defaultMessageTemplate, Object... params)
- {
- addToTasks(severity, key, defaultMessageTemplate, params);
- }
-
- /**
- * Add a templated FacesMessage to a particular component id by looking
- * for the message template in the resource bundle.
- */
- public void addToControlFromResourceBundle(String id, String key, Object... params)
- {
- addToControlFromResourceBundle(id, FacesMessage.SEVERITY_INFO, key, params);
- }
-
- /**
- * Add a templated FacesMessage to a particular component id by looking
- * for the message template in the resource bundle.
- */
- public void addToControlFromResourceBundle(String id, Severity severity, String key, Object... params)
- {
- addToControlFromResourceBundleOrDefault(id, severity, key, key, params);
- }
-
- /**
- * Add a templated FacesMessage to a particular component id by looking
- * for the message template in the resource bundle. If it is missing, use
- * the given message template.
- */
- public void addToControlFromResourceBundleOrDefault(String id, String key, String defaultMessageTemplate, Object... params)
- {
- addToControlFromResourceBundleOrDefault(id, FacesMessage.SEVERITY_INFO, key, defaultMessageTemplate, params);
- }
-
- /**
- * Add a templated FacesMessage to a particular component id by looking
- * for the message template in the resource bundle. If it is missing, use
- * the given message template.
- */
- public void addToControlFromResourceBundleOrDefault(String id, Severity severity, String key, String defaultMessageTemplate, Object... params)
- {
- addToTasks(id, severity, key, defaultMessageTemplate, params);
- }
-
- private static String getBundleMessage(String key, String defaultMessageTemplate)
- {
- String messageTemplate = defaultMessageTemplate;
- if ( key!=null )
- {
- ResourceBundle resourceBundle = SeamResourceBundle.getBundle();
- if ( resourceBundle!=null )
- {
- try
- {
- String bundleMessage = resourceBundle.getString(key);
- if (bundleMessage!=null) messageTemplate = bundleMessage;
- }
- catch (MissingResourceException mre) {} //swallow
- }
- }
- return messageTemplate;
- }
-
- public void add(InvalidValue[] ivs)
- {
- for (InvalidValue iv: ivs)
- {
- add(iv);
- }
- }
-
- public void addToControls(InvalidValue[] ivs)
- {
- for (InvalidValue iv: ivs)
- {
- addToControl(iv);
- }
- }
-
- public void add(InvalidValue iv)
- {
- add( FacesMessage.SEVERITY_WARN, iv.getMessage() );
- }
-
- public void addToControl(InvalidValue iv)
- {
- addToControl( iv.getPropertyName(), iv );
- }
-
- public void addToControl(String id, InvalidValue iv)
- {
- addToControl( id, FacesMessage.SEVERITY_WARN, iv.getMessage() );
- }
-
- public static FacesMessage createFacesMessage(Severity severity, String messageTemplate, Object... params)
- {
- return new FacesMessage( severity, Interpolator.instance().interpolate(messageTemplate, params), null );
- }
-
- public static FacesMessage createFacesMessage(Severity severity, String key, String defaultMessageTemplate, Object... params)
- {
- String message = getBundleMessage(key, defaultMessageTemplate);
- if ( !Strings.isEmpty(message) )
- {
- return createFacesMessage( severity, message, params );
- }
- else
- {
- return null;
- }
- }
-
- private String getClientId(String id)
- {
- FacesContext facesContext = FacesContext.getCurrentInstance();
- return getClientId( facesContext.getViewRoot(), id, facesContext);
- }
-
- private static String getClientId(UIComponent component, String id, FacesContext facesContext)
- {
- String componentId = component.getId();
- if (componentId!=null && componentId.equals(id))
- {
- return component.getClientId(facesContext);
- }
- else
- {
- Iterator iter = component.getFacetsAndChildren();
- while ( iter.hasNext() )
- {
- UIComponent child = (UIComponent) iter.next();
- String clientId = getClientId(child, id, facesContext);
- if (clientId!=null) return clientId;
- }
- return null;
- }
- }
-
- private List<Runnable> getTasks()
- {
- if (tasks==null)
- {
- tasks = new ArrayList<Runnable>();
- }
- return tasks;
- }
-
- private void addToTasks(final Severity severity, final String key, final String messageTemplate, final Object... params)
- {
- getTasks().add( new Runnable() {
- public void run() { add( createFacesMessage(severity, key, messageTemplate, params) ); }
- } );
- }
-
- private void addToTasks(final String id, final Severity severity, final String key, final String messageTemplate, final Object... params)
- {
- getTasks().add( new Runnable() {
- public void run() { addToControl( id, createFacesMessage(severity, key, messageTemplate, params) ); }
- } );
- }
-
- public static FacesMessages instance()
- {
- if ( !Contexts.isConversationContextActive() )
- {
- throw new IllegalStateException("No active conversation context");
- }
- return (FacesMessages) Component.getInstance(FacesMessages.class, ScopeType.CONVERSATION);
- }
-
-}
Added: trunk/src/main/org/jboss/seam/faces/FacesMessages.java
===================================================================
--- trunk/src/main/org/jboss/seam/faces/FacesMessages.java (rev 0)
+++ trunk/src/main/org/jboss/seam/faces/FacesMessages.java 2008-05-13 12:45:40 UTC (rev 8177)
@@ -0,0 +1,354 @@
+package org.jboss.seam.faces;
+
+import static org.jboss.seam.annotations.Install.BUILT_IN;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.international.StatusMessage;
+import org.jboss.seam.international.StatusMessages;
+import org.jboss.seam.util.Strings;
+
+/**
+ * A Seam component that propagates FacesMessages across redirects
+ * and interpolates EL expressions in the message string.
+ *
+ * @author Gavin King
+ * @author Pete Muir
+ */
+ at Scope(ScopeType.CONVERSATION)
+ at Name(StatusMessages.COMPONENT_NAME)
+ at Install(precedence=BUILT_IN, classDependencies="javax.faces.context.FacesContext")
+ at BypassInterceptors
+public class FacesMessages extends StatusMessages
+{
+
+ /**
+ * Called by Seam to transfer messages from FacesMessages to JSF
+ */
+ public void beforeRenderResponse()
+ {
+ for (StatusMessage statusMessage: getMessages())
+ {
+ FacesContext.getCurrentInstance().addMessage( null, toFacesMessage(statusMessage) );
+ }
+ for ( Map.Entry<String, List<StatusMessage>> entry: getKeyedMessages().entrySet() )
+ {
+ for ( StatusMessage statusMessage: entry.getValue() )
+ {
+ FacesContext.getCurrentInstance().addMessage( entry.getKey(), toFacesMessage(statusMessage) );
+ }
+ }
+ clear();
+ }
+
+ /**
+ * Called by Seam to transfer any messages added in the phase just processed
+ * to the FacesMessages component.
+ *
+ * A task runner is used to allow the messages access to outjected values.
+ */
+ public static void afterPhase()
+ {
+ runTasks();
+ }
+
+ /**
+ * Convert a StatusMessage to a FacesMessage
+ */
+ private static FacesMessage toFacesMessage(StatusMessage statusMessage)
+ {
+ if (!Strings.isEmpty(statusMessage.getSummary()))
+ {
+ return new FacesMessage(toSeverity(statusMessage.getSeverity()), statusMessage.getSummary(), statusMessage.getDetail() );
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ * Convert a StatusMessage.Severity to a FacesMessage.Severity
+ */
+ private static javax.faces.application.FacesMessage.Severity toSeverity(org.jboss.seam.international.StatusMessage.Severity severity)
+ {
+ switch (severity)
+ {
+ case ERROR:
+ return FacesMessage.SEVERITY_ERROR;
+ case FATAL:
+ return FacesMessage.SEVERITY_FATAL;
+ case INFO:
+ return FacesMessage.SEVERITY_INFO;
+ case WARN:
+ return FacesMessage.SEVERITY_WARN;
+ default:
+ return null;
+ }
+ }
+
+ /**
+ * Convert a FacesMessage.Severity to a StatusMessage.Severity
+ */
+ private static org.jboss.seam.international.StatusMessage.Severity toSeverity(javax.faces.application.FacesMessage.Severity severity)
+ {
+ if (FacesMessage.SEVERITY_ERROR.equals(severity))
+ {
+ return org.jboss.seam.international.StatusMessage.Severity.ERROR;
+ }
+ else if (FacesMessage.SEVERITY_FATAL.equals(severity))
+ {
+ return org.jboss.seam.international.StatusMessage.Severity.FATAL;
+ }
+ else if (FacesMessage.SEVERITY_INFO.equals(severity))
+ {
+ return org.jboss.seam.international.StatusMessage.Severity.INFO;
+ }
+ else if (FacesMessage.SEVERITY_WARN.equals(severity))
+ {
+ return org.jboss.seam.international.StatusMessage.Severity.WARN;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ @Override
+ /**
+ * Calculate the JSF client ID from the provided widget ID
+ */
+ protected String getClientId(String id)
+ {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ return getClientId( facesContext.getViewRoot(), id, facesContext);
+ }
+
+ private static String getClientId(UIComponent component, String id, FacesContext facesContext)
+ {
+ String componentId = component.getId();
+ if (componentId!=null && componentId.equals(id))
+ {
+ return component.getClientId(facesContext);
+ }
+ else
+ {
+ Iterator iter = component.getFacetsAndChildren();
+ while ( iter.hasNext() )
+ {
+ UIComponent child = (UIComponent) iter.next();
+ String clientId = getClientId(child, id, facesContext);
+ if (clientId!=null) return clientId;
+ }
+ return null;
+ }
+ }
+
+ /**
+ * Get all faces messages that have already been added
+ * to the context.
+ *
+ */
+ public List<FacesMessage> getCurrentMessages()
+ {
+ List<FacesMessage> result = new ArrayList<FacesMessage>();
+ Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages();
+ while ( iter.hasNext() )
+ {
+ result.add( iter.next() );
+ }
+ return result;
+ }
+
+ /**
+ * Get all faces global messages that have already been added
+ * to the context.
+ *
+ */
+ public List<FacesMessage> getCurrentGlobalMessages()
+ {
+ List<FacesMessage> result = new ArrayList<FacesMessage>();
+ Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(null);
+ while ( iter.hasNext() )
+ {
+ result.add( iter.next() );
+ }
+ return result;
+ }
+
+ /**
+ * Get all faces messages that have already been added
+ * to the control.
+ *
+ */
+ public List<FacesMessage> getCurrentMessagesForControl(String id)
+ {
+ String clientId = getClientId(id);
+ List<FacesMessage> result = new ArrayList<FacesMessage>();
+ Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(clientId);
+ while ( iter.hasNext() )
+ {
+ result.add( iter.next() );
+ }
+ return result;
+ }
+
+ /**
+ * Utility method to create a FacesMessage from a Severity, messageTemplate
+ * and params.
+ *
+ * This method interpolates the parameters provided
+ */
+ public static FacesMessage createFacesMessage(javax.faces.application.FacesMessage.Severity severity, String messageTemplate, Object... params)
+ {
+ return toFacesMessage(new StatusMessage(toSeverity(severity), null, null, messageTemplate, null, params));
+ }
+
+ /**
+ * Utility method to create a FacesMessage from a Severity, key,
+ * defaultMessageTemplate and params.
+ *
+ * This method interpolates the parameters provided
+ */
+ public static FacesMessage createFacesMessage(javax.faces.application.FacesMessage.Severity severity, String key, String defaultMessageTemplate, Object... params)
+ {
+ return toFacesMessage(new StatusMessage(toSeverity(severity), key, null, defaultMessageTemplate, null, params));
+ }
+
+ /**
+ * Add a FacesMessage that will be used
+ * the next time a page is rendered.
+ *
+ * Deprecated, use {@link #add(StatusMessage)} instead
+ */
+ @Deprecated
+ public void add(FacesMessage facesMessage)
+ {
+ if (facesMessage!=null)
+ {
+ add(new StatusMessage(facesMessage.getSummary(), facesMessage.getDetail(), toSeverity(facesMessage.getSeverity())));
+ }
+ }
+
+ /**
+ * Create a new status message, with the messageTemplate is as the message.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ * Deprecated, use {@link #add(org.jboss.seam.international.StatusMessage.Severity, String, Object...)}
+ * instead
+ */
+ @Deprecated
+ public void add(javax.faces.application.FacesMessage.Severity severity, String messageTemplate, Object... params)
+ {
+ add(toSeverity(severity), messageTemplate, params);
+ }
+
+
+ /**
+ * Create a new status message, with the messageTemplate is as the message.
+ *
+ * A severity of INFO will be used, and you can specify paramters to be
+ * interpolated
+ *
+ * Deprecated, use {@link #addToControl(String, org.jboss.seam.international.StatusMessage.Severity, String, Object...)}
+ * instead
+ */
+ @Deprecated
+ public void addToControl(String id, javax.faces.application.FacesMessage.Severity severity, String messageTemplate, Object... params)
+ {
+ addToControl(id, toSeverity(severity), messageTemplate, params);
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ * Deprecated, use {@link #addFromResourceBundle(org.jboss.seam.international.StatusMessage.Severity, String, Object...)}
+ * instead
+ */
+ @Deprecated
+ public void addFromResourceBundle(javax.faces.application.FacesMessage.Severity severity, String key, Object... params)
+ {
+ addFromResourceBundle(toSeverity(severity), key, params);
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ * Deprecated, use {@link #addFromResourceBundleOrDefault(javax.faces.application.FacesMessage.Severity, String, String, Object...)}
+ * instead
+ */
+ @Deprecated
+ public void addFromResourceBundleOrDefault(javax.faces.application.FacesMessage.Severity severity, String key, String defaultMessageTemplate, Object... params)
+ {
+ addFromResourceBundle(toSeverity(severity), key, defaultMessageTemplate, params);
+ }
+
+ /**
+ * Create a new status message, looking up the message in the resource bundle
+ * using the provided key.
+ *
+ * The message will be added to the widget specified by the ID. The algorithm
+ * used determine which widget the id refers to is determined by the view
+ * layer implementation in use.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ * Deprecated, use {@link #addToControlFromResourceBundle(String, org.jboss.seam.international.StatusMessage.Severity, String, Object...)}
+ * instead
+ */
+ @Deprecated
+ public void addToControlFromResourceBundle(String id, javax.faces.application.FacesMessage.Severity severity, String key, Object... params)
+ {
+ addToControlFromResourceBundle(id, toSeverity(severity), key, params);
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key. If the message is found, it is used, otherwise,
+ * the defaultMessageTemplate will be used.
+ *
+ * The message will be added to the widget specified by the ID. The algorithm
+ * used determine which widget the id refers to is determined by the view
+ * layer implementation in use.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ * Deprecated, use {@link #addToControlFromResourceBundleOrDefault(String, org.jboss.seam.international.StatusMessage.Severity, String, String, Object...)}
+ * instead
+ */
+ @Deprecated
+ public void addToControlFromResourceBundleOrDefault(String id, javax.faces.application.FacesMessage.Severity severity, String key, String defaultMessageTemplate, Object... params)
+ {
+ addToControlFromResourceBundleOrDefault(id, toSeverity(severity), key, defaultMessageTemplate, params);
+ }
+
+ public static FacesMessages instance()
+ {
+ if ( !Contexts.isConversationContextActive() )
+ {
+ throw new IllegalStateException("No active conversation context");
+ }
+ return (FacesMessages) Component.getInstance(StatusMessages.COMPONENT_NAME, ScopeType.CONVERSATION);
+ }
+}
Property changes on: trunk/src/main/org/jboss/seam/faces/FacesMessages.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: trunk/src/main/org/jboss/seam/international/StatusMessage.java
===================================================================
--- trunk/src/main/org/jboss/seam/international/StatusMessage.java (rev 0)
+++ trunk/src/main/org/jboss/seam/international/StatusMessage.java 2008-05-13 12:45:40 UTC (rev 8177)
@@ -0,0 +1,115 @@
+package org.jboss.seam.international;
+
+import java.io.Serializable;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+import org.jboss.seam.core.Interpolator;
+import org.jboss.seam.core.SeamResourceBundle;
+import org.jboss.seam.util.Strings;
+
+/**
+ * A status message which can be created in the business layer and displayed
+ * in the view layer
+ *
+ * @author Pete Muir
+ *
+ */
+public class StatusMessage implements Serializable
+{
+
+ /**
+ * The severity of the status message
+ *
+ */
+ public enum Severity
+ {
+ INFO,
+ WARN,
+ ERROR,
+ FATAL;
+ }
+
+ private String summary;
+ private String detail;
+ private Severity severity = Severity.INFO;
+
+ /**
+ * Create a status message, looking up the message in the resource bundle
+ * using the provided key. If the message is found, it is used, otherwise,
+ * the defaultMessageTemplate will be used.
+ *
+ */
+ public StatusMessage(Severity severity, String key, String detailKey, String defaultMessageTemplate, String defaultMessageDetailTemplate, Object... params)
+ {
+ String messageTemplate = getBundleMessage(key, defaultMessageTemplate);
+ String messageDetailTemplate = getBundleMessage(detailKey, defaultMessageDetailTemplate);
+ if ( !Strings.isEmpty(messageTemplate) )
+ {
+ this.severity = severity;
+ this.summary = Interpolator.instance().interpolate(messageTemplate, params);
+ if (!Strings.isEmpty(messageDetailTemplate))
+ {
+ this.detail = Interpolator.instance().interpolate(messageDetailTemplate, params);
+ }
+ }
+ }
+
+ public StatusMessage(String summary, String detail, Severity severity)
+ {
+ this.summary = summary;
+ this.detail = detail;
+ this.severity = severity;
+ }
+
+ /**
+ * Get the message
+ *
+ */
+ public String getSummary()
+ {
+ return summary;
+ }
+
+ /**
+ * Get the message severity
+ */
+ public Severity getSeverity()
+ {
+ return severity;
+ }
+
+ public String getDetail()
+ {
+ return detail;
+ }
+
+ public static String getBundleMessage(String key, String defaultMessageTemplate)
+ {
+ String messageTemplate = defaultMessageTemplate;
+ if ( key!=null )
+ {
+ ResourceBundle resourceBundle = SeamResourceBundle.getBundle();
+ if ( resourceBundle!=null )
+ {
+ try
+ {
+ String bundleMessage = resourceBundle.getString(key);
+ if (bundleMessage!=null)
+ {
+ messageTemplate = bundleMessage;
+ }
+ }
+ catch (MissingResourceException mre) {} //swallow
+ }
+ }
+ return messageTemplate;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "[" + severity + "] " + summary + " (" + detail +")";
+ }
+
+}
Property changes on: trunk/src/main/org/jboss/seam/international/StatusMessage.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: trunk/src/main/org/jboss/seam/international/StatusMessages.java
===================================================================
--- trunk/src/main/org/jboss/seam/international/StatusMessages.java (rev 0)
+++ trunk/src/main/org/jboss/seam/international/StatusMessages.java 2008-05-13 12:45:40 UTC (rev 8177)
@@ -0,0 +1,392 @@
+package org.jboss.seam.international;
+
+import static org.jboss.seam.international.StatusMessage.Severity.INFO;
+import static org.jboss.seam.international.StatusMessage.Severity.WARN;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.hibernate.validator.InvalidValue;
+import org.jboss.seam.Component;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.international.StatusMessage.Severity;
+
+/**
+ * Abstract base class for providing status messages. View layers should provide
+ * a concrete implementation.
+ *
+ * @author Pete Muir
+ *
+ */
+public abstract class StatusMessages implements Serializable
+{
+ private static final long serialVersionUID = -5395975397632138270L;
+
+ public static final String COMPONENT_NAME = "org.jboss.seam.international.statusMessages";
+
+ private List<StatusMessage> messages = new ArrayList<StatusMessage>();
+ private Map<String, List<StatusMessage>> keyedMessages = new HashMap<String, List<StatusMessage>>();
+
+ private transient List<Runnable> tasks;
+
+ protected List<StatusMessage> getMessages()
+ {
+ return messages;
+ }
+
+ protected Map<String, List<StatusMessage>> getKeyedMessages()
+ {
+ return keyedMessages;
+ }
+
+ /**
+ * Clear all status messages
+ */
+ public void clear()
+ {
+ messages.clear();
+ keyedMessages.clear();
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key. If the message is found, it is used, otherwise,
+ * the defaultMessageTemplate will be used.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ */
+ public void add(final Severity severity, final String key, final String messageTemplate, final Object... params)
+ {
+ add(new StatusMessage(severity, key, null, messageTemplate, null, params));
+ }
+
+ public void add(final StatusMessage statusMessage)
+ {
+ getTasks().add(
+ new Runnable()
+ {
+
+ public void run()
+ {
+ messages.add(statusMessage);
+ }
+
+ }
+ );
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key. If the message is found, it is used, otherwise,
+ * the defaultMessageTemplate will be used.
+ *
+ * The message will be added to the widget specified by the ID. The algorithm
+ * used determine which widget the id refers to is determined by the view
+ * layer implementation in use.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ */
+ public void add(final String id, final Severity severity, final String key, final String messageTemplate, final Object... params)
+ {
+ getTasks().add(
+ new Runnable()
+ {
+
+ public void run()
+ {
+ StatusMessage message = new StatusMessage(severity, key, null, messageTemplate, null, params);
+ String clientId = getClientId(id);
+ if (keyedMessages.containsKey(clientId))
+ {
+ keyedMessages.get(clientId).add(message);
+ }
+ else
+ {
+ List<StatusMessage> list = new ArrayList<StatusMessage>();
+ list.add(message);
+ keyedMessages.put(clientId, list);
+ }
+ }
+
+ }
+ );
+
+ }
+
+ protected abstract String getClientId(String id);
+
+ /**
+ * Create a new status message, with the messageTemplate is as the message.
+ *
+ * A severity of INFO will be used, and you can specify paramters to be
+ * interpolated
+ */
+ public void add(String messageTemplate, Object... params)
+ {
+ add(INFO, null, messageTemplate, params);
+ }
+
+ /**
+ * Create a new status message, with the messageTemplate is as the message.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ */
+ public void add(Severity severity, String messageTemplate, Object... params)
+ {
+ add(severity, null, messageTemplate, params);
+ }
+
+ /**
+ * Create a new status message, with the messageTemplate is as the message.
+ *
+ * The message will be added to the widget specified by the ID. The algorithm
+ * used determine which widget the id refers to is determined by the view
+ * layer implementation in use.
+ *
+ * A severity of INFO will be used, and you can specify parameters to be
+ * interpolated
+ *
+ */
+ public void addToControl(String id, String messageTemplate, Object... params)
+ {
+ add(id, INFO, null, messageTemplate, params);
+ }
+
+ /**
+ * Create a new status message, with the messageTemplate is as the message.
+ *
+ * The message will be added to the widget specified by the ID. The algorithm
+ * used determine which widget the id refers to is determined by the view
+ * layer implementation in use.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ */
+ public void addToControl(String id, Severity severity, String messageTemplate, Object... params)
+ {
+ add(id, severity, null, messageTemplate, params);
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key. If the message is found, it is used, otherwise,
+ * the defaultMessageTemplate will be used.
+ *
+ * A severity of INFO will be used, and you can specify parameters to be
+ * interpolated
+ */
+ public void addFromResourceBundle(String key, Object... params)
+ {
+ addFromResourceBundle(INFO, key, params);
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ */
+ public void addFromResourceBundle(Severity severity, String key, Object... params)
+ {
+ addFromResourceBundleOrDefault(severity, key, key, params);
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key. If the message is found, it is used, otherwise,
+ * the defaultMessageTemplate will be used.
+ *
+ * A severity of INFO will be used, and you can specify parameters to be
+ * interpolated
+ *
+ */
+ public void addFromResourceBundleOrDefault(String key, String defaultMessageTemplate, Object... params)
+ {
+ addFromResourceBundleOrDefault(INFO, key, defaultMessageTemplate, params);
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key. If the message is found, it is used, otherwise,
+ * the defaultMessageTemplate will be used.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ */
+ public void addFromResourceBundleOrDefault(Severity severity, String key, String defaultMessageTemplate, Object... params)
+ {
+ add(severity, key, defaultMessageTemplate, params);
+ }
+
+ /**
+ * Create a new status message, looking up the message in the resource bundle
+ * using the provided key.
+ *
+ * The message will be added to the widget specified by the ID. The algorithm
+ * used determine which widget the id refers to is determined by the view
+ * layer implementation in use.
+ *
+ * A severity of INFO will be used, and you can specify parameters to be
+ * interpolated
+ *
+ */
+ public void addToControlFromResourceBundle(String id, String key, Object... params)
+ {
+ addToControlFromResourceBundle(id, INFO, key, params);
+ }
+
+ /**
+ * Create a new status message, looking up the message in the resource bundle
+ * using the provided key.
+ *
+ * The message will be added to the widget specified by the ID. The algorithm
+ * used determine which widget the id refers to is determined by the view
+ * layer implementation in use.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ */
+ public void addToControlFromResourceBundle(String id, Severity severity, String key, Object... params)
+ {
+ addToControlFromResourceBundleOrDefault(id, severity, key, key, params);
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key. If the message is found, it is used, otherwise,
+ * the defaultMessageTemplate will be used.
+ *
+ * The message will be added to the widget specified by the ID. The algorithm
+ * used determine which widget the id refers to is determined by the view
+ * layer implementation in use.
+ *
+ * A severity of INFO will be used, and you can specify parameters to be
+ * interpolated
+ *
+ */
+ public void addToControlFromResourceBundleOrDefault(String id, String key, String defaultMessageTemplate, Object... params)
+ {
+ addToControlFromResourceBundleOrDefault(id, INFO, key, defaultMessageTemplate, params);
+ }
+
+ /**
+ * Add a status message, looking up the message in the resource bundle
+ * using the provided key. If the message is found, it is used, otherwise,
+ * the defaultMessageTemplate will be used.
+ *
+ * The message will be added to the widget specified by the ID. The algorithm
+ * used determine which widget the id refers to is determined by the view
+ * layer implementation in use.
+ *
+ * You can also specify the severity, and parameters to be interpolated
+ *
+ */
+ public void addToControlFromResourceBundleOrDefault(String id, Severity severity, String key, String defaultMessageTemplate, Object... params)
+ {
+ add(id, severity, key, defaultMessageTemplate, params);
+ }
+
+ /**
+ * Add an array of InvalidValues from Hibernate Validator. Each message will
+ * be added with a severity of WARN.
+ */
+ public void add(InvalidValue[] ivs)
+ {
+ for (InvalidValue iv: ivs)
+ {
+ add(iv);
+ }
+ }
+
+ /**
+ * Add an array of InvalidValues from Hibernate Validator. Each message will
+ * be added with a severity of WARN.
+ *
+ * The name of the property that was validated will be used as the widget ID
+ */
+ public void addToControls(InvalidValue[] ivs)
+ {
+ for (InvalidValue iv: ivs)
+ {
+ addToControl(iv);
+ }
+ }
+
+ /**
+ * Add an InvalidValue from Hibernate Validator. The message will
+ * be added with a severity of WARN.
+ */
+ public void add(InvalidValue iv)
+ {
+ add( WARN, iv.getMessage() );
+ }
+
+ /**
+ * Add an InvalidValue from Hibernate Validator. The message will
+ * be added with a severity of WARN.
+ *
+ * The name of the property that was validated will be used as the widget ID
+ */
+ public void addToControl(InvalidValue iv)
+ {
+ addToControl( iv.getPropertyName(), iv );
+ }
+
+ /**
+ * Add an InvalidValue from Hibernate Validator. The message will
+ * be added with a severity of WARN.
+ *
+ * You can also specify the id of the widget to add the message to
+ */
+ public void addToControl(String id, InvalidValue iv)
+ {
+ addToControl( id, WARN, iv.getMessage() );
+ }
+
+ private List<Runnable> getTasks()
+ {
+ if (tasks == null)
+ {
+ tasks = new ArrayList<Runnable>();
+ }
+ return tasks;
+ }
+
+ protected static void runTasks()
+ {
+ if ( Contexts.isConversationContextActive() )
+ {
+ StatusMessages statusMessages = instance();
+ if (statusMessages != null)
+ {
+ statusMessages.doRunTasks();
+ }
+ }
+ }
+
+ private void doRunTasks()
+ {
+ if (tasks!=null)
+ {
+ for (Runnable task: tasks) task.run();
+ tasks.clear();
+ }
+ }
+
+ public static StatusMessages instance()
+ {
+ if ( !Contexts.isConversationContextActive() )
+ {
+ throw new IllegalStateException("No active conversation context");
+ }
+ return (StatusMessages) Component.getInstance(COMPONENT_NAME, ScopeType.CONVERSATION);
+ }
+
+}
Property changes on: trunk/src/main/org/jboss/seam/international/StatusMessages.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
More information about the seam-commits
mailing list