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.

MenuBar.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. /*
  6. * Copyright 2007 Google Inc.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  9. * use this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. * License for the specific language governing permissions and limitations under
  18. * the License.
  19. */
  20. // COPIED HERE DUE package privates in GWT
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import com.google.gwt.user.client.Command;
  24. import com.google.gwt.user.client.DOM;
  25. import com.google.gwt.user.client.DeferredCommand;
  26. import com.google.gwt.user.client.Element;
  27. import com.google.gwt.user.client.Event;
  28. import com.google.gwt.user.client.ui.PopupListener;
  29. import com.google.gwt.user.client.ui.PopupPanel;
  30. import com.google.gwt.user.client.ui.Widget;
  31. /**
  32. * A standard menu bar widget. A menu bar can contain any number of menu items,
  33. * each of which can either fire a {@link com.google.gwt.user.client.Command} or
  34. * open a cascaded menu bar.
  35. *
  36. * <p>
  37. * <img class='gallery' src='MenuBar.png'/>
  38. * </p>
  39. *
  40. * <h3>CSS Style Rules</h3> <ul class='css'> <li>.gwt-MenuBar { the menu bar
  41. * itself }</li> <li>.gwt-MenuBar .gwt-MenuItem { menu items }</li> <li>
  42. * .gwt-MenuBar .gwt-MenuItem-selected { selected menu items }</li> </ul>
  43. *
  44. * <p>
  45. * <h3>Example</h3>
  46. * {@example com.google.gwt.examples.MenuBarExample}
  47. * </p>
  48. *
  49. * @deprecated
  50. */
  51. public class MenuBar extends Widget implements PopupListener {
  52. private final Element body;
  53. private final ArrayList items = new ArrayList();
  54. private MenuBar parentMenu;
  55. private PopupPanel popup;
  56. private MenuItem selectedItem;
  57. private MenuBar shownChildMenu;
  58. private final boolean vertical;
  59. private boolean autoOpen;
  60. /**
  61. * Creates an empty horizontal menu bar.
  62. */
  63. public MenuBar() {
  64. this(false);
  65. }
  66. /**
  67. * Creates an empty menu bar.
  68. *
  69. * @param vertical
  70. * <code>true</code> to orient the menu bar vertically
  71. */
  72. public MenuBar(boolean vertical) {
  73. super();
  74. final Element table = DOM.createTable();
  75. body = DOM.createTBody();
  76. DOM.appendChild(table, body);
  77. if (!vertical) {
  78. final Element tr = DOM.createTR();
  79. DOM.appendChild(body, tr);
  80. }
  81. this.vertical = vertical;
  82. final Element outer = DOM.createDiv();
  83. DOM.appendChild(outer, table);
  84. setElement(outer);
  85. sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT);
  86. setStyleName("gwt-MenuBar");
  87. }
  88. /**
  89. * Adds a menu item to the bar.
  90. *
  91. * @param item
  92. * the item to be added
  93. */
  94. public void addItem(MenuItem item) {
  95. Element tr;
  96. if (vertical) {
  97. tr = DOM.createTR();
  98. DOM.appendChild(body, tr);
  99. } else {
  100. tr = DOM.getChild(body, 0);
  101. }
  102. DOM.appendChild(tr, item.getElement());
  103. item.setParentMenu(this);
  104. item.setSelectionStyle(false);
  105. items.add(item);
  106. }
  107. /**
  108. * Adds a menu item to the bar, that will fire the given command when it is
  109. * selected.
  110. *
  111. * @param text
  112. * the item's text
  113. * @param asHTML
  114. * <code>true</code> to treat the specified text as html
  115. * @param cmd
  116. * the command to be fired
  117. * @return the {@link MenuItem} object created
  118. */
  119. public MenuItem addItem(String text, boolean asHTML, Command cmd) {
  120. final MenuItem item = new MenuItem(text, asHTML, cmd);
  121. addItem(item);
  122. return item;
  123. }
  124. /**
  125. * Adds a menu item to the bar, that will open the specified menu when it is
  126. * selected.
  127. *
  128. * @param text
  129. * the item's text
  130. * @param asHTML
  131. * <code>true</code> to treat the specified text as html
  132. * @param popup
  133. * the menu to be cascaded from it
  134. * @return the {@link MenuItem} object created
  135. */
  136. public MenuItem addItem(String text, boolean asHTML, MenuBar popup) {
  137. final MenuItem item = new MenuItem(text, asHTML, popup);
  138. addItem(item);
  139. return item;
  140. }
  141. /**
  142. * Adds a menu item to the bar, that will fire the given command when it is
  143. * selected.
  144. *
  145. * @param text
  146. * the item's text
  147. * @param cmd
  148. * the command to be fired
  149. * @return the {@link MenuItem} object created
  150. */
  151. public MenuItem addItem(String text, Command cmd) {
  152. final MenuItem item = new MenuItem(text, cmd);
  153. addItem(item);
  154. return item;
  155. }
  156. /**
  157. * Adds a menu item to the bar, that will open the specified menu when it is
  158. * selected.
  159. *
  160. * @param text
  161. * the item's text
  162. * @param popup
  163. * the menu to be cascaded from it
  164. * @return the {@link MenuItem} object created
  165. */
  166. public MenuItem addItem(String text, MenuBar popup) {
  167. final MenuItem item = new MenuItem(text, popup);
  168. addItem(item);
  169. return item;
  170. }
  171. /**
  172. * Removes all menu items from this menu bar.
  173. */
  174. public void clearItems() {
  175. final Element container = getItemContainerElement();
  176. while (DOM.getChildCount(container) > 0) {
  177. DOM.removeChild(container, DOM.getChild(container, 0));
  178. }
  179. items.clear();
  180. }
  181. /**
  182. * Gets whether this menu bar's child menus will open when the mouse is
  183. * moved over it.
  184. *
  185. * @return <code>true</code> if child menus will auto-open
  186. */
  187. public boolean getAutoOpen() {
  188. return autoOpen;
  189. }
  190. public void onBrowserEvent(Event event) {
  191. super.onBrowserEvent(event);
  192. final MenuItem item = findItem(DOM.eventGetTarget(event));
  193. switch (DOM.eventGetType(event)) {
  194. case Event.ONCLICK: {
  195. // Fire an item's command when the user clicks on it.
  196. if (item != null) {
  197. doItemAction(item, true);
  198. }
  199. break;
  200. }
  201. case Event.ONMOUSEOVER: {
  202. if (item != null) {
  203. itemOver(item);
  204. }
  205. break;
  206. }
  207. case Event.ONMOUSEOUT: {
  208. if (item != null) {
  209. itemOver(null);
  210. }
  211. break;
  212. }
  213. }
  214. }
  215. public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
  216. // If the menu popup was auto-closed, close all of its parents as well.
  217. if (autoClosed) {
  218. closeAllParents();
  219. }
  220. // When the menu popup closes, remember that no item is
  221. // currently showing a popup menu.
  222. onHide();
  223. shownChildMenu = null;
  224. popup = null;
  225. }
  226. /**
  227. * Removes the specified menu item from the bar.
  228. *
  229. * @param item
  230. * the item to be removed
  231. */
  232. public void removeItem(MenuItem item) {
  233. final int idx = items.indexOf(item);
  234. if (idx == -1) {
  235. return;
  236. }
  237. final Element container = getItemContainerElement();
  238. DOM.removeChild(container, DOM.getChild(container, idx));
  239. items.remove(idx);
  240. }
  241. /**
  242. * Sets whether this menu bar's child menus will open when the mouse is
  243. * moved over it.
  244. *
  245. * @param autoOpen
  246. * <code>true</code> to cause child menus to auto-open
  247. */
  248. public void setAutoOpen(boolean autoOpen) {
  249. this.autoOpen = autoOpen;
  250. }
  251. /**
  252. * Returns a list containing the <code>MenuItem</code> objects in the menu
  253. * bar. If there are no items in the menu bar, then an empty
  254. * <code>List</code> object will be returned.
  255. *
  256. * @return a list containing the <code>MenuItem</code> objects in the menu
  257. * bar
  258. */
  259. protected List getItems() {
  260. return items;
  261. }
  262. /**
  263. * Returns the <code>MenuItem</code> that is currently selected
  264. * (highlighted) by the user. If none of the items in the menu are currently
  265. * selected, then <code>null</code> will be returned.
  266. *
  267. * @return the <code>MenuItem</code> that is currently selected, or
  268. * <code>null</code> if no items are currently selected
  269. */
  270. protected MenuItem getSelectedItem() {
  271. return selectedItem;
  272. }
  273. protected void onDetach() {
  274. // When the menu is detached, make sure to close all of its children.
  275. if (popup != null) {
  276. popup.hide();
  277. }
  278. super.onDetach();
  279. }
  280. /*
  281. * Closes all parent menu popups.
  282. */
  283. void closeAllParents() {
  284. MenuBar curMenu = this;
  285. while (curMenu != null) {
  286. curMenu.close();
  287. if ((curMenu.parentMenu == null) && (curMenu.selectedItem != null)) {
  288. curMenu.selectedItem.setSelectionStyle(false);
  289. curMenu.selectedItem = null;
  290. }
  291. curMenu = curMenu.parentMenu;
  292. }
  293. }
  294. /*
  295. * Performs the action associated with the given menu item. If the item has
  296. * a popup associated with it, the popup will be shown. If it has a command
  297. * associated with it, and 'fireCommand' is true, then the command will be
  298. * fired. Popups associated with other items will be hidden.
  299. *
  300. * @param item the item whose popup is to be shown. @param fireCommand
  301. * <code>true</code> if the item's command should be fired,
  302. * <code>false</code> otherwise.
  303. */
  304. void doItemAction(final MenuItem item, boolean fireCommand) {
  305. // If the given item is already showing its menu, we're done.
  306. if ((shownChildMenu != null) && (item.getSubMenu() == shownChildMenu)) {
  307. return;
  308. }
  309. // If another item is showing its menu, then hide it.
  310. if (shownChildMenu != null) {
  311. shownChildMenu.onHide();
  312. popup.hide();
  313. }
  314. // If the item has no popup, optionally fire its command.
  315. if (item.getSubMenu() == null) {
  316. if (fireCommand) {
  317. // Close this menu and all of its parents.
  318. closeAllParents();
  319. // Fire the item's command.
  320. final Command cmd = item.getCommand();
  321. if (cmd != null) {
  322. DeferredCommand.addCommand(cmd);
  323. }
  324. }
  325. return;
  326. }
  327. // Ensure that the item is selected.
  328. selectItem(item);
  329. // Create a new popup for this item, and position it next to
  330. // the item (below if this is a horizontal menu bar, to the
  331. // right if it's a vertical bar).
  332. popup = new IToolkitOverlay(true) {
  333. {
  334. setWidget(item.getSubMenu());
  335. item.getSubMenu().onShow();
  336. }
  337. public boolean onEventPreview(Event event) {
  338. // Hook the popup panel's event preview. We use this to keep it
  339. // from
  340. // auto-hiding when the parent menu is clicked.
  341. switch (DOM.eventGetType(event)) {
  342. case Event.ONCLICK:
  343. // If the event target is part of the parent menu, suppress
  344. // the
  345. // event altogether.
  346. final Element target = DOM.eventGetTarget(event);
  347. final Element parentMenuElement = item.getParentMenu()
  348. .getElement();
  349. if (DOM.isOrHasChild(parentMenuElement, target)) {
  350. return false;
  351. }
  352. break;
  353. }
  354. return super.onEventPreview(event);
  355. }
  356. };
  357. popup.addPopupListener(this);
  358. if (vertical) {
  359. popup.setPopupPosition(item.getAbsoluteLeft()
  360. + item.getOffsetWidth(), item.getAbsoluteTop());
  361. } else {
  362. popup.setPopupPosition(item.getAbsoluteLeft(), item
  363. .getAbsoluteTop()
  364. + item.getOffsetHeight());
  365. }
  366. shownChildMenu = item.getSubMenu();
  367. item.getSubMenu().parentMenu = this;
  368. // Show the popup, ensuring that the menubar's event preview remains on
  369. // top
  370. // of the popup's.
  371. popup.show();
  372. }
  373. void itemOver(MenuItem item) {
  374. if (item == null) {
  375. // Don't clear selection if the currently selected item's menu is
  376. // showing.
  377. if ((selectedItem != null)
  378. && (shownChildMenu == selectedItem.getSubMenu())) {
  379. return;
  380. }
  381. }
  382. // Style the item selected when the mouse enters.
  383. selectItem(item);
  384. // If child menus are being shown, or this menu is itself
  385. // a child menu, automatically show an item's child menu
  386. // when the mouse enters.
  387. if (item != null) {
  388. if ((shownChildMenu != null) || (parentMenu != null) || autoOpen) {
  389. doItemAction(item, false);
  390. }
  391. }
  392. }
  393. void selectItem(MenuItem item) {
  394. if (item == selectedItem) {
  395. return;
  396. }
  397. if (selectedItem != null) {
  398. selectedItem.setSelectionStyle(false);
  399. }
  400. if (item != null) {
  401. item.setSelectionStyle(true);
  402. }
  403. selectedItem = item;
  404. }
  405. /**
  406. * Closes this menu (if it is a popup).
  407. */
  408. private void close() {
  409. if (parentMenu != null) {
  410. parentMenu.popup.hide();
  411. }
  412. }
  413. private MenuItem findItem(Element hItem) {
  414. for (int i = 0; i < items.size(); ++i) {
  415. final MenuItem item = (MenuItem) items.get(i);
  416. if (DOM.isOrHasChild(item.getElement(), hItem)) {
  417. return item;
  418. }
  419. }
  420. return null;
  421. }
  422. private Element getItemContainerElement() {
  423. if (vertical) {
  424. return body;
  425. } else {
  426. return DOM.getChild(body, 0);
  427. }
  428. }
  429. /*
  430. * This method is called when a menu bar is hidden, so that it can hide any
  431. * child popups that are currently being shown.
  432. */
  433. private void onHide() {
  434. if (shownChildMenu != null) {
  435. shownChildMenu.onHide();
  436. popup.hide();
  437. }
  438. }
  439. /*
  440. * This method is called when a menu bar is shown.
  441. */
  442. private void onShow() {
  443. // Select the first item when a menu is shown.
  444. if (items.size() > 0) {
  445. selectItem((MenuItem) items.get(0));
  446. }
  447. }
  448. }