]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix for #5174
authorJohn Alhroos <john.ahlroos@itmill.com>
Tue, 15 Jun 2010 15:01:06 +0000 (15:01 +0000)
committerJohn Alhroos <john.ahlroos@itmill.com>
Tue, 15 Jun 2010 15:01:06 +0000 (15:01 +0000)
svn changeset:13678/svn branch:6.4

src/com/vaadin/terminal/gwt/client/ui/VMenuBar.java

index f80edc4bc4f5b7045dca2613aafdb223d40cf28c..09f3fadb70418902f18e8f89b0a59fc16849557a 100644 (file)
@@ -68,6 +68,8 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,
     protected CustomMenuItem selected;
 
     private Timer layoutTimer;
+    private Timer blurDelayTimer;
+    private Timer focusDelayTimer;
 
     private boolean enabled = true;
 
@@ -108,6 +110,7 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,
         this.subMenu = subMenu;
 
         sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT
+                | Event.ONMOUSEDOWN
                 | Event.ONLOAD);
     }
 
@@ -288,6 +291,17 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,
      *            id of the item that was clicked
      */
     public void onMenuClick(int clickedItemId) {
+        // Cancel the blur event handling, focus was lost to a submenu
+        if (blurDelayTimer != null) {
+            blurDelayTimer.cancel();
+        }
+
+        // Cancel the focus event handling since focus was gained by
+        // clicking an item.
+        if (focusDelayTimer != null) {
+            focusDelayTimer.cancel();
+        }
+
         // Updating the state to the server can not be done before
         // the server connection is known, i.e., before updateFromUIDL()
         // has been called.
@@ -323,6 +337,7 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,
      * 
      * @return
      */
+    @Override
     public Element getContainerElement() {
         return containerElement;
     }
@@ -409,6 +424,12 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,
         if (targetItem != null) {
             switch (DOM.eventGetType(e)) {
 
+            case Event.ONMOUSEDOWN:
+                if (isEnabled() && targetItem.isEnabled()) {
+                    selected = targetItem;
+                }
+                break;
+
             case Event.ONCLICK:
                 if (isEnabled() && targetItem.isEnabled()) {
                     itemClick(targetItem);
@@ -1224,9 +1245,19 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,
      * .dom.client.BlurEvent)
      */
     public void onBlur(BlurEvent event) {
-        setSelected(null);
-        hideChildren();
-        menuVisible = false;
+        /*
+         * Delay the action so a mouse click can cancel the blur event if needed
+         */
+        blurDelayTimer = new Timer() {
+            @Override
+            public void run() {
+                setSelected(null);
+                hideChildren();
+                menuVisible = false;
+            }
+        };
+
+        blurDelayTimer.schedule(100);
     }
 
     /*
@@ -1237,9 +1268,19 @@ public class VMenuBar extends SimpleFocusablePanel implements Paintable,
      * .dom.client.FocusEvent)
      */
     public void onFocus(FocusEvent event) {
-        if (getSelected() == null) {
-            // If nothing is selected then select the first item
-            setSelected(items.get(0));
-        }
+        /*
+         * Delay the action so a mouse click can cancel the blur event if needed
+         */
+        focusDelayTimer = new Timer() {
+            @Override
+            public void run() {
+                if (getSelected() == null) {
+                    // If nothing is selected then select the first item
+                    setSelected(items.get(0));
+                }
+            }
+        };
+
+        focusDelayTimer.schedule(100);
     }
 }