You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/02/12 02:15:12 UTC

svn commit: r909199 - in /pivot/trunk: wtk-terra/src/org/apache/pivot/wtk/skin/terra/ wtk/src/org/apache/pivot/wtk/ wtk/src/org/apache/pivot/wtk/content/

Author: gbrown
Date: Fri Feb 12 01:14:52 2010
New Revision: 909199

URL: http://svn.apache.org/viewvc?rev=909199&view=rev
Log:
Add toString() to Button.DataRenderer to support keyboard-based menu navigation and other future enhancements.

Modified:
    pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java
    pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuPopupSkin.java
    pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuSkin.java
    pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraSuggestionPopupSkin.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Button.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Menu.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ButtonDataRenderer.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/content/LinkButtonDataRenderer.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListViewItemRenderer.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuBarItemDataRenderer.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuItemDataRenderer.java

Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java (original)
+++ pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java Fri Feb 12 01:14:52 2010
@@ -167,6 +167,17 @@
                 label.setText(text);
             }
         }
+
+        @Override
+        public String toString(Object item) {
+            File file = (File)item;
+            String text = file.getName();
+            if (text.length() == 0) {
+                text = System.getProperty("file.separator");
+            }
+
+            return text;
+        }
     }
 
     /**
@@ -363,6 +374,11 @@
                 label.setText(file.toString());
             }
         }
+
+        @Override
+        public String toString(Object data) {
+            return null;
+        }
     }
 
     /**

Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuPopupSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuPopupSkin.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuPopupSkin.java (original)
+++ pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuPopupSkin.java Fri Feb 12 01:14:52 2010
@@ -198,7 +198,6 @@
 
         Display display = window.getDisplay();
         display.getContainerMouseListeners().add(displayMouseListener);
-        display.reenterMouse();
 
         MenuPopup menuPopup = (MenuPopup)window;
         Menu menu = menuPopup.getMenu();

Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuSkin.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuSkin.java (original)
+++ pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraMenuSkin.java Fri Feb 12 01:14:52 2010
@@ -22,6 +22,7 @@
 
 import org.apache.pivot.collections.Dictionary;
 import org.apache.pivot.collections.Sequence;
+import org.apache.pivot.wtk.Button;
 import org.apache.pivot.wtk.Component;
 import org.apache.pivot.wtk.Dimensions;
 import org.apache.pivot.wtk.GraphicsUtilities;
@@ -30,6 +31,8 @@
 import org.apache.pivot.wtk.MenuListener;
 import org.apache.pivot.wtk.Theme;
 import org.apache.pivot.wtk.Window;
+import org.apache.pivot.wtk.Menu.Item;
+import org.apache.pivot.wtk.Menu.Section;
 import org.apache.pivot.wtk.skin.ContainerSkin;
 
 /**
@@ -391,10 +394,90 @@
         Menu menu = (Menu)component;
 
         if (keyCode == Keyboard.KeyCode.UP) {
-            menu.activatePreviousItem();
+            Menu.SectionSequence sections = menu.getSections();
+            int sectionCount = sections.getLength();
+
+            Menu.Item activeItem = menu.getActiveItem();
+            int sectionIndex;
+            int itemIndex;
+            if (activeItem == null) {
+                sectionIndex = sectionCount - 1;
+                itemIndex = -1;
+            } else {
+                Menu.Section section = activeItem.getSection();
+                sectionIndex = sections.indexOf(section);
+                itemIndex = section.indexOf(activeItem) - 1;
+
+                if (itemIndex == -1) {
+                    sectionIndex--;
+                }
+            }
+
+            while (sectionIndex >= 0) {
+                Section section = sections.get(sectionIndex);
+                if (itemIndex == -1) {
+                    int sectionLength = section.getLength();
+                    itemIndex = sectionLength - 1;
+                }
+
+                while (itemIndex >= 0) {
+                    Item item = section.get(itemIndex);
+
+                    if (item.isEnabled()) {
+                        item.setActive(true);
+                        break;
+                    }
+
+                    itemIndex--;
+                }
+
+                if (itemIndex >= 0) {
+                    break;
+                }
+
+                sectionIndex--;
+            }
+
             consumed = true;
         } else if (keyCode == Keyboard.KeyCode.DOWN) {
-            menu.activateNextItem();
+            Menu.SectionSequence sections = menu.getSections();
+            int sectionCount = sections.getLength();
+
+            Menu.Item activeItem = menu.getActiveItem();
+            int sectionIndex;
+            int itemIndex;
+            if (activeItem == null) {
+                sectionIndex = 0;
+                itemIndex = 0;
+            } else {
+                Menu.Section section = activeItem.getSection();
+                sectionIndex = sections.indexOf(section);
+                itemIndex = section.indexOf(activeItem) + 1;
+            }
+
+            while (sectionIndex < sectionCount) {
+                Section section = sections.get(sectionIndex);
+                int sectionLength = section.getLength();
+
+                while (itemIndex < sectionLength) {
+                    Item item = section.get(itemIndex);
+
+                    if (item.isEnabled()) {
+                        item.setActive(true);
+                        break;
+                    }
+
+                    itemIndex++;
+                }
+
+                if (itemIndex < sectionLength) {
+                    break;
+                }
+
+                sectionIndex++;
+                itemIndex = 0;
+            }
+
             consumed = true;
         } else if (keyCode == Keyboard.KeyCode.LEFT) {
             // Close the window if this is not a top-level menu
@@ -447,6 +530,65 @@
     }
 
     @Override
+    public boolean keyTyped(Component component, char character) {
+        boolean consumed = super.keyTyped(component, character);
+
+        Menu menu = (Menu)component;
+        Menu.SectionSequence sections = menu.getSections();
+        int sectionCount = sections.getLength();
+
+        Menu.Item activeItem = menu.getActiveItem();
+
+        int sectionIndex;
+        int itemIndex;
+        if (activeItem == null) {
+            sectionIndex = 0;
+            itemIndex = 0;
+        } else {
+            Menu.Section section = activeItem.getSection();
+            sectionIndex = sections.indexOf(section);
+            itemIndex = section.indexOf(activeItem) + 1;
+        }
+
+        character = Character.toUpperCase(character);
+
+        while (sectionIndex < sectionCount) {
+            Section section = sections.get(sectionIndex);
+            int sectionLength = section.getLength();
+
+            while (itemIndex < sectionLength) {
+                Item item = section.get(itemIndex);
+                if (item.isEnabled()) {
+                    Button.DataRenderer itemDataRenderer = item.getDataRenderer();
+                    String string = itemDataRenderer.toString(item.getButtonData());
+
+                    if (string != null
+                        && string.length() > 0) {
+                        char first = Character.toUpperCase(string.charAt(0));
+
+                        if (first == character) {
+                            item.setActive(true);
+                            consumed = true;
+                            break;
+                        }
+                    }
+                }
+
+                itemIndex++;
+            }
+
+            if (itemIndex < sectionLength) {
+                break;
+            }
+
+            sectionIndex++;
+            itemIndex = 0;
+        }
+
+        return consumed;
+    }
+
+    @Override
     public void sectionInserted(Menu menu, int index) {
         Menu.Section section = menu.getSections().get(index);
         section.getSectionListeners().add(this);

Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraSuggestionPopupSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraSuggestionPopupSkin.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraSuggestionPopupSkin.java (original)
+++ pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraSuggestionPopupSkin.java Fri Feb 12 01:14:52 2010
@@ -247,7 +247,6 @@
 
         Display display = window.getDisplay();
         display.getContainerMouseListeners().add(displayMouseListener);
-        display.reenterMouse();
 
         dropShadowDecorator.setShadowOpacity(DropShadowDecorator.DEFAULT_SHADOW_OPACITY);
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Button.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Button.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Button.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Button.java Fri Feb 12 01:14:52 2010
@@ -37,7 +37,34 @@
      * Button data renderer interface.
      */
     public interface DataRenderer extends Renderer {
+        /**
+         * Prepares the renderer for layout or paint.
+         *
+         * @param data
+         * The data to render, or <tt>null</tt> if called to calculate preferred
+         * height for skins that assume a fixed renderer height.
+         *
+         * @param button
+         * The button that contains the data.
+         *
+         * @param highlighted
+         * If <tt>true</tt>, the item is highlighted.
+         */
         public void render(Object data, Button button, boolean highlighted);
+
+        /**
+         * Converts button data to a string representation.
+         *
+         * @param data
+         *
+         * @return
+         * The data's string representation, or <tt>null</tt> if the data does not
+         * have a string representation.
+         * <p>
+         * Note that this method may be called often during keyboard navigation, so
+         * implementations should avoid unnecessary string allocations.
+         */
+        public String toString(Object data);
     }
 
     /**

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ListView.java Fri Feb 12 01:14:52 2010
@@ -97,6 +97,13 @@
          * Converts a list item to a string representation.
          *
          * @param item
+         *
+         * @return
+         * The item's string representation, or <tt>null</tt> if the item does not
+         * have a string representation.
+         * <p>
+         * Note that this method may be called often during keyboard navigation, so
+         * implementations should avoid unnecessary string allocations.
          */
         public String toString(Object item);
     }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Menu.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Menu.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Menu.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Menu.java Fri Feb 12 01:14:52 2010
@@ -663,88 +663,6 @@
         }
     }
 
-    public void activateNextItem() {
-        int sectionCount = sections.getLength();
-
-        int sectionIndex;
-        int itemIndex;
-
-        if (activeItem == null) {
-            sectionIndex = 0;
-            itemIndex = 0;
-        } else {
-            sectionIndex = sections.indexOf(activeItem.section);
-            itemIndex = activeItem.section.indexOf(activeItem) + 1;
-        }
-
-        while (sectionIndex < sectionCount) {
-            Section section = sections.get(sectionIndex);
-            int sectionLength = section.getLength();
-
-            while (itemIndex < sectionLength) {
-                Item item = section.get(itemIndex);
-
-                if (item.isEnabled()) {
-                    item.setActive(true);
-                    break;
-                }
-
-                itemIndex++;
-            }
-
-            if (itemIndex < sectionLength) {
-                break;
-            }
-
-            sectionIndex++;
-            itemIndex = 0;
-        }
-    }
-
-    public void activatePreviousItem() {
-        int sectionCount = sections.getLength();
-
-        int sectionIndex;
-        int itemIndex;
-
-        if (activeItem == null) {
-            sectionIndex = sectionCount - 1;
-            itemIndex = -1;
-        } else {
-            sectionIndex = sections.indexOf(activeItem.section);
-            itemIndex = activeItem.section.indexOf(activeItem) - 1;
-
-            if (itemIndex == -1) {
-                sectionIndex--;
-            }
-        }
-
-        while (sectionIndex >= 0) {
-            Section section = sections.get(sectionIndex);
-            if (itemIndex == -1) {
-                int sectionLength = section.getLength();
-                itemIndex = sectionLength - 1;
-            }
-
-            while (itemIndex >= 0) {
-                Item item = section.get(itemIndex);
-
-                if (item.isEnabled()) {
-                    item.setActive(true);
-                    break;
-                }
-
-                itemIndex--;
-            }
-
-            if (itemIndex >= 0) {
-                break;
-            }
-
-            sectionIndex--;
-        }
-    }
-
     @Override
     public Sequence<Component> remove(int index, int count) {
         for (int i = index, n = index + count; i < n; i++) {

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ButtonDataRenderer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ButtonDataRenderer.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ButtonDataRenderer.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ButtonDataRenderer.java Fri Feb 12 01:14:52 2010
@@ -102,4 +102,21 @@
             label.getStyles().put("color", color);
         }
     }
+
+    @Override
+    public String toString(Object data) {
+        if (data == null) {
+            throw new IllegalArgumentException();
+        }
+
+        String string;
+        if (data instanceof ButtonData) {
+            ButtonData buttonData = (ButtonData)data;
+            string = buttonData.getText();
+        } else {
+            string = data.toString();
+        }
+
+        return string;
+    }
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/content/LinkButtonDataRenderer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/LinkButtonDataRenderer.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/content/LinkButtonDataRenderer.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/content/LinkButtonDataRenderer.java Fri Feb 12 01:14:52 2010
@@ -108,4 +108,21 @@
         label.getStyles().put("textDecoration", highlighted ?
             TextDecoration.UNDERLINE : null);
     }
+
+    @Override
+    public String toString(Object data) {
+        if (data == null) {
+            throw new IllegalArgumentException();
+        }
+
+        String string;
+        if (data instanceof ButtonData) {
+            ButtonData buttonData = (ButtonData)data;
+            string = buttonData.getText();
+        } else {
+            string = data.toString();
+        }
+
+        return string;
+    }
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListButtonColorItemRenderer.java Fri Feb 12 01:14:52 2010
@@ -60,4 +60,9 @@
 
         colorBadge.setColor(color);
     }
+
+    @Override
+    public String toString(Object data) {
+        return null;
+    }
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListViewItemRenderer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListViewItemRenderer.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListViewItemRenderer.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/content/ListViewItemRenderer.java Fri Feb 12 01:14:52 2010
@@ -86,17 +86,17 @@
         label.setText(text);
     }
 
-    public String toString(Object suggestion) {
-        if (suggestion == null) {
+    public String toString(Object item) {
+        if (item == null) {
             throw new IllegalArgumentException();
         }
 
         String string;
-        if (suggestion instanceof ListItem) {
-            ListItem listItem = (ListItem)suggestion;
+        if (item instanceof ListItem) {
+            ListItem listItem = (ListItem)item;
             string = listItem.getText();
         } else {
-            string = suggestion.toString();
+            string = item.toString();
         }
 
         return string;

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuBarItemDataRenderer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuBarItemDataRenderer.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuBarItemDataRenderer.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuBarItemDataRenderer.java Fri Feb 12 01:14:52 2010
@@ -111,4 +111,21 @@
             label.getStyles().put("color", color);
         }
     }
+
+    @Override
+    public String toString(Object data) {
+        if (data == null) {
+            throw new IllegalArgumentException();
+        }
+
+        String string;
+        if (data instanceof ButtonData) {
+            ButtonData buttonData = (ButtonData)data;
+            string = buttonData.getText();
+        } else {
+            string = data.toString();
+        }
+
+        return string;
+    }
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuItemDataRenderer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuItemDataRenderer.java?rev=909199&r1=909198&r2=909199&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuItemDataRenderer.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/content/MenuItemDataRenderer.java Fri Feb 12 01:14:52 2010
@@ -142,4 +142,21 @@
             keyboardShortcutLabel.setVisible(false);
         }
     }
+
+    @Override
+    public String toString(Object data) {
+        if (data == null) {
+            throw new IllegalArgumentException();
+        }
+
+        String string;
+        if (data instanceof ButtonData) {
+            ButtonData buttonData = (ButtonData)data;
+            string = buttonData.getText();
+        } else {
+            string = data.toString();
+        }
+
+        return string;
+    }
 }