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

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