You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VMenuBar.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Stack;
  9. import com.google.gwt.dom.client.NodeList;
  10. import com.google.gwt.event.logical.shared.CloseEvent;
  11. import com.google.gwt.event.logical.shared.CloseHandler;
  12. import com.google.gwt.event.logical.shared.ValueChangeEvent;
  13. import com.google.gwt.event.logical.shared.ValueChangeHandler;
  14. import com.google.gwt.user.client.Command;
  15. import com.google.gwt.user.client.DOM;
  16. import com.google.gwt.user.client.DeferredCommand;
  17. import com.google.gwt.user.client.Element;
  18. import com.google.gwt.user.client.Event;
  19. import com.google.gwt.user.client.History;
  20. import com.google.gwt.user.client.Timer;
  21. import com.google.gwt.user.client.ui.HasHTML;
  22. import com.google.gwt.user.client.ui.PopupPanel;
  23. import com.google.gwt.user.client.ui.RootPanel;
  24. import com.google.gwt.user.client.ui.UIObject;
  25. import com.google.gwt.user.client.ui.Widget;
  26. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  27. import com.vaadin.terminal.gwt.client.BrowserInfo;
  28. import com.vaadin.terminal.gwt.client.ContainerResizedListener;
  29. import com.vaadin.terminal.gwt.client.Paintable;
  30. import com.vaadin.terminal.gwt.client.UIDL;
  31. import com.vaadin.terminal.gwt.client.Util;
  32. public class VMenuBar extends Widget implements Paintable,
  33. CloseHandler<PopupPanel>, ContainerResizedListener,
  34. ValueChangeHandler<String> {
  35. /** Set the CSS class name to allow styling. */
  36. public static final String CLASSNAME = "v-menubar";
  37. /** For server connections **/
  38. protected String uidlId;
  39. protected ApplicationConnection client;
  40. protected final VMenuBar hostReference = this;
  41. protected String submenuIcon = null;
  42. protected CustomMenuItem moreItem = null;
  43. protected VMenuBar collapsedRootItems;
  44. // Construct an empty command to be used when the item has no command
  45. // associated
  46. protected static final Command emptyCommand = null;
  47. /** Widget fields **/
  48. protected boolean subMenu;
  49. protected ArrayList<CustomMenuItem> items;
  50. protected Element containerElement;
  51. protected VOverlay popup;
  52. protected VMenuBar visibleChildMenu;
  53. protected boolean menuVisible = false;
  54. protected VMenuBar parentMenu;
  55. protected CustomMenuItem selected;
  56. private Timer layoutTimer;
  57. private boolean enabled = true;
  58. public VMenuBar() {
  59. // Create an empty horizontal menubar
  60. this(false);
  61. }
  62. public VMenuBar(boolean subMenu) {
  63. super();
  64. setElement(DOM.createDiv());
  65. items = new ArrayList<CustomMenuItem>();
  66. popup = null;
  67. visibleChildMenu = null;
  68. containerElement = getElement();
  69. if (!subMenu) {
  70. setStylePrimaryName(CLASSNAME);
  71. // Monitor back&forward buttons
  72. History.addValueChangeHandler(this);
  73. } else {
  74. setStylePrimaryName(CLASSNAME + "-submenu");
  75. }
  76. this.subMenu = subMenu;
  77. sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT
  78. | Event.ONLOAD);
  79. }
  80. @Override
  81. public void setWidth(String width) {
  82. Util.setWidthExcludingPaddingAndBorder(this, width, 0);
  83. if (!subMenu) {
  84. // Only needed for root level menu
  85. hideChildren();
  86. setSelected(null);
  87. menuVisible = false;
  88. }
  89. }
  90. /**
  91. * This method must be implemented to update the client-side component from
  92. * UIDL data received from server.
  93. *
  94. * This method is called when the page is loaded for the first time, and
  95. * every time UI changes in the component are received from the server.
  96. */
  97. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  98. // This call should be made first. Ensure correct implementation,
  99. // and let the containing layout manage caption, etc.
  100. if (client.updateComponent(this, uidl, true)) {
  101. return;
  102. }
  103. this.enabled = !uidl.getBooleanAttribute("disabled");
  104. // For future connections
  105. this.client = client;
  106. uidlId = uidl.getId();
  107. // Empty the menu every time it receives new information
  108. if (!getItems().isEmpty()) {
  109. clearItems();
  110. }
  111. UIDL options = uidl.getChildUIDL(0);
  112. // FIXME remove in version 7
  113. if (options.hasAttribute("submenuIcon")) {
  114. submenuIcon = client.translateVaadinUri(uidl.getChildUIDL(0)
  115. .getStringAttribute("submenuIcon"));
  116. } else {
  117. submenuIcon = null;
  118. }
  119. if (uidl.hasAttribute("width")) {
  120. UIDL moreItemUIDL = options.getChildUIDL(0);
  121. StringBuffer itemHTML = new StringBuffer();
  122. if (moreItemUIDL.hasAttribute("icon")) {
  123. itemHTML.append("<img src=\""
  124. + client.translateVaadinUri(moreItemUIDL
  125. .getStringAttribute("icon")) + "\" class=\""
  126. + Icon.CLASSNAME + "\" alt=\"\" />");
  127. }
  128. String moreItemText = moreItemUIDL.getStringAttribute("text");
  129. if ("".equals(moreItemText)) {
  130. moreItemText = "&#x25BA;";
  131. }
  132. itemHTML.append(moreItemText);
  133. moreItem = new CustomMenuItem(itemHTML.toString(), emptyCommand);
  134. collapsedRootItems = new VMenuBar(true);
  135. moreItem.setSubMenu(collapsedRootItems);
  136. moreItem.addStyleName(CLASSNAME + "-more-menuitem");
  137. }
  138. UIDL uidlItems = uidl.getChildUIDL(1);
  139. Iterator<Object> itr = uidlItems.getChildIterator();
  140. Stack<Iterator<Object>> iteratorStack = new Stack<Iterator<Object>>();
  141. Stack<VMenuBar> menuStack = new Stack<VMenuBar>();
  142. VMenuBar currentMenu = this;
  143. while (itr.hasNext()) {
  144. UIDL item = (UIDL) itr.next();
  145. CustomMenuItem currentItem = null;
  146. String itemText = item.getStringAttribute("text");
  147. final int itemId = item.getIntAttribute("id");
  148. boolean itemHasCommand = item.hasAttribute("command");
  149. // Construct html from the text and the optional icon
  150. StringBuffer itemHTML = new StringBuffer();
  151. Command cmd = null;
  152. if (item.hasAttribute("separator")) {
  153. itemHTML.append("<span>---</span>");
  154. } else {
  155. // Add submenu indicator
  156. if (item.getChildCount() > 0) {
  157. // FIXME For compatibility reasons: remove in version 7
  158. String bgStyle = "";
  159. if (submenuIcon != null) {
  160. bgStyle = " style=\"background-image: url("
  161. + submenuIcon
  162. + "); text-indent: -999px; width: 1em;\"";
  163. }
  164. itemHTML.append("<span class=\"" + CLASSNAME
  165. + "-submenu-indicator\"" + bgStyle
  166. + ">&#x25BA;</span>");
  167. }
  168. itemHTML.append("<span class=\"" + CLASSNAME
  169. + "-menuitem-caption\">");
  170. if (item.hasAttribute("icon")) {
  171. itemHTML
  172. .append("<img src=\""
  173. + client.translateVaadinUri(item
  174. .getStringAttribute("icon"))
  175. + "\" class=\"" + Icon.CLASSNAME
  176. + "\" alt=\"\" />");
  177. }
  178. itemHTML.append(Util.escapeHTML(itemText) + "</span>");
  179. if (itemHasCommand) {
  180. // Construct a command that fires onMenuClick(int) with the
  181. // item's id-number
  182. cmd = new Command() {
  183. public void execute() {
  184. hostReference.onMenuClick(itemId);
  185. }
  186. };
  187. }
  188. }
  189. currentItem = currentMenu.addItem(itemHTML.toString(), cmd);
  190. currentItem.setSeparator(item.hasAttribute("separator"));
  191. currentItem.setEnabled(!item.hasAttribute("disabled"));
  192. if (item.hasAttribute("style")) {
  193. String itemStyle = item.getStringAttribute("style");
  194. currentItem.addStyleDependentName(itemStyle);
  195. }
  196. if (item.getChildCount() > 0) {
  197. menuStack.push(currentMenu);
  198. iteratorStack.push(itr);
  199. itr = item.getChildIterator();
  200. currentMenu = new VMenuBar(true);
  201. if (uidl.hasAttribute("style")) {
  202. for (String style : uidl.getStringAttribute("style").split(
  203. " ")) {
  204. currentMenu.addStyleDependentName(style);
  205. }
  206. }
  207. currentItem.setSubMenu(currentMenu);
  208. }
  209. while (!itr.hasNext() && !iteratorStack.empty()) {
  210. itr = iteratorStack.pop();
  211. currentMenu = menuStack.pop();
  212. }
  213. }// while
  214. iLayout();
  215. }// updateFromUIDL
  216. /**
  217. * This is called by the items in the menu and it communicates the
  218. * information to the server
  219. *
  220. * @param clickedItemId
  221. * id of the item that was clicked
  222. */
  223. public void onMenuClick(int clickedItemId) {
  224. // Updating the state to the server can not be done before
  225. // the server connection is known, i.e., before updateFromUIDL()
  226. // has been called.
  227. if (uidlId != null && client != null) {
  228. // Communicate the user interaction parameters to server. This call
  229. // will initiate an AJAX request to the server.
  230. client.updateVariable(uidlId, "clickedId", clickedItemId, true);
  231. }
  232. }
  233. /** Widget methods **/
  234. /**
  235. * Returns a list of items in this menu
  236. */
  237. public List<CustomMenuItem> getItems() {
  238. return items;
  239. }
  240. /**
  241. * Remove all the items in this menu
  242. */
  243. public void clearItems() {
  244. Element e = getContainerElement();
  245. while (DOM.getChildCount(e) > 0) {
  246. DOM.removeChild(e, DOM.getChild(e, 0));
  247. }
  248. items.clear();
  249. }
  250. /**
  251. * Returns the containing element of the menu
  252. *
  253. * @return
  254. */
  255. public Element getContainerElement() {
  256. return containerElement;
  257. }
  258. /**
  259. * Add a new item to this menu
  260. *
  261. * @param html
  262. * items text
  263. * @param cmd
  264. * items command
  265. * @return the item created
  266. */
  267. public CustomMenuItem addItem(String html, Command cmd) {
  268. CustomMenuItem item = new CustomMenuItem(html, cmd);
  269. addItem(item);
  270. return item;
  271. }
  272. /**
  273. * Add a new item to this menu
  274. *
  275. * @param item
  276. */
  277. public void addItem(CustomMenuItem item) {
  278. if (items.contains(item)) {
  279. return;
  280. }
  281. DOM.appendChild(getContainerElement(), item.getElement());
  282. item.setParentMenu(this);
  283. item.setSelected(false);
  284. items.add(item);
  285. }
  286. public void addItem(CustomMenuItem item, int index) {
  287. if (items.contains(item)) {
  288. return;
  289. }
  290. DOM.insertChild(getContainerElement(), item.getElement(), index);
  291. item.setParentMenu(this);
  292. item.setSelected(false);
  293. items.add(index, item);
  294. }
  295. /**
  296. * Remove the given item from this menu
  297. *
  298. * @param item
  299. */
  300. public void removeItem(CustomMenuItem item) {
  301. if (items.contains(item)) {
  302. int index = items.indexOf(item);
  303. DOM.removeChild(getContainerElement(), DOM.getChild(
  304. getContainerElement(), index));
  305. items.remove(index);
  306. }
  307. }
  308. /*
  309. * @see
  310. * com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt.user
  311. * .client.Event)
  312. */
  313. @Override
  314. public void onBrowserEvent(Event e) {
  315. super.onBrowserEvent(e);
  316. // Handle onload events (icon loaded, size changes)
  317. if (DOM.eventGetType(e) == Event.ONLOAD) {
  318. requestLayout();
  319. return;
  320. }
  321. Element targetElement = DOM.eventGetTarget(e);
  322. CustomMenuItem targetItem = null;
  323. for (int i = 0; i < items.size(); i++) {
  324. CustomMenuItem item = items.get(i);
  325. if (DOM.isOrHasChild(item.getElement(), targetElement)) {
  326. targetItem = item;
  327. }
  328. }
  329. if (targetItem != null) {
  330. switch (DOM.eventGetType(e)) {
  331. case Event.ONCLICK:
  332. if (isEnabled() && targetItem.isEnabled()) {
  333. itemClick(targetItem);
  334. }
  335. break;
  336. case Event.ONMOUSEOVER:
  337. if (isEnabled() && targetItem.isEnabled()) {
  338. itemOver(targetItem);
  339. }
  340. break;
  341. case Event.ONMOUSEOUT:
  342. itemOut(targetItem);
  343. break;
  344. }
  345. }
  346. }
  347. private boolean isEnabled() {
  348. return enabled;
  349. }
  350. private void requestLayout() {
  351. if (layoutTimer == null) {
  352. layoutTimer = new Timer() {
  353. @Override
  354. public void run() {
  355. layoutTimer = null;
  356. iLayout();
  357. }
  358. };
  359. }
  360. layoutTimer.schedule(100);
  361. }
  362. /**
  363. * When an item is clicked
  364. *
  365. * @param item
  366. */
  367. public void itemClick(CustomMenuItem item) {
  368. if (item.getCommand() != null) {
  369. setSelected(null);
  370. if (visibleChildMenu != null) {
  371. visibleChildMenu.hideChildren();
  372. }
  373. hideParents(true);
  374. menuVisible = false;
  375. DeferredCommand.addCommand(item.getCommand());
  376. } else {
  377. if (item.getSubMenu() != null
  378. && item.getSubMenu() != visibleChildMenu) {
  379. setSelected(item);
  380. showChildMenu(item);
  381. menuVisible = true;
  382. } else if (!subMenu) {
  383. setSelected(null);
  384. hideChildren();
  385. menuVisible = false;
  386. }
  387. }
  388. }
  389. /**
  390. * When the user hovers the mouse over the item
  391. *
  392. * @param item
  393. */
  394. public void itemOver(CustomMenuItem item) {
  395. if ((subMenu || menuVisible) && !item.isSeparator()) {
  396. setSelected(item);
  397. }
  398. if (menuVisible && visibleChildMenu != item.getSubMenu()
  399. && popup != null) {
  400. popup.hide();
  401. }
  402. if (menuVisible && item.getSubMenu() != null
  403. && visibleChildMenu != item.getSubMenu()) {
  404. showChildMenu(item);
  405. }
  406. }
  407. /**
  408. * When the mouse is moved away from an item
  409. *
  410. * @param item
  411. */
  412. public void itemOut(CustomMenuItem item) {
  413. if (visibleChildMenu != item.getSubMenu()) {
  414. hideChildMenu(item);
  415. setSelected(null);
  416. } else if (visibleChildMenu == null) {
  417. setSelected(null);
  418. }
  419. }
  420. /**
  421. * Shows the child menu of an item. The caller must ensure that the item has
  422. * a submenu.
  423. *
  424. * @param item
  425. */
  426. public void showChildMenu(CustomMenuItem item) {
  427. final int shadowSpace = 10;
  428. popup = new VOverlay(true, false, true);
  429. popup.setStylePrimaryName(CLASSNAME + "-popup");
  430. popup.setWidget(item.getSubMenu());
  431. popup.addCloseHandler(this);
  432. popup.addAutoHidePartner(item.getElement());
  433. int left = 0;
  434. int top = 0;
  435. if (subMenu) {
  436. left = item.getParentMenu().getAbsoluteLeft()
  437. + item.getParentMenu().getOffsetWidth();
  438. top = item.getAbsoluteTop();
  439. } else {
  440. left = item.getAbsoluteLeft();
  441. top = item.getParentMenu().getAbsoluteTop()
  442. + item.getParentMenu().getOffsetHeight();
  443. }
  444. popup.setPopupPosition(left, top);
  445. item.getSubMenu().onShow();
  446. visibleChildMenu = item.getSubMenu();
  447. item.getSubMenu().setParentMenu(this);
  448. popup.show();
  449. if (left + popup.getOffsetWidth() >= RootPanel.getBodyElement()
  450. .getOffsetWidth()
  451. - shadowSpace) {
  452. if (subMenu) {
  453. left = item.getParentMenu().getAbsoluteLeft()
  454. - popup.getOffsetWidth() - shadowSpace;
  455. } else {
  456. left = RootPanel.getBodyElement().getOffsetWidth()
  457. - popup.getOffsetWidth() - shadowSpace;
  458. }
  459. // Accommodate space for shadow
  460. if (left < shadowSpace) {
  461. left = shadowSpace;
  462. }
  463. popup.setPopupPosition(left, top);
  464. }
  465. // IE7 really tests one's patience sometimes
  466. // Part of a fix to correct #3850
  467. if (BrowserInfo.get().isIE7()) {
  468. popup.getElement().getStyle().setProperty("zoom", "");
  469. DeferredCommand.addCommand(new Command() {
  470. public void execute() {
  471. if (popup.getElement().getStyle().getProperty("width") == null
  472. || popup.getElement().getStyle().getProperty(
  473. "width") == "") {
  474. popup.setWidth(popup.getOffsetWidth() + "px");
  475. }
  476. popup.getElement().getStyle().setProperty("zoom", "1");
  477. }
  478. });
  479. }
  480. }
  481. /**
  482. * Hides the submenu of an item
  483. *
  484. * @param item
  485. */
  486. public void hideChildMenu(CustomMenuItem item) {
  487. if (visibleChildMenu != null
  488. && !(visibleChildMenu == item.getSubMenu())) {
  489. popup.hide();
  490. }
  491. }
  492. /**
  493. * When the menu is shown.
  494. */
  495. public void onShow() {
  496. // remove possible previous selection
  497. if (selected != null) {
  498. selected.setSelected(false);
  499. selected = null;
  500. }
  501. menuVisible = true;
  502. }
  503. /**
  504. * Listener method, fired when this menu is closed
  505. */
  506. public void onClose(CloseEvent<PopupPanel> event) {
  507. hideChildren();
  508. if (event.isAutoClosed()) {
  509. hideParents(true);
  510. menuVisible = false;
  511. }
  512. visibleChildMenu = null;
  513. popup = null;
  514. }
  515. /**
  516. * Recursively hide all child menus
  517. */
  518. public void hideChildren() {
  519. if (visibleChildMenu != null) {
  520. visibleChildMenu.hideChildren();
  521. popup.hide();
  522. }
  523. }
  524. /**
  525. * Recursively hide all parent menus
  526. */
  527. public void hideParents(boolean autoClosed) {
  528. if (visibleChildMenu != null) {
  529. popup.hide();
  530. setSelected(null);
  531. menuVisible = !autoClosed;
  532. }
  533. if (getParentMenu() != null) {
  534. getParentMenu().hideParents(autoClosed);
  535. }
  536. }
  537. /**
  538. * Returns the parent menu of this menu, or null if this is the top-level
  539. * menu
  540. *
  541. * @return
  542. */
  543. public VMenuBar getParentMenu() {
  544. return parentMenu;
  545. }
  546. /**
  547. * Set the parent menu of this menu
  548. *
  549. * @param parent
  550. */
  551. public void setParentMenu(VMenuBar parent) {
  552. parentMenu = parent;
  553. }
  554. /**
  555. * Returns the currently selected item of this menu, or null if nothing is
  556. * selected
  557. *
  558. * @return
  559. */
  560. public CustomMenuItem getSelected() {
  561. return selected;
  562. }
  563. /**
  564. * Set the currently selected item of this menu
  565. *
  566. * @param item
  567. */
  568. public void setSelected(CustomMenuItem item) {
  569. // If we had something selected, unselect
  570. if (item != selected && selected != null) {
  571. selected.setSelected(false);
  572. }
  573. // If we have a valid selection, select it
  574. if (item != null) {
  575. item.setSelected(true);
  576. }
  577. selected = item;
  578. }
  579. /**
  580. *
  581. * A class to hold information on menu items
  582. *
  583. */
  584. private class CustomMenuItem extends UIObject implements HasHTML {
  585. protected String html = null;
  586. protected Command command = null;
  587. protected VMenuBar subMenu = null;
  588. protected VMenuBar parentMenu = null;
  589. protected boolean enabled = true;
  590. protected boolean isSeparator = false;
  591. public CustomMenuItem(String html, Command cmd) {
  592. // We need spans to allow inline-block in IE
  593. setElement(DOM.createSpan());
  594. setHTML(html);
  595. setCommand(cmd);
  596. setSelected(false);
  597. setStylePrimaryName(CLASSNAME + "-menuitem");
  598. }
  599. public void setSelected(boolean selected) {
  600. if (selected && !isSeparator) {
  601. addStyleDependentName("selected");
  602. } else {
  603. removeStyleDependentName("selected");
  604. }
  605. }
  606. /*
  607. * setters and getters for the fields
  608. */
  609. public void setSubMenu(VMenuBar subMenu) {
  610. this.subMenu = subMenu;
  611. }
  612. public VMenuBar getSubMenu() {
  613. return subMenu;
  614. }
  615. public void setParentMenu(VMenuBar parentMenu) {
  616. this.parentMenu = parentMenu;
  617. }
  618. public VMenuBar getParentMenu() {
  619. return parentMenu;
  620. }
  621. public void setCommand(Command command) {
  622. this.command = command;
  623. }
  624. public Command getCommand() {
  625. return command;
  626. }
  627. public String getHTML() {
  628. return html;
  629. }
  630. public void setHTML(String html) {
  631. this.html = html;
  632. DOM.setInnerHTML(getElement(), html);
  633. if (BrowserInfo.get().isIE6() && client != null) {
  634. // Find possible icon element
  635. final NodeList imgs = getElement().getElementsByTagName("IMG");
  636. if (imgs.getLength() > 0) {
  637. client.addPngFix((Element) imgs.getItem(0).cast());
  638. }
  639. }
  640. }
  641. public String getText() {
  642. return html;
  643. }
  644. public void setText(String text) {
  645. setHTML(Util.escapeHTML(text));
  646. }
  647. public void setEnabled(boolean enabled) {
  648. this.enabled = enabled;
  649. if (enabled) {
  650. removeStyleDependentName("disabled");
  651. } else {
  652. addStyleDependentName("disabled");
  653. }
  654. }
  655. public boolean isEnabled() {
  656. return enabled;
  657. }
  658. private void setSeparator(boolean separator) {
  659. isSeparator = separator;
  660. if (separator) {
  661. setStyleName(CLASSNAME + "-separator");
  662. } else {
  663. setStyleName(CLASSNAME + "-menuitem");
  664. setEnabled(enabled);
  665. }
  666. }
  667. public boolean isSeparator() {
  668. return isSeparator;
  669. }
  670. }
  671. /**
  672. * @author Jouni Koivuviita / IT Mill Ltd.
  673. */
  674. private int paddingWidth = -1;
  675. public void iLayout() {
  676. // Only collapse if there is more than one item in the root menu and the
  677. // menu has an explicit size
  678. if ((getItems().size() > 1 || (collapsedRootItems != null && collapsedRootItems
  679. .getItems().size() > 0))
  680. && getElement().getStyle().getProperty("width") != null
  681. && moreItem != null) {
  682. // Measure the width of the "more" item
  683. final boolean morePresent = getItems().contains(moreItem);
  684. addItem(moreItem);
  685. final int moreItemWidth = moreItem.getOffsetWidth();
  686. if (!morePresent) {
  687. removeItem(moreItem);
  688. }
  689. // Measure available space
  690. if (paddingWidth == -1) {
  691. int widthBefore = getElement().getClientWidth();
  692. getElement().getStyle().setProperty("padding", "0");
  693. paddingWidth = widthBefore - getElement().getClientWidth();
  694. getElement().getStyle().setProperty("padding", "");
  695. }
  696. String overflow = "";
  697. if (BrowserInfo.get().isIE6()) {
  698. // IE6 cannot measure available width correctly without
  699. // overflow:hidden
  700. overflow = getElement().getStyle().getProperty("overflow");
  701. getElement().getStyle().setProperty("overflow", "hidden");
  702. }
  703. int availableWidth = getElement().getClientWidth() - paddingWidth;
  704. if (BrowserInfo.get().isIE6()) {
  705. // IE6 cannot measure available width correctly without
  706. // overflow:hidden
  707. getElement().getStyle().setProperty("overflow", overflow);
  708. }
  709. int diff = availableWidth - getConsumedWidth();
  710. removeItem(moreItem);
  711. if (diff < 0) {
  712. // Too many items: collapse last items from root menu
  713. final int widthNeeded = moreItemWidth - diff;
  714. int widthReduced = 0;
  715. while (widthReduced < widthNeeded && getItems().size() > 0) {
  716. // Move last root menu item to collapsed menu
  717. CustomMenuItem collapse = getItems().get(
  718. getItems().size() - 1);
  719. widthReduced += collapse.getOffsetWidth();
  720. removeItem(collapse);
  721. collapsedRootItems.addItem(collapse, 0);
  722. }
  723. } else if (collapsedRootItems.getItems().size() > 0) {
  724. // Space available for items: expand first items from collapsed
  725. // menu
  726. int widthAvailable = diff + moreItemWidth;
  727. int widthGrowth = 0;
  728. while (widthAvailable > widthGrowth) {
  729. // Move first item from collapsed menu to the root menu
  730. CustomMenuItem expand = collapsedRootItems.getItems()
  731. .get(0);
  732. collapsedRootItems.removeItem(expand);
  733. addItem(expand);
  734. widthGrowth += expand.getOffsetWidth();
  735. if (collapsedRootItems.getItems().size() > 0) {
  736. widthAvailable -= moreItemWidth;
  737. }
  738. if (widthGrowth > widthAvailable) {
  739. removeItem(expand);
  740. collapsedRootItems.addItem(expand, 0);
  741. } else {
  742. widthAvailable = diff;
  743. }
  744. }
  745. }
  746. if (collapsedRootItems.getItems().size() > 0) {
  747. addItem(moreItem);
  748. }
  749. }
  750. }
  751. private int getConsumedWidth() {
  752. int w = 0;
  753. for (CustomMenuItem item : getItems()) {
  754. if (!collapsedRootItems.getItems().contains(item)) {
  755. w += item.getOffsetWidth();
  756. }
  757. }
  758. return w;
  759. }
  760. public void onValueChange(ValueChangeEvent<String> arg0) {
  761. // Close menu if user uses back & forward buttons #4109
  762. if (!subMenu) {
  763. setSelected(null);
  764. hideChildren();
  765. menuVisible = false;
  766. }
  767. }
  768. }