From 6f4fdce8c6021711067988c7aaa6e0e6db4517e4 Mon Sep 17 00:00:00 2001 From: Philipp Gruhn Date: Wed, 20 Apr 2016 20:11:07 +0200 Subject: [PATCH] generalize generateTooltip, remove duplicated code may help for #348 --- .../component/alert/AlertRenderer.java | 2 +- .../component/badge/BadgeRenderer.java | 2 +- .../component/datePicker/DatePicker.java | 2 +- .../component/icon/IconRenderer.java | 2 +- .../net/bootsfaces/component/label/Label.java | 2 +- .../linksContainer/LinksContainer.java | 2 +- .../component/navLink/NavLinkRenderer.java | 4 +- .../bootsfaces/component/row/RowRenderer.java | 2 +- .../component/tabView/TabViewRenderer.java | 4 +- src/main/java/net/bootsfaces/render/R.java | 4 +- .../net/bootsfaces/render/RJumbotron.java | 2 +- .../net/bootsfaces/render/RThumbnail.java | 2 +- .../java/net/bootsfaces/render/RWell.java | 2 +- .../java/net/bootsfaces/render/Tooltip.java | 60 ++++--------------- 14 files changed, 29 insertions(+), 63 deletions(-) diff --git a/src/main/java/net/bootsfaces/component/alert/AlertRenderer.java b/src/main/java/net/bootsfaces/component/alert/AlertRenderer.java index d2b357c75..873653cff 100644 --- a/src/main/java/net/bootsfaces/component/alert/AlertRenderer.java +++ b/src/main/java/net/bootsfaces/component/alert/AlertRenderer.java @@ -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) diff --git a/src/main/java/net/bootsfaces/component/badge/BadgeRenderer.java b/src/main/java/net/bootsfaces/component/badge/BadgeRenderer.java index d5aacea36..4b56ca7d7 100644 --- a/src/main/java/net/bootsfaces/component/badge/BadgeRenderer.java +++ b/src/main/java/net/bootsfaces/component/badge/BadgeRenderer.java @@ -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"); diff --git a/src/main/java/net/bootsfaces/component/datePicker/DatePicker.java b/src/main/java/net/bootsfaces/component/datePicker/DatePicker.java index 16ada0bf5..79e5a7465 100644 --- a/src/main/java/net/bootsfaces/component/datePicker/DatePicker.java +++ b/src/main/java/net/bootsfaces/component/datePicker/DatePicker.java @@ -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"); diff --git a/src/main/java/net/bootsfaces/component/icon/IconRenderer.java b/src/main/java/net/bootsfaces/component/icon/IconRenderer.java index 599d5bee3..57041ed1e 100644 --- a/src/main/java/net/bootsfaces/component/icon/IconRenderer.java +++ b/src/main/java/net/bootsfaces/component/icon/IconRenderer.java @@ -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 diff --git a/src/main/java/net/bootsfaces/component/label/Label.java b/src/main/java/net/bootsfaces/component/label/Label.java index 19d2963f8..b14c7d583 100644 --- a/src/main/java/net/bootsfaces/component/label/Label.java +++ b/src/main/java/net/bootsfaces/component/label/Label.java @@ -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; diff --git a/src/main/java/net/bootsfaces/component/linksContainer/LinksContainer.java b/src/main/java/net/bootsfaces/component/linksContainer/LinksContainer.java index 4f0d2886b..bceae91fa 100644 --- a/src/main/java/net/bootsfaces/component/linksContainer/LinksContainer.java +++ b/src/main/java/net/bootsfaces/component/linksContainer/LinksContainer.java @@ -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=""; diff --git a/src/main/java/net/bootsfaces/component/navLink/NavLinkRenderer.java b/src/main/java/net/bootsfaces/component/navLink/NavLinkRenderer.java index 23f38bef1..77be5ea15 100644 --- a/src/main/java/net/bootsfaces/component/navLink/NavLinkRenderer.java +++ b/src/main/java/net/bootsfaces/component/navLink/NavLinkRenderer.java @@ -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 = ""; @@ -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); diff --git a/src/main/java/net/bootsfaces/component/row/RowRenderer.java b/src/main/java/net/bootsfaces/component/row/RowRenderer.java index a874a697b..2763bdc81 100644 --- a/src/main/java/net/bootsfaces/component/row/RowRenderer.java +++ b/src/main/java/net/bootsfaces/component/row/RowRenderer.java @@ -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"); diff --git a/src/main/java/net/bootsfaces/component/tabView/TabViewRenderer.java b/src/main/java/net/bootsfaces/component/tabView/TabViewRenderer.java index c2419ac82..c7d30eeb5 100644 --- a/src/main/java/net/bootsfaces/component/tabView/TabViewRenderer.java +++ b/src/main/java/net/bootsfaces/component/tabView/TabViewRenderer.java @@ -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"; @@ -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) { diff --git a/src/main/java/net/bootsfaces/render/R.java b/src/main/java/net/bootsfaces/render/R.java index 03981acca..6640a56f5 100644 --- a/src/main/java/net/bootsfaces/render/R.java +++ b/src/main/java/net/bootsfaces/render/R.java @@ -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(); } @@ -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"); diff --git a/src/main/java/net/bootsfaces/render/RJumbotron.java b/src/main/java/net/bootsfaces/render/RJumbotron.java index a661655ca..a3ca17d17 100644 --- a/src/main/java/net/bootsfaces/render/RJumbotron.java +++ b/src/main/java/net/bootsfaces/render/RJumbotron.java @@ -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"); } diff --git a/src/main/java/net/bootsfaces/render/RThumbnail.java b/src/main/java/net/bootsfaces/render/RThumbnail.java index c835563ec..4ef44867c 100644 --- a/src/main/java/net/bootsfaces/render/RThumbnail.java +++ b/src/main/java/net/bootsfaces/render/RThumbnail.java @@ -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()); diff --git a/src/main/java/net/bootsfaces/render/RWell.java b/src/main/java/net/bootsfaces/render/RWell.java index 35d4cf222..3a088f21f 100644 --- a/src/main/java/net/bootsfaces/render/RWell.java +++ b/src/main/java/net/bootsfaces/render/RWell.java @@ -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"); } diff --git a/src/main/java/net/bootsfaces/render/Tooltip.java b/src/main/java/net/bootsfaces/render/Tooltip.java index da4c5b959..8631e0a17 100644 --- a/src/main/java/net/bootsfaces/render/Tooltip.java +++ b/src/main/java/net/bootsfaces/render/Tooltip.java @@ -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 attrs, ResponseWriter rw) + public static void generateTooltip(FacesContext context, UIComponent component, ResponseWriter rw) throws IOException { + Map 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"); @@ -67,11 +53,11 @@ public static void generateTooltip(FacesContext context, Map 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"; @@ -110,32 +96,12 @@ private static String generateDelayAttributes(Map 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 attrs, String htmlAttributeName) throws FacesException { Object value = attrs.get(attributeName); @@ -145,19 +111,19 @@ private static String getAndCheckDelayAttribute(String attributeName, Map