Revision: 8980
Author: [email protected]
Date: Fri Oct  8 10:03:48 2010
Log: Let MenuItem implement HasEnabled
http://gwt-code-reviews.appspot.com/846801

http://code.google.com/p/google-web-toolkit/source/detail?r=8980

Modified:
 /trunk/user/src/com/google/gwt/user/client/ui/MenuBar.java
 /trunk/user/src/com/google/gwt/user/client/ui/MenuItem.java
/trunk/user/src/com/google/gwt/user/theme/chrome/public/gwt/chrome/chrome.css /trunk/user/src/com/google/gwt/user/theme/chrome/public/gwt/chrome/chrome_rtl.css
 /trunk/user/src/com/google/gwt/user/theme/dark/public/gwt/dark/dark.css
 /trunk/user/src/com/google/gwt/user/theme/dark/public/gwt/dark/dark_rtl.css
/trunk/user/src/com/google/gwt/user/theme/standard/public/gwt/standard/standard.css /trunk/user/src/com/google/gwt/user/theme/standard/public/gwt/standard/standard_rtl.css
 /trunk/user/test/com/google/gwt/user/client/ui/MenuBarTest.java

=======================================
--- /trunk/user/src/com/google/gwt/user/client/ui/MenuBar.java Fri Oct 8 06:15:38 2010 +++ /trunk/user/src/com/google/gwt/user/client/ui/MenuBar.java Fri Oct 8 10:03:48 2010
@@ -147,6 +147,7 @@
 @SuppressWarnings("deprecation")
 public class MenuBar extends Widget implements PopupListener, HasAnimation,
     HasCloseHandlers<PopupPanel> {
+
   /**
    * An {...@link ImageBundle} that provides images for {...@link MenuBar}.
    *
@@ -872,6 +873,11 @@
    * otherwise.
    */
void doItemAction(final MenuItem item, boolean fireCommand, boolean focus) {
+    // Should not perform any action if the item is disabled
+    if (!item.isEnabled()) {
+      return;
+    }
+
     // Ensure that the item is selected.
     selectItem(item);

@@ -937,6 +943,10 @@
         return;
       }
     }
+
+    if (!item.isEnabled()) {
+      return;
+    }

     // Style the item selected when the mouse enters.
     selectItem(item);
@@ -1281,14 +1291,16 @@
    */
   private boolean selectFirstItemIfNoneSelected() {
     if (selectedItem == null) {
-      if (items.size() > 0) {
-        MenuItem nextItem = items.get(0);
-        selectItem(nextItem);
+      for (MenuItem nextItem : items) {
+        if (nextItem.isEnabled()) {
+          selectItem(nextItem);
+          break;
+        }
       }
       return true;
     }
     return false;
-  }
+ }

   private void selectNextItem() {
     if (selectedItem == null) {
@@ -1303,10 +1315,22 @@

     MenuItem itemToBeSelected;

-    if (index < items.size() - 1) {
-      itemToBeSelected = items.get(index + 1);
-    } else { // we're at the end, loop around to the start
-      itemToBeSelected = items.get(0);
+    int firstIndex = index;
+    while (true) {
+      index = index + 1;
+      if (index == items.size()) {
+        // we're at the end, loop around to the start
+        index = 0;
+      }
+      if (index == firstIndex) {
+        itemToBeSelected = items.get(firstIndex);
+        break;
+      } else {
+        itemToBeSelected = items.get(index);
+        if (itemToBeSelected.isEnabled()) {
+          break;
+        }
+      }
     }

     selectItem(itemToBeSelected);
@@ -1327,11 +1351,23 @@
     assert (index != -1);

     MenuItem itemToBeSelected;
-    if (index > 0) {
-      itemToBeSelected = items.get(index - 1);
-
-    } else { // we're at the start, loop around to the end
-      itemToBeSelected = items.get(items.size() - 1);
+
+    int firstIndex = index;
+    while (true) {
+      index = index - 1;
+      if (index < 0) {
+        // we're at the start, loop around to the end
+        index = items.size() - 1;
+      }
+      if (index == firstIndex) {
+        itemToBeSelected = items.get(firstIndex);
+        break;
+      } else {
+        itemToBeSelected = items.get(index);
+        if (itemToBeSelected.isEnabled()) {
+          break;
+        }
+      }
     }

     selectItem(itemToBeSelected);
=======================================
--- /trunk/user/src/com/google/gwt/user/client/ui/MenuItem.java Tue Sep 21 07:53:19 2010 +++ /trunk/user/src/com/google/gwt/user/client/ui/MenuItem.java Fri Oct 8 10:03:48 2010
@@ -29,12 +29,14 @@
  * Each menu item is assigned a unique DOM id in order to support ARIA. See
* {...@link com.google.gwt.user.client.ui.Accessibility} for more information.
  */
-public class MenuItem extends UIObject implements HasHTML, HasSafeHtml {
+public class MenuItem extends UIObject implements HasHTML, HasEnabled, HasSafeHtml {

private static final String DEPENDENT_STYLENAME_SELECTED_ITEM = "selected"; + private static final String DEPENDENT_STYLENAME_DISABLED_ITEM = "disabled";

   private Command command;
   private MenuBar parentMenu, subMenu;
+  private boolean enabled = true;

   /**
    * Constructs a new menu item that fires a command when it is selected.
@@ -56,14 +58,13 @@
   }

   /**
-   * Constructs a new menu item that fires a command when it is selected.
+ * Constructs a new menu item that cascades to a sub-menu when it is selected.
    *
-   * @param text the item's text
-   * @param cmd the command to be fired when it is selected
+   * @param html the item's text
+   * @param subMenu the sub-menu to be displayed when it is selected
    */
-  public MenuItem(String text, Command cmd) {
-    this(text, false);
-    setCommand(cmd);
+  public MenuItem(SafeHtml html, MenuBar subMenu) {
+    this(html.asString(), true, subMenu);
   }

   /**
@@ -81,33 +82,34 @@
   /**
* Constructs a new menu item that cascades to a sub-menu when it is selected.
    *
-   * @param html the item's text
+   * @param text the item's text
+   * @param asHTML <code>true</code> to treat the specified text as html
    * @param subMenu the sub-menu to be displayed when it is selected
    */
-  public MenuItem(SafeHtml html, MenuBar subMenu) {
-    this(html.asString(), true, subMenu);
+  public MenuItem(String text, boolean asHTML, MenuBar subMenu) {
+    this(text, asHTML);
+    setSubMenu(subMenu);
   }

   /**
- * Constructs a new menu item that cascades to a sub-menu when it is selected.
+   * Constructs a new menu item that fires a command when it is selected.
    *
    * @param text the item's text
-   * @param subMenu the sub-menu to be displayed when it is selected
+   * @param cmd the command to be fired when it is selected
    */
-  public MenuItem(String text, MenuBar subMenu) {
+  public MenuItem(String text, Command cmd) {
     this(text, false);
-    setSubMenu(subMenu);
+    setCommand(cmd);
   }

   /**
* Constructs a new menu item that cascades to a sub-menu when it is selected.
    *
    * @param text the item's text
-   * @param asHTML <code>true</code> to treat the specified text as html
    * @param subMenu the sub-menu to be displayed when it is selected
    */
-  public MenuItem(String text, boolean asHTML, MenuBar subMenu) {
-    this(text, asHTML);
+  public MenuItem(String text, MenuBar subMenu) {
+    this(text, false);
     setSubMenu(subMenu);
   }

@@ -161,6 +163,10 @@
   public String getText() {
     return DOM.getInnerText(getElement());
   }
+
+  public boolean isEnabled() {
+    return enabled;
+  }

   /**
    * Sets the command associated with this item.
@@ -171,13 +177,23 @@
     command = cmd;
   }

-  public void setHTML(String html) {
-    DOM.setInnerHTML(getElement(), html);
+  @Override
+  public void setEnabled(boolean enabled) {
+    if (enabled) {
+      removeStyleDependentName(DEPENDENT_STYLENAME_DISABLED_ITEM);
+    } else {
+      addStyleDependentName(DEPENDENT_STYLENAME_DISABLED_ITEM);
+    }
+    this.enabled = enabled;
   }

   public void setHTML(SafeHtml html) {
     setHTML(html.asString());
   }
+
+  public void setHTML(String html) {
+    DOM.setInnerHTML(getElement(), html);
+  }

   /**
    * Sets the sub-menu associated with this item.
=======================================
--- /trunk/user/src/com/google/gwt/user/theme/chrome/public/gwt/chrome/chrome.css Wed Feb 10 08:44:48 2010 +++ /trunk/user/src/com/google/gwt/user/theme/chrome/public/gwt/chrome/chrome.css Fri Oct 8 10:03:48 2010
@@ -263,6 +263,9 @@
 .gwt-MenuBar .gwt-MenuItem-selected {
   background: #cdcdcd;
 }
+.gwt-MenuBar .gwt-MenuItem-disabled {
+  color: #cdcdcd;
+}
 .gwt-MenuBar-horizontal {
   background: #ebebeb url(images/hborder.png) repeat-x 0px -2003px;
   border: 1px solid #BBBBBB;
=======================================
--- /trunk/user/src/com/google/gwt/user/theme/chrome/public/gwt/chrome/chrome_rtl.css Wed Feb 10 08:44:48 2010 +++ /trunk/user/src/com/google/gwt/user/theme/chrome/public/gwt/chrome/chrome_rtl.css Fri Oct 8 10:03:48 2010
@@ -263,6 +263,9 @@
 .gwt-MenuBar .gwt-MenuItem-selected {
   background: #cdcdcd;
 }
+.gwt-MenuBar .gwt-MenuItem-disabled {
+  color: #cdcdcd;
+}
 .gwt-MenuBar-horizontal {
   background: #ebebeb url(images/hborder.png) repeat-x 0px -2003px;
   border: 1px solid #BBBBBB;
=======================================
--- /trunk/user/src/com/google/gwt/user/theme/dark/public/gwt/dark/dark.css Wed Feb 10 08:44:48 2010 +++ /trunk/user/src/com/google/gwt/user/theme/dark/public/gwt/dark/dark.css Fri Oct 8 10:03:48 2010
@@ -247,6 +247,9 @@
   background: #666;
   color: #0cf;
 }
+.gwt-MenuBar .gwt-MenuItem-disabled {
+  color: #ccc;
+}
 .gwt-MenuBar-horizontal {
   background: #222222;
   border: 1px solid #777;
=======================================
--- /trunk/user/src/com/google/gwt/user/theme/dark/public/gwt/dark/dark_rtl.css Wed Feb 10 08:44:48 2010 +++ /trunk/user/src/com/google/gwt/user/theme/dark/public/gwt/dark/dark_rtl.css Fri Oct 8 10:03:48 2010
@@ -247,6 +247,9 @@
   background: #666;
   color: #0cf;
 }
+.gwt-MenuBar .gwt-MenuItem-disabled {
+  color: #ccc;
+}
 .gwt-MenuBar-horizontal {
   background: #222222;
   border: 1px solid #777;
=======================================
--- /trunk/user/src/com/google/gwt/user/theme/standard/public/gwt/standard/standard.css Wed Feb 10 08:44:48 2010 +++ /trunk/user/src/com/google/gwt/user/theme/standard/public/gwt/standard/standard.css Fri Oct 8 10:03:48 2010
@@ -263,6 +263,9 @@
 .gwt-MenuBar .gwt-MenuItem-selected {
   background: #E0EDFE;
 }
+.gwt-MenuBar .gwt-MenuItem-disabled {
+  color: #c0c0c0;
+}
 .gwt-MenuBar-horizontal {
   background: #e3e8f3 url(images/hborder.png) repeat-x 0px -2003px;
   border: 1px solid #BBBBBB;
=======================================
--- /trunk/user/src/com/google/gwt/user/theme/standard/public/gwt/standard/standard_rtl.css Wed Feb 10 08:44:48 2010 +++ /trunk/user/src/com/google/gwt/user/theme/standard/public/gwt/standard/standard_rtl.css Fri Oct 8 10:03:48 2010
@@ -263,6 +263,9 @@
 .gwt-MenuBar .gwt-MenuItem-selected {
   background: #E0EDFE;
 }
+.gwt-MenuBar .gwt-MenuItem-disabled {
+  color: #c0c0c0;
+}
 .gwt-MenuBar-horizontal {
   background: #e3e8f3 url(images/hborder.png) repeat-x 0px -2003px;
   border: 1px solid #BBBBBB;
=======================================
--- /trunk/user/test/com/google/gwt/user/client/ui/MenuBarTest.java Tue Sep 21 07:53:19 2010 +++ /trunk/user/test/com/google/gwt/user/client/ui/MenuBarTest.java Fri Oct 8 10:03:48 2010
@@ -202,6 +202,26 @@
       }
     });
   }
+
+  public void testDisabledItem() {
+    MenuBar bar = new MenuBar(true);
+    MenuItem item1 = new MenuItem("item1", BLANK_COMMAND);
+    MenuItem item2 = new MenuItem("item2", BLANK_COMMAND);
+    MenuItem item3 = new MenuItem("item3", BLANK_COMMAND);
+    bar.addItem(item1);
+    bar.addItem(item2);
+    bar.addItem(item3);
+    RootPanel.get().add(bar);
+
+    item2.setEnabled(false);
+
+    bar.moveSelectionDown();
+    assertEquals(item1, bar.getSelectedItem());
+    bar.moveSelectionDown();
+    assertEquals(item3, bar.getSelectedItem());
+    bar.moveSelectionUp();
+    assertEquals(item1, bar.getSelectedItem());
+  }

   public void testEscapeKey() {
     // Create a menu bar with children.

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to