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

VMenuBar.java 55KB

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