From: Artur Signell Date: Thu, 21 May 2009 15:36:15 +0000 (+0000) Subject: Fix for #2997 - Add API for registering tooltips for any element in the DOM tree X-Git-Tag: 6.7.0.beta1~2809 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f641cbb1b15db660142e4df185de88dd6c3d97e3;p=vaadin-framework.git Fix for #2997 - Add API for registering tooltips for any element in the DOM tree svn changeset:7940/svn branch:6.0 --- diff --git a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java index 04003a0651..5b4c762f31 100755 --- a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java +++ b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java @@ -65,6 +65,9 @@ public class ApplicationConnection { public static final String PARAM_UNLOADBURST = "onunloadburst"; + public static final String ATTRIBUTE_DESCRIPTION = "description"; + public static final String ATTRIBUTE_ERROR = "error"; + private static String uidl_security_key = "init"; private final HashMap resourcesMap = new HashMap(); @@ -1095,22 +1098,30 @@ public class ApplicationConnection { styleBuf.append(MODIFIED_CLASSNAME); } - TooltipInfo tooltipInfo = componentDetail.getTooltipInfo(); - if (uidl.hasAttribute("description")) { - tooltipInfo.setTitle(uidl.getStringAttribute("description")); + // Update tooltip + if (uidl.hasAttribute(ATTRIBUTE_DESCRIPTION) + || uidl.hasAttribute(ATTRIBUTE_ERROR)) { + TooltipInfo tooltipInfo = new TooltipInfo(); + + if (uidl.hasAttribute(ATTRIBUTE_DESCRIPTION)) { + tooltipInfo.setTitle(uidl + .getStringAttribute(ATTRIBUTE_DESCRIPTION)); + } + + if (uidl.hasAttribute(ATTRIBUTE_ERROR)) { + tooltipInfo.setErrorUidl(uidl.getErrors()); + } + + registerTooltip(component.getElement(), tooltipInfo); } else { - tooltipInfo.setTitle(null); + registerTooltip(component.getElement(), (TooltipInfo) null); } // add error classname to components w/ error - if (uidl.hasAttribute("error")) { + if (uidl.hasAttribute(ATTRIBUTE_ERROR)) { styleBuf.append(" "); styleBuf.append(primaryName); styleBuf.append(ERROR_CLASSNAME_EXT); - - tooltipInfo.setErrorUidl(uidl.getErrors()); - } else { - tooltipInfo.setErrorUidl(null); } // add required style to required components @@ -1529,15 +1540,7 @@ public class ApplicationConnection { * */ public TooltipInfo getTitleInfo(Paintable titleOwner) { - if (null == titleOwner) { - return null; - } - ComponentDetail pd = idToPaintableDetail.get(getPid(titleOwner)); - if (null != pd) { - return pd.getTooltipInfo(); - } else { - return null; - } + return tooltip.getTooltip(((Widget) titleOwner).getElement()); } private final VTooltip tooltip = new VTooltip(this); @@ -1629,4 +1632,16 @@ public class ApplicationConnection { return view; } + public void registerTooltip(Element e, String tooltip) { + if (tooltip == null || tooltip.equals("")) { + registerTooltip(e, (TooltipInfo) null); + } else { + registerTooltip(e, new TooltipInfo(tooltip)); + } + } + + public void registerTooltip(Element e, TooltipInfo tooltip) { + this.tooltip.registerTooltip(e, tooltip); + } + } diff --git a/src/com/vaadin/terminal/gwt/client/ComponentDetail.java b/src/com/vaadin/terminal/gwt/client/ComponentDetail.java index 8ee91ad9e4..3254346c76 100644 --- a/src/com/vaadin/terminal/gwt/client/ComponentDetail.java +++ b/src/com/vaadin/terminal/gwt/client/ComponentDetail.java @@ -40,21 +40,6 @@ class ComponentDetail { this.component = component; } - /** - * @return the tooltipInfo - */ - TooltipInfo getTooltipInfo() { - return tooltipInfo; - } - - /** - * @param tooltipInfo - * the tooltipInfo to set - */ - void setTooltipInfo(TooltipInfo tooltipInfo) { - this.tooltipInfo = tooltipInfo; - } - /** * @return the relativeSize */ diff --git a/src/com/vaadin/terminal/gwt/client/TooltipInfo.java b/src/com/vaadin/terminal/gwt/client/TooltipInfo.java index 9a66bb14c8..b5cc0382de 100644 --- a/src/com/vaadin/terminal/gwt/client/TooltipInfo.java +++ b/src/com/vaadin/terminal/gwt/client/TooltipInfo.java @@ -9,6 +9,13 @@ public class TooltipInfo { private UIDL errorUidl; + public TooltipInfo() { + } + + public TooltipInfo(String tooltip) { + setTitle(tooltip); + } + public String getTitle() { return title; } diff --git a/src/com/vaadin/terminal/gwt/client/VCaption.java b/src/com/vaadin/terminal/gwt/client/VCaption.java index 3f6de7c9b2..de40940ad2 100644 --- a/src/com/vaadin/terminal/gwt/client/VCaption.java +++ b/src/com/vaadin/terminal/gwt/client/VCaption.java @@ -33,12 +33,12 @@ public class VCaption extends HTML { private int maxWidth = -1; - private static String ATTRIBUTE_ICON = "icon"; - private static String ATTRIBUTE_CAPTION = "caption"; - private static String ATTRIBUTE_DESCRIPTION = "description"; - private static String ATTRIBUTE_REQUIRED = "required"; - private static String ATTRIBUTE_ERROR = "error"; - private static String ATTRIBUTE_HIDEERRORS = "hideErrors"; + protected static final String ATTRIBUTE_ICON = "icon"; + protected static final String ATTRIBUTE_CAPTION = "caption"; + protected static final String ATTRIBUTE_DESCRIPTION = "description"; + protected static final String ATTRIBUTE_REQUIRED = "required"; + protected static final String ATTRIBUTE_ERROR = "error"; + protected static final String ATTRIBUTE_HIDEERRORS = "hideErrors"; /** * diff --git a/src/com/vaadin/terminal/gwt/client/VTooltip.java b/src/com/vaadin/terminal/gwt/client/VTooltip.java index 77059bd21e..6ab99819c6 100644 --- a/src/com/vaadin/terminal/gwt/client/VTooltip.java +++ b/src/com/vaadin/terminal/gwt/client/VTooltip.java @@ -28,11 +28,14 @@ public class VTooltip extends VOverlay { VErrorMessage em = new VErrorMessage(); Element description = DOM.createDiv(); private Paintable tooltipOwner; + private Element tooltipTargetElement; + private boolean closing = false; private boolean opening = false; private ApplicationConnection ac; // Open next tooltip faster. Disabled after 2 sec of showTooltip-silence. private boolean justClosed = false; + private TooltipInfo visibleTooltip = null; public VTooltip(ApplicationConnection client) { super(false, false, true); @@ -45,6 +48,11 @@ public class VTooltip extends VOverlay { DOM.appendChild(layout.getElement(), description); } + /** + * Show a popup containing the information in the "info" tooltip + * + * @param info + */ private void show(TooltipInfo info) { boolean hasContent = false; if (info.getErrorUidl() != null) { @@ -89,19 +97,25 @@ public class VTooltip extends VOverlay { sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT); } }); + visibleTooltip = info; } else { hide(); + visibleTooltip = null; } } public void showTooltip(Paintable owner, Event event) { + Element targetElement = event.getEventTarget().cast(); if (closing && tooltipOwner == owner) { - // return to same tooltip, cancel closing - closeTimer.cancel(); - closing = false; - justClosedTimer.cancel(); - justClosed = false; - return; + TooltipInfo newToolTip = getTooltip(owner, targetElement); + if (newToolTip == visibleTooltip) { + // return to same tooltip, cancel closing + closeTimer.cancel(); + closing = false; + justClosedTimer.cancel(); + justClosed = false; + return; + } } if (closing) { @@ -114,6 +128,10 @@ public class VTooltip extends VOverlay { showTimer.cancel(); } tooltipOwner = owner; + tooltipTargetElement = targetElement; + + // Schedule timer for showing the tooltip according to if it was + // recently closed or not. if (justClosed) { showTimer.schedule(QUICK_OPEN_DELAY); } else { @@ -134,7 +152,7 @@ public class VTooltip extends VOverlay { private Timer showTimer = new Timer() { @Override public void run() { - TooltipInfo info = ac.getTitleInfo(tooltipOwner); + TooltipInfo info = getTooltip(tooltipOwner, tooltipTargetElement); if (null != info) { show(info); } @@ -178,6 +196,37 @@ public class VTooltip extends VOverlay { } + /** + * Returns the tooltip that should be shown for the element. Searches upward + * in the DOM tree for registered tooltips until the root of the Paintable + * is found. Returns null if no tooltip was found (none should be shown). + * + * @param paintable + * @param element + * @return + */ + private TooltipInfo getTooltip(Paintable paintable, Element element) { + /* Try to find registered tooltips */ + while (element != null) { + TooltipInfo info = getTooltip(element); + if (info != null) { + return info; + } + + if (ac.getPid(element) != null) { + // This is the Paintable root so we stop searching + break; + } + + element = DOM.getParent(element); + } + + /* + * No registered tooltips found + */ + return null; + } + private int tooltipEventMouseX; private int tooltipEventMouseY; @@ -222,4 +271,13 @@ public class VTooltip extends VOverlay { } } + native TooltipInfo getTooltip(Element e) + /*-{ + return e.vaadinTooltip; + }-*/; + + native void registerTooltip(Element e, TooltipInfo tooltipInfo) + /*-{ + e.vaadinTooltip = tooltipInfo; + }-*/; }