You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VMenuBar.java 49KB

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