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 45KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui.menubar;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.google.gwt.core.client.GWT;
  8. import com.google.gwt.core.client.Scheduler;
  9. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  10. import com.google.gwt.dom.client.Style;
  11. import com.google.gwt.dom.client.Style.Overflow;
  12. import com.google.gwt.dom.client.Style.Unit;
  13. import com.google.gwt.event.dom.client.FocusEvent;
  14. import com.google.gwt.event.dom.client.FocusHandler;
  15. import com.google.gwt.event.dom.client.KeyCodes;
  16. import com.google.gwt.event.dom.client.KeyDownEvent;
  17. import com.google.gwt.event.dom.client.KeyDownHandler;
  18. import com.google.gwt.event.dom.client.KeyPressEvent;
  19. import com.google.gwt.event.dom.client.KeyPressHandler;
  20. import com.google.gwt.event.logical.shared.CloseEvent;
  21. import com.google.gwt.event.logical.shared.CloseHandler;
  22. import com.google.gwt.user.client.Command;
  23. import com.google.gwt.user.client.DOM;
  24. import com.google.gwt.user.client.Element;
  25. import com.google.gwt.user.client.Event;
  26. import com.google.gwt.user.client.Timer;
  27. import com.google.gwt.user.client.ui.HasHTML;
  28. import com.google.gwt.user.client.ui.PopupPanel;
  29. import com.google.gwt.user.client.ui.RootPanel;
  30. import com.google.gwt.user.client.ui.Widget;
  31. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  32. import com.vaadin.terminal.gwt.client.BrowserInfo;
  33. import com.vaadin.terminal.gwt.client.LayoutManager;
  34. import com.vaadin.terminal.gwt.client.TooltipInfo;
  35. import com.vaadin.terminal.gwt.client.UIDL;
  36. import com.vaadin.terminal.gwt.client.Util;
  37. import com.vaadin.terminal.gwt.client.ui.Icon;
  38. import com.vaadin.terminal.gwt.client.ui.SimpleFocusablePanel;
  39. import com.vaadin.terminal.gwt.client.ui.SubPartAware;
  40. import com.vaadin.terminal.gwt.client.ui.VLazyExecutor;
  41. import com.vaadin.terminal.gwt.client.ui.VOverlay;
  42. public class VMenuBar extends SimpleFocusablePanel implements
  43. CloseHandler<PopupPanel>, KeyPressHandler, KeyDownHandler,
  44. FocusHandler, SubPartAware {
  45. // The hierarchy of VMenuBar is a bit weird as VMenuBar is the Paintable,
  46. // used for the root menu but also used for the sub menus.
  47. /** Set the CSS class name to allow styling. */
  48. public static final String CLASSNAME = "v-menubar";
  49. /** For server connections **/
  50. protected String uidlId;
  51. protected ApplicationConnection client;
  52. protected final VMenuBar hostReference = this;
  53. protected CustomMenuItem moreItem = null;
  54. // Only used by the root menu bar
  55. protected VMenuBar collapsedRootItems;
  56. // Construct an empty command to be used when the item has no command
  57. // associated
  58. protected static final Command emptyCommand = null;
  59. public static final String OPEN_ROOT_MENU_ON_HOWER = "ormoh";
  60. public static final String ATTRIBUTE_CHECKED = "checked";
  61. public static final String ATTRIBUTE_ITEM_DESCRIPTION = "description";
  62. public static final String ATTRIBUTE_ITEM_ICON = "icon";
  63. public static final String ATTRIBUTE_ITEM_DISABLED = "disabled";
  64. public static final String ATTRIBUTE_ITEM_STYLE = "style";
  65. public static final String HTML_CONTENT_ALLOWED = "usehtml";
  66. /** Widget fields **/
  67. protected boolean subMenu;
  68. protected ArrayList<CustomMenuItem> items;
  69. protected Element containerElement;
  70. protected VOverlay popup;
  71. protected VMenuBar visibleChildMenu;
  72. protected boolean menuVisible = false;
  73. protected VMenuBar parentMenu;
  74. protected CustomMenuItem selected;
  75. boolean enabled = true;
  76. private VLazyExecutor iconLoadedExecutioner = new VLazyExecutor(100,
  77. new ScheduledCommand() {
  78. @Override
  79. public void execute() {
  80. iLayout(true);
  81. }
  82. });
  83. boolean openRootOnHover;
  84. boolean htmlContentAllowed;
  85. public VMenuBar() {
  86. // Create an empty horizontal menubar
  87. this(false, null);
  88. // Navigation is only handled by the root bar
  89. addFocusHandler(this);
  90. /*
  91. * Firefox auto-repeat works correctly only if we use a key press
  92. * handler, other browsers handle it correctly when using a key down
  93. * handler
  94. */
  95. if (BrowserInfo.get().isGecko()) {
  96. addKeyPressHandler(this);
  97. } else {
  98. addKeyDownHandler(this);
  99. }
  100. }
  101. public VMenuBar(boolean subMenu, VMenuBar parentMenu) {
  102. items = new ArrayList<CustomMenuItem>();
  103. popup = null;
  104. visibleChildMenu = null;
  105. containerElement = getElement();
  106. if (!subMenu) {
  107. setStyleName(CLASSNAME);
  108. } else {
  109. setStyleName(CLASSNAME + "-submenu");
  110. this.parentMenu = parentMenu;
  111. }
  112. this.subMenu = subMenu;
  113. sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT
  114. | Event.ONLOAD);
  115. }
  116. @Override
  117. protected void onDetach() {
  118. super.onDetach();
  119. if (!subMenu) {
  120. setSelected(null);
  121. hideChildren();
  122. menuVisible = false;
  123. }
  124. }
  125. void updateSize() {
  126. // Take from setWidth
  127. if (!subMenu) {
  128. // Only needed for root level menu
  129. hideChildren();
  130. setSelected(null);
  131. menuVisible = false;
  132. }
  133. }
  134. /**
  135. * Build the HTML content for a menu item.
  136. *
  137. * @param item
  138. * @return
  139. */
  140. protected String buildItemHTML(UIDL item) {
  141. // Construct html from the text and the optional icon
  142. StringBuffer itemHTML = new StringBuffer();
  143. if (item.hasAttribute("separator")) {
  144. itemHTML.append("<span>---</span>");
  145. } else {
  146. // Add submenu indicator
  147. if (item.getChildCount() > 0) {
  148. String bgStyle = "";
  149. itemHTML.append("<span class=\"" + CLASSNAME
  150. + "-submenu-indicator\"" + bgStyle + ">&#x25BA;</span>");
  151. }
  152. itemHTML.append("<span class=\"" + CLASSNAME
  153. + "-menuitem-caption\">");
  154. if (item.hasAttribute("icon")) {
  155. itemHTML.append("<img src=\""
  156. + Util.escapeAttribute(client.translateVaadinUri(item
  157. .getStringAttribute("icon"))) + "\" class=\""
  158. + Icon.CLASSNAME + "\" alt=\"\" />");
  159. }
  160. String itemText = item.getStringAttribute("text");
  161. if (!htmlContentAllowed) {
  162. itemText = Util.escapeHTML(itemText);
  163. }
  164. itemHTML.append(itemText);
  165. itemHTML.append("</span>");
  166. }
  167. return itemHTML.toString();
  168. }
  169. /**
  170. * This is called by the items in the menu and it communicates the
  171. * information to the server
  172. *
  173. * @param clickedItemId
  174. * id of the item that was clicked
  175. */
  176. public void onMenuClick(int clickedItemId) {
  177. // Updating the state to the server can not be done before
  178. // the server connection is known, i.e., before updateFromUIDL()
  179. // has been called.
  180. if (uidlId != null && client != null) {
  181. // Communicate the user interaction parameters to server. This call
  182. // will initiate an AJAX request to the server.
  183. client.updateVariable(uidlId, "clickedId", clickedItemId, true);
  184. }
  185. }
  186. /** Widget methods **/
  187. /**
  188. * Returns a list of items in this menu
  189. */
  190. public List<CustomMenuItem> getItems() {
  191. return items;
  192. }
  193. /**
  194. * Remove all the items in this menu
  195. */
  196. public void clearItems() {
  197. Element e = getContainerElement();
  198. while (DOM.getChildCount(e) > 0) {
  199. DOM.removeChild(e, DOM.getChild(e, 0));
  200. }
  201. items.clear();
  202. }
  203. /**
  204. * Returns the containing element of the menu
  205. *
  206. * @return
  207. */
  208. @Override
  209. public Element getContainerElement() {
  210. return containerElement;
  211. }
  212. /**
  213. * Add a new item to this menu
  214. *
  215. * @param html
  216. * items text
  217. * @param cmd
  218. * items command
  219. * @return the item created
  220. */
  221. public CustomMenuItem addItem(String html, Command cmd) {
  222. CustomMenuItem item = GWT.create(CustomMenuItem.class);
  223. item.setHTML(html);
  224. item.setCommand(cmd);
  225. addItem(item);
  226. return item;
  227. }
  228. /**
  229. * Add a new item to this menu
  230. *
  231. * @param item
  232. */
  233. public void addItem(CustomMenuItem item) {
  234. if (items.contains(item)) {
  235. return;
  236. }
  237. DOM.appendChild(getContainerElement(), item.getElement());
  238. item.setParentMenu(this);
  239. item.setSelected(false);
  240. items.add(item);
  241. }
  242. public void addItem(CustomMenuItem item, int index) {
  243. if (items.contains(item)) {
  244. return;
  245. }
  246. DOM.insertChild(getContainerElement(), item.getElement(), index);
  247. item.setParentMenu(this);
  248. item.setSelected(false);
  249. items.add(index, item);
  250. }
  251. /**
  252. * Remove the given item from this menu
  253. *
  254. * @param item
  255. */
  256. public void removeItem(CustomMenuItem item) {
  257. if (items.contains(item)) {
  258. int index = items.indexOf(item);
  259. DOM.removeChild(getContainerElement(),
  260. DOM.getChild(getContainerElement(), index));
  261. items.remove(index);
  262. }
  263. }
  264. /*
  265. * @see
  266. * com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt.user
  267. * .client.Event)
  268. */
  269. @Override
  270. public void onBrowserEvent(Event e) {
  271. super.onBrowserEvent(e);
  272. // Handle onload events (icon loaded, size changes)
  273. if (DOM.eventGetType(e) == Event.ONLOAD) {
  274. VMenuBar parent = getParentMenu();
  275. if (parent != null) {
  276. // The onload event for an image in a popup should be sent to
  277. // the parent, which owns the popup
  278. parent.iconLoaded();
  279. } else {
  280. // Onload events for images in the root menu are handled by the
  281. // root menu itself
  282. iconLoaded();
  283. }
  284. return;
  285. }
  286. Element targetElement = DOM.eventGetTarget(e);
  287. CustomMenuItem targetItem = null;
  288. for (int i = 0; i < items.size(); i++) {
  289. CustomMenuItem item = items.get(i);
  290. if (DOM.isOrHasChild(item.getElement(), targetElement)) {
  291. targetItem = item;
  292. }
  293. }
  294. if (targetItem != null) {
  295. switch (DOM.eventGetType(e)) {
  296. case Event.ONCLICK:
  297. if (isEnabled() && targetItem.isEnabled()) {
  298. itemClick(targetItem);
  299. }
  300. if (subMenu) {
  301. // Prevent moving keyboard focus to child menus
  302. VMenuBar parent = parentMenu;
  303. while (parent.getParentMenu() != null) {
  304. parent = parent.getParentMenu();
  305. }
  306. parent.setFocus(true);
  307. }
  308. break;
  309. case Event.ONMOUSEOVER:
  310. LazyCloser.cancelClosing();
  311. if (isEnabled() && targetItem.isEnabled()) {
  312. itemOver(targetItem);
  313. }
  314. break;
  315. case Event.ONMOUSEOUT:
  316. itemOut(targetItem);
  317. LazyCloser.schedule();
  318. break;
  319. }
  320. } else if (subMenu && DOM.eventGetType(e) == Event.ONCLICK && subMenu) {
  321. // Prevent moving keyboard focus to child menus
  322. VMenuBar parent = parentMenu;
  323. while (parent.getParentMenu() != null) {
  324. parent = parent.getParentMenu();
  325. }
  326. parent.setFocus(true);
  327. }
  328. }
  329. private boolean isEnabled() {
  330. return enabled;
  331. }
  332. private void iconLoaded() {
  333. iconLoadedExecutioner.trigger();
  334. }
  335. /**
  336. * When an item is clicked
  337. *
  338. * @param item
  339. */
  340. public void itemClick(CustomMenuItem item) {
  341. if (item.getCommand() != null) {
  342. setSelected(null);
  343. if (visibleChildMenu != null) {
  344. visibleChildMenu.hideChildren();
  345. }
  346. hideParents(true);
  347. menuVisible = false;
  348. Scheduler.get().scheduleDeferred(item.getCommand());
  349. } else {
  350. if (item.getSubMenu() != null
  351. && item.getSubMenu() != visibleChildMenu) {
  352. setSelected(item);
  353. showChildMenu(item);
  354. menuVisible = true;
  355. } else if (!subMenu) {
  356. setSelected(null);
  357. hideChildren();
  358. menuVisible = false;
  359. }
  360. }
  361. }
  362. /**
  363. * When the user hovers the mouse over the item
  364. *
  365. * @param item
  366. */
  367. public void itemOver(CustomMenuItem item) {
  368. if ((openRootOnHover || subMenu || menuVisible) && !item.isSeparator()) {
  369. setSelected(item);
  370. if (!subMenu && openRootOnHover && !menuVisible) {
  371. menuVisible = true; // start opening menus
  372. LazyCloser.prepare(this);
  373. }
  374. }
  375. if (menuVisible && visibleChildMenu != item.getSubMenu()
  376. && popup != null) {
  377. popup.hide();
  378. }
  379. if (menuVisible && item.getSubMenu() != null
  380. && visibleChildMenu != item.getSubMenu()) {
  381. showChildMenu(item);
  382. }
  383. }
  384. /**
  385. * When the mouse is moved away from an item
  386. *
  387. * @param item
  388. */
  389. public void itemOut(CustomMenuItem item) {
  390. if (visibleChildMenu != item.getSubMenu()) {
  391. hideChildMenu(item);
  392. setSelected(null);
  393. } else if (visibleChildMenu == null) {
  394. setSelected(null);
  395. }
  396. }
  397. /**
  398. * Used to autoclose submenus when they the menu is in a mode which opens
  399. * root menus on mouse hover.
  400. */
  401. private static class LazyCloser extends Timer {
  402. static LazyCloser INSTANCE;
  403. private VMenuBar activeRoot;
  404. @Override
  405. public void run() {
  406. activeRoot.hideChildren();
  407. activeRoot.setSelected(null);
  408. activeRoot.menuVisible = false;
  409. activeRoot = null;
  410. }
  411. public static void cancelClosing() {
  412. if (INSTANCE != null) {
  413. INSTANCE.cancel();
  414. }
  415. }
  416. public static void prepare(VMenuBar vMenuBar) {
  417. if (INSTANCE == null) {
  418. INSTANCE = new LazyCloser();
  419. }
  420. if (INSTANCE.activeRoot == vMenuBar) {
  421. INSTANCE.cancel();
  422. } else if (INSTANCE.activeRoot != null) {
  423. INSTANCE.cancel();
  424. INSTANCE.run();
  425. }
  426. INSTANCE.activeRoot = vMenuBar;
  427. }
  428. public static void schedule() {
  429. if (INSTANCE != null && INSTANCE.activeRoot != null) {
  430. INSTANCE.schedule(750);
  431. }
  432. }
  433. }
  434. /**
  435. * Shows the child menu of an item. The caller must ensure that the item has
  436. * a submenu.
  437. *
  438. * @param item
  439. */
  440. public void showChildMenu(CustomMenuItem item) {
  441. int left = 0;
  442. int top = 0;
  443. if (subMenu) {
  444. left = item.getParentMenu().getAbsoluteLeft()
  445. + item.getParentMenu().getOffsetWidth();
  446. top = item.getAbsoluteTop();
  447. } else {
  448. left = item.getAbsoluteLeft();
  449. top = item.getParentMenu().getAbsoluteTop()
  450. + item.getParentMenu().getOffsetHeight();
  451. }
  452. showChildMenuAt(item, top, left);
  453. }
  454. protected void showChildMenuAt(CustomMenuItem item, int top, int left) {
  455. final int shadowSpace = 10;
  456. popup = new VOverlay(true, false, true);
  457. // Setting owner and handlers to support tooltips. Needed for tooltip
  458. // handling of overlay widgets (will direct queries to parent menu)
  459. if (parentMenu == null) {
  460. popup.setOwner(this);
  461. } else {
  462. VMenuBar parent = parentMenu;
  463. while (parent.getParentMenu() != null) {
  464. parent = parent.getParentMenu();
  465. }
  466. popup.setOwner(parent);
  467. }
  468. if (client != null) {
  469. client.getVTooltip().connectHandlersToWidget(popup);
  470. }
  471. popup.setStyleName(CLASSNAME + "-popup");
  472. popup.setWidget(item.getSubMenu());
  473. popup.addCloseHandler(this);
  474. popup.addAutoHidePartner(item.getElement());
  475. // at 0,0 because otherwise IE7 add extra scrollbars (#5547)
  476. popup.setPopupPosition(0, 0);
  477. item.getSubMenu().onShow();
  478. visibleChildMenu = item.getSubMenu();
  479. item.getSubMenu().setParentMenu(this);
  480. popup.show();
  481. if (left + popup.getOffsetWidth() >= RootPanel.getBodyElement()
  482. .getOffsetWidth() - shadowSpace) {
  483. if (subMenu) {
  484. left = item.getParentMenu().getAbsoluteLeft()
  485. - popup.getOffsetWidth() - shadowSpace;
  486. } else {
  487. left = RootPanel.getBodyElement().getOffsetWidth()
  488. - popup.getOffsetWidth() - shadowSpace;
  489. }
  490. // Accommodate space for shadow
  491. if (left < shadowSpace) {
  492. left = shadowSpace;
  493. }
  494. }
  495. top = adjustPopupHeight(top, shadowSpace);
  496. popup.setPopupPosition(left, top);
  497. }
  498. private int adjustPopupHeight(int top, final int shadowSpace) {
  499. // Check that the popup will fit the screen
  500. int availableHeight = RootPanel.getBodyElement().getOffsetHeight()
  501. - top - shadowSpace;
  502. int missingHeight = popup.getOffsetHeight() - availableHeight;
  503. if (missingHeight > 0) {
  504. // First move the top of the popup to get more space
  505. // Don't move above top of screen, don't move more than needed
  506. int moveUpBy = Math.min(top - shadowSpace, missingHeight);
  507. // Update state
  508. top -= moveUpBy;
  509. missingHeight -= moveUpBy;
  510. availableHeight += moveUpBy;
  511. if (missingHeight > 0) {
  512. int contentWidth = visibleChildMenu.getOffsetWidth();
  513. // If there's still not enough room, limit height to fit and add
  514. // a scroll bar
  515. Style style = popup.getElement().getStyle();
  516. style.setHeight(availableHeight, Unit.PX);
  517. style.setOverflowY(Overflow.SCROLL);
  518. // Make room for the scroll bar by adjusting the width of the
  519. // popup
  520. style.setWidth(contentWidth + Util.getNativeScrollbarSize(),
  521. Unit.PX);
  522. popup.sizeOrPositionUpdated();
  523. }
  524. }
  525. return top;
  526. }
  527. /**
  528. * Hides the submenu of an item
  529. *
  530. * @param item
  531. */
  532. public void hideChildMenu(CustomMenuItem item) {
  533. if (visibleChildMenu != null
  534. && !(visibleChildMenu == item.getSubMenu())) {
  535. popup.hide();
  536. }
  537. }
  538. /**
  539. * When the menu is shown.
  540. */
  541. public void onShow() {
  542. // remove possible previous selection
  543. if (selected != null) {
  544. selected.setSelected(false);
  545. selected = null;
  546. }
  547. menuVisible = true;
  548. }
  549. /**
  550. * Listener method, fired when this menu is closed
  551. */
  552. @Override
  553. public void onClose(CloseEvent<PopupPanel> event) {
  554. hideChildren();
  555. if (event.isAutoClosed()) {
  556. hideParents(true);
  557. menuVisible = false;
  558. }
  559. visibleChildMenu = null;
  560. popup = null;
  561. }
  562. /**
  563. * Recursively hide all child menus
  564. */
  565. public void hideChildren() {
  566. if (visibleChildMenu != null) {
  567. visibleChildMenu.hideChildren();
  568. popup.hide();
  569. }
  570. }
  571. /**
  572. * Recursively hide all parent menus
  573. */
  574. public void hideParents(boolean autoClosed) {
  575. if (visibleChildMenu != null) {
  576. popup.hide();
  577. setSelected(null);
  578. menuVisible = !autoClosed;
  579. }
  580. if (getParentMenu() != null) {
  581. getParentMenu().hideParents(autoClosed);
  582. }
  583. }
  584. /**
  585. * Returns the parent menu of this menu, or null if this is the top-level
  586. * menu
  587. *
  588. * @return
  589. */
  590. public VMenuBar getParentMenu() {
  591. return parentMenu;
  592. }
  593. /**
  594. * Set the parent menu of this menu
  595. *
  596. * @param parent
  597. */
  598. public void setParentMenu(VMenuBar parent) {
  599. parentMenu = parent;
  600. }
  601. /**
  602. * Returns the currently selected item of this menu, or null if nothing is
  603. * selected
  604. *
  605. * @return
  606. */
  607. public CustomMenuItem getSelected() {
  608. return selected;
  609. }
  610. /**
  611. * Set the currently selected item of this menu
  612. *
  613. * @param item
  614. */
  615. public void setSelected(CustomMenuItem item) {
  616. // If we had something selected, unselect
  617. if (item != selected && selected != null) {
  618. selected.setSelected(false);
  619. }
  620. // If we have a valid selection, select it
  621. if (item != null) {
  622. item.setSelected(true);
  623. }
  624. selected = item;
  625. }
  626. /**
  627. *
  628. * A class to hold information on menu items
  629. *
  630. */
  631. public static class CustomMenuItem extends Widget implements HasHTML {
  632. protected String html = null;
  633. protected Command command = null;
  634. protected VMenuBar subMenu = null;
  635. protected VMenuBar parentMenu = null;
  636. protected boolean enabled = true;
  637. protected boolean isSeparator = false;
  638. protected boolean checkable = false;
  639. protected boolean checked = false;
  640. protected String description = null;
  641. /**
  642. * Default menu item {@link Widget} constructor for GWT.create().
  643. *
  644. * Use {@link #setHTML(String)} and {@link #setCommand(Command)} after
  645. * constructing a menu item.
  646. */
  647. public CustomMenuItem() {
  648. this("", null);
  649. }
  650. /**
  651. * Creates a menu item {@link Widget}.
  652. *
  653. * @param html
  654. * @param cmd
  655. * @deprecated use the default constructor and {@link #setHTML(String)}
  656. * and {@link #setCommand(Command)} instead
  657. */
  658. @Deprecated
  659. public CustomMenuItem(String html, Command cmd) {
  660. // We need spans to allow inline-block in IE
  661. setElement(DOM.createSpan());
  662. setHTML(html);
  663. setCommand(cmd);
  664. setSelected(false);
  665. setStyleName(CLASSNAME + "-menuitem");
  666. }
  667. public void setSelected(boolean selected) {
  668. if (selected && isSelectable()) {
  669. addStyleDependentName("selected");
  670. // needed for IE6 to have a single style name to match for an
  671. // element
  672. // TODO Can be optimized now that IE6 is not supported any more
  673. if (checkable) {
  674. if (checked) {
  675. removeStyleDependentName("selected-unchecked");
  676. addStyleDependentName("selected-checked");
  677. } else {
  678. removeStyleDependentName("selected-checked");
  679. addStyleDependentName("selected-unchecked");
  680. }
  681. }
  682. } else {
  683. removeStyleDependentName("selected");
  684. // needed for IE6 to have a single style name to match for an
  685. // element
  686. removeStyleDependentName("selected-checked");
  687. removeStyleDependentName("selected-unchecked");
  688. }
  689. }
  690. public void setChecked(boolean checked) {
  691. if (checkable && !isSeparator) {
  692. this.checked = checked;
  693. if (checked) {
  694. addStyleDependentName("checked");
  695. removeStyleDependentName("unchecked");
  696. } else {
  697. addStyleDependentName("unchecked");
  698. removeStyleDependentName("checked");
  699. }
  700. } else {
  701. this.checked = false;
  702. }
  703. }
  704. public boolean isChecked() {
  705. return checked;
  706. }
  707. public void setCheckable(boolean checkable) {
  708. if (checkable && !isSeparator) {
  709. this.checkable = true;
  710. } else {
  711. setChecked(false);
  712. this.checkable = false;
  713. }
  714. }
  715. public boolean isCheckable() {
  716. return checkable;
  717. }
  718. /*
  719. * setters and getters for the fields
  720. */
  721. public void setSubMenu(VMenuBar subMenu) {
  722. this.subMenu = subMenu;
  723. }
  724. public VMenuBar getSubMenu() {
  725. return subMenu;
  726. }
  727. public void setParentMenu(VMenuBar parentMenu) {
  728. this.parentMenu = parentMenu;
  729. }
  730. public VMenuBar getParentMenu() {
  731. return parentMenu;
  732. }
  733. public void setCommand(Command command) {
  734. this.command = command;
  735. }
  736. public Command getCommand() {
  737. return command;
  738. }
  739. @Override
  740. public String getHTML() {
  741. return html;
  742. }
  743. @Override
  744. public void setHTML(String html) {
  745. this.html = html;
  746. DOM.setInnerHTML(getElement(), html);
  747. // Sink the onload event for any icons. The onload
  748. // events are handled by the parent VMenuBar.
  749. Util.sinkOnloadForImages(getElement());
  750. }
  751. @Override
  752. public String getText() {
  753. return html;
  754. }
  755. @Override
  756. public void setText(String text) {
  757. setHTML(Util.escapeHTML(text));
  758. }
  759. public void setEnabled(boolean enabled) {
  760. this.enabled = enabled;
  761. if (enabled) {
  762. removeStyleDependentName("disabled");
  763. } else {
  764. addStyleDependentName("disabled");
  765. }
  766. }
  767. public boolean isEnabled() {
  768. return enabled;
  769. }
  770. private void setSeparator(boolean separator) {
  771. isSeparator = separator;
  772. if (separator) {
  773. setStyleName(CLASSNAME + "-separator");
  774. } else {
  775. setStyleName(CLASSNAME + "-menuitem");
  776. setEnabled(enabled);
  777. }
  778. }
  779. public boolean isSeparator() {
  780. return isSeparator;
  781. }
  782. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  783. setSeparator(uidl.hasAttribute("separator"));
  784. setEnabled(!uidl.hasAttribute(ATTRIBUTE_ITEM_DISABLED));
  785. if (!isSeparator() && uidl.hasAttribute(ATTRIBUTE_CHECKED)) {
  786. // if the selected attribute is present (either true or false),
  787. // the item is selectable
  788. setCheckable(true);
  789. setChecked(uidl.getBooleanAttribute(ATTRIBUTE_CHECKED));
  790. } else {
  791. setCheckable(false);
  792. }
  793. if (uidl.hasAttribute(ATTRIBUTE_ITEM_STYLE)) {
  794. String itemStyle = uidl
  795. .getStringAttribute(ATTRIBUTE_ITEM_STYLE);
  796. addStyleDependentName(itemStyle);
  797. }
  798. if (uidl.hasAttribute(ATTRIBUTE_ITEM_DESCRIPTION)) {
  799. description = uidl
  800. .getStringAttribute(ATTRIBUTE_ITEM_DESCRIPTION);
  801. }
  802. }
  803. public TooltipInfo getTooltip() {
  804. if (description == null) {
  805. return null;
  806. }
  807. return new TooltipInfo(description);
  808. }
  809. /**
  810. * Checks if the item can be selected.
  811. *
  812. * @return true if it is possible to select this item, false otherwise
  813. */
  814. public boolean isSelectable() {
  815. return !isSeparator() && isEnabled();
  816. }
  817. }
  818. /**
  819. * @author Jouni Koivuviita / Vaadin Ltd.
  820. */
  821. public void iLayout() {
  822. iLayout(false);
  823. updateSize();
  824. }
  825. public void iLayout(boolean iconLoadEvent) {
  826. // Only collapse if there is more than one item in the root menu and the
  827. // menu has an explicit size
  828. if ((getItems().size() > 1 || (collapsedRootItems != null && collapsedRootItems
  829. .getItems().size() > 0))
  830. && getElement().getStyle().getProperty("width") != null
  831. && moreItem != null) {
  832. // Measure the width of the "more" item
  833. final boolean morePresent = getItems().contains(moreItem);
  834. addItem(moreItem);
  835. final int moreItemWidth = moreItem.getOffsetWidth();
  836. if (!morePresent) {
  837. removeItem(moreItem);
  838. }
  839. int availableWidth = LayoutManager.get(client).getInnerWidth(
  840. getElement());
  841. // Used width includes the "more" item if present
  842. int usedWidth = getConsumedWidth();
  843. int diff = availableWidth - usedWidth;
  844. removeItem(moreItem);
  845. if (diff < 0) {
  846. // Too many items: collapse last items from root menu
  847. int widthNeeded = usedWidth - availableWidth;
  848. if (!morePresent) {
  849. widthNeeded += moreItemWidth;
  850. }
  851. int widthReduced = 0;
  852. while (widthReduced < widthNeeded && getItems().size() > 0) {
  853. // Move last root menu item to collapsed menu
  854. CustomMenuItem collapse = getItems().get(
  855. getItems().size() - 1);
  856. widthReduced += collapse.getOffsetWidth();
  857. removeItem(collapse);
  858. collapsedRootItems.addItem(collapse, 0);
  859. }
  860. } else if (collapsedRootItems.getItems().size() > 0) {
  861. // Space available for items: expand first items from collapsed
  862. // menu
  863. int widthAvailable = diff + moreItemWidth;
  864. int widthGrowth = 0;
  865. while (widthAvailable > widthGrowth
  866. && collapsedRootItems.getItems().size() > 0) {
  867. // Move first item from collapsed menu to the root menu
  868. CustomMenuItem expand = collapsedRootItems.getItems()
  869. .get(0);
  870. collapsedRootItems.removeItem(expand);
  871. addItem(expand);
  872. widthGrowth += expand.getOffsetWidth();
  873. if (collapsedRootItems.getItems().size() > 0) {
  874. widthAvailable -= moreItemWidth;
  875. }
  876. if (widthGrowth > widthAvailable) {
  877. removeItem(expand);
  878. collapsedRootItems.addItem(expand, 0);
  879. } else {
  880. widthAvailable = diff + moreItemWidth;
  881. }
  882. }
  883. }
  884. if (collapsedRootItems.getItems().size() > 0) {
  885. addItem(moreItem);
  886. }
  887. }
  888. // If a popup is open we might need to adjust the shadow as well if an
  889. // icon shown in that popup was loaded
  890. if (popup != null) {
  891. // Forces a recalculation of the shadow size
  892. popup.show();
  893. }
  894. if (iconLoadEvent) {
  895. // Size have changed if the width is undefined
  896. Util.notifyParentOfSizeChange(this, false);
  897. }
  898. }
  899. private int getConsumedWidth() {
  900. int w = 0;
  901. for (CustomMenuItem item : getItems()) {
  902. if (!collapsedRootItems.getItems().contains(item)) {
  903. w += item.getOffsetWidth();
  904. }
  905. }
  906. return w;
  907. }
  908. /*
  909. * (non-Javadoc)
  910. *
  911. * @see
  912. * com.google.gwt.event.dom.client.KeyPressHandler#onKeyPress(com.google
  913. * .gwt.event.dom.client.KeyPressEvent)
  914. */
  915. @Override
  916. public void onKeyPress(KeyPressEvent event) {
  917. if (handleNavigation(event.getNativeEvent().getKeyCode(),
  918. event.isControlKeyDown() || event.isMetaKeyDown(),
  919. event.isShiftKeyDown())) {
  920. event.preventDefault();
  921. }
  922. }
  923. /*
  924. * (non-Javadoc)
  925. *
  926. * @see
  927. * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt
  928. * .event.dom.client.KeyDownEvent)
  929. */
  930. @Override
  931. public void onKeyDown(KeyDownEvent event) {
  932. if (handleNavigation(event.getNativeEvent().getKeyCode(),
  933. event.isControlKeyDown() || event.isMetaKeyDown(),
  934. event.isShiftKeyDown())) {
  935. event.preventDefault();
  936. }
  937. }
  938. /**
  939. * Get the key that moves the selection upwards. By default it is the up
  940. * arrow key but by overriding this you can change the key to whatever you
  941. * want.
  942. *
  943. * @return The keycode of the key
  944. */
  945. protected int getNavigationUpKey() {
  946. return KeyCodes.KEY_UP;
  947. }
  948. /**
  949. * Get the key that moves the selection downwards. By default it is the down
  950. * arrow key but by overriding this you can change the key to whatever you
  951. * want.
  952. *
  953. * @return The keycode of the key
  954. */
  955. protected int getNavigationDownKey() {
  956. return KeyCodes.KEY_DOWN;
  957. }
  958. /**
  959. * Get the key that moves the selection left. By default it is the left
  960. * arrow key but by overriding this you can change the key to whatever you
  961. * want.
  962. *
  963. * @return The keycode of the key
  964. */
  965. protected int getNavigationLeftKey() {
  966. return KeyCodes.KEY_LEFT;
  967. }
  968. /**
  969. * Get the key that moves the selection right. By default it is the right
  970. * arrow key but by overriding this you can change the key to whatever you
  971. * want.
  972. *
  973. * @return The keycode of the key
  974. */
  975. protected int getNavigationRightKey() {
  976. return KeyCodes.KEY_RIGHT;
  977. }
  978. /**
  979. * Get the key that selects a menu item. By default it is the Enter key but
  980. * by overriding this you can change the key to whatever you want.
  981. *
  982. * @return
  983. */
  984. protected int getNavigationSelectKey() {
  985. return KeyCodes.KEY_ENTER;
  986. }
  987. /**
  988. * Get the key that closes the menu. By default it is the escape key but by
  989. * overriding this yoy can change the key to whatever you want.
  990. *
  991. * @return
  992. */
  993. protected int getCloseMenuKey() {
  994. return KeyCodes.KEY_ESCAPE;
  995. }
  996. /**
  997. * Handles the keyboard events handled by the MenuBar
  998. *
  999. * @param event
  1000. * The keyboard event received
  1001. * @return true iff the navigation event was handled
  1002. */
  1003. public boolean handleNavigation(int keycode, boolean ctrl, boolean shift) {
  1004. // If tab or shift+tab close menus
  1005. if (keycode == KeyCodes.KEY_TAB) {
  1006. setSelected(null);
  1007. hideChildren();
  1008. menuVisible = false;
  1009. return false;
  1010. }
  1011. if (ctrl || shift || !isEnabled()) {
  1012. // Do not handle tab key, nor ctrl keys
  1013. return false;
  1014. }
  1015. if (keycode == getNavigationLeftKey()) {
  1016. if (getSelected() == null) {
  1017. // If nothing is selected then select the last item
  1018. setSelected(items.get(items.size() - 1));
  1019. if (!getSelected().isSelectable()) {
  1020. handleNavigation(keycode, ctrl, shift);
  1021. }
  1022. } else if (visibleChildMenu == null && getParentMenu() == null) {
  1023. // If this is the root menu then move to the left
  1024. int idx = items.indexOf(getSelected());
  1025. if (idx > 0) {
  1026. setSelected(items.get(idx - 1));
  1027. } else {
  1028. setSelected(items.get(items.size() - 1));
  1029. }
  1030. if (!getSelected().isSelectable()) {
  1031. handleNavigation(keycode, ctrl, shift);
  1032. }
  1033. } else if (visibleChildMenu != null) {
  1034. // Redirect all navigation to the submenu
  1035. visibleChildMenu.handleNavigation(keycode, ctrl, shift);
  1036. } else if (getParentMenu().getParentMenu() == null) {
  1037. // Inside a sub menu, whose parent is a root menu item
  1038. VMenuBar root = getParentMenu();
  1039. root.getSelected().getSubMenu().setSelected(null);
  1040. root.hideChildren();
  1041. // Get the root menus items and select the previous one
  1042. int idx = root.getItems().indexOf(root.getSelected());
  1043. idx = idx > 0 ? idx : root.getItems().size();
  1044. CustomMenuItem selected = root.getItems().get(--idx);
  1045. while (selected.isSeparator() || !selected.isEnabled()) {
  1046. idx = idx > 0 ? idx : root.getItems().size();
  1047. selected = root.getItems().get(--idx);
  1048. }
  1049. root.setSelected(selected);
  1050. openMenuAndFocusFirstIfPossible(selected);
  1051. } else {
  1052. getParentMenu().getSelected().getSubMenu().setSelected(null);
  1053. getParentMenu().hideChildren();
  1054. }
  1055. return true;
  1056. } else if (keycode == getNavigationRightKey()) {
  1057. if (getSelected() == null) {
  1058. // If nothing is selected then select the first item
  1059. setSelected(items.get(0));
  1060. if (!getSelected().isSelectable()) {
  1061. handleNavigation(keycode, ctrl, shift);
  1062. }
  1063. } else if (visibleChildMenu == null && getParentMenu() == null) {
  1064. // If this is the root menu then move to the right
  1065. int idx = items.indexOf(getSelected());
  1066. if (idx < items.size() - 1) {
  1067. setSelected(items.get(idx + 1));
  1068. } else {
  1069. setSelected(items.get(0));
  1070. }
  1071. if (!getSelected().isSelectable()) {
  1072. handleNavigation(keycode, ctrl, shift);
  1073. }
  1074. } else if (visibleChildMenu == null
  1075. && getSelected().getSubMenu() != null) {
  1076. // If the item has a submenu then show it and move the selection
  1077. // there
  1078. showChildMenu(getSelected());
  1079. menuVisible = true;
  1080. visibleChildMenu.handleNavigation(keycode, ctrl, shift);
  1081. } else if (visibleChildMenu == null) {
  1082. // Get the root menu
  1083. VMenuBar root = getParentMenu();
  1084. while (root.getParentMenu() != null) {
  1085. root = root.getParentMenu();
  1086. }
  1087. // Hide the submenu
  1088. root.hideChildren();
  1089. // Get the root menus items and select the next one
  1090. int idx = root.getItems().indexOf(root.getSelected());
  1091. idx = idx < root.getItems().size() - 1 ? idx : -1;
  1092. CustomMenuItem selected = root.getItems().get(++idx);
  1093. while (selected.isSeparator() || !selected.isEnabled()) {
  1094. idx = idx < root.getItems().size() - 1 ? idx : -1;
  1095. selected = root.getItems().get(++idx);
  1096. }
  1097. root.setSelected(selected);
  1098. openMenuAndFocusFirstIfPossible(selected);
  1099. } else if (visibleChildMenu != null) {
  1100. // Redirect all navigation to the submenu
  1101. visibleChildMenu.handleNavigation(keycode, ctrl, shift);
  1102. }
  1103. return true;
  1104. } else if (keycode == getNavigationUpKey()) {
  1105. if (getSelected() == null) {
  1106. // If nothing is selected then select the last item
  1107. setSelected(items.get(items.size() - 1));
  1108. if (!getSelected().isSelectable()) {
  1109. handleNavigation(keycode, ctrl, shift);
  1110. }
  1111. } else if (visibleChildMenu != null) {
  1112. // Redirect all navigation to the submenu
  1113. visibleChildMenu.handleNavigation(keycode, ctrl, shift);
  1114. } else {
  1115. // Select the previous item if possible or loop to the last item
  1116. int idx = items.indexOf(getSelected());
  1117. if (idx > 0) {
  1118. setSelected(items.get(idx - 1));
  1119. } else {
  1120. setSelected(items.get(items.size() - 1));
  1121. }
  1122. if (!getSelected().isSelectable()) {
  1123. handleNavigation(keycode, ctrl, shift);
  1124. }
  1125. }
  1126. return true;
  1127. } else if (keycode == getNavigationDownKey()) {
  1128. if (getSelected() == null) {
  1129. // If nothing is selected then select the first item
  1130. selectFirstItem();
  1131. } else if (visibleChildMenu == null && getParentMenu() == null) {
  1132. // If this is the root menu the show the child menu with arrow
  1133. // down, if there is a child menu
  1134. openMenuAndFocusFirstIfPossible(getSelected());
  1135. } else if (visibleChildMenu != null) {
  1136. // Redirect all navigation to the submenu
  1137. visibleChildMenu.handleNavigation(keycode, ctrl, shift);
  1138. } else {
  1139. // Select the next item if possible or loop to the first item
  1140. int idx = items.indexOf(getSelected());
  1141. if (idx < items.size() - 1) {
  1142. setSelected(items.get(idx + 1));
  1143. } else {
  1144. setSelected(items.get(0));
  1145. }
  1146. if (!getSelected().isSelectable()) {
  1147. handleNavigation(keycode, ctrl, shift);
  1148. }
  1149. }
  1150. return true;
  1151. } else if (keycode == getCloseMenuKey()) {
  1152. setSelected(null);
  1153. hideChildren();
  1154. menuVisible = false;
  1155. } else if (keycode == getNavigationSelectKey()) {
  1156. if (getSelected() == null) {
  1157. // If nothing is selected then select the first item
  1158. selectFirstItem();
  1159. } else if (visibleChildMenu != null) {
  1160. // Redirect all navigation to the submenu
  1161. visibleChildMenu.handleNavigation(keycode, ctrl, shift);
  1162. menuVisible = false;
  1163. } else if (visibleChildMenu == null
  1164. && getSelected().getSubMenu() != null) {
  1165. // If the item has a sub menu then show it and move the
  1166. // selection there
  1167. openMenuAndFocusFirstIfPossible(getSelected());
  1168. } else {
  1169. Command command = getSelected().getCommand();
  1170. if (command != null) {
  1171. command.execute();
  1172. }
  1173. setSelected(null);
  1174. hideParents(true);
  1175. }
  1176. }
  1177. return false;
  1178. }
  1179. private void selectFirstItem() {
  1180. for (int i = 0; i < items.size(); i++) {
  1181. CustomMenuItem item = items.get(i);
  1182. if (item.isSelectable()) {
  1183. setSelected(item);
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. private void openMenuAndFocusFirstIfPossible(CustomMenuItem menuItem) {
  1189. VMenuBar subMenu = menuItem.getSubMenu();
  1190. if (subMenu == null) {
  1191. // No child menu? Nothing to do
  1192. return;
  1193. }
  1194. VMenuBar parentMenu = menuItem.getParentMenu();
  1195. parentMenu.showChildMenu(menuItem);
  1196. menuVisible = true;
  1197. // Select the first item in the newly open submenu
  1198. subMenu.selectFirstItem();
  1199. }
  1200. /*
  1201. * (non-Javadoc)
  1202. *
  1203. * @see
  1204. * com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event
  1205. * .dom.client.FocusEvent)
  1206. */
  1207. @Override
  1208. public void onFocus(FocusEvent event) {
  1209. }
  1210. private final String SUBPART_PREFIX = "item";
  1211. @Override
  1212. public Element getSubPartElement(String subPart) {
  1213. int index = Integer
  1214. .parseInt(subPart.substring(SUBPART_PREFIX.length()));
  1215. CustomMenuItem item = getItems().get(index);
  1216. return item.getElement();
  1217. }
  1218. @Override
  1219. public String getSubPartName(Element subElement) {
  1220. if (!getElement().isOrHasChild(subElement)) {
  1221. return null;
  1222. }
  1223. Element menuItemRoot = subElement;
  1224. while (menuItemRoot != null && menuItemRoot.getParentElement() != null
  1225. && menuItemRoot.getParentElement() != getElement()) {
  1226. menuItemRoot = menuItemRoot.getParentElement().cast();
  1227. }
  1228. // "menuItemRoot" is now the root of the menu item
  1229. final int itemCount = getItems().size();
  1230. for (int i = 0; i < itemCount; i++) {
  1231. if (getItems().get(i).getElement() == menuItemRoot) {
  1232. String name = SUBPART_PREFIX + i;
  1233. return name;
  1234. }
  1235. }
  1236. return null;
  1237. }
  1238. /**
  1239. * Get menu item with given DOM element
  1240. *
  1241. * @param element
  1242. * Element used in search
  1243. * @return Menu item or null if not found
  1244. */
  1245. public CustomMenuItem getMenuItemWithElement(Element element) {
  1246. for (int i = 0; i < items.size(); i++) {
  1247. CustomMenuItem item = items.get(i);
  1248. if (DOM.isOrHasChild(item.getElement(), element)) {
  1249. return item;
  1250. }
  1251. if (item.getSubMenu() != null) {
  1252. item = item.getSubMenu().getMenuItemWithElement(element);
  1253. if (item != null) {
  1254. return item;
  1255. }
  1256. }
  1257. }
  1258. return null;
  1259. }
  1260. }