Skip to content

Commit

Permalink
generalize generateTooltip, remove duplicated code
Browse files Browse the repository at this point in the history
may help for TheCoder4eu#348
  • Loading branch information
zhedar committed Apr 27, 2016
1 parent fce86d8 commit 6f4fdce
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx

rw.startElement("div", alert);
rw.writeAttribute("id", clientId, "id");
Tooltip.generateTooltip(context, attrs, rw);
Tooltip.generateTooltip(context, component, rw);

String style = alert.getStyle();
if (null != style)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected void generateBadge(FacesContext context, UIComponent component, Respon
styleClass = "badge";
else
styleClass += " badge";
Tooltip.generateTooltip(context, component.getAttributes(), rw);
Tooltip.generateTooltip(context, component, rw);
rw.writeAttribute("class", styleClass, "class");
if (null != style)
rw.writeAttribute("style", style, "style");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void encodeHTML(FacesContext fc) throws IOException {
rw.startElement("input", null);
rw.writeAttribute("id", clientId, null);
rw.writeAttribute("name", clientId, null);
Tooltip.generateTooltip(fc, attrs, rw);
Tooltip.generateTooltip(fc, this, rw);
rw.writeAttribute("type", type, null);
String styleClass = new CoreRenderer().getErrorAndRequiredClass(this, clientId);
rw.writeAttribute("class", "form-control " + styleClass, "class");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static final void encodeIcon(ResponseWriter rw, UIComponent c, String ico
rw.writeAttribute("id", c.getClientId() + "_icon", null);
else
rw.writeAttribute("id", c.getClientId(), null);
Tooltip.generateTooltip(FacesContext.getCurrentInstance(), c.getAttributes(), rw);
Tooltip.generateTooltip(FacesContext.getCurrentInstance(), c, rw);
}

StringBuilder sb = new StringBuilder(100); // optimize int
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/bootsfaces/component/label/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void encodeBegin(FacesContext context) throws IOException {

rw.startElement("span", this);
rw.writeAttribute("id", this.getClientId(), "id");
Tooltip.generateTooltip(context, this.getAttributes(), rw);
Tooltip.generateTooltip(context, this, rw);
String sclass = "label" + " " + "label";
if (sev != null) {
sclass += "-" + sev;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void encodeBegin(FacesContext fc) throws IOException {
if (null != style && style.length()>0) {
rw.writeAttribute("style", style, "style");
}
Tooltip.generateTooltip(fc, attrs, rw);
Tooltip.generateTooltip(fc, this, rw);
String styleClass = (String) attrs.get("styleClass");
if (null == styleClass) {
styleClass="";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void encodeHeader(FacesContext context, String h, UIComponent navlink) th
public void encodeDivider(FacesContext context, AbstractNavLink navlink) throws IOException {
ResponseWriter rw = context.getResponseWriter();
rw.startElement("li", (UIComponent) navlink);
Tooltip.generateTooltip(context, navlink, rw);
Tooltip.generateTooltip(context, (UIComponent) navlink, rw);
String styleClass = navlink.getStyleClass();
if (null == styleClass)
styleClass = "";
Expand All @@ -134,7 +134,7 @@ public void encodeHTML(FacesContext context, UIComponent navlink) throws IOExcep
String value = (String) ((AbstractNavLink)navlink).getValue();
rw.startElement("li", navlink);
writeAttribute(rw, "id", navlink.getClientId(context), "id");
Tooltip.generateTooltip(context, ((AbstractNavLink)navlink), rw);
Tooltip.generateTooltip(context, navlink, rw);
AJAXRenderer.generateBootsFacesAJAXAndJavaScript(context, (ClientBehaviorHolder)navlink, rw);

R.encodeHTML4DHTMLAttrs(rw, navlink.getAttributes(), H.ALLBUTTON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
String clientId = row.getClientId();

rw.startElement("div", row);
Tooltip.generateTooltip(context, row.getAttributes(), rw);
Tooltip.generateTooltip(context, row, rw);
String dir = row.getDir();
if (null != dir)
rw.writeAttribute("dir", dir, "dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private static void encodeTabLinks(FacesContext context, ResponseWriter writer,
throws IOException {
writer.startElement("ul", tabView);
writer.writeAttribute("id", clientId, "id");
Tooltip.generateTooltip(context, tabView.getAttributes(), writer);
Tooltip.generateTooltip(context, tabView, writer);
String classes = "nav ";
if("left".equalsIgnoreCase(tabView.getTabPosition()) || "right".equalsIgnoreCase(tabView.getTabPosition())) {
classes += " nav-pills nav-stacked";
Expand Down Expand Up @@ -362,7 +362,7 @@ private static void encodeTab(FacesContext context, ResponseWriter writer, UICom
writer.writeAttribute("dir", tab.getDir(), "dir");
writer.writeAttribute("id", tab.getClientId(), "id");
writer.writeAttribute("role", "presentation", "role");
Tooltip.generateTooltip(context, tab.getAttributes(), writer);
Tooltip.generateTooltip(context, tab, writer);

String classes = isActive ? "active" : "";
if (tab.getStyleClass() != null) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/bootsfaces/render/R.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static final void encodeColumn(ResponseWriter rw, UIComponent c, int span

if (c != null) {
rw.writeAttribute("id", c.getClientId(), "id");
Tooltip.generateTooltip(FacesContext.getCurrentInstance(), c.getAttributes(), rw);
Tooltip.generateTooltip(FacesContext.getCurrentInstance(), c, rw);
componentAttrs = c.getAttributes();
}

Expand Down Expand Up @@ -149,7 +149,7 @@ public static void genDivContainer(GenContainerDiv c, FacesContext fc) throws IO

rw.startElement("div", c);
rw.writeAttribute("id", c.getClientId(fc), "id");
Tooltip.generateTooltip(fc, attrs, rw);
Tooltip.generateTooltip(fc, c, rw);
if (pull != null && (pull.equals("right") || pull.equals("left"))) {
rw.writeAttribute("class", c.getContainerStyles().concat(" ").concat("pull").concat("-").concat(pull),
"class");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/bootsfaces/render/RJumbotron.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static final void encBegin(UIComponent c, FacesContext fc) throws IOExcep

rw.startElement("div", c);
rw.writeAttribute("id",c.getClientId(fc),"id");
Tooltip.generateTooltip(fc, c.getAttributes(), rw);
Tooltip.generateTooltip(fc, c, rw);
rw.writeAttribute("class", jumbotron, "class");
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/bootsfaces/render/RThumbnail.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static final void encBegin(UIComponent c, FacesContext fc) throws IOExcep
ResponseWriter rw = fc.getResponseWriter();
rw.startElement("div", c);
rw.writeAttribute("id", c.getClientId(fc), "id");
Tooltip.generateTooltip(fc, c.getAttributes(), rw);
Tooltip.generateTooltip(fc, c, rw);
rw.writeAttribute("class", thumbnail, "class");
/*UIComponent capt;
capt = c.getFacet(caption.name());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/bootsfaces/render/RWell.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static final void encBegin(UIComponent c, FacesContext fc) throws IOExcep
}
String styleClass=(String) attrs.get("styleClass");
if (null ==styleClass) styleClass=""; else styleClass=" "+styleClass;
Tooltip.generateTooltip(fc, attrs, rw);
Tooltip.generateTooltip(fc, c, rw);

if(sz!=null) { rw.writeAttribute("class", well+" "+well+"-"+sz+styleClass,"class"); }
else { rw.writeAttribute("class", well+styleClass, "class"); }
Expand Down
60 changes: 13 additions & 47 deletions src/main/java/net/bootsfaces/render/Tooltip.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,12 @@
*/
public class Tooltip {

public static void generateTooltip(FacesContext context, IHasTooltip component, ResponseWriter rw)
throws IOException {
if (null != component.getTooltip()) {
String tooltipPosition = component.getTooltipPosition();
if (null == tooltipPosition) // compatibility for the HTML-style using "-" characters instead of camelcase
tooltipPosition = (String) ((UIComponent) component).getAttributes().get("tooltip-position");
String tooltipContainer = component.getTooltipContainer();
if ("body".equals(tooltipContainer)) // compatibility for the HTML-style using "-" characters instead of camelcase
tooltipContainer = (String) ((UIComponent) component).getAttributes().get("tooltip-container");
if (null == tooltipContainer)
tooltipContainer = "body";

generateTooltipInternal(context, rw, component.getTooltip(), tooltipPosition, tooltipContainer);
}
}

public static void generateTooltip(FacesContext context, Map<String, Object> attrs, ResponseWriter rw)
public static void generateTooltip(FacesContext context, UIComponent component, ResponseWriter rw)
throws IOException {
Map<String, Object> attrs = component.getAttributes();
String tooltip = (String) attrs.get("tooltip");
if (null != tooltip) {
//set default values first, if not present
String position = (String) attrs.get("tooltipPosition");
if (null == position) // compatibility for the HTML-style using "-" characters instead of camelcase
position = (String) attrs.get("tooltip-position");
Expand All @@ -67,11 +53,11 @@ public static void generateTooltip(FacesContext context, Map<String, Object> att
container = (String) attrs.get("tooltip-container");
if (null == container || container.length() == 0)
container = "body";
generateTooltipInternal(context, rw, tooltip, position, container);
verifyAndWriteTooltip(context, rw, tooltip, position, container);
}
}

private static void generateTooltipInternal(FacesContext context, ResponseWriter rw, String tooltip,
private static void verifyAndWriteTooltip(FacesContext context, ResponseWriter rw, String tooltip,
String position, String container) throws IOException {
if (null == position)
position="bottom";
Expand Down Expand Up @@ -110,32 +96,12 @@ private static String generateDelayAttributes(Map<String, Object> attrs) throws
}
if (null != delayHide)
json += delayHide + ",";
if (json.length() > 0) {
if (!json.isEmpty()) {
return "{" + json.substring(0, json.length() - 1) + "}";
}
return null;
}

private static String generateDelayAttributes(IHasTooltip component) throws IOException {
String json = "";
int tooltipDelayShow = component.getTooltipDelayShow();
if (0 == tooltipDelayShow)
tooltipDelayShow = component.getTooltipDelay();

if (0 != tooltipDelayShow)
json += "tooltip-delay-show" + tooltipDelayShow + ",";
int tooltipDelayHide = component.getTooltipDelayHide();
if (0 == tooltipDelayHide)
tooltipDelayHide = component.getTooltipDelay();
if (0 != tooltipDelayHide)
json += "tooltip-delay-show" + tooltipDelayShow + ",";

if (json.length() > 0) {
return "{" + json.substring(0, json.length() - 1) + "}";
} else // compatibility for the HTML-style using "-" characters instead of camelcase
return generateDelayAttributes(((UIComponent) component).getAttributes());
}


private static String getAndCheckDelayAttribute(String attributeName, Map<String, Object> attrs,
String htmlAttributeName) throws FacesException {
Object value = attrs.get(attributeName);
Expand All @@ -145,19 +111,19 @@ private static String getAndCheckDelayAttribute(String attributeName, Map<String
Integer.parseInt((String)value);
return htmlAttributeName + ":" + value;
} catch (NumberFormatException ex) {
throw new FacesException("The attribute " + attributeName + " has to be numeric. The value '"
+ value + "' is invalid.");
//throw exception later
}
}
else if (value instanceof Integer) {
return htmlAttributeName + ":" + value;
} else {
throw new FacesException("The attribute " + attributeName + " has to be numeric. The value '"
+ value + "' is invalid.");
}

//if we reach this point, the value wasn't accepted as Integer
throw new FacesException("The attribute " + attributeName + " has to be numeric. The value '"
+ value + "' is invalid.");
}

return null;

}

/**
Expand Down

0 comments on commit 6f4fdce

Please sign in to comment.