]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added keyboard support for TwinColSelect. #5057
authorJohn Alhroos <john.ahlroos@itmill.com>
Tue, 1 Jun 2010 06:21:53 +0000 (06:21 +0000)
committerJohn Alhroos <john.ahlroos@itmill.com>
Tue, 1 Jun 2010 06:21:53 +0000 (06:21 +0000)
svn changeset:13446/svn branch:6.4

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

index 880144a11ba482542ed0d163d95da9f767e7ce31..55acabb07ddd5ea481b4a83402003a73329b164b 100644 (file)
@@ -8,6 +8,9 @@ import java.util.ArrayList;
 import java.util.Iterator;\r
 \r
 import com.google.gwt.event.dom.client.ClickEvent;\r
+import com.google.gwt.event.dom.client.KeyCodes;\r
+import com.google.gwt.event.dom.client.KeyDownEvent;\r
+import com.google.gwt.event.dom.client.KeyDownHandler;\r
 import com.google.gwt.user.client.DOM;\r
 import com.google.gwt.user.client.ui.FlowPanel;\r
 import com.google.gwt.user.client.ui.HTML;\r
@@ -15,7 +18,7 @@ import com.google.gwt.user.client.ui.ListBox;
 import com.google.gwt.user.client.ui.Panel;\r
 import com.vaadin.terminal.gwt.client.UIDL;\r
 \r
-public class VTwinColSelect extends VOptionGroupBase {\r
+public class VTwinColSelect extends VOptionGroupBase implements KeyDownHandler {\r
 \r
     private static final String CLASSNAME = "v-select-twincol";\r
 \r
@@ -31,9 +34,9 @@ public class VTwinColSelect extends VOptionGroupBase {
 \r
     private final VButton remove;\r
 \r
-    private FlowPanel buttons;\r
+    private final FlowPanel buttons;\r
 \r
-    private Panel panel;\r
+    private final Panel panel;\r
 \r
     private boolean widthSet = false;\r
 \r
@@ -64,6 +67,9 @@ public class VTwinColSelect extends VOptionGroupBase {
         buttons.add(remove);\r
         panel.add(buttons);\r
         panel.add(selections);\r
+\r
+        options.addKeyDownHandler(this);\r
+        selections.addKeyDownHandler(this);\r
     }\r
 \r
     @Override\r
@@ -142,47 +148,81 @@ public class VTwinColSelect extends VOptionGroupBase {
         return selectedIndexes;\r
     }\r
 \r
+    private void addItem() {\r
+        final boolean[] sel = getItemsToAdd();\r
+        for (int i = 0; i < sel.length; i++) {\r
+            if (sel[i]) {\r
+                final int optionIndex = i\r
+                        - (sel.length - options.getItemCount());\r
+                selectedKeys.add(options.getValue(optionIndex));\r
+\r
+                // Move selection to another column\r
+                final String text = options.getItemText(optionIndex);\r
+                final String value = options.getValue(optionIndex);\r
+                selections.addItem(text, value);\r
+                selections.setItemSelected(selections.getItemCount() - 1, true);\r
+                options.removeItem(optionIndex);\r
+\r
+                if (options.getItemCount() > 0) {\r
+                    options.setItemSelected(optionIndex > 0 ? optionIndex - 1\r
+                            : 0, true);\r
+                }\r
+            }\r
+        }\r
+\r
+        // If no items are left move the focus to the selections\r
+        if (options.getItemCount() == 0) {\r
+            selections.setFocus(true);\r
+        } else {\r
+            options.setFocus(true);\r
+        }\r
+\r
+        client.updateVariable(id, "selected", selectedKeys\r
+                .toArray(new String[selectedKeys.size()]), isImmediate());\r
+    }\r
+\r
+    private void removeItem() {\r
+        final boolean[] sel = getItemsToRemove();\r
+        for (int i = 0; i < sel.length; i++) {\r
+            if (sel[i]) {\r
+                final int selectionIndex = i\r
+                        - (sel.length - selections.getItemCount());\r
+                selectedKeys.remove(selections.getValue(selectionIndex));\r
+\r
+                // Move selection to another column\r
+                final String text = selections.getItemText(selectionIndex);\r
+                final String value = selections.getValue(selectionIndex);\r
+                options.addItem(text, value);\r
+                options.setItemSelected(options.getItemCount() - 1, true);\r
+                selections.removeItem(selectionIndex);\r
+\r
+                if (selections.getItemCount() > 0) {\r
+                    selections.setItemSelected(\r
+                            selectionIndex > 0 ? selectionIndex - 1 : 0, true);\r
+                }\r
+            }\r
+        }\r
+\r
+        // If no items are left move the focus to the selections\r
+        if (selections.getItemCount() == 0) {\r
+            options.setFocus(true);\r
+        } else {\r
+            selections.setFocus(true);\r
+        }\r
+\r
+        client.updateVariable(id, "selected", selectedKeys\r
+                .toArray(new String[selectedKeys.size()]), isImmediate());\r
+    }\r
+\r
     @Override\r
     public void onClick(ClickEvent event) {\r
         super.onClick(event);\r
         if (event.getSource() == add) {\r
-            final boolean[] sel = getItemsToAdd();\r
-            for (int i = 0; i < sel.length; i++) {\r
-                if (sel[i]) {\r
-                    final int optionIndex = i\r
-                            - (sel.length - options.getItemCount());\r
-                    selectedKeys.add(options.getValue(optionIndex));\r
-\r
-                    // Move selection to another column\r
-                    final String text = options.getItemText(optionIndex);\r
-                    final String value = options.getValue(optionIndex);\r
-                    selections.addItem(text, value);\r
-                    selections.setItemSelected(selections.getItemCount() - 1,\r
-                            true);\r
-                    options.removeItem(optionIndex);\r
-                }\r
-            }\r
-            client.updateVariable(id, "selected", selectedKeys\r
-                    .toArray(new String[selectedKeys.size()]), isImmediate());\r
+            addItem();\r
 \r
         } else if (event.getSource() == remove) {\r
-            final boolean[] sel = getItemsToRemove();\r
-            for (int i = 0; i < sel.length; i++) {\r
-                if (sel[i]) {\r
-                    final int selectionIndex = i\r
-                            - (sel.length - selections.getItemCount());\r
-                    selectedKeys.remove(selections.getValue(selectionIndex));\r
-\r
-                    // Move selection to another column\r
-                    final String text = selections.getItemText(selectionIndex);\r
-                    final String value = selections.getValue(selectionIndex);\r
-                    options.addItem(text, value);\r
-                    options.setItemSelected(options.getItemCount() - 1, true);\r
-                    selections.removeItem(selectionIndex);\r
-                }\r
-            }\r
-            client.updateVariable(id, "selected", selectedKeys\r
-                    .toArray(new String[selectedKeys.size()]), isImmediate());\r
+            removeItem();\r
+\r
         } else if (event.getSource() == options) {\r
             // unselect all in other list, to avoid mistakes (i.e wrong button)\r
             final int c = selections.getItemCount();\r
@@ -241,4 +281,78 @@ public class VTwinColSelect extends VOptionGroupBase {
     public void focus() {\r
         options.setFocus(true);\r
     }\r
+\r
+    /**\r
+     * Get the key that selects an item in the table. By default it is the Enter\r
+     * key but by overriding this you can change the key to whatever you want.\r
+     *\r
+     * @return\r
+     */\r
+    protected int getNavigationSelectKey() {\r
+        return KeyCodes.KEY_ENTER;\r
+    }\r
+\r
+    /*\r
+     * (non-Javadoc)\r
+     *\r
+     * @see\r
+     * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt\r
+     * .event.dom.client.KeyDownEvent)\r
+     */\r
+    public void onKeyDown(KeyDownEvent event) {\r
+        int keycode = event.getNativeKeyCode();\r
+\r
+        // Catch tab and move between select:s\r
+        if (keycode == KeyCodes.KEY_TAB && event.getSource() == options) {\r
+            // Prevent default behavior\r
+            event.preventDefault();\r
+\r
+            // Remove current selections\r
+            for (int i = 0; i < options.getItemCount(); i++) {\r
+                options.setItemSelected(i, false);\r
+            }\r
+\r
+            // Focus selections\r
+            selections.setFocus(true);\r
+        }\r
+\r
+        if (keycode == KeyCodes.KEY_TAB && event.isShiftKeyDown()\r
+                && event.getSource() == selections) {\r
+            // Prevent default behavior\r
+            event.preventDefault();\r
+\r
+            // Remove current selections\r
+            for (int i = 0; i < selections.getItemCount(); i++) {\r
+                selections.setItemSelected(i, false);\r
+            }\r
+\r
+            // Focus options\r
+            options.setFocus(true);\r
+        }\r
+\r
+        if (keycode == getNavigationSelectKey()) {\r
+            // Prevent default behavior\r
+            event.preventDefault();\r
+\r
+            // Decide which select the selection was made in\r
+            if (event.getSource() == options) {\r
+                // Prevents the selection to become a single selection when\r
+                // using Enter key\r
+                // as the selection key (default)\r
+                options.setFocus(false);\r
+\r
+                addItem();\r
+\r
+            } else if (event.getSource() == selections) {\r
+                // Prevents the selection to become a single selection when\r
+                // using Enter key\r
+                // as the selection key (default)\r
+                selections.setFocus(false);\r
+\r
+                removeItem();\r
+            }\r
+        }\r
+\r
+    }\r
+\r
 }\r