您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VMenuBar.java 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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.Style;
  23. import com.google.gwt.dom.client.Style.Overflow;
  24. import com.google.gwt.dom.client.Style.Unit;
  25. import com.google.gwt.event.dom.client.FocusEvent;
  26. import com.google.gwt.event.dom.client.FocusHandler;
  27. import com.google.gwt.event.dom.client.KeyCodes;
  28. import com.google.gwt.event.dom.client.KeyDownEvent;
  29. import com.google.gwt.event.dom.client.KeyDownHandler;
  30. import com.google.gwt.event.dom.client.KeyPressEvent;
  31. import com.google.gwt.event.dom.client.KeyPressHandler;
  32. import com.google.gwt.event.logical.shared.CloseEvent;
  33. import com.google.gwt.event.logical.shared.CloseHandler;
  34. import com.google.gwt.user.client.Command;
  35. import com.google.gwt.user.client.DOM;
  36. import com.google.gwt.user.client.Event;
  37. import com.google.gwt.user.client.Timer;
  38. import com.google.gwt.user.client.ui.HasHTML;
  39. import com.google.gwt.user.client.ui.PopupPanel;
  40. import com.google.gwt.user.client.ui.RootPanel;
  41. import com.google.gwt.user.client.ui.Widget;
  42. import com.vaadin.client.ApplicationConnection;
  43. import com.vaadin.client.BrowserInfo;
  44. import com.vaadin.client.LayoutManager;
  45. import com.vaadin.client.TooltipInfo;
  46. import com.vaadin.client.UIDL;
  47. import com.vaadin.client.Util;
  48. import com.vaadin.shared.ui.menubar.MenuBarConstants;
  49. public class VMenuBar extends SimpleFocusablePanel implements
  50. CloseHandler<PopupPanel>, KeyPressHandler, KeyDownHandler,
  51. FocusHandler, SubPartAware {
  52. // The hierarchy of VMenuBar is a bit weird as VMenuBar is the Paintable,
  53. // used for the root menu but also used for the sub menus.
  54. /** Set the CSS class name to allow styling. */
  55. public static final String CLASSNAME = "v-menubar";
  56. public static final String SUBMENU_CLASSNAME_PREFIX = "-submenu";
  57. /**
  58. * For server connections.
  59. * <p>
  60. * For internal use only. May be removed or replaced in the future.
  61. */
  62. public String uidlId;
  63. /** For internal use only. May be removed or replaced in the future. */
  64. public ApplicationConnection client;
  65. /** For internal use only. May be removed or replaced in the future. */
  66. public final VMenuBar hostReference = this;
  67. /** For internal use only. May be removed or replaced in the future. */
  68. public CustomMenuItem moreItem = null;
  69. /** For internal use only. May be removed or replaced in the future. */
  70. public VMenuBar collapsedRootItems;
  71. /**
  72. * An empty command to be used when the item has no command associated
  73. * <p>
  74. * For internal use only. May be removed or replaced in the future.
  75. */
  76. public static final Command emptyCommand = null;
  77. /** Widget fields **/
  78. protected boolean subMenu;
  79. protected ArrayList<CustomMenuItem> items;
  80. protected com.google.gwt.user.client.Element containerElement;
  81. protected VOverlay popup;
  82. protected VMenuBar visibleChildMenu;
  83. protected boolean menuVisible = false;
  84. protected VMenuBar parentMenu;
  85. protected CustomMenuItem selected;
  86. /** For internal use only. May be removed or replaced in the future. */
  87. public boolean enabled = true;
  88. private VLazyExecutor iconLoadedExecutioner = new VLazyExecutor(100,
  89. new ScheduledCommand() {
  90. @Override
  91. public void execute() {
  92. iLayout(true);
  93. }
  94. });
  95. /** For internal use only. May be removed or replaced in the future. */
  96. public boolean openRootOnHover;
  97. /** For internal use only. May be removed or replaced in the future. */
  98. public boolean htmlContentAllowed;
  99. public VMenuBar() {
  100. // Create an empty horizontal menubar
  101. this(false, null);
  102. // Navigation is only handled by the root bar
  103. addFocusHandler(this);
  104. /*
  105. * Firefox auto-repeat works correctly only if we use a key press
  106. * handler, other browsers handle it correctly when using a key down
  107. * handler
  108. */
  109. if (BrowserInfo.get().isGecko()) {
  110. addKeyPressHandler(this);
  111. } else {
  112. addKeyDownHandler(this);
  113. }
  114. }
  115. public VMenuBar(boolean subMenu, VMenuBar parentMenu) {
  116. items = new ArrayList<CustomMenuItem>();
  117. popup = null;
  118. visibleChildMenu = null;
  119. this.subMenu = subMenu;
  120. containerElement = getElement();
  121. sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT
  122. | Event.ONLOAD);
  123. if (parentMenu == null) {
  124. // Root menu
  125. setStyleName(CLASSNAME);
  126. } else {
  127. // Child menus inherits style name
  128. setStyleName(parentMenu.getStyleName());
  129. }
  130. }
  131. @Override
  132. public void setStyleName(String style) {
  133. super.setStyleName(style);
  134. updateStyleNames();
  135. }
  136. @Override
  137. public void setStylePrimaryName(String style) {
  138. super.setStylePrimaryName(style);
  139. updateStyleNames();
  140. }
  141. protected void updateStyleNames() {
  142. String primaryStyleName = getParentMenu() != null ? getParentMenu()
  143. .getStylePrimaryName() : getStylePrimaryName();
  144. // Reset the style name for all the items
  145. for (CustomMenuItem item : items) {
  146. item.setStyleName(primaryStyleName + "-menuitem");
  147. }
  148. if (subMenu
  149. && !getStylePrimaryName().endsWith(SUBMENU_CLASSNAME_PREFIX)) {
  150. /*
  151. * Sub-menus should get the sub-menu prefix
  152. */
  153. super.setStylePrimaryName(primaryStyleName
  154. + SUBMENU_CLASSNAME_PREFIX);
  155. }
  156. }
  157. @Override
  158. protected void onDetach() {
  159. super.onDetach();
  160. if (!subMenu) {
  161. setSelected(null);
  162. hideChildren();
  163. menuVisible = false;
  164. }
  165. }
  166. void updateSize() {
  167. // Take from setWidth
  168. if (!subMenu) {
  169. // Only needed for root level menu
  170. hideChildren();
  171. setSelected(null);
  172. menuVisible = false;
  173. }
  174. }
  175. /**
  176. * Build the HTML content for a menu item.
  177. * <p>
  178. * For internal use only. May be removed or replaced in the future.
  179. */
  180. public String buildItemHTML(UIDL item) {
  181. // Construct html from the text and the optional icon
  182. StringBuffer itemHTML = new StringBuffer();
  183. if (item.hasAttribute("separator")) {
  184. itemHTML.append("<span>---</span>");
  185. } else {
  186. // Add submenu indicator
  187. if (item.getChildCount() > 0) {
  188. String bgStyle = "";
  189. itemHTML.append("<span class=\"" + getStylePrimaryName()
  190. + "-submenu-indicator\"" + bgStyle + ">&#x25BA;</span>");
  191. }
  192. itemHTML.append("<span class=\"" + getStylePrimaryName()
  193. + "-menuitem-caption\">");
  194. Icon icon = client.getIcon(item.getStringAttribute("icon"));
  195. if (icon != null) {
  196. itemHTML.append(icon.getElement().getString());
  197. }
  198. String itemText = item.getStringAttribute("text");
  199. if (!htmlContentAllowed) {
  200. itemText = Util.escapeHTML(itemText);
  201. }
  202. itemHTML.append(itemText);
  203. itemHTML.append("</span>");
  204. }
  205. return itemHTML.toString();
  206. }
  207. /**
  208. * This is called by the items in the menu and it communicates the
  209. * information to the server
  210. *
  211. * @param clickedItemId
  212. * id of the item that was clicked
  213. */
  214. public void onMenuClick(int clickedItemId) {
  215. // Updating the state to the server can not be done before
  216. // the server connection is known, i.e., before updateFromUIDL()
  217. // has been called.
  218. if (uidlId != null && client != null) {
  219. // Communicate the user interaction parameters to server. This call
  220. // will initiate an AJAX request to the server.
  221. client.updateVariable(uidlId, "clickedId", clickedItemId, true);
  222. }
  223. }
  224. /** Widget methods **/
  225. /**
  226. * Returns a list of items in this menu
  227. */
  228. public List<CustomMenuItem> getItems() {
  229. return items;
  230. }
  231. /**
  232. * Remove all the items in this menu
  233. */
  234. public void clearItems() {
  235. com.google.gwt.user.client.Element e = getContainerElement();
  236. while (DOM.getChildCount(e) > 0) {
  237. DOM.removeChild(e, DOM.getChild(e, 0));
  238. }
  239. items.clear();
  240. }
  241. /**
  242. * Returns the containing element of the menu
  243. *
  244. * @return
  245. */
  246. @Override
  247. public com.google.gwt.user.client.Element getContainerElement() {
  248. return containerElement;
  249. }
  250. /**
  251. * Add a new item to this menu
  252. *
  253. * @param html
  254. * items text
  255. * @param cmd
  256. * items command
  257. * @return the item created
  258. */
  259. public CustomMenuItem addItem(String html, Command cmd) {
  260. CustomMenuItem item = GWT.create(CustomMenuItem.class);
  261. item.setHTML(html);
  262. item.setCommand(cmd);
  263. addItem(item);
  264. return item;
  265. }
  266. /**
  267. * Add a new item to this menu
  268. *
  269. * @param item
  270. */
  271. public void addItem(CustomMenuItem item) {
  272. if (items.contains(item)) {
  273. return;
  274. }
  275. DOM.appendChild(getContainerElement(), item.getElement());
  276. item.setParentMenu(this);
  277. item.setSelected(false);
  278. items.add(item);
  279. }
  280. public void addItem(CustomMenuItem item, int index) {
  281. if (items.contains(item)) {
  282. return;
  283. }
  284. DOM.insertChild(getContainerElement(), item.getElement(), index);
  285. item.setParentMenu(this);
  286. item.setSelected(false);
  287. items.add(index, item);
  288. }
  289. /**
  290. * Remove the given item from this menu
  291. *
  292. * @param item
  293. */
  294. public void removeItem(CustomMenuItem item) {
  295. if (items.contains(item)) {
  296. int index = items.indexOf(item);
  297. DOM.removeChild(getContainerElement(),
  298. DOM.getChild(getContainerElement(), index));
  299. items.remove(index);
  300. }
  301. }
  302. /*
  303. * @see
  304. * com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt.user
  305. * .client.Event)
  306. */
  307. @Override
  308. public void onBrowserEvent(Event e) {
  309. super.onBrowserEvent(e);
  310. // Handle onload events (icon loaded, size changes)
  311. if (DOM.eventGetType(e) == Event.ONLOAD) {
  312. VMenuBar parent = getParentMenu();
  313. if (parent != null) {
  314. // The onload event for an image in a popup should be sent to
  315. // the parent, which owns the popup
  316. parent.iconLoaded();
  317. } else {
  318. // Onload events for images in the root menu are handled by the
  319. // root menu itself
  320. iconLoaded();
  321. }
  322. return;
  323. }
  324. com.google.gwt.user.client.Element targetElement = DOM
  325. .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. com.google.gwt.user.client.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. */
  1333. public CustomMenuItem getMenuItemWithElement(
  1334. com.google.gwt.user.client.Element element) {
  1335. for (int i = 0; i < items.size(); i++) {
  1336. CustomMenuItem item = items.get(i);
  1337. if (DOM.isOrHasChild(item.getElement(), element)) {
  1338. return item;
  1339. }
  1340. if (item.getSubMenu() != null) {
  1341. item = item.getSubMenu().getMenuItemWithElement(element);
  1342. if (item != null) {
  1343. return item;
  1344. }
  1345. }
  1346. }
  1347. return null;
  1348. }
  1349. }