]> source.dussan.org Git - vaadin-framework.git/commitdiff
Behaviour is now more consistent + code cleanup
authorRisto Yrjänä <risto.yrjana@itmill.com>
Mon, 19 Jan 2009 13:19:17 +0000 (13:19 +0000)
committerRisto Yrjänä <risto.yrjana@itmill.com>
Mon, 19 Jan 2009 13:19:17 +0000 (13:19 +0000)
svn changeset:6593/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/ui/IMenuBar.java
src/com/itmill/toolkit/ui/MenuBar.java

index 2b4e1ea4256b2761c6faeb4eabb171b11846253f..c11ace6ba9b7095a1ed19ec5f5fa79039b7f3c6c 100644 (file)
@@ -33,25 +33,19 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
     protected boolean collapseItems = true;
     protected CustomMenuItem moreItem = null;
 
-    protected static int number = 0;
-
     // Construct an empty command to be used when the item has no command
     // associated
     protected static final Command emptyCommand = null;
 
     /** Widget fields **/
-    boolean subMenu;
-    ArrayList items;
-    Element containerElement;
-    IToolkitOverlay popup;
-    IMenuBar visibleChildMenu;
-    IMenuBar parentMenu;
-    CustomMenuItem selected;
+    protected boolean subMenu;
+    protected ArrayList<CustomMenuItem> items;
+    protected Element containerElement;
+    protected IToolkitOverlay popup;
+    protected IMenuBar visibleChildMenu;
+    protected IMenuBar parentMenu;
+    protected CustomMenuItem selected;
 
-    /**
-     * The constructor should first call super() to initialize the component and
-     * then handle any initialization relevant to IT Mill Toolkit.
-     */
     public IMenuBar() {
         // Create an empty horizontal menubar
         this(false);
@@ -61,7 +55,7 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
         super();
         setElement(DOM.createDiv());
 
-        items = new ArrayList();
+        items = new ArrayList<CustomMenuItem>();
         popup = null;
         visibleChildMenu = null;
 
@@ -133,15 +127,15 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
             moreItem = new CustomMenuItem(itemHTML.toString(), emptyCommand);
         }
 
-        UIDL items = uidl.getChildUIDL(1);
-        Iterator itr = items.getChildIterator();
-        Stack iteratorStack = new Stack();
-        Stack menuStack = new Stack();
+        UIDL uidlItems = uidl.getChildUIDL(1);
+        Iterator<UIDL> itr = uidlItems.getChildIterator();
+        Stack<Iterator<UIDL>> iteratorStack = new Stack<Iterator<UIDL>>();
+        Stack<IMenuBar> menuStack = new Stack<IMenuBar>();
         IMenuBar currentMenu = this;
 
         while (itr.hasNext()) {
             UIDL item = (UIDL) itr.next();
-            CustomMenuItem currentItem = null; // For receiving the item
+            CustomMenuItem currentItem = null;
 
             String itemText = item.getStringAttribute("text");
             final int itemId = item.getIntAttribute("id");
@@ -168,7 +162,6 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
 
             Command cmd = null;
 
-            // Check if we need to create a command to this item
             if (itemHasCommand) {
                 // Construct a command that fires onMenuClick(int) with the
                 // item's id-number
@@ -190,7 +183,7 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
             }
 
             while (!itr.hasNext() && !iteratorStack.empty()) {
-                itr = (Iterator) iteratorStack.pop();
+                itr = iteratorStack.pop();
                 currentMenu = (IMenuBar) menuStack.pop();
             }
         }// while
@@ -208,7 +201,7 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
             }
 
             if (topLevelWidth > getOffsetWidth()) {
-                ArrayList toBeCollapsed = new ArrayList();
+                ArrayList<CustomMenuItem> toBeCollapsed = new ArrayList<CustomMenuItem>();
                 IMenuBar collapsed = new IMenuBar(true);
                 for (int j = i - 2; j < getItems().size(); j++) {
                     toBeCollapsed.add(getItems().get(j));
@@ -221,8 +214,9 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
                     // it's ugly, but we have to insert the submenu icon
                     if (item.getSubMenu() != null && submenuIcon != null) {
                         StringBuffer itemText = new StringBuffer(item.getHTML());
-                        itemText.append("<img src=\"" + submenuIcon
-                                + "\" align=\"right\" />");
+                        itemText.append("<img src=\"");
+                        itemText.append(submenuIcon);
+                        itemText.append("\" align=\"right\" />");
                         item.setHTML(itemText.toString());
                     }
 
@@ -243,6 +237,7 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
      *            id of the item that was clicked
      */
     public void onMenuClick(int clickedItemId) {
+        System.out.println("onMenuClick");
         // Updating the state to the server can not be done before
         // the server connection is known, i.e., before updateFromUIDL()
         // has been called.
@@ -258,7 +253,7 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
     /**
      * Returns a list of items in this menu
      */
-    public List getItems() {
+    public List<CustomMenuItem> getItems() {
         return items;
     }
 
@@ -366,13 +361,10 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
                 break;
 
             case Event.ONMOUSEOVER:
-
                 itemOver(targetItem);
-
                 break;
 
             case Event.ONMOUSEOUT:
-
                 itemOut(targetItem);
                 break;
             }
@@ -386,6 +378,7 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
      */
     public void itemClick(CustomMenuItem item) {
         if (item.getCommand() != null) {
+            System.out.println("itemClick, running command");
             setSelected(null);
 
             if (visibleChildMenu != null) {
@@ -410,11 +403,12 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
      * @param item
      */
     public void itemOver(CustomMenuItem item) {
+        System.out.println("ItemOver " + item.getText());
         setSelected(item);
 
         boolean menuWasVisible = visibleChildMenu != null;
 
-        if (visibleChildMenu != null && visibleChildMenu != item.getSubMenu()) {
+        if (menuWasVisible && visibleChildMenu != item.getSubMenu()) {
             popup.hide();
             visibleChildMenu = null;
         }
@@ -431,6 +425,7 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
      * @param item
      */
     public void itemOut(CustomMenuItem item) {
+        System.out.println("ItemOut " + item.getText());
         if (visibleChildMenu != item.getSubMenu() || visibleChildMenu == null) {
             hideChildMenu(item);
             setSelected(null);
@@ -504,6 +499,7 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
 
         if (visibleChildMenu != null) {
             popup.hide();
+            setSelected(null);
         }
 
         if (getParentMenu() != null) {
@@ -546,6 +542,11 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
      * @param item
      */
     public void setSelected(CustomMenuItem item) {
+        if (item != null) {
+            System.out.println("setSelected " + item.getText());
+        } else {
+            System.out.println("setSelected was null");
+        }
         // If we had something selected, unselect
         if (item != selected && selected != null) {
             selected.setSelected(false);
@@ -562,12 +563,12 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
      * Listener method, fired when this menu is closed
      */
     public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
-
+        System.out.println("onPopupClosed, auto: " + autoClosed);
         hideChildren();
         if (autoClosed) {
             hideParents();
         }
-        setSelected(null);
+        // setSelected(null);
         visibleChildMenu = null;
         popup = null;
 
@@ -603,6 +604,10 @@ public class IMenuBar extends Widget implements Paintable, PopupListener {
             }
         }
 
+        /*
+         * setters and getters for the fields
+         */
+
         public void setSubMenu(IMenuBar subMenu) {
             this.subMenu = subMenu;
         }
index af8d6d775244bba0596dbe8f39b507a338a6ae52..c0f400933cc8ae96896e25cf4f9268dc4603bd2d 100644 (file)
@@ -11,19 +11,20 @@ import com.itmill.toolkit.terminal.PaintTarget;
 import com.itmill.toolkit.terminal.Resource;
 
 /**
- * The top-level menu class. This can contain MenuItems, which in turn can
- * contain Submenus and MenuCommands.
- * 
+ * <p>
+ * A class representing a horizontal menu bar. The menu can contain MenuItem
+ * objects, which in turn can contain more MenuBars. These sub-level MenuBars
+ * are represented as vertical menu.
+ * </p>
  */
 public class MenuBar extends AbstractComponent {
 
     // Items of the top-level menu
-    private final List menuItems;
+    private final List<MenuItem> menuItems;
 
     // Number of items in this menu
     private static int numberOfItems = 0;
 
-    private boolean animationEnabled;
     private boolean collapseItems;
     private Resource submenuIcon;
     private MenuItem moreItem;
@@ -34,7 +35,7 @@ public class MenuBar extends AbstractComponent {
         return "menubar";
     }
 
-    /** Paint (serialize) the component for the client. */
+    /** Paint (serialise) the component for the client. */
     @Override
     public void paintContent(PaintTarget target) throws PaintException {
 
@@ -42,10 +43,9 @@ public class MenuBar extends AbstractComponent {
         super.paintContent(target);
 
         // Stack for list iterators
-        Stack iteratorStack = new Stack();
+        Stack<Iterator<MenuItem>> iteratorStack = new Stack<Iterator<MenuItem>>();
 
         target.startTag("options");
-        target.addAttribute("animationEnabled", animationEnabled);
 
         if (submenuIcon != null) {
             target.addAttribute("submenuIcon", submenuIcon);
@@ -65,7 +65,7 @@ public class MenuBar extends AbstractComponent {
         target.endTag("options");
         target.startTag("items");
 
-        Iterator itr = menuItems.iterator();
+        Iterator<MenuItem> itr = menuItems.iterator();
 
         // This generates the tree from the contents of the menu
         while (itr.hasNext()) {
@@ -100,7 +100,7 @@ public class MenuBar extends AbstractComponent {
 
             // The end submenu. More than one submenu may end at once.
             while (!itr.hasNext() && !iteratorStack.empty()) {
-                itr = (Iterator) iteratorStack.pop();
+                itr = iteratorStack.pop();
                 target.endTag("item");
             }
 
@@ -112,13 +112,13 @@ public class MenuBar extends AbstractComponent {
     /** Deserialize changes received from client. */
     @Override
     public void changeVariables(Object source, Map variables) {
-        Stack items = new Stack();
+        Stack<MenuItem> items = new Stack<MenuItem>();
         boolean found = false;
 
         if (variables.containsKey("clickedId")) {
 
             Integer clickedId = (Integer) variables.get("clickedId");
-            Iterator itr = getItems().iterator();
+            Iterator<MenuItem> itr = getItems().iterator();
             while (itr.hasNext()) {
                 items.push(itr.next());
             }
@@ -150,15 +150,14 @@ public class MenuBar extends AbstractComponent {
      * Constructs an empty, horizontal menu
      */
     public MenuBar() {
-        menuItems = new ArrayList();
-        setAnimation(false);
+        menuItems = new ArrayList<MenuItem>();
         setCollapse(true);
         setMoreMenuItem(null);
     }
 
     /**
-     * Add a new item to the menubar. Command can be null, but a caption must be
-     * given.
+     * Add a new item to the menu bar. Command can be null, but a caption must
+     * be given.
      * 
      * @param caption
      *            the text for the menu item
@@ -171,7 +170,7 @@ public class MenuBar extends AbstractComponent {
     }
 
     /**
-     * Add a new item to the menubar. Icon and command can be null, but a
+     * Add a new item to the menu bar. Icon and command can be null, but a
      * caption must be given.
      * 
      * @param caption
@@ -231,11 +230,11 @@ public class MenuBar extends AbstractComponent {
     }
 
     /**
-     * Returns a list with all the MenuItem objects in the menubar
+     * Returns a list with all the MenuItem objects in the menu bar
      * 
-     * @return a list containing the MenuItem objects in the menubar
+     * @return a list containing the MenuItem objects in the menu bar
      */
-    public java.util.List getItems() {
+    public List<MenuItem> getItems() {
         return menuItems;
     }
 
@@ -253,7 +252,7 @@ public class MenuBar extends AbstractComponent {
     }
 
     /**
-     * Empty the menubar
+     * Empty the menu bar
      */
     public void removeItems() {
         menuItems.clear();
@@ -269,28 +268,6 @@ public class MenuBar extends AbstractComponent {
         return menuItems.size();
     }
 
-    /**
-     * Enable or disable animated menubar appearance. Animation is disabled by
-     * default. Currently does nothing.
-     * 
-     * @param hasAnimation
-     */
-    public void setAnimation(boolean animation) {
-        animationEnabled = animation;
-        requestRepaint();
-    }
-
-    /**
-     * Returns true if the animation is enabled. Animation of this class is
-     * disabled by default.
-     * 
-     * @return true if the animation is enabled
-     * 
-     */
-    public boolean hasAnimation() {
-        return animationEnabled;
-    }
-
     /**
      * Set the icon to be used if a sub-menu has children. Defaults to null;
      * 
@@ -334,9 +311,10 @@ public class MenuBar extends AbstractComponent {
     }
 
     /**
-     * Set the item that is used when collapsing the top level menu. The item
-     * command will be ignored. If set to null, the default item with the "More"
-     * text will be used.
+     * Set the item that is used when collapsing the top level menu. All
+     * "overflowing" items will be added below this. The item command will be
+     * ignored. If set to null, the default item with the "More" text is be
+     * used.
      * 
      * @param item
      */
@@ -359,17 +337,21 @@ public class MenuBar extends AbstractComponent {
     }
 
     /**
-     * This interface contains the layer for menu commands of the MenuBar class
-     * . It's method will fire when the user clicks on the containing MenuItem.
-     * The selected item is given as an argument.
+     * This interface contains the layer for menu commands of the
+     * {@link com.itmill.toolkit.ui.MenuBar} class. It's method will fire when
+     * the user clicks on the containing
+     * {@link com.itmill.toolkit.ui.MenuBar.MenuItem}. The selected item is
+     * given as an argument.
      */
     public interface Command {
         public void menuSelected(MenuBar.MenuItem selectedItem);
     }
 
     /**
-     * A composite class for menu items and submenus. You can set commands to be
-     * fired on user click by implementing the MenuBar.Command interface.
+     * A composite class for menu items and sub-menus. You can set commands to
+     * be fired on user click by implementing the
+     * {@link com.itmill.toolkit.ui.MenuBar.Command} interface. You can also add
+     * multiple MenuItems to a MenuItem and create a sub-menu.
      * 
      */
     public class MenuItem {
@@ -378,7 +360,7 @@ public class MenuBar extends AbstractComponent {
         private final int itsId;
         private Command itsCommand;
         private String itsText;
-        private List itsChildren;
+        private List<MenuItem> itsChildren;
         private Resource itsIcon;
         private MenuItem itsParent;
 
@@ -404,7 +386,7 @@ public class MenuBar extends AbstractComponent {
         }
 
         /**
-         * Checks if the item has children (if it is a submenu).
+         * Checks if the item has children (if it is a sub-menu).
          * 
          * @return True if this item has children
          */
@@ -413,8 +395,8 @@ public class MenuBar extends AbstractComponent {
         }
 
         /**
-         * Add a new item inside this item, thus creating a submenu. Command can
-         * be null, but a caption must be given.
+         * Add a new item inside this item, thus creating a sub-menu. Command
+         * can be null, but a caption must be given.
          * 
          * @param caption
          *            the text for the menu item
@@ -426,7 +408,7 @@ public class MenuBar extends AbstractComponent {
         }
 
         /**
-         * Add a new item inside this item, thus creating a submenu. Icon and
+         * Add a new item inside this item, thus creating a sub-menu. Icon and
          * command can be null, but a caption must be given.
          * 
          * @param caption
@@ -443,7 +425,7 @@ public class MenuBar extends AbstractComponent {
             }
 
             if (itsChildren == null) {
-                itsChildren = new ArrayList();
+                itsChildren = new ArrayList<MenuItem>();
             }
 
             MenuItem newItem = new MenuItem(caption, icon, command);
@@ -512,9 +494,10 @@ public class MenuBar extends AbstractComponent {
 
         /**
          * For the containing item. This will return null if the item is in the
-         * top-level menubar.
+         * top-level menu bar.
          * 
-         * @return The containing MenuBar.MenuItem, or null if there is none
+         * @return The containing {@link com.itmill.toolkit.ui.MenuBar.MenuItem}
+         *         , or null if there is none
          */
         public MenuBar.MenuItem getParent() {
             return itsParent;
@@ -525,7 +508,7 @@ public class MenuBar extends AbstractComponent {
          * 
          * @return List of children items, or null if there are none
          */
-        public java.util.List getChildren() {
+        public List<MenuItem> getChildren() {
             return itsChildren;
         }