Skip to content

Commit

Permalink
Merge pull request #662 from sergiowww/master
Browse files Browse the repository at this point in the history
#662 BS feedback and error classes fixed
  • Loading branch information
stephanrauh authored Mar 22, 2017
2 parents a91b4d1 + 8d420d0 commit c5ba4c0
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.bootsfaces.render.Responsive;
import net.bootsfaces.render.Tooltip;
import net.bootsfaces.utils.BsfUtils;
import net.bootsfaces.utils.FacesMessages;
import net.bootsfaces.utils.LocaleUtils;
import net.bootsfaces.utils.TestSingleton;

Expand Down Expand Up @@ -167,12 +168,13 @@ private String encodeHTML(FacesContext fc, ResponseWriter rw, DateTimePicker dtp
}

String divSuffix="";
String errorSeverityClass = FacesMessages.getErrorSeverityClass(clientId);
if (null != responsiveStyleClass && responsiveStyleClass.trim().length()>0) {
rw.startElement("div", dtp);
if (!isHorizontalForm(dtp)) {
rw.writeAttribute("class", responsiveStyleClass + " form-group", "class");
rw.writeAttribute("class", responsiveStyleClass + " form-group " + errorSeverityClass, "class");
} else {
rw.writeAttribute("class", "form-group", "class");
rw.writeAttribute("class", "form-group " + errorSeverityClass, "class");
}
rw.writeAttribute("id", clientId, null);
Tooltip.generateTooltip(fc, dtp, rw);
Expand All @@ -181,7 +183,7 @@ private String encodeHTML(FacesContext fc, ResponseWriter rw, DateTimePicker dtp
} else if (label != null) {
rw.startElement("div", dtp);
rw.writeAttribute("id", clientId, null);
rw.writeAttribute("class", "form-group", "class");
rw.writeAttribute("class", "form-group " + errorSeverityClass, "class");
divSuffix=DTP_OUTER_CONTAINER_SUFFIX;
Tooltip.generateTooltip(fc, dtp, rw);
clientIdHasBeenRendered=true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import net.bootsfaces.render.Responsive;
import net.bootsfaces.render.Tooltip;
import net.bootsfaces.utils.BsfUtils;
import net.bootsfaces.utils.FacesMessages;

/**
*
Expand Down Expand Up @@ -233,7 +234,7 @@ protected void addLabel(ResponseWriter rw, String clientId, String fieldId) thro
if (label != null) {
rw.startElement("label", this);
rw.writeAttribute("for", clientId, "for");
new CoreRenderer().generateErrorAndRequiredClassForLabels(this, rw, fieldId, getLabelStyleClass());
new CoreRenderer().generateErrorAndRequiredClassForLabels(this, rw, fieldId, getLabelStyleClass() + " control-label");
if (getLabelStyle() != null) {
rw.writeAttribute("style", getLabelStyle(), "style");
}
Expand Down Expand Up @@ -265,7 +266,8 @@ private void encodeHTML(FacesContext fc) throws IOException {
}
rw.startElement("div", this);
numberOfDivs++;
rw.writeAttribute("class", "form-group", "class");
String errorSeverityClass = BsfUtils.isLegacyFeedbackClassesEnabled() ? "" : FacesMessages.getErrorSeverityClass(clientId);
rw.writeAttribute("class", "form-group " + errorSeverityClass, "class");
if (!idHasBeenRendered) {
rw.writeAttribute("id", clientId, "id");
Tooltip.generateTooltip(fc, this, rw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
clientIdHasBeenRendered=true;
}
if (inputText.isInline()) {
rw.writeAttribute("class", "form-inline", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
LOGGER.warning("The inline attribute of b:inputText is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");
} else {
rw.writeAttribute("class", "form-group", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
clientIdHasBeenRendered=true;
}
if (inputText.isInline()) {
rw.writeAttribute("class", "form-inline", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
LOGGER.warning("The inline attribute of b:inputText is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");
} else {
rw.writeAttribute("class", "form-group", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}

if (label != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
span = startColSpanDiv(rw, selectBooleanCheckbox);
}
rw.startElement("div", component);
writeAttribute(rw, "class", "form-group");
writeAttribute(rw, "class", getFormGroupWithFeedback("form-group", clientId));
addLabel(rw, clientId, selectBooleanCheckbox);

renderInputTag(context, rw, clientId, selectBooleanCheckbox);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import javax.faces.component.UIForm;
import javax.faces.component.UISelectItem;
import javax.faces.component.UISelectItems;
import javax.faces.component.behavior.ClientBehaviorHolder;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
Expand Down Expand Up @@ -156,9 +155,9 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
if (menu.isInline()) {
LOGGER.warning(
"The inline attribute of b:selectMultiMenu is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");
rw.writeAttribute("class", "form-inline", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
} else {
rw.writeAttribute("class", "form-group", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}

addLabel(rw, clientId + "Inner", menu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
rw.startElement("div", menu);

if (menu.isInline()) {
rw.writeAttribute("class", "form-inline", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", outerClientId), "class");
LOGGER.warning("The inline attribute of b:inputText is deprecated and generates faulty HTML code. Please use <b:form inline=\"true\"> instead.");
} else {
rw.writeAttribute("class", "form-group", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", outerClientId), "class");
}
if (!clientIdHasBeenRendered) {
rw.writeAttribute("id", outerClientId, "id");
Expand Down Expand Up @@ -193,7 +193,7 @@ protected void addLabel(ResponseWriter rw, String clientId, SelectOneMenu menu,
if (label != null) {
rw.startElement("label", menu);
rw.writeAttribute("for", clientId, "for");
generateErrorAndRequiredClassForLabels(menu, rw, outerClientId, null);
generateErrorAndRequiredClassForLabels(menu, rw, outerClientId, "control-label");
writeAttribute(rw, "style", menu.getLabelStyle());
rw.writeText(label, null);
rw.endElement("label");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ private void encodeHTML(Slider slider, FacesContext context, ResponseWriter rw)
Tooltip.generateTooltip(context, slider, rw);

if (slider.isInline()) {
rw.writeAttribute("class", "form-inline", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
} else {
rw.writeAttribute("class", "form-group", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}
rw.startElement("div", null);
String s = "row " + (isVertical ? "slider-vertical" : "slider");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ private void encodeHTML(Slider2 slider, FacesContext context, ResponseWriter rw)
Tooltip.generateTooltip(context, slider, rw);

if (slider.isInline()) {
rw.writeAttribute("class", "form-inline", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-inline", clientId), "class");
} else {
rw.writeAttribute("class", "form-group", "class");
rw.writeAttribute("class", getFormGroupWithFeedback("form-group", clientId), "class");
}
rw.startElement("div", null);
String s = "row " + (isVertical ? "slider2-vertical" : "slider2");
Expand Down
46 changes: 21 additions & 25 deletions src/main/java/net/bootsfaces/render/CoreRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.application.ProjectStage;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
Expand All @@ -40,9 +38,10 @@
import javax.faces.render.Renderer;

import net.bootsfaces.beans.ELTools;
import net.bootsfaces.component.accordion.Accordion;
import net.bootsfaces.component.ajax.AJAXRenderer;
import net.bootsfaces.component.form.Form;
import net.bootsfaces.utils.BsfUtils;
import net.bootsfaces.utils.FacesMessages;

public class CoreRenderer extends Renderer {

Expand Down Expand Up @@ -118,7 +117,7 @@ public void generateErrorAndRequiredClassForLabels(UIInput input, ResponseWriter
}
}
UIForm currentForm = AJAXRenderer.getSurroundingForm((UIComponent) input, true);
if (null != currentForm && currentForm instanceof Form) {
if (currentForm instanceof Form) {
if (((Form) currentForm).isHorizontal()) {
styleClass += " control-label";
}
Expand Down Expand Up @@ -174,28 +173,10 @@ protected void generateErrorAndRequiredClass(UIInput input, ResponseWriter rw, S
* @return can never be null
*/
public String getErrorAndRequiredClass(UIInput input, String clientId) {
String[] levels = { "bf-no-message has-success", "bf-info", "bf-warning has-warning", "bf-error has-error",
"bf-fatal has-error" };
int level = 0;
Iterator<FacesMessage> messages = FacesContext.getCurrentInstance().getMessages(clientId);
if (null != messages) {
while (messages.hasNext()) {
FacesMessage message = messages.next();
if (message.getSeverity().equals(FacesMessage.SEVERITY_INFO))
if (level < 1)
level = 1;
if (message.getSeverity().equals(FacesMessage.SEVERITY_WARN))
if (level < 2)
level = 2;
if (message.getSeverity().equals(FacesMessage.SEVERITY_ERROR))
if (level < 3)
level = 3;
if (message.getSeverity().equals(FacesMessage.SEVERITY_FATAL))
if (level < 4)
level = 4;
}
String styleClass = "";
if (BsfUtils.isLegacyFeedbackClassesEnabled()) {
styleClass = FacesMessages.getErrorSeverityClass(clientId);
}
String styleClass = levels[level];
if (input.isRequired()) {
styleClass += " bf-required";
} else {
Expand All @@ -213,6 +194,7 @@ public String getErrorAndRequiredClass(UIInput input, String clientId) {
return styleClass;
}


protected boolean shouldRenderAttribute(Object value) {
if (value == null)
return false;
Expand Down Expand Up @@ -549,4 +531,18 @@ public static boolean beginDisabledFieldset(IContentDisabled component, Response
return false;
}

/**
* Get the main field container
* @param additionalClass
* @param clientId
* @return
*/
protected String getFormGroupWithFeedback(String additionalClass, String clientId){
if (BsfUtils.isLegacyFeedbackClassesEnabled()) {
return additionalClass;
}
return additionalClass + " " + FacesMessages.getErrorSeverityClass(clientId);
}


}
12 changes: 12 additions & 0 deletions src/main/java/net/bootsfaces/utils/BsfUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.application.Application;
Expand All @@ -24,6 +25,7 @@
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.xml.bind.DatatypeConverter;

import net.bootsfaces.expressions.ExpressionResolver;

public class BsfUtils {
Expand Down Expand Up @@ -651,4 +653,14 @@ public static float getSliderValue(String value) {
public static int getIntSliderValue(String value) {
return (int)getSliderValue(value);
}

/**
* It checks where the framework should place BS feedback classes.
*
* @return
*/
public static boolean isLegacyFeedbackClassesEnabled() {
String legacyErrorClasses = getInitParam("net.bootsfaces.legacy_error_classes");
return legacyErrorClasses.equalsIgnoreCase("true") || legacyErrorClasses.equalsIgnoreCase("yes");
}
}
35 changes: 35 additions & 0 deletions src/main/java/net/bootsfaces/utils/FacesMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package net.bootsfaces.utils;

import java.util.Iterator;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -196,4 +197,38 @@ public static void createErrorMessageFromResourceBundle(String clientId, String
FacesMessages.error(clientId, summary, detail);
}

/**
* Returns style matching the severity error.
*
* @param clientId
* @return
*/
public static String getErrorSeverityClass(String clientId) {
String[] levels = { "bf-no-message has-success", "bf-info", "bf-warning has-warning", "bf-error has-error", "bf-fatal has-error" };
int level = 0;
Iterator<FacesMessage> messages = FacesContext.getCurrentInstance().getMessages(clientId);
if (null != messages) {
if (!messages.hasNext()) {
return "";
}
while (messages.hasNext()) {
FacesMessage message = messages.next();
if (message.getSeverity().equals(FacesMessage.SEVERITY_INFO))
if (level < 1)
level = 1;
if (message.getSeverity().equals(FacesMessage.SEVERITY_WARN))
if (level < 2)
level = 2;
if (message.getSeverity().equals(FacesMessage.SEVERITY_ERROR))
if (level < 3)
level = 3;
if (message.getSeverity().equals(FacesMessage.SEVERITY_FATAL))
if (level < 4)
level = 4;
}
return levels[level];
}
return "";
}

}

0 comments on commit c5ba4c0

Please sign in to comment.