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

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