]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix for #2997 - Add API for registering tooltips for any element in the DOM tree
authorArtur Signell <artur.signell@itmill.com>
Thu, 21 May 2009 15:36:15 +0000 (15:36 +0000)
committerArtur Signell <artur.signell@itmill.com>
Thu, 21 May 2009 15:36:15 +0000 (15:36 +0000)
svn changeset:7940/svn branch:6.0

src/com/vaadin/terminal/gwt/client/ApplicationConnection.java
src/com/vaadin/terminal/gwt/client/ComponentDetail.java
src/com/vaadin/terminal/gwt/client/TooltipInfo.java
src/com/vaadin/terminal/gwt/client/VCaption.java
src/com/vaadin/terminal/gwt/client/VTooltip.java

index 04003a065157d0619b7db3932a0b81b16b76961c..5b4c762f317a3b85585234bcb042480035c9a7f6 100755 (executable)
@@ -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<String, String> resourcesMap = new HashMap<String, String>();
@@ -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);
+    }
+
 }
index 8ee91ad9e46ef9965f3a4e19e1d0546820f327d9..3254346c76f4ab0e2d65416842b4d29f2b33aeb7 100644 (file)
@@ -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
      */
index 9a66bb14c83c8b056bb5d2157b6d1b709e2d38f7..b5cc0382de8783d6904ca474269db6e61fd22f06 100644 (file)
@@ -9,6 +9,13 @@ public class TooltipInfo {
 
     private UIDL errorUidl;
 
+    public TooltipInfo() {
+    }
+
+    public TooltipInfo(String tooltip) {
+        setTitle(tooltip);
+    }
+
     public String getTitle() {
         return title;
     }
index 3f6de7c9b284d011231447a3406de19e0b650a0c..de40940ad27da0e4c2eb0657027eb1f3b4c86820 100644 (file)
@@ -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";
 
     /**
      * 
index 77059bd21ea6660e1e062eeb099ae31b45646131..6ab99819c689ee89432c03f981723625b03d06eb 100644 (file)
@@ -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;
+    }-*/;
 }