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

Adds #1946 collapsing + submenu icons.

Files:
1 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