]> source.dussan.org Git - vaadin-framework.git/commitdiff
Tooltips for Tree + Testbech test for them #6637
authorJohn Alhroos <john.ahlroos@itmill.com>
Tue, 16 Aug 2011 11:57:04 +0000 (11:57 +0000)
committerJohn Alhroos <john.ahlroos@itmill.com>
Tue, 16 Aug 2011 11:57:04 +0000 (11:57 +0000)
svn changeset:20415/svn branch:6.7

src/com/vaadin/terminal/gwt/client/ui/VTree.java
src/com/vaadin/ui/Tree.java
tests/src/com/vaadin/tests/components/tree/TreeToolTips.java [new file with mode: 0644]
tests/src/com/vaadin/tests/components/tree/TreeTooltip.html [new file with mode: 0644]

index a8974aa5bcf4723b711e979b3fb6bc359cdeb08c..a1647c2c5222bd8b1522a6f0d1eeac38c51749c4 100644 (file)
@@ -41,8 +41,10 @@ import com.vaadin.terminal.gwt.client.ApplicationConnection;
 import com.vaadin.terminal.gwt.client.BrowserInfo;
 import com.vaadin.terminal.gwt.client.MouseEventDetails;
 import com.vaadin.terminal.gwt.client.Paintable;
+import com.vaadin.terminal.gwt.client.TooltipInfo;
 import com.vaadin.terminal.gwt.client.UIDL;
 import com.vaadin.terminal.gwt.client.Util;
+import com.vaadin.terminal.gwt.client.VTooltip;
 import com.vaadin.terminal.gwt.client.ui.dd.DDUtil;
 import com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler;
 import com.vaadin.terminal.gwt.client.ui.dd.VAcceptCallback;
@@ -760,6 +762,11 @@ public class VTree extends FocusElementPanel implements Paintable,
             if (disabled) {
                 return;
             }
+
+            if (target == nodeCaptionSpan) {
+                client.handleTooltipEvent(event, VTree.this, key);
+            }
+
             final boolean inCaption = target == nodeCaptionSpan
                     || (icon != null && target == icon.getElement());
             if (inCaption
@@ -932,6 +939,7 @@ public class VTree extends FocusElementPanel implements Paintable,
                     + "-caption");
             Element wrapper = DOM.createDiv();
             nodeCaptionSpan = DOM.createSpan();
+            DOM.sinkEvents(nodeCaptionSpan, VTooltip.TOOLTIP_EVENTS);
             DOM.appendChild(getElement(), nodeCaptionDiv);
             DOM.appendChild(nodeCaptionDiv, wrapper);
             DOM.appendChild(wrapper, nodeCaptionSpan);
@@ -977,6 +985,16 @@ public class VTree extends FocusElementPanel implements Paintable,
                         + uidl.getStringAttribute("style"));
             }
 
+            String description = uidl.getStringAttribute("descr");
+            if (description != null && client != null) {
+                // Set tooltip
+                TooltipInfo info = new TooltipInfo(description);
+                client.registerTooltip(VTree.this, key, info);
+            } else {
+                // Remove possible previous tooltip
+                client.registerTooltip(VTree.this, key, null);
+            }
+
             if (uidl.getBooleanAttribute("expanded") && !getState()) {
                 setState(true, false);
             }
index 3e6d62033edfce2102dd80e8a971523d8d8a371c..ede37f2c60393b1a10f0cd1ec12e63271a434368 100644 (file)
@@ -103,6 +103,11 @@ public class Tree extends AbstractSelect implements Container.Hierarchical,
      */
     private boolean initialPaint = true;
 
+    /**
+     * Item tooltip generator
+     */
+    private ItemDescriptionGenerator itemDescriptionGenerator;
+
     /**
      * Supported drag modes for Tree.
      */
@@ -608,6 +613,14 @@ public class Tree extends AbstractSelect implements Container.Hierarchical,
                     }
                 }
 
+                if (itemDescriptionGenerator != null) {
+                    String description = itemDescriptionGenerator
+                            .generateDescription(this, itemId, null);
+                    if (description != null && !description.equals("")) {
+                        target.addAttribute("descr", description);
+                    }
+                }
+
                 // Adds the attributes
                 target.addAttribute("caption", getItemCaption(itemId));
                 final Resource icon = getItemIcon(itemId);
@@ -1562,4 +1575,26 @@ public class Tree extends AbstractSelect implements Container.Hierarchical,
 
     }
 
+    /**
+     * Set the item description generator which generates tooltips for the tree
+     * items
+     * 
+     * @param generator
+     *            The generator to use or null to disable
+     */
+    public void setItemDescriptionGenerator(ItemDescriptionGenerator generator) {
+        if (generator != itemDescriptionGenerator) {
+            itemDescriptionGenerator = generator;
+            requestRepaint();
+        }
+    }
+
+    /**
+     * Get the item description generator which generates tooltips for tree
+     * items
+     */
+    public ItemDescriptionGenerator getItemDescriptionGenerator() {
+        return itemDescriptionGenerator;
+    }
+
 }
diff --git a/tests/src/com/vaadin/tests/components/tree/TreeToolTips.java b/tests/src/com/vaadin/tests/components/tree/TreeToolTips.java
new file mode 100644 (file)
index 0000000..c6e29bc
--- /dev/null
@@ -0,0 +1,89 @@
+package com.vaadin.tests.components.tree;
+
+import com.vaadin.data.Item;
+import com.vaadin.data.util.HierarchicalContainer;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.AbstractSelect;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.Tree;
+
+public class TreeToolTips extends TestBase {
+
+    @Override
+    protected void setup() {
+        final Tree tree = new Tree(null, createContainer());
+        tree.setItemDescriptionGenerator(new AbstractSelect.ItemDescriptionGenerator() {
+            public String generateDescription(Component source, Object itemId,
+                    Object propertyId) {
+                return "This is a tooltip for item id '" + itemId + "'";
+            }
+        });
+
+        for (Object rootItems : tree.rootItemIds())
+            tree.expandItemsRecursively(rootItems);
+
+        addComponent(tree);
+    }
+
+    @Override
+    protected String getDescription() {
+        return "Tree items should have tooltips";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 6637;
+    }
+
+    private HierarchicalContainer createContainer() {
+        HierarchicalContainer cont = new HierarchicalContainer();
+        cont.addContainerProperty("name", String.class, "");
+
+        for (int i = 0; i < 20; i++) {
+            Item item = cont.addItem("Item " + i);
+            item.getItemProperty("name").setValue("Item " + i);
+            cont.setChildrenAllowed("Item " + i, false);
+
+            if (i == 1 || i == 4) {
+                cont.setChildrenAllowed("Item " + i, true);
+            }
+
+            // Add three items to item 1
+            if (i > 1 && i < 4) {
+                cont.setParent("Item " + i, "Item 1");
+            }
+
+            // Add 5 items to item 4
+            if (i > 4 && i < 10) {
+                cont.setChildrenAllowed("Item " + i, true);
+
+                if (i == 7) {
+                    item = cont.addItem("Item 71");
+                    item.getItemProperty("name").setValue("Item 71");
+                    cont.setParent("Item 71", "Item " + i);
+                    cont.setChildrenAllowed("Item 71", false);
+
+                    item = cont.addItem("Item 72");
+                    item.getItemProperty("name").setValue("Item 72");
+                    cont.setParent("Item 72", "Item " + i);
+                    cont.setChildrenAllowed("Item 72", true);
+
+                    item = cont.addItem("Item 73");
+                    item.getItemProperty("name").setValue("Item 73");
+                    cont.setParent("Item 73", "Item 72");
+                    cont.setChildrenAllowed("Item 73", true);
+
+                    item = cont.addItem("Item 74");
+                    item.getItemProperty("name").setValue("Item 74");
+                    cont.setParent("Item 74", "Item " + i);
+                    cont.setChildrenAllowed("Item 74", true);
+                }
+
+                cont.setParent("Item " + i, "Item " + (i - 1));
+
+            }
+        }
+
+        return cont;
+    }
+}
diff --git a/tests/src/com/vaadin/tests/components/tree/TreeTooltip.html b/tests/src/com/vaadin/tests/components/tree/TreeTooltip.html
new file mode 100644 (file)
index 0000000..afebdaa
--- /dev/null
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head profile="http://selenium-ide.openqa.org/profiles/test-case">
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<link rel="selenium.base" href="" />
+<title>New Test</title>
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">New Test</td></tr>
+</thead><tbody>
+<tr>
+       <td>open</td>
+       <td>/run/com.vaadin.tests.components.tree.TreeToolTips?restartApplication</td>
+       <td></td>
+</tr>
+<tr>
+       <td>showTooltip</td>
+       <td>vaadin=runcomvaadintestscomponentstreeTreeToolTips::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTree[0]#n[0]</td>
+       <td>20,9</td>
+</tr>
+<tr>
+       <td>pause</td>
+       <td>1000</td>
+       <td>1000</td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>vaadin=runcomvaadintestscomponentstreeTreeToolTips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]</td>
+       <td>This is a tooltip for item id 'Item 0'</td>
+</tr>
+<tr>
+       <td>showTooltip</td>
+       <td>vaadin=runcomvaadintestscomponentstreeTreeToolTips::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VTree[0]#n[2]/n[0]/n[0]/n[0]</td>
+       <td>25,8</td>
+</tr>
+<tr>
+       <td>pause</td>
+       <td>1000</td>
+       <td>1000</td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>vaadin=runcomvaadintestscomponentstreeTreeToolTips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]</td>
+       <td>This is a tooltip for item id 'Item 7'</td>
+</tr>
+
+</tbody></table>
+</body>
+</html>