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

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