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.

ContextMenu.java 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package com.itmill.toolkit.terminal.gwt.client.ui;
  2. import com.google.gwt.user.client.DOM;
  3. import com.google.gwt.user.client.Window;
  4. import com.google.gwt.user.client.ui.MenuBar;
  5. import com.google.gwt.user.client.ui.MenuItem;
  6. import com.google.gwt.user.client.ui.PopupPanel;
  7. public class ContextMenu extends PopupPanel {
  8. private ActionOwner actionOwner;
  9. private CMenuBar menu = new CMenuBar();
  10. /**
  11. * This method should be used only by Client object as only one per client
  12. * should exists. Request an instance via client.getContextMenu();
  13. *
  14. * @param cli
  15. * to be set as an owner of menu
  16. */
  17. public ContextMenu() {
  18. super(true);
  19. setWidget(menu);
  20. setStyleName("i-contextmenu");
  21. }
  22. /**
  23. * Sets the element from which to build menu
  24. *
  25. * @param ao
  26. */
  27. public void setActionOwner(ActionOwner ao) {
  28. this.actionOwner = ao;
  29. }
  30. /**
  31. * Shows context menu at given location.
  32. *
  33. * @param left
  34. * @param top
  35. */
  36. public void showAt(int left, int top) {
  37. menu.clearItems();
  38. Action[] actions = actionOwner.getActions();
  39. for (int i = 0; i < actions.length; i++) {
  40. Action a = actions[i];
  41. menu.addItem(new MenuItem(a.getHTML(), true, a));
  42. }
  43. setPopupPosition(left, top);
  44. show();
  45. // fix position if "outside" screen
  46. if (DOM.getElementPropertyInt(getElement(), "offsetWidth") + left > Window
  47. .getClientWidth()) {
  48. left = Window.getClientWidth()
  49. - DOM.getElementPropertyInt(getElement(), "offsetWidth");
  50. setPopupPosition(left, top);
  51. }
  52. }
  53. public void showAt(ActionOwner ao, int left, int top) {
  54. setActionOwner(ao);
  55. showAt(left, top);
  56. }
  57. /**
  58. * Extend standard Gwt MenuBar to set proper settings and to override
  59. * onPopupClosed method so that PopupPanel gets closed.
  60. */
  61. class CMenuBar extends MenuBar {
  62. public CMenuBar() {
  63. super(true);
  64. }
  65. public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
  66. super.onPopupClosed(sender, autoClosed);
  67. ContextMenu.this.hide();
  68. }
  69. }
  70. }