- Timestamp:
- 07/22/08 05:42:33 (4 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/itmill/toolkit/terminal/gwt/client/ui/IMenuBar.java
r4955 r5109 1 1 package com.itmill.toolkit.terminal.gwt.client.ui; 2 2 3 import java.util.ArrayList; 3 4 import java.util.Iterator; 4 5 import java.util.Stack; 5 6 6 7 import com.google.gwt.user.client.Command; 8 import com.google.gwt.user.client.DOM; 7 9 import com.google.gwt.user.client.ui.MenuBar; 8 10 import com.google.gwt.user.client.ui.MenuItem; … … 16 18 public static final String CLASSNAME = "i-menubar"; 17 19 18 /** Component identifier in UIDL communications.*/20 /** For server connections **/ 19 21 protected String uidlId; 20 21 /** Reference to the server connection object. */22 22 protected ApplicationConnection client; 23 23 24 /** A host reference for the Command objects */25 24 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; 26 34 27 35 /** … … 30 38 */ 31 39 public IMenuBar() { 32 // The superclass has a lot of relevant initialization40 // Create an empty horizontal menubar 33 41 super(); 42 DOM.setStyleAttribute(this.getElement(), "white-space", "nowrap"); 34 43 35 44 // This method call of the Paintable interface sets the component 36 45 // style name in DOM tree 37 46 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"); 38 56 } 39 57 … … 52 70 } 53 71 54 // Save reference to server connection object to be able to send 55 // user interaction later 72 // For future connections 56 73 this.client = client; 57 58 // Save the UIDL identifier for the component59 74 uidlId = uidl.getId(); 60 75 … … 64 79 } 65 80 66 /* Get tree received from server and actualize it in the GWT-MenuBar */ 67 81 UIDL options = uidl.getChildUIDL(0); 68 82 // 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 70 111 UIDL items = uidl.getChildUIDL(1); 71 112 Iterator itr = items.getChildIterator(); … … 74 115 MenuBar currentMenu = this; 75 116 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; 82 118 83 119 while (itr.hasNext()) { 84 120 UIDL item = (UIDL) itr.next(); 85 MenuItem menuItem = null; // For receiving the item121 MenuItem currentItem = null; // For receiving the item 86 122 87 123 String itemText = item.getStringAttribute("text"); … … 91 127 92 128 // 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=\"" 98 135 + 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 104 153 if (itemHasCommand) { 105 154 // Construct a command that fires onMenuClick(int) with the 106 155 // item's id-number 107 Command normalCommand = new Command() {156 cmd = new Command() { 108 157 public void execute() { 109 158 hostReference.onMenuClick(itemId); 110 159 } 111 160 }; 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); 118 164 119 165 if (item.getChildCount() > 0) { … … 121 167 iteratorStack.push(itr); 122 168 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()) { 128 174 itr = (Iterator) iteratorStack.pop(); 129 175 currentMenu = (MenuBar) menuStack.pop(); … … 131 177 }// while 132 178 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 } 133 219 }// updateFromUIDL 134 220 … … 138 224 * 139 225 * @param clickedItemId 140 * id of the item that was clicked226 * id of the item that was clicked 141 227 */ 142 228 public void onMenuClick(int clickedItemId) { … … 150 236 } 151 237 } 238 152 239 }// class IMenuBar
