Skip to content

Commit

Permalink
Merge pull request #692 from scireum/tbi/se9177-messagelevel
Browse files Browse the repository at this point in the history
Creates a message level enum
  • Loading branch information
sabieber authored Jan 9, 2020
2 parents 43e23f3 + 797e588 commit f076ddd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
40 changes: 10 additions & 30 deletions src/main/java/sirius/web/controller/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,9 @@
*/
public class Message {

/**
* Declares a message as success.
*/
public static final String SUCCESS = "alert-success";

/**
* Declares a message as information.
*/
public static final String INFO = "alert-info";

/**
* Declares a message as warning.
*/
public static final String WARN = "alert-warning";

/**
* Declares a message as error.
*/
public static final String ERROR = "alert-danger";

private String messageText;
private String details;
private String type;
private MessageLevel type = MessageLevel.INFO;
private String action;
private String actionLabel;
private boolean actionJavascript;
Expand All @@ -51,7 +31,7 @@ public class Message {
* @param details the details for this message
* @param type the type for the message
*/
public Message(String message, String details, String type) {
public Message(String message, String details, MessageLevel type) {
this.messageText = message;
this.details = details;
this.type = type;
Expand Down Expand Up @@ -84,9 +64,9 @@ public String getDetails() {
/**
* Returns the type of the message
*
* @return the type (one of {@link #SUCCESS}, {@link #INFO}, {@link #WARN}, {@link #ERROR})
* @return the {@link MessageLevel type}
*/
public String getType() {
public MessageLevel getType() {
return type;
}

Expand Down Expand Up @@ -156,7 +136,7 @@ public String toString() {
* @return a new message with the given content and SUCCESS as type
*/
public static Message success(String message) {
return new Message(message, null, SUCCESS);
return new Message(message, null, MessageLevel.SUCCESS);
}

/**
Expand All @@ -166,7 +146,7 @@ public static Message success(String message) {
* @return a new message with the given content and INFO as type
*/
public static Message info(String message) {
return new Message(message, null, INFO);
return new Message(message, null, MessageLevel.INFO);
}

/**
Expand All @@ -176,7 +156,7 @@ public static Message info(String message) {
* @return a new message with the given content and WARN as type
*/
public static Message warn(String message) {
return new Message(message, null, WARN);
return new Message(message, null, MessageLevel.WARNING);
}

/**
Expand All @@ -186,7 +166,7 @@ public static Message warn(String message) {
* @return a new message with the given content and ERROR as type
*/
public static Message error(String message) {
return new Message(message, null, ERROR);
return new Message(message, null, MessageLevel.PROBLEM);
}

/**
Expand All @@ -199,7 +179,7 @@ public static Message error(Throwable t) {
if (t instanceof HandledException) {
return error(t.getMessage());
}
return new Message(Exceptions.handle(UserContext.LOG, t).getMessage(), null, ERROR);
return new Message(Exceptions.handle(UserContext.LOG, t).getMessage(), null, MessageLevel.PROBLEM);
}

public void setMessage(String message) {
Expand All @@ -210,7 +190,7 @@ public void setDetails(String details) {
this.details = details;
}

public void setType(String type) {
public void setType(MessageLevel type) {
this.type = type;
}

Expand Down
43 changes: 43 additions & 0 deletions src/main/java/sirius/web/controller/MessageLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package sirius.web.controller;

/**
* Declares the severity of a message and provides a convenient way to determine a proper Bootstrap CSS class to render
* the message.
*/
public enum MessageLevel {

/**
* Declares a message as success.
*/
SUCCESS("alert-success"),

/**
* Declares a message as information.
*/
INFO("alert-info"),

/**
* Declares a message as warning.
*/
WARNING("alert-warning"),

/**
* Declares a message as error.
*/
PROBLEM("alert-danger");

private String cssClass;

MessageLevel(String cssClass) {
this.cssClass = cssClass;
}

/**
* Returns the Bootstrap CSS class used to render the message.
*
* @return the name of the css class used to render the message
*/
public String getCssClass() {
return cssClass;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row">
<div class="col-md-12" id="messageBox">
<i:for type="sirius.web.controller.Message" var="msg" items="user.getMessages()">
<div class="alert @msg.getType()">
<div class="alert @msg.getType().getCssClass()">
<i:raw>@msg.getMessage()</i:raw>
<i:if test="msg.getAction() != null">
<i:if test="msg.isActionJavascript()">
Expand Down

0 comments on commit f076ddd

Please sign in to comment.