added convenience methods for multiple ui messages to be displayed
This commit is contained in:
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2019 joern.muehlencord.
|
||||
*
|
||||
* 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 de.muehlencord.shared.jeeutil;
|
||||
|
||||
import javax.faces.application.FacesMessage;
|
||||
import javax.faces.application.FacesMessage.Severity;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class DefaultUIMessage implements UIMessage {
|
||||
|
||||
private static final long serialVersionUID = 3417186732917401077L;
|
||||
|
||||
public static DefaultUIMessage create(String message) {
|
||||
return new DefaultUIMessage(UIMessageType.INFO, message, null);
|
||||
}
|
||||
|
||||
public static DefaultUIMessage createWarning(String message) {
|
||||
return new DefaultUIMessage(UIMessageType.WARNING, message, null);
|
||||
}
|
||||
|
||||
public static DefaultUIMessage create(UIMessageType messageType, String message, String detail) {
|
||||
return new DefaultUIMessage(messageType, message, detail);
|
||||
}
|
||||
|
||||
private UIMessageType messageType;
|
||||
private String message;
|
||||
private String detail;
|
||||
|
||||
public DefaultUIMessage(UIMessageType messageType, String message) {
|
||||
this.messageType = messageType;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
private DefaultUIMessage(UIMessageType messageType, String message, String detail) {
|
||||
this.messageType = messageType;
|
||||
this.message = message;
|
||||
this.detail = detail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UIMessageType getMessageType() {
|
||||
return messageType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetail() {
|
||||
return (detail == null) ? "" : detail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FacesMessage getFacesMessage() {
|
||||
return getFacesMessage(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FacesMessage getFacesMessage(String prefix) {
|
||||
|
||||
Severity severity;
|
||||
switch (getMessageType()) {
|
||||
case INFO:
|
||||
severity = FacesMessage.SEVERITY_INFO;
|
||||
break;
|
||||
case WARNING:
|
||||
severity = FacesMessage.SEVERITY_WARN;
|
||||
break;
|
||||
case ERROR:
|
||||
severity = FacesMessage.SEVERITY_WARN;
|
||||
break;
|
||||
default:
|
||||
severity = FacesMessage.SEVERITY_ERROR;
|
||||
break;
|
||||
}
|
||||
String summary;
|
||||
if ((prefix == null) || (prefix.equals(""))) {
|
||||
summary = getMessage();
|
||||
} else {
|
||||
summary = prefix + ": " + getMessage();
|
||||
}
|
||||
return new FacesMessage(severity, summary, getDetail());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,5 +1,21 @@
|
||||
/*
|
||||
* Copyright 2019 joern.muehlencord.
|
||||
*
|
||||
* 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 de.muehlencord.shared.jeeutil;
|
||||
|
||||
import java.util.List;
|
||||
import javax.faces.application.FacesMessage;
|
||||
import javax.faces.context.FacesContext;
|
||||
|
||||
@ -12,11 +28,11 @@ public abstract class FacesUtil {
|
||||
private FacesUtil() {
|
||||
// hide constructor of abstract class
|
||||
}
|
||||
|
||||
|
||||
public static boolean isRenderRequest() {
|
||||
return !FacesContext.getCurrentInstance().getRenderResponse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static boolean isRenderResponse() {
|
||||
return FacesContext.getCurrentInstance().getRenderResponse();
|
||||
}
|
||||
@ -39,41 +55,41 @@ public abstract class FacesUtil {
|
||||
public static void addGlobalMessage(FacesMessage message) {
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
}
|
||||
|
||||
|
||||
public static void addGlobalInfoMessage(String summary) {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null);
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void addGlobalInfoMessage(String summary, String detail) {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
}
|
||||
|
||||
|
||||
public static void addGlobalWarningMessage(String summary) {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, summary, null);
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void addGlobalWarningMessage(String summary, String detail) {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, summary, detail);
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
}
|
||||
|
||||
|
||||
public static void addGlobalErrorMessage(String summary) {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, null);
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void addGlobalErrorMessage(String summary, String detail) {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail);
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
}
|
||||
|
||||
|
||||
public static void addGlobalFatalMessage(String summary) {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, summary, null);
|
||||
FacesContext.getCurrentInstance().addMessage(null, message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void addGlobalFatalMessage(String summary, String detail) {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, summary, detail);
|
||||
@ -182,4 +198,19 @@ public abstract class FacesUtil {
|
||||
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, summary, detail);
|
||||
FacesContext.getCurrentInstance().addMessage(clientId, message);
|
||||
}
|
||||
|
||||
public static void addMessages(List<UIMessage> messages) {
|
||||
messages.stream().map(msg -> msg.getFacesMessage()).forEach(msg -> {
|
||||
if (msg.getSeverity() == FacesMessage.SEVERITY_ERROR) {
|
||||
FacesUtil.addGlobalErrorMessage(msg.getSummary(), msg.getDetail());
|
||||
} else if (msg.getSeverity() == FacesMessage.SEVERITY_WARN) {
|
||||
FacesUtil.addGlobalWarningMessage(msg.getSummary(), msg.getDetail());
|
||||
} else if (msg.getSeverity() == FacesMessage.SEVERITY_FATAL) {
|
||||
FacesUtil.addGlobalFatalMessage(msg.getSummary(), msg.getDetail());
|
||||
} else {
|
||||
FacesUtil.addGlobalInfoMessage(msg.getSummary(), msg.getDetail());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2019 joern.muehlencord.
|
||||
*
|
||||
* 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 de.muehlencord.shared.jeeutil;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.faces.application.FacesMessage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public interface UIMessage extends Serializable {
|
||||
|
||||
String getDetail();
|
||||
|
||||
String getMessage();
|
||||
|
||||
UIMessageType getMessageType();
|
||||
|
||||
FacesMessage getFacesMessage();
|
||||
|
||||
FacesMessage getFacesMessage(String prefix);
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2019 joern.muehlencord.
|
||||
*
|
||||
* 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 de.muehlencord.shared.jeeutil;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public enum UIMessageType {
|
||||
INFO,
|
||||
WARNING,
|
||||
ERROR;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user