Changeset 5109
- Timestamp:
- 07/22/08 05:42:33 (7 weeks ago)
- Location:
- trunk/src/com/itmill/toolkit
- Files:
-
- 2 modified
-
terminal/gwt/client/ui/IMenuBar.java (modified) (11 diffs)
-
ui/MenuBar.java (modified) (19 diffs)
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 -
trunk/src/com/itmill/toolkit/ui/MenuBar.java
r4993 r5109 25 25 26 26 private boolean animationEnabled; 27 private boolean collapseItems; 28 private Resource submenuIcon; 29 private MenuItem moreItem; 27 30 28 31 /** Tag is the UIDL element name for client-server communications. */ … … 42 45 target.startTag("options"); 43 46 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 44 63 target.endTag("options"); 45 64 target.startTag("items"); … … 78 97 } 79 98 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()) { 81 101 itr = (Iterator) iteratorStack.pop(); 82 102 target.endTag("item"); … … 115 135 } 116 136 117 } 137 }// while 138 118 139 // If we got the clicked item, launch the command. 119 140 if (found) { 120 141 tmpItem.getCommand().menuSelected(tmpItem); 121 } // while142 } 122 143 }// if 123 144 }// changeVariables … … 128 149 public MenuBar() { 129 150 menuItems = new ArrayList(); 130 animationEnabled = false; 151 setAnimation(false); 152 setCollapse(true); 153 setMoreMenuItem(null); 131 154 } 132 155 … … 136 159 * 137 160 * @param caption 138 * the text for the menu item161 * the text for the menu item 139 162 * @param icon 140 * the icon for the menu item163 * the icon for the menu item 141 164 * @param command 142 * the command for the menu item165 * the command for the menu item 143 166 * @throws IllegalArgumentException 144 167 */ … … 162 185 * 163 186 * @param caption 164 * the text for the menu item187 * the text for the menu item 165 188 * @param icon 166 * the icon for the menu item189 * the icon for the menu item 167 190 * @param command 168 * the command for the menu item191 * the command for the menu item 169 192 * @param itemToAddBefore 170 * the item that will be after the new item193 * the item that will be after the new item 171 194 * @throws IllegalArgumentException 172 195 */ … … 204 227 * 205 228 * @param item 206 * The item to be removed229 * The item to be removed 207 230 */ 208 231 public void removeItem(MenuBar.MenuItem item) { … … 232 255 /** 233 256 * Enable or disable animated menubar appearance. Animation is disabled by 234 * default. 257 * default. Currently does nothing. 235 258 * 236 259 * @param hasAnimation … … 238 261 public void setAnimation(boolean animation) { 239 262 animationEnabled = animation; 263 requestRepaint(); 240 264 } 241 265 … … 252 276 253 277 /** 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. 256 347 * The selected item is given as an argument. 257 348 */ … … 281 372 * 282 373 * @param text 283 * The text associated with the command374 * The text associated with the command 284 375 * @param command 285 * The command to be fired376 * The command to be fired 286 377 * @throws IllegalArgumentException 287 378 */ … … 310 401 * 311 402 * @param caption 312 * the text for the menu item403 * the text for the menu item 313 404 * @param icon 314 * the icon for the menu item405 * the icon for the menu item 315 406 * @param command 316 * the command for the menu item407 * the command for the menu item 317 408 */ 318 409 public MenuBar.MenuItem addItem(String caption, Resource icon, … … 343 434 * 344 435 * @param caption 345 * the text for the menu item436 * the text for the menu item 346 437 * @param icon 347 * the icon for the menu item438 * the icon for the menu item 348 439 * @param command 349 * the command for the menu item440 * the command for the menu item 350 441 * @param itemToAddBefore 351 * the item that will be after the new item442 * the item that will be after the new item 352 443 * 353 444 */ … … 440 531 * 441 532 * @param command 442 * The MenuCommand of this item533 * The MenuCommand of this item 443 534 */ 444 535 public void setCommand(MenuBar.Command command) { … … 450 541 * 451 542 * @param icon 452 * The icon for this item543 * The icon for this item 453 544 */ 454 545 public void setIcon(Resource icon) { … … 461 552 * 462 553 * @param text 463 * Text for this object554 * Text for this object 464 555 */ 465 556 public void setText(java.lang.String text) { … … 474 565 * 475 566 * @param item 476 * The item to be removed567 * The item to be removed 477 568 */ 478 569 public void removeChild(MenuBar.MenuItem item) { … … 501 592 * 502 593 * @param parent 503 * The parent item594 * The parent item 504 595 */ 505 596 protected void setParent(MenuBar.MenuItem parent) {
