]> source.dussan.org Git - vaadin-framework.git/commitdiff
Subwindows can now receive focus w/o focusing some field within the window. Fixes...
authorMarc Englund <marc.englund@itmill.com>
Wed, 16 Dec 2009 14:14:57 +0000 (14:14 +0000)
committerMarc Englund <marc.englund@itmill.com>
Wed, 16 Dec 2009 14:14:57 +0000 (14:14 +0000)
svn changeset:10356/svn branch:6.2

src/com/vaadin/terminal/gwt/client/Util.java
src/com/vaadin/terminal/gwt/client/ui/VView.java
src/com/vaadin/terminal/gwt/client/ui/VWindow.java
tests/src/com/vaadin/tests/components/window/SubWindowFocus.java [new file with mode: 0644]

index 9ae18cc01d7f1bc723fa2980d3c21bfded0966f0..a46f9fa7d7fa708f61fe32278666743b113b75aa 100644 (file)
@@ -800,4 +800,19 @@ public class Util {
         return null;
     }
 
+    /**
+     * Will (attempt) to focus the given DOM Element.
+     * 
+     * @param el
+     *            the element to focus
+     */
+    public static native void focus(Element el)
+    /*-{
+        try {
+            el.focus();
+        } catch (e) {
+
+        }
+    }-*/;
+
 }
index c5e81b82f67b9142751aadbdb7e676a3d58e1d76..838b60a6d8b0f845dc7787afa33b14cb90bc513c 100644 (file)
@@ -114,23 +114,14 @@ public class VView extends SimplePanel implements Container, ResizeHandler,
             DOM.setStyleAttribute(fElem, "position", "absolute");
             DOM.setStyleAttribute(fElem, "opacity", "0.1");
             DOM.appendChild(getElement(), fElem);
-            focus(fElem);
+            Util.focus(fElem);
         } else {
-            focus(getElement());
+            Util.focus(getElement());
         }
 
         parentFrame = getParentFrame();
     }
 
-    private static native void focus(Element el)
-    /*-{
-        try {
-            el.focus();
-        } catch (e) {
-
-        }
-    }-*/;
-
     public String getTheme() {
         return theme;
     }
index b84b7a77f38ec60a6dda23a3cdf2f5273214539b..e8a96c63fb5d9645b79662558156f6d77c8a24ff 100644 (file)
@@ -160,6 +160,9 @@ public class VWindow extends VOverlay implements Container, ScrollListener {
         setPopupPosition(order * STACKING_OFFSET_PIXELS, order
                 * STACKING_OFFSET_PIXELS);
         contentPanel.addScrollListener(this);
+
+        // make it focusable, but last in focus chain
+        DOM.setElementProperty(contentPanel.getElement(), "tabIndex", "0");
     }
 
     private void bringToFront() {
@@ -784,12 +787,18 @@ public class VWindow extends VOverlay implements Container, ScrollListener {
             } else if (dragging || !DOM.isOrHasChild(contents, target)) {
                 onDragEvent(event);
                 event.cancelBubble(true);
+
             } else if (type == Event.ONCLICK) {
                 // clicked inside window, ensure to be on top
                 if (!isActive()) {
                     bringToFront();
                 }
             }
+
+            if (type == Event.ONMOUSEDOWN) {
+                // !DOM.isOrHasChild(contentPanel.getElement(), target)
+                Util.focus(contentPanel.getElement());
+            }
         }
     }
 
diff --git a/tests/src/com/vaadin/tests/components/window/SubWindowFocus.java b/tests/src/com/vaadin/tests/components/window/SubWindowFocus.java
new file mode 100644 (file)
index 0000000..b0fbf29
--- /dev/null
@@ -0,0 +1,83 @@
+package com.vaadin.tests.components.window;
+
+import com.vaadin.event.Action;
+import com.vaadin.event.ShortcutAction;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.Window;
+import com.vaadin.ui.Button.ClickEvent;
+
+public class SubWindowFocus extends TestBase {
+
+    @Override
+    protected String getDescription() {
+        return "A subwindow that listens to ESC and SPACE can be opened. It "
+                + "should receive focus and thus receive keyboard events when "
+                + "anything within the window is clicked. It should be last in"
+                + "the tabbing order. The window can only be closed using ESC.";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 3498;
+    }
+
+    @Override
+    protected void setup() {
+
+        // some fields with tabindex
+        for (int i = 1; i < 4; i++) {
+            TextField tf = new TextField();
+            tf.setTabIndex(i);
+            tf.setInputPrompt("Tab index " + i);
+            addComponent(tf);
+        }
+        // field with tabindex 0
+        TextField tf = new TextField();
+        tf.setTabIndex(0);
+        tf.setInputPrompt("Tab index 0");
+        addComponent(tf);
+
+        Button b = new Button("new", new Button.ClickListener() {
+            public void buttonClick(ClickEvent event) {
+                final Window win = new Window("Subwin");
+                win.getContent().setWidth(null);
+                win.center();
+                win.setClosable(false);
+                getMainWindow().addWindow(win);
+                win.addComponent(new Label("SPACE notifies, ESC closes."));
+
+                win.addActionHandler(new Action.Handler() {
+
+                    ShortcutAction esc = new ShortcutAction("Close",
+                            ShortcutAction.KeyCode.ESCAPE, null);
+                    ShortcutAction spc = new ShortcutAction("Space",
+                            ShortcutAction.KeyCode.SPACEBAR, null);
+
+                    public Action[] getActions(Object target, Object sender) {
+                        return new Action[] { esc, spc };
+                    }
+
+                    public void handleAction(Action action, Object sender,
+                            Object target) {
+                        if (action == esc) {
+                            getMainWindow().removeWindow(win);
+                        } else {
+                            getMainWindow().showNotification(
+                                    action.getCaption());
+                        }
+
+                    }
+
+                });
+
+                win.addComponent(new TextField());
+            }
+
+        });
+        addComponent(b);
+
+    }
+}