]> source.dussan.org Git - vaadin-framework.git/commitdiff
VForm now listens key down events lazily, only when needed
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Tue, 6 Apr 2010 15:21:41 +0000 (15:21 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Tue, 6 Apr 2010 15:21:41 +0000 (15:21 +0000)
svn changeset:12331/svn branch:6.3

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

index dcc1a39b83c48303de5cb1059893559088865a4c..090063f007419f51710809e3c5a8e8d82be12e92 100644 (file)
@@ -8,6 +8,7 @@ import java.util.Set;
 \r
 import com.google.gwt.event.dom.client.KeyDownEvent;\r
 import com.google.gwt.event.dom.client.KeyDownHandler;\r
+import com.google.gwt.event.shared.HandlerRegistration;\r
 import com.google.gwt.user.client.DOM;\r
 import com.google.gwt.user.client.Element;\r
 import com.google.gwt.user.client.Event;\r
@@ -23,7 +24,7 @@ import com.vaadin.terminal.gwt.client.UIDL;
 import com.vaadin.terminal.gwt.client.Util;\r
 import com.vaadin.terminal.gwt.client.VErrorMessage;\r
 \r
-public class VForm extends ComplexPanel implements Container {\r
+public class VForm extends ComplexPanel implements Container, KeyDownHandler {\r
 \r
     protected String id;\r
 \r
@@ -59,6 +60,8 @@ public class VForm extends ComplexPanel implements Container {
 \r
     ShortcutActionHandler shortcutHandler;\r
 \r
+    private HandlerRegistration keyDownRegistration;\r
+\r
     public VForm() {\r
         setElement(DOM.createDiv());\r
         DOM.appendChild(getElement(), fieldSet);\r
@@ -76,14 +79,6 @@ public class VForm extends ComplexPanel implements Container {
         errorMessage.setStyleName(CLASSNAME + "-errormessage");\r
         DOM.appendChild(fieldSet, errorMessage.getElement());\r
         DOM.appendChild(fieldSet, footerContainer);\r
-\r
-        addDomHandler(new KeyDownHandler() {\r
-            public void onKeyDown(KeyDownEvent event) {\r
-                shortcutHandler.handleKeyboardEvent(Event.as(event\r
-                        .getNativeEvent()));\r
-                return;\r
-            }\r
-        }, KeyDownEvent.getType());\r
     }\r
 \r
     public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {\r
@@ -181,16 +176,19 @@ public class VForm extends ComplexPanel implements Container {
 \r
         // We may have actions attached\r
         if (uidl.getChildCount() > 1) {\r
-            final int cnt = uidl.getChildCount();\r
-            for (int i = 1; i < cnt; i++) {\r
-                UIDL childUidl = uidl.getChildUIDL(i);\r
-                if (childUidl.getTag().equals("actions")) {\r
-                    if (shortcutHandler == null) {\r
-                        shortcutHandler = new ShortcutActionHandler(id, client);\r
-                    }\r
-                    shortcutHandler.updateActionMap(childUidl);\r
+            UIDL childUidl = uidl.getChildByTagName("actions");\r
+            if (childUidl != null) {\r
+                if (shortcutHandler == null) {\r
+                    shortcutHandler = new ShortcutActionHandler(id, client);\r
+                    keyDownRegistration = addDomHandler(this, KeyDownEvent\r
+                            .getType());\r
                 }\r
+                shortcutHandler.updateActionMap(childUidl);\r
             }\r
+        } else if (shortcutHandler != null) {\r
+            keyDownRegistration.removeHandler();\r
+            shortcutHandler = null;\r
+            keyDownRegistration = null;\r
         }\r
 \r
         rendering = false;\r
@@ -315,4 +313,8 @@ public class VForm extends ComplexPanel implements Container {
             Util.updateRelativeChildrenAndSendSizeUpdateEvent(client, this);\r
         }\r
     }\r
+\r
+    public void onKeyDown(KeyDownEvent event) {\r
+        shortcutHandler.handleKeyboardEvent(Event.as(event.getNativeEvent()));\r
+    }\r
 }\r