Alona Kaplan has uploaded a new change for review.

Change subject: webadmin: LabelWithTooltip trims long text and add "..."
......................................................................

webadmin: LabelWithTooltip trims long text and add "..."

In case the lable's text is cropped, the text is visually trimmed.
To let the user know that the text was trimmed '...' are appended
to the string.
The tooltip contains the full text string.

Bug-Url: https://bugzilla.redhat.com/1096662
Change-Id: I3a0c6292b874d0994042366940123b42b38b7a8d
Signed-off-by: Alona Kaplan <[email protected]>
---
A 
frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/label/LabelWithToolTip.java
M 
frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/column/AbstractCellWithTooltip.java
M 
frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/InterfaceLabelWithToolTip.java
M 
frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/StatisticsPanel.java
D 
frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/label/LabelWithToolTip.java
5 files changed, 115 insertions(+), 108 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/25/29125/1

diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/label/LabelWithToolTip.java
 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/label/LabelWithToolTip.java
new file mode 100644
index 0000000..a096c5f
--- /dev/null
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/label/LabelWithToolTip.java
@@ -0,0 +1,108 @@
+package org.ovirt.engine.ui.common.widget.label;
+
+import org.ovirt.engine.ui.common.utils.ElementUtils;
+import org.ovirt.engine.ui.uicompat.external.StringUtils;
+
+import com.google.gwt.event.dom.client.MouseOutEvent;
+import com.google.gwt.event.dom.client.MouseOutHandler;
+import com.google.gwt.event.dom.client.MouseOverEvent;
+import com.google.gwt.event.dom.client.MouseOverHandler;
+import com.google.gwt.safehtml.shared.SafeHtml;
+import com.google.gwt.user.client.ui.DecoratedPopupPanel;
+import com.google.gwt.user.client.ui.HTML;
+
+/**
+ * This class represents a label with a tooltip.
+ *
+ * Both the label and the tooltip can be <code>String</code> or 
<code>SafeHtml</code>.
+ *
+ * The default tooltip is the label's text.
+ * Overriding the tooltip (<code>setTitle(String title)</code>) should be done 
after setting the text (<code>setText(String text)</code>).
+ *
+ * In case the lable's text is cropped, the text is visually trimmed.
+ * To let the user know that the text was trimmed '...' (three dots) are 
appended to the string.
+ * The tooltip (in case it is not overridden) contains the full text string.
+ */
+public class LabelWithToolTip extends HTML {
+
+    private final HTML tooltip = new HTML();
+    private final DecoratedPopupPanel tooltipPanel = new DecoratedPopupPanel();
+    private String title;
+
+    public LabelWithToolTip() {
+        initTooltipPanel();
+    }
+
+    public LabelWithToolTip(String text) {
+        this();
+        setText(text);
+    }
+
+    public LabelWithToolTip(SafeHtml html) {
+        this();
+        setHTML(html);
+    }
+
+    @Override
+    public void setText(final String text) {
+        super.setText(text);
+        setTitle(text);
+        trimToPx(text);
+    }
+
+    private void trimToPx(final String text) {
+        if (!isAttached() || !isVisible()) {
+            return;
+        }
+
+        String tmp = text;
+        int gap = 1;
+        while (ElementUtils.detectOverflowUsingScrollWidth(getElement())) {
+            tmp = tmp.substring(0, text.length() - gap) + "..."; //$NON-NLS-1$
+            LabelWithToolTip.super.setText(tmp);
+            ++gap;
+        }
+
+    }
+
+    private void initTooltipPanel() {
+        tooltipPanel.setWidget(tooltip);
+        tooltipPanel.getElement().getStyle().setZIndex(1);
+        registerHandlers();
+    }
+
+    private void registerHandlers() {
+        addMouseOverHandler(new MouseOverHandler() {
+
+            @Override
+            public void onMouseOver(MouseOverEvent event) {
+                if (StringUtils.isEmpty(title)) {
+                    tooltip.setHTML(title);
+                    tooltipPanel.showRelativeTo(LabelWithToolTip.this);
+                }
+            }
+        });
+        addMouseOutHandler(new MouseOutHandler() {
+
+            @Override
+            public void onMouseOut(MouseOutEvent event) {
+                tooltipPanel.hide(true);
+            }
+        });
+    }
+
+    @Override
+    public void setTitle(String text) {
+        this.title = text;
+    }
+
+    public void setTitle(SafeHtml text) {
+        setTitle(text == null ? (String)null : text.asString());
+    }
+
+    @Override
+    protected void onLoad() {
+        super.onLoad();
+        trimToPx(getText());
+    }
+}
diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/column/AbstractCellWithTooltip.java
 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/column/AbstractCellWithTooltip.java
index e850fd1..bfdb2d6 100644
--- 
a/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/column/AbstractCellWithTooltip.java
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/java/org/ovirt/engine/ui/common/widget/table/column/AbstractCellWithTooltip.java
@@ -1,5 +1,7 @@
 package org.ovirt.engine.ui.common.widget.table.column;
 
+import org.ovirt.engine.ui.common.utils.ElementUtils;
+
 import com.google.gwt.cell.client.AbstractCell;
 import com.google.gwt.cell.client.ValueUpdater;
 import com.google.gwt.dom.client.BrowserEvents;
@@ -53,39 +55,6 @@
      * Returns {@code true} when the content of the given {@code parent} 
element overflows its area.
      */
     protected boolean contentOverflows(Element parent) {
-        return parent != null && (detectOverflowUsingScrollWidth(parent) || 
detectOverflowUsingClientHeight(parent));
+        return parent != null && 
(ElementUtils.detectOverflowUsingScrollWidth(parent) || 
ElementUtils.detectOverflowUsingClientHeight(parent));
     }
-
-    /**
-     * Uses scrollWidth with temporary CSS 'overflow:auto' to detect 
horizontal overflow.
-     */
-    boolean detectOverflowUsingScrollWidth(Element parent) {
-        int scrollWidthBefore = parent.getScrollWidth();
-        String overflowValue = parent.getStyle().getProperty("overflow"); 
//$NON-NLS-1$
-        parent.getStyle().setProperty("overflow", "auto"); //$NON-NLS-1$ 
//$NON-NLS-2$
-
-        int scrollWidthAfter = parent.getScrollWidth();
-        int clientWidthAfter = parent.getClientWidth();
-        parent.getStyle().setProperty("overflow", overflowValue); //$NON-NLS-1$
-
-        return scrollWidthAfter > scrollWidthBefore || scrollWidthAfter > 
clientWidthAfter;
-    }
-
-    /**
-     * Uses clientHeight with temporary CSS 'whiteSpace:normal' to detect 
vertical overflow.
-     * <p>
-     * This is necessary due to some browsers (Firefox) having issues with 
scrollWidth (e.g. elements with CSS 'display'
-     * other than 'block' have incorrect scrollWidth value).
-     */
-    boolean detectOverflowUsingClientHeight(Element parent) {
-        int clientHeightBefore = parent.getClientHeight();
-        String whiteSpaceValue = parent.getStyle().getProperty("whiteSpace"); 
//$NON-NLS-1$
-        parent.getStyle().setProperty("whiteSpace", "normal"); //$NON-NLS-1$ 
//$NON-NLS-2$
-        int scrollHeightAfter = parent.getScrollHeight();
-        int clientHeightAfter = parent.getClientHeight();
-        parent.getStyle().setProperty("whiteSpace", whiteSpaceValue); 
//$NON-NLS-1$
-
-        return clientHeightAfter > clientHeightBefore || clientHeightAfter < 
scrollHeightAfter;
-    }
-
 }
diff --git 
a/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/InterfaceLabelWithToolTip.java
 
b/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/InterfaceLabelWithToolTip.java
index bbd060f..8b2adc8 100644
--- 
a/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/InterfaceLabelWithToolTip.java
+++ 
b/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/InterfaceLabelWithToolTip.java
@@ -3,9 +3,9 @@
 import java.util.Set;
 
 import 
org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
+import org.ovirt.engine.ui.common.widget.label.LabelWithToolTip;
 import org.ovirt.engine.ui.webadmin.ApplicationResources;
 import org.ovirt.engine.ui.webadmin.ApplicationTemplates;
-import org.ovirt.engine.ui.webadmin.widget.label.LabelWithToolTip;
 
 import com.google.gwt.core.shared.GWT;
 import com.google.gwt.safehtml.shared.SafeHtml;
diff --git 
a/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/StatisticsPanel.java
 
b/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/StatisticsPanel.java
index 907a4b1..2078eb2 100644
--- 
a/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/StatisticsPanel.java
+++ 
b/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/host/StatisticsPanel.java
@@ -3,11 +3,11 @@
 import java.util.List;
 
 import org.ovirt.engine.ui.common.widget.TogglePanel;
+import org.ovirt.engine.ui.common.widget.label.LabelWithToolTip;
 import org.ovirt.engine.ui.common.widget.renderer.RxTxRateRenderer;
 import org.ovirt.engine.ui.common.widget.renderer.SumUpRenderer;
 import org.ovirt.engine.ui.uicommonweb.models.hosts.HostInterface;
 import org.ovirt.engine.ui.webadmin.gin.ClientGinjectorProvider;
-import org.ovirt.engine.ui.webadmin.widget.label.LabelWithToolTip;
 
 import com.google.gwt.dom.client.Style;
 import com.google.gwt.dom.client.Style.BorderStyle;
@@ -49,7 +49,8 @@
         row.getColumnFormatter().setWidth(4, "105px"); //$NON-NLS-1$
 
         // MAC
-        LabelWithToolTip macLabel = new 
LabelWithToolTip(hostInterface.getMAC(), 17);
+        LabelWithToolTip macLabel = new 
LabelWithToolTip(hostInterface.getMAC());
+        macLabel.setWidth("210px"); //$NON-NLS-1$
         row.setWidget(0, 0, macLabel);
 
         // Speed
diff --git 
a/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/label/LabelWithToolTip.java
 
b/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/label/LabelWithToolTip.java
deleted file mode 100644
index e0c8a91..0000000
--- 
a/frontend/webadmin/modules/webadmin/src/main/java/org/ovirt/engine/ui/webadmin/widget/label/LabelWithToolTip.java
+++ /dev/null
@@ -1,71 +0,0 @@
-package org.ovirt.engine.ui.webadmin.widget.label;
-
-import org.ovirt.engine.ui.uicompat.external.StringUtils;
-
-import com.google.gwt.event.dom.client.MouseOutEvent;
-import com.google.gwt.event.dom.client.MouseOutHandler;
-import com.google.gwt.event.dom.client.MouseOverEvent;
-import com.google.gwt.event.dom.client.MouseOverHandler;
-import com.google.gwt.safehtml.shared.SafeHtml;
-import com.google.gwt.user.client.ui.DecoratedPopupPanel;
-import com.google.gwt.user.client.ui.HTML;
-
-public class LabelWithToolTip extends HTML {
-
-    private final HTML tooltip = new HTML();
-    private final DecoratedPopupPanel tooltipPanel = new DecoratedPopupPanel();
-    private String title;
-
-    public LabelWithToolTip() {
-        this("", -1); //$NON-NLS-1$
-    }
-
-    public LabelWithToolTip(String text) {
-        this(text, -1);
-    }
-
-    public LabelWithToolTip(SafeHtml text) {
-        this(text.asString());
-    }
-
-    public LabelWithToolTip(String text, int length) {
-        super(text);
-
-        if (length > -1 && text.length() > length) {
-            setText(text.substring(0, length - 3) + "..."); //$NON-NLS-1$
-        }
-        tooltipPanel.setWidget(tooltip);
-        tooltipPanel.getElement().getStyle().setZIndex(1);
-        setTitle(text);
-        registerHandlers();
-    }
-
-    private void registerHandlers() {
-        addMouseOverHandler(new MouseOverHandler() {
-
-            @Override
-            public void onMouseOver(MouseOverEvent event) {
-                if (StringUtils.isEmpty(title)) {
-                    tooltip.setHTML(title);
-                    tooltipPanel.showRelativeTo(LabelWithToolTip.this);
-                }
-            }
-        });
-        addMouseOutHandler(new MouseOutHandler() {
-
-            @Override
-            public void onMouseOut(MouseOutEvent event) {
-                tooltipPanel.hide(true);
-            }
-        });
-    }
-
-    @Override
-    public void setTitle(String text) {
-        this.title = text;
-    }
-
-    public void setTitle(SafeHtml text) {
-        setTitle(text == null ? (String)null : text.asString());
-    }
-}


-- 
To view, visit http://gerrit.ovirt.org/29125
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3a0c6292b874d0994042366940123b42b38b7a8d
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Alona Kaplan <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to