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

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