Skip to content

Commit b12bb0f

Browse files
committed
Additional utility method to customized tooltip component.
1 parent e6bb7dc commit b12bb0f

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

gwt-material/src/main/java/gwt/material/design/client/base/HasTooltip.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import gwt.material.design.client.base.mixin.TooltipMixin;
2323
import gwt.material.design.client.constants.Position;
2424
import gwt.material.design.client.ui.MaterialTooltip;
25+
import gwt.material.design.jquery.client.api.JQueryElement;
2526

2627
/**
2728
* Interface that determines the class has a {@link MaterialTooltip} attached to
@@ -43,6 +44,11 @@ public interface HasTooltip {
4344
*/
4445
void setTooltip(String tooltip);
4546

47+
/**
48+
* Sets the tooltip text to be shown with additional classes
49+
*/
50+
void setTooltip(String tooltip, String... classes);
51+
4652
/**
4753
* @return the position where the tooltip text should appear, relative to
4854
* the component
@@ -69,4 +75,8 @@ public interface HasTooltip {
6975

7076
String getTooltipHTML();
7177

78+
/**
79+
* Get the Tooltip element to ease of customization
80+
*/
81+
JQueryElement getTooltipElement();
7282
}

gwt-material/src/main/java/gwt/material/design/client/base/MaterialWidget.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,11 @@ public void setTooltip(String tooltip) {
529529
getTooltipMixin().setTooltip(tooltip);
530530
}
531531

532+
@Override
533+
public void setTooltip(String tooltip, String... classes) {
534+
getTooltipMixin().setTooltip(tooltip, classes);
535+
}
536+
532537
@Override
533538
public Position getTooltipPosition() {
534539
return getTooltipMixin().getTooltipPosition();
@@ -559,6 +564,11 @@ public String getTooltipHTML() {
559564
return getTooltipMixin().getTooltipHTML();
560565
}
561566

567+
@Override
568+
public JQueryElement getTooltipElement() {
569+
return getTooltipMixin().getTooltipElement();
570+
}
571+
562572
public void setVisibility(Style.Visibility visibility) {
563573
getElement().getStyle().setVisibility(visibility);
564574
}

gwt-material/src/main/java/gwt/material/design/client/base/mixin/TooltipMixin.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import gwt.material.design.client.base.MaterialWidget;
2525
import gwt.material.design.client.constants.Position;
2626
import gwt.material.design.client.ui.MaterialTooltip;
27+
import gwt.material.design.jquery.client.api.JQueryElement;
2728

2829
/**
2930
* Mixin for the {@link MaterialTooltip} component.
@@ -68,6 +69,26 @@ public void setTooltip(String text) {
6869
tooltip.setText(text);
6970
}
7071

72+
@Override
73+
public void setTooltip(String tooltip, String... classes) {
74+
setTooltip(tooltip);
75+
76+
if (this.tooltip.getWidget().isAttached()) {
77+
addTooltipClass(classes);
78+
} else {
79+
this.tooltip.getWidget().addAttachHandler(event -> addTooltipClass(classes));
80+
}
81+
}
82+
83+
public void addTooltipClass(String... classes) {
84+
JQueryElement tooltipElement = this.tooltip.getTooltipElement();
85+
if (tooltipElement != null) {
86+
for (String tooltipClass : classes) {
87+
tooltipElement.addClass(tooltipClass);
88+
}
89+
}
90+
}
91+
7192
@Override
7293
public Position getTooltipPosition() {
7394
return tooltip == null ? null : tooltip.getPosition();
@@ -100,4 +121,9 @@ public void setTooltipHTML(String html) {
100121
public String getTooltipHTML() {
101122
return tooltip.getHtml();
102123
}
124+
125+
@Override
126+
public JQueryElement getTooltipElement() {
127+
return tooltip.getTooltipElement();
128+
}
103129
}

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialTooltip.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import gwt.material.design.client.base.helper.EventHelper;
3131
import gwt.material.design.client.constants.Position;
3232
import gwt.material.design.client.js.JsTooltipOptions;
33+
import gwt.material.design.jquery.client.api.JQueryElement;
3334

3435
import java.util.Iterator;
3536
import java.util.NoSuchElementException;
@@ -58,6 +59,7 @@ public class MaterialTooltip implements JsLoader, IsWidget, HasWidgets, HasOneWi
5859
private String html;
5960
private Widget widget;
6061
private JsTooltipOptions options = JsTooltipOptions.create();
62+
private JQueryElement tooltipElement;
6163

6264
/**
6365
* Creates the empty Tooltip
@@ -88,6 +90,7 @@ public MaterialTooltip(final Widget w, final String text) {
8890
@Override
8991
public void load() {
9092
$(widget.getElement()).tooltip(options);
93+
tooltipElement = $("#" + $(widget.getElement()).attr("data-tooltip-id"));
9194
}
9295

9396
@Override
@@ -323,15 +326,12 @@ public void setHtml(String html) {
323326
this.html = html;
324327

325328
if (widget != null) {
326-
Element element = widget.getElement();
327329
if (widget.isAttached()) {
328-
$("#" + element.getAttribute("data-tooltip-id"))
329-
.find("span")
330+
tooltipElement.find("span")
330331
.html(html != null ? html : "");
331332
} else {
332333
widget.addAttachHandler(event ->
333-
$("#" + element.getAttribute("data-tooltip-id"))
334-
.find("span")
334+
tooltipElement.find("span")
335335
.html(html != null ? html : ""));
336336
}
337337
} else {
@@ -349,8 +349,12 @@ public void setAttribute(String attr, String value) {
349349
} else {
350350
EventHelper.onAttachOnce(widget, handler);
351351
}
352-
}else {
352+
} else {
353353
GWT.log("Please initialize the Target widget.", new IllegalStateException());
354354
}
355355
}
356+
357+
public JQueryElement getTooltipElement() {
358+
return tooltipElement;
359+
}
356360
}

0 commit comments

Comments
 (0)