Skip to content

Commit 4ea373b

Browse files
author
Keith Donald
committed
factored out alert from message; made binding responsible for alert generation
1 parent f749eac commit 4ea373b

25 files changed

+515
-545
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2004-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.ui.alert;
17+
18+
/**
19+
* Communicates an event of interest to the user.
20+
* For example, an alert may inform a user of a web application a business rule was violated.
21+
* TODO - should we introduce detail messages here
22+
* @author Keith Donald
23+
* @since 3.0
24+
*/
25+
public interface Alert {
26+
27+
/**
28+
* The user interface element this alert is associated with; for example, "registration.password"
29+
*/
30+
public String getElement();
31+
32+
/**
33+
* The code uniquely identifying this kind of alert; for example, "weakPassword".
34+
* May be used as a key to lookup additional alert details.
35+
*/
36+
public String getCode();
37+
38+
/**
39+
* The level of impact this alert has on the user.
40+
*/
41+
public Severity getSeverity();
42+
43+
/**
44+
* The localized message to display to the user; for example, "Please enter a stronger password".
45+
*/
46+
public String getMessage();
47+
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2004-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.ui.alert;
17+
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
/**
22+
* A context for adding and getting alerts for display in a user interface.
23+
* @author Keith Donald
24+
* @since 3.0
25+
*/
26+
public interface AlertContext {
27+
28+
/**
29+
* Return all alerts in this context indexed by the UI element they are associated with.
30+
* @return the message map
31+
*/
32+
public Map<String, List<Alert>> getAlerts();
33+
34+
/**
35+
* Get all alerts on the UI element provided.
36+
* Returns an empty list if no alerts have been added for the element.
37+
* Alerts are returned in the order they were added.
38+
* @param element the id of the element to lookup alerts against
39+
*/
40+
public List<Alert> getAlerts(String element);
41+
42+
/**
43+
* Add an alert to this context.
44+
* @param alert the alert to add
45+
*/
46+
public void add(Alert alert);
47+
48+
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.ui.message;
16+
package org.springframework.ui.alert;
1717

1818
/**
19-
* Enum exposing supported message severities.
20-
*
19+
* The set of alert severities.
2120
* @author Keith Donald
2221
* @since 3.0
23-
* @see Message
22+
* @see Alert
2423
*/
2524
public enum Severity {
2625

@@ -30,7 +29,7 @@ public enum Severity {
3029
INFO,
3130

3231
/**
33-
* The "Warning" severity. Used to indicate there is a minor problem, or to inform the message receiver of possible
32+
* The "Warning" severity. Used to indicate there is a minor problem, or to inform the user of possible
3433
* misuse, or to indicate a problem may arise in the future.
3534
*/
3635
WARNING,
@@ -41,7 +40,7 @@ public enum Severity {
4140
ERROR,
4241

4342
/**
44-
* The "Fatal" severity. Used to indicate a fatal problem like a system error.
43+
* The "Fatal" severity. Used to indicate a fatal problem like a system error or runtime exception.
4544
*/
4645
FATAL
4746

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<body>
3+
<p>
4+
An API for alerts to display in a user interface.
5+
</p>
6+
</body>
7+
</html>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2004-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.ui.alert.support;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collections;
20+
import java.util.LinkedHashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.springframework.core.style.ToStringCreator;
25+
import org.springframework.ui.alert.Alert;
26+
import org.springframework.ui.alert.AlertContext;
27+
import org.springframework.util.CachingMapDecorator;
28+
29+
/**
30+
* The default alert context implementation.
31+
* @author Keith Donald
32+
* @since 3.0
33+
*/
34+
public class DefaultAlertContext implements AlertContext {
35+
36+
@SuppressWarnings("serial")
37+
private Map<String, List<Alert>> alertsByElement = new CachingMapDecorator<String, List<Alert>>(new LinkedHashMap<String, List<Alert>>()) {
38+
protected List<Alert> create(String element) {
39+
return new ArrayList<Alert>();
40+
}
41+
};
42+
43+
// implementing AlertContext
44+
45+
public Map<String, List<Alert>> getAlerts() {
46+
return Collections.unmodifiableMap(alertsByElement);
47+
}
48+
49+
public List<Alert> getAlerts(String element) {
50+
List<Alert> messages = alertsByElement.get(element);
51+
if (messages.isEmpty()) {
52+
return Collections.emptyList();
53+
}
54+
return Collections.unmodifiableList(messages);
55+
}
56+
57+
public void add(Alert alert) {
58+
List<Alert> alerts = alertsByElement.get(alert.getElement());
59+
alerts.add(alert);
60+
}
61+
62+
public String toString() {
63+
return new ToStringCreator(this).append("alertsByElement", alertsByElement).toString();
64+
}
65+
66+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<body>
3+
<p>
4+
Support implementation of the Alert API.
5+
</p>
6+
</body>
7+
</html>

org.springframework.context/src/main/java/org/springframework/ui/binding/BindingResult.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.ui.binding;
1717

18+
import org.springframework.ui.alert.Alert;
19+
1820
/**
1921
* The result of a bind operation.
2022
* @author Keith Donald
@@ -28,32 +30,24 @@ public interface BindingResult {
2830
* The name of the model property associated with this binding result.
2931
*/
3032
String getProperty();
31-
32-
/**
33-
* Indicates if this result is an error result.
34-
*/
35-
boolean isError();
36-
37-
/**
38-
* If an error result, the error code; for example, "invalidFormat" or "propertyNotFound".
39-
*/
40-
String getErrorCode();
4133

4234
/**
43-
* If an error, result returns a default message describing what went wrong.
35+
* The raw user-entered value for which binding was attempted.
36+
* If not a failure, this value was successfully bound to the model.
37+
* @see #isFailure()
4438
*/
45-
String getErrorMessage();
46-
47-
39+
Object getUserValue();
40+
4841
/**
49-
* If an error result, the cause of the error.
42+
* Indicates if the binding failed.
5043
*/
51-
Throwable getErrorCause();
44+
boolean isFailure();
5245

5346
/**
54-
* The raw user-entered value for which binding was attempted.
55-
* If not an error result, this value was successfully bound to the model.
47+
* Gets the alert for this binding result, appropriate for rendering the result to the user.
48+
* An alert describing a successful binding will have info severity.
49+
* An alert describing a failed binding will have either warning, error, or fatal severity.
5650
*/
57-
Object getUserValue();
51+
Alert getAlert();
5852

5953
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.springframework.ui.binding;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Target({ElementType.METHOD})
10+
@Retention(RetentionPolicy.RUNTIME)
11+
@Documented
12+
public @interface Bound {
13+
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.springframework.ui.binding;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Target({ElementType.TYPE})
10+
@Retention(RetentionPolicy.RUNTIME)
11+
@Documented
12+
public @interface Model {
13+
14+
/**
15+
* The name of the model
16+
*/
17+
String value() default "";
18+
19+
/**
20+
* Configures strict model binding.
21+
* @see Binder#setStrict(boolean)
22+
*/
23+
boolean strict() default false;
24+
25+
}

0 commit comments

Comments
 (0)