Skip to content

Commit 606b8af

Browse files
committed
Truncate Ellipsis detection then we must show tooltip.
1 parent a69e739 commit 606b8af

File tree

3 files changed

+111
-9
lines changed

3 files changed

+111
-9
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* #%L
3+
* GwtMaterial
4+
* %%
5+
* Copyright (C) 2015 - 2022 GwtMaterialDesign
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package gwt.material.design.client.base;
21+
22+
public interface HasTruncate {
23+
24+
/**
25+
* If true the label inside this component will be truncated by ellipsis
26+
**/
27+
void setTruncate(boolean truncate);
28+
29+
boolean isTruncate();
30+
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class MaterialWidget extends ComplexPanel implements HasId, HasEnabled, H
5454
HasShadow, Focusable, HasInlineStyle, HasSeparator, HasScrollspy, HasHideOn, HasShowOn, HasCenterOn, HasCircle, HasWaves,
5555
HasDataAttributes, HasFloat, HasTooltip, HasFlexbox, HasHoverable, HasFontWeight, HasFontSize, HasDepth, HasInitialClasses,
5656
HasInteractionHandlers, HasAllFocusHandlers, HasFilterStyle, HasBorder, HasVerticalAlign, HasTransform, HasOrientation,
57-
HasContainer, HasWordBreak, HasZoom, HasGridLayout, HasResize, HasContentEditable {
57+
HasContainer, HasWordBreak, HasZoom, HasGridLayout, HasResize, HasContentEditable, HasTruncate {
5858

5959
private static JQueryElement window = null;
6060
private static JQueryElement body = null;
@@ -125,7 +125,7 @@ public Appender(Widget widget) {
125125
private FlexboxMixin<MaterialWidget> flexboxMixin;
126126
private ToggleStyleMixin<MaterialWidget> hoverableMixin;
127127
private CssNameMixin<MaterialWidget, FontWeight> fontWeightMixin;
128-
private ToggleStyleMixin<MaterialWidget> truncateMixin;
128+
private TruncateMixin<MaterialWidget> truncateMixin;
129129
private FilterStyleMixin<MaterialWidget> filterMixin;
130130
private BorderMixin<MaterialWidget> borderMixin;
131131
private DimensionMixin<MaterialWidget> dimensionMixin;
@@ -1065,15 +1065,15 @@ public String getFilterStyle() {
10651065
return getFilterStyleMixin().getFilterStyle();
10661066
}
10671067

1068-
/**
1069-
* If true the label inside this component will be truncated by ellipsis
1070-
**/
1068+
1069+
@Override
10711070
public void setTruncate(boolean truncate) {
1072-
getTruncateMixin().setOn(truncate);
1071+
getTruncateMixin().setTruncate(truncate);
10731072
}
10741073

1074+
@Override
10751075
public boolean isTruncate() {
1076-
return getTruncateMixin().isOn();
1076+
return getTruncateMixin().isTruncate();
10771077
}
10781078

10791079
@Override
@@ -1880,9 +1880,9 @@ protected CssNameMixin<MaterialWidget, FontWeight> getFontWeightMixin() {
18801880
return fontWeightMixin;
18811881
}
18821882

1883-
public ToggleStyleMixin<MaterialWidget> getTruncateMixin() {
1883+
public TruncateMixin<MaterialWidget> getTruncateMixin() {
18841884
if (truncateMixin == null) {
1885-
truncateMixin = new ToggleStyleMixin<>(this, CssName.TRUNCATE);
1885+
truncateMixin = new TruncateMixin<>(this);
18861886
}
18871887
return truncateMixin;
18881888
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* #%L
3+
* GwtMaterial
4+
* %%
5+
* Copyright (C) 2015 - 2022 GwtMaterialDesign
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package gwt.material.design.client.base.mixin;
21+
22+
import com.google.gwt.user.client.Element;
23+
import com.google.gwt.user.client.ui.HasText;
24+
import com.google.gwt.user.client.ui.Widget;
25+
import gwt.material.design.client.base.HasTruncate;
26+
import gwt.material.design.client.base.MaterialWidget;
27+
import gwt.material.design.client.constants.CssName;
28+
29+
public class TruncateMixin<T extends MaterialWidget & HasTruncate> extends AbstractMixin<T> implements HasTruncate {
30+
31+
private ToggleStyleMixin<MaterialWidget> toggleStyleMixin;
32+
33+
public TruncateMixin(T uiObject) {
34+
super(uiObject);
35+
}
36+
37+
@Override
38+
public void setTruncate(boolean truncate) {
39+
getToggleStyleMixin().setOn(truncate);
40+
checkEllipsis();
41+
}
42+
43+
public void checkEllipsis() {
44+
if (uiObject instanceof HasText) {
45+
Element element = uiObject.getElement();
46+
HasText hasText = (HasText) uiObject;
47+
element.setAttribute("title", "");
48+
uiObject.addMouseOverHandler(event -> {
49+
if (!uiObject.getElement().hasAttribute("title")) return;
50+
String text = hasText.getText();
51+
boolean withEllipsis = element.getOffsetWidth() < element.getScrollWidth();
52+
if (withEllipsis) {
53+
element.setAttribute("title", text);
54+
}
55+
});
56+
57+
uiObject.addMouseOutHandler(event -> element.setAttribute("title", ""));
58+
}
59+
}
60+
61+
@Override
62+
public boolean isTruncate() {
63+
return getToggleStyleMixin().isOn();
64+
}
65+
66+
public ToggleStyleMixin<MaterialWidget> getToggleStyleMixin() {
67+
if (toggleStyleMixin == null) {
68+
toggleStyleMixin = new ToggleStyleMixin<>(uiObject, CssName.TRUNCATE);
69+
}
70+
return toggleStyleMixin;
71+
}
72+
}

0 commit comments

Comments
 (0)