選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VMenuBar.java 54KB

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