Changeset 5109

Show
Ignore:
Timestamp:
07/22/08 05:42:33 (7 weeks ago)
Author:
risto.yrjana@…
Message:

Adds #1946 collapsing + submenu icons.

Location:
trunk/src/com/itmill/toolkit
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/com/itmill/toolkit/terminal/gwt/client/ui/IMenuBar.java

    r4955 r5109  
    11package com.itmill.toolkit.terminal.gwt.client.ui; 
    22 
     3import java.util.ArrayList; 
    34import java.util.Iterator; 
    45import java.util.Stack; 
    56 
    67import com.google.gwt.user.client.Command; 
     8import com.google.gwt.user.client.DOM; 
    79import com.google.gwt.user.client.ui.MenuBar; 
    810import com.google.gwt.user.client.ui.MenuItem; 
     
    1618    public static final String CLASSNAME = "i-menubar"; 
    1719 
    18     /** Component identifier in UIDL communications. */ 
     20    /** For server connections **/ 
    1921    protected String uidlId; 
    20  
    21     /** Reference to the server connection object. */ 
    2222    protected ApplicationConnection client; 
    2323 
    24     /** A host reference for the Command objects */ 
    2524    protected final IMenuBar hostReference = this; 
     25    protected static final boolean vertical = true; 
     26    protected String submenuIcon = null; 
     27    protected boolean collapseItems = true; 
     28 
     29    protected MenuItem moreItem = null; 
     30 
     31    // Construct an empty command to be used when the item has no command 
     32    // associated 
     33    protected static final Command emptyCommand = null; 
    2634 
    2735    /** 
     
    3038     */ 
    3139    public IMenuBar() { 
    32         // The superclass has a lot of relevant initialization 
     40        // Create an empty horizontal menubar 
    3341        super(); 
     42        DOM.setStyleAttribute(this.getElement(), "white-space", "nowrap"); 
    3443 
    3544        // This method call of the Paintable interface sets the component 
    3645        // style name in DOM tree 
    3746        setStyleName(CLASSNAME); 
     47    } 
     48 
     49    public IMenuBar(boolean vertical) { 
     50        super(vertical); 
     51        DOM.setStyleAttribute(this.getElement(), "white-space", "nowrap"); 
     52 
     53        // This method call of the Paintable interface sets the component 
     54        // style name in DOM tree 
     55        setStyleName(CLASSNAME + "_submenu"); 
    3856    } 
    3957 
     
    5270        } 
    5371 
    54         // Save reference to server connection object to be able to send 
    55         // user interaction later 
     72        // For future connections 
    5673        this.client = client; 
    57  
    58         // Save the UIDL identifier for the component 
    5974        uidlId = uidl.getId(); 
    6075 
     
    6479        } 
    6580 
    66         /* Get tree received from server and actualize it in the GWT-MenuBar */ 
    67  
     81        UIDL options = uidl.getChildUIDL(0); 
    6882        // For GWT 1.5 
    69         // this.setAnimationEnabled(uidl.getBooleanAttribute("animationEnabled")); 
     83        //this.setAnimationEnabled(options.getBooleanAttribute("animationEnabled" 
     84        // )) 
     85        // ; 
     86 
     87        if (options.hasAttribute("submenuIcon")) { 
     88            submenuIcon = client.translateToolkitUri(uidl.getChildUIDL(0) 
     89                    .getStringAttribute("submenuIcon")); 
     90        } else { 
     91            submenuIcon = null; 
     92        } 
     93 
     94        collapseItems = options.getBooleanAttribute("collapseItems"); 
     95 
     96        if (collapseItems) { 
     97            UIDL moreItemUIDL = options.getChildUIDL(0); 
     98            StringBuffer itemHTML = new StringBuffer(); 
     99            itemHTML.append("<p>"); 
     100            if (moreItemUIDL.hasAttribute("icon")) { 
     101                itemHTML.append("<img src=\"" 
     102                        + client.translateToolkitUri(moreItemUIDL 
     103                                .getStringAttribute("icon")) 
     104                        + "\" align=\"left\" />"); 
     105            } 
     106            itemHTML.append(moreItemUIDL.getStringAttribute("text")); 
     107            itemHTML.append("</p>"); 
     108            moreItem = new MenuItem(itemHTML.toString(), true, emptyCommand); 
     109        } 
     110 
    70111        UIDL items = uidl.getChildUIDL(1); 
    71112        Iterator itr = items.getChildIterator(); 
     
    74115        MenuBar currentMenu = this; 
    75116 
    76         // Construct an empty command to be used when the item has no command 
    77         // associated 
    78         Command emptyCommand = new Command() { 
    79             public void execute() { 
    80             } 
    81         }; 
     117        // int topLevelWidth = 0; 
    82118 
    83119        while (itr.hasNext()) { 
    84120            UIDL item = (UIDL) itr.next(); 
    85             MenuItem menuItem = null; // For receiving the item 
     121            MenuItem currentItem = null; // For receiving the item 
    86122 
    87123            String itemText = item.getStringAttribute("text"); 
     
    91127 
    92128            // Construct html from the text and the optional icon 
    93             if (!item.hasAttribute("icon")) { 
    94                 itemText = "<p>" + itemText + "</p>"; 
    95             } else { 
    96                 itemText = "<p>" 
    97                         + "<img src=\"" 
     129            StringBuffer itemHTML = new StringBuffer(); 
     130 
     131            itemHTML.append("<p>"); 
     132 
     133            if (item.hasAttribute("icon")) { 
     134                itemHTML.append("<img src=\"" 
    98135                        + client.translateToolkitUri(item 
    99                                 .getStringAttribute("icon")) + "\"</img>" 
    100                         + itemText + "</p>"; 
    101             } 
    102  
    103             // Check if we need to attach a command to this item 
     136                                .getStringAttribute("icon")) 
     137                        + "\" align=\"left\" />"); 
     138            } 
     139 
     140            itemHTML.append(itemText); 
     141 
     142            if (currentMenu != this && item.getChildCount() > 0 
     143                    && submenuIcon != null) { 
     144                itemHTML.append("<img src=\"" + submenuIcon 
     145                        + "\" align=\"right\" />"); 
     146            } 
     147 
     148            itemHTML.append("</p>"); 
     149 
     150            Command cmd = null; 
     151 
     152            // Check if we need to create a command to this item 
    104153            if (itemHasCommand) { 
    105154                // Construct a command that fires onMenuClick(int) with the 
    106155                // item's id-number 
    107                 Command normalCommand = new Command() { 
     156                cmd = new Command() { 
    108157                    public void execute() { 
    109158                        hostReference.onMenuClick(itemId); 
    110159                    } 
    111160                }; 
    112  
    113                 menuItem = currentMenu.addItem(itemText, true, normalCommand); 
    114  
    115             } else { 
    116                 menuItem = currentMenu.addItem(itemText, true, emptyCommand); 
    117             } 
     161            } 
     162 
     163            currentItem = currentMenu.addItem(itemHTML.toString(), true, cmd); 
    118164 
    119165            if (item.getChildCount() > 0) { 
     
    121167                iteratorStack.push(itr); 
    122168                itr = item.getChildIterator(); 
    123                 currentMenu = new MenuBar(true); 
    124                 menuItem.setSubMenu(currentMenu); 
    125             } 
    126  
    127             if (!itr.hasNext() && !iteratorStack.empty()) { 
     169                currentMenu = new IMenuBar(vertical); 
     170                currentItem.setSubMenu(currentMenu); 
     171            } 
     172 
     173            while (!itr.hasNext() && !iteratorStack.empty()) { 
    128174                itr = (Iterator) iteratorStack.pop(); 
    129175                currentMenu = (MenuBar) menuStack.pop(); 
     
    131177        }// while 
    132178 
     179        // we might need to collapse the top-level menu 
     180        if (collapseItems) { 
     181            int topLevelWidth = 0; 
     182 
     183            int ourWidth = this.getOffsetWidth(); 
     184 
     185            int i = 0; 
     186            for (; i < getItems().size() && topLevelWidth < ourWidth; i++) { 
     187                MenuItem item = (MenuItem) getItems().get(i); 
     188                topLevelWidth += item.getOffsetWidth(); 
     189            } 
     190 
     191            if (topLevelWidth > this.getOffsetWidth()) { 
     192                ArrayList toBeCollapsed = new ArrayList(); 
     193                MenuBar collapsed = new IMenuBar(vertical); 
     194                for (int j = i - 2; j < getItems().size(); j++) { 
     195                    toBeCollapsed.add(getItems().get(j)); 
     196                } 
     197 
     198                for (int j = 0; j < toBeCollapsed.size(); j++) { 
     199                    MenuItem item = (MenuItem) toBeCollapsed.get(j); 
     200                    removeItem(item); 
     201 
     202                    // it's ugly, but we have to insert the submenu icon 
     203                    if (item.getSubMenu() != null && submenuIcon != null) { 
     204                        String itemHTML = item.getHTML(); 
     205                        StringBuffer itemText = new StringBuffer(itemHTML 
     206                                .substring(0, itemHTML.length() - 4)); 
     207                        itemText.append("<img src=\"" + submenuIcon 
     208                                + "\" align=\"right\" /></p>"); 
     209                        item.setHTML(itemText.toString()); 
     210                    } 
     211 
     212                    collapsed.addItem(item); 
     213                } 
     214 
     215                moreItem.setSubMenu(collapsed); 
     216                addItem(moreItem); 
     217            } 
     218        } 
    133219    }// updateFromUIDL 
    134220 
     
    138224     *  
    139225     * @param clickedItemId 
    140      *                id of the item that was clicked 
     226     *            id of the item that was clicked 
    141227     */ 
    142228    public void onMenuClick(int clickedItemId) { 
     
    150236        } 
    151237    } 
     238 
    152239}// class IMenuBar 
  • trunk/src/com/itmill/toolkit/ui/MenuBar.java

    r4993 r5109  
    2525 
    2626    private boolean animationEnabled; 
     27    private boolean collapseItems; 
     28    private Resource submenuIcon; 
     29    private MenuItem moreItem; 
    2730 
    2831    /** Tag is the UIDL element name for client-server communications. */ 
     
    4245        target.startTag("options"); 
    4346        target.addAttribute("animationEnabled", animationEnabled); 
     47 
     48        if (submenuIcon != null) { 
     49            target.addAttribute("submenuIcon", submenuIcon); 
     50        } 
     51 
     52        target.addAttribute("collapseItems", collapseItems); 
     53 
     54        if (collapseItems) { 
     55            target.startTag("moreItem"); 
     56            target.addAttribute("text", moreItem.getText()); 
     57            if (moreItem.getIcon() != null) { 
     58                target.addAttribute("icon", moreItem.getIcon()); 
     59            } 
     60            target.endTag("moreItem"); 
     61        } 
     62 
    4463        target.endTag("options"); 
    4564        target.startTag("items"); 
     
    7897            } 
    7998 
    80             if (!itr.hasNext() && !iteratorStack.empty()) { // The end submenu 
     99            // The end submenu. More than one submenu may end at once. 
     100            while (!itr.hasNext() && !iteratorStack.empty()) { 
    81101                itr = (Iterator) iteratorStack.pop(); 
    82102                target.endTag("item"); 
     
    115135                } 
    116136 
    117             } 
     137            }// while 
     138 
    118139            // If we got the clicked item, launch the command. 
    119140            if (found) { 
    120141                tmpItem.getCommand().menuSelected(tmpItem); 
    121             }// while 
     142            } 
    122143        }// if 
    123144    }// changeVariables 
     
    128149    public MenuBar() { 
    129150        menuItems = new ArrayList(); 
    130         animationEnabled = false; 
     151        setAnimation(false); 
     152        setCollapse(true); 
     153        setMoreMenuItem(null); 
    131154    } 
    132155 
     
    136159     *  
    137160     * @param caption 
    138      *                the text for the menu item 
     161     *            the text for the menu item 
    139162     * @param icon 
    140      *                the icon for the menu item 
     163     *            the icon for the menu item 
    141164     * @param command 
    142      *                the command for the menu item 
     165     *            the command for the menu item 
    143166     * @throws IllegalArgumentException 
    144167     */ 
     
    162185     *  
    163186     * @param caption 
    164      *                the text for the menu item 
     187     *            the text for the menu item 
    165188     * @param icon 
    166      *                the icon for the menu item 
     189     *            the icon for the menu item 
    167190     * @param command 
    168      *                the command for the menu item 
     191     *            the command for the menu item 
    169192     * @param itemToAddBefore 
    170      *                the item that will be after the new item 
     193     *            the item that will be after the new item 
    171194     * @throws IllegalArgumentException 
    172195     */ 
     
    204227     *  
    205228     * @param item 
    206      *                The item to be removed 
     229     *            The item to be removed 
    207230     */ 
    208231    public void removeItem(MenuBar.MenuItem item) { 
     
    232255    /** 
    233256     * Enable or disable animated menubar appearance. Animation is disabled by 
    234      * default. 
     257     * default. Currently does nothing. 
    235258     *  
    236259     * @param hasAnimation 
     
    238261    public void setAnimation(boolean animation) { 
    239262        animationEnabled = animation; 
     263        requestRepaint(); 
    240264    } 
    241265 
     
    252276 
    253277    /** 
    254      * This interface contains the layer for menu commands of the MenuBar class . 
    255      * It's method will fire when the user clicks on the containing MenuItem. 
     278     * Set the icon to be used if a sub-menu has children. Defaults to null; 
     279     *  
     280     * @param icon 
     281     */ 
     282    public void setSubmenuIcon(Resource icon) { 
     283        submenuIcon = icon; 
     284        requestRepaint(); 
     285    } 
     286 
     287    /** 
     288     * Get the icon used for sub-menus. Returns null if no icon is set. 
     289     *  
     290     * @return 
     291     */ 
     292    public Resource getSubmenuIcon() { 
     293        return submenuIcon; 
     294    } 
     295 
     296    /** 
     297     * Enable or disable collapsing top-level items. Top-level items will 
     298     * collapse to if there is not enough room for them. Items that don't fit 
     299     * will be placed under the "More" menu item. 
     300     *  
     301     * Collapsing is enabled by default. 
     302     *  
     303     * @param collapse 
     304     */ 
     305    public void setCollapse(boolean collapse) { 
     306        collapseItems = collapse; 
     307        requestRepaint(); 
     308    } 
     309 
     310    /** 
     311     * Collapsing is enabled by default. 
     312     *  
     313     * @return true if the top-level items will be collapsed 
     314     */ 
     315    public boolean getCollapse() { 
     316        return collapseItems; 
     317    } 
     318 
     319    /** 
     320     * Set the item that is used when collapsing the top level menu. The item 
     321     * command will be ignored. If set to null, the default item with the "More" 
     322     * text will be used. 
     323     *  
     324     * @param item 
     325     */ 
     326    public void setMoreMenuItem(MenuItem item) { 
     327        if (item != null) { 
     328            moreItem = item; 
     329        } else { 
     330            moreItem = new MenuItem("More", null, null); 
     331        } 
     332        requestRepaint(); 
     333    } 
     334 
     335    /** 
     336     * Get the MenuItem used as the collapse menu item. 
     337     *  
     338     * @return 
     339     */ 
     340    public MenuItem getMoreMenuItem() { 
     341        return moreItem; 
     342    } 
     343 
     344    /** 
     345     * This interface contains the layer for menu commands of the MenuBar class 
     346     * . It's method will fire when the user clicks on the containing MenuItem. 
    256347     * The selected item is given as an argument. 
    257348     */ 
     
    281372         *  
    282373         * @param text 
    283          *                The text associated with the command 
     374         *            The text associated with the command 
    284375         * @param command 
    285          *                The command to be fired 
     376         *            The command to be fired 
    286377         * @throws IllegalArgumentException 
    287378         */ 
     
    310401         *  
    311402         * @param caption 
    312          *                the text for the menu item 
     403         *            the text for the menu item 
    313404         * @param icon 
    314          *                the icon for the menu item 
     405         *            the icon for the menu item 
    315406         * @param command 
    316          *                the command for the menu item 
     407         *            the command for the menu item 
    317408         */ 
    318409        public MenuBar.MenuItem addItem(String caption, Resource icon, 
     
    343434         *  
    344435         * @param caption 
    345          *                the text for the menu item 
     436         *            the text for the menu item 
    346437         * @param icon 
    347          *                the icon for the menu item 
     438         *            the icon for the menu item 
    348439         * @param command 
    349          *                the command for the menu item 
     440         *            the command for the menu item 
    350441         * @param itemToAddBefore 
    351          *                the item that will be after the new item 
     442         *            the item that will be after the new item 
    352443         *  
    353444         */ 
     
    440531         *  
    441532         * @param command 
    442          *                The MenuCommand of this item 
     533         *            The MenuCommand of this item 
    443534         */ 
    444535        public void setCommand(MenuBar.Command command) { 
     
    450541         *  
    451542         * @param icon 
    452          *                The icon for this item 
     543         *            The icon for this item 
    453544         */ 
    454545        public void setIcon(Resource icon) { 
     
    461552         *  
    462553         * @param text 
    463          *                Text for this object 
     554         *            Text for this object 
    464555         */ 
    465556        public void setText(java.lang.String text) { 
     
    474565         *  
    475566         * @param item 
    476          *                The item to be removed 
     567         *            The item to be removed 
    477568         */ 
    478569        public void removeChild(MenuBar.MenuItem item) { 
     
    501592         *  
    502593         * @param parent 
    503          *                The parent item 
     594         *            The parent item 
    504595         */ 
    505596        protected void setParent(MenuBar.MenuItem parent) {