diff --git a/org.springframework.context/src/main/java/org/springframework/ui/alert/Alert.java b/org.springframework.context/src/main/java/org/springframework/ui/alert/Alert.java index 12297ff3d50..4dcef335546 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/alert/Alert.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/alert/Alert.java @@ -18,17 +18,11 @@ package org.springframework.ui.alert; /** * Communicates an event of interest to the user. * For example, an alert may inform a user of a web application a business rule was violated. - * TODO - should we introduce detail messages here * @author Keith Donald * @since 3.0 */ public interface Alert { - /** - * The user interface element this alert is associated with; for example, "registration.password" - */ - public String getElement(); - /** * The code uniquely identifying this kind of alert; for example, "weakPassword". * May be used as a key to lookup additional alert details. diff --git a/org.springframework.context/src/main/java/org/springframework/ui/alert/AlertContext.java b/org.springframework.context/src/main/java/org/springframework/ui/alert/AlertContext.java index 44554463e3a..e9ef14665b1 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/alert/AlertContext.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/alert/AlertContext.java @@ -41,8 +41,9 @@ public interface AlertContext { /** * Add an alert to this context. + * @param the element this alert is associated with * @param alert the alert to add */ - public void add(Alert alert); + public void add(String element, Alert alert); -} +} \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/ui/alert/Alerts.java b/org.springframework.context/src/main/java/org/springframework/ui/alert/Alerts.java new file mode 100644 index 00000000000..22aa1eee0de --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/ui/alert/Alerts.java @@ -0,0 +1,111 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ui.alert; + +/** + * A static factory for conveniently constructing Alerts. + * Usage example: + *
+ * static import org.springframework.ui.alert.Alerts;
+ *
+ * public void example() {
+ * info("An info alert");
+ * warning("A warning alert");
+ * error("An error alert");
+ * fatal("A fatal alert");
+ * }
+ *
+ * @author Keith Donald
+ * @since 3.0
+ */
+public final class Alerts {
+
+ /**
+ * Creates a new info alert.
+ * @param message the alert message
+ * @return the info alert
+ * @see Severity#INFO
+ */
+ public static Alert info(String message) {
+ return new GenericAlert(Severity.INFO, null, message);
+ }
+
+ /**
+ * Creates a new warning alert.
+ * @param message the alert message
+ * @return the info alert
+ * @see Severity#WARNING
+ */
+ public static Alert warning(String message) {
+ return new GenericAlert(Severity.WARNING, null, message);
+ }
+
+ /**
+ * Creates a new error alert.
+ * @param message the alert message
+ * @return the info alert
+ * @see Severity#ERROR
+ */
+ public static Alert error(String message) {
+ return new GenericAlert(Severity.ERROR, null, message);
+ }
+
+ /**
+ * Creates a new fatal alert.
+ * @param message the alert message
+ * @return the info alert
+ * @see Severity#ERROR
+ */
+ public static Alert fatal(String message) {
+ return new GenericAlert(Severity.FATAL, null, message);
+ }
+
+ private static class GenericAlert implements Alert {
+
+ private Severity severity;
+
+ private String code;
+
+ private String message;
+
+ public GenericAlert(Severity severity, String code, String message) {
+ this.severity = severity;
+ this.code = code;
+ this.message = message;
+ }
+
+ public Severity getSeverity() {
+ return severity;
+ }
+
+ public String getCode() {
+ return code;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public String toString() {
+ if (getCode() != null) {
+ return getCode() + " - " + getMessage();
+ } else {
+ return getMessage();
+ }
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/org.springframework.context/src/main/java/org/springframework/ui/alert/support/DefaultAlertContext.java b/org.springframework.context/src/main/java/org/springframework/ui/alert/support/DefaultAlertContext.java
index 7d3880b6255..90187b38a51 100644
--- a/org.springframework.context/src/main/java/org/springframework/ui/alert/support/DefaultAlertContext.java
+++ b/org.springframework.context/src/main/java/org/springframework/ui/alert/support/DefaultAlertContext.java
@@ -54,8 +54,8 @@ public class DefaultAlertContext implements AlertContext {
return Collections.unmodifiableList(messages);
}
- public void add(Alert alert) {
- List