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.

IContextMenu.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import com.google.gwt.user.client.Window;
  6. import com.google.gwt.user.client.ui.MenuBar;
  7. import com.google.gwt.user.client.ui.MenuItem;
  8. import com.google.gwt.user.client.ui.PopupPanel;
  9. public class IContextMenu extends IToolkitOverlay {
  10. private ActionOwner actionOwner;
  11. private final CMenuBar menu = new CMenuBar();
  12. private int left;
  13. private int top;
  14. /**
  15. * This method should be used only by Client object as only one per client
  16. * should exists. Request an instance via client.getContextMenu();
  17. *
  18. * @param cli
  19. * to be set as an owner of menu
  20. */
  21. public IContextMenu() {
  22. super(true, false, true);
  23. setWidget(menu);
  24. setStyleName("i-contextmenu");
  25. }
  26. /**
  27. * Sets the element from which to build menu
  28. *
  29. * @param ao
  30. */
  31. public void setActionOwner(ActionOwner ao) {
  32. actionOwner = ao;
  33. }
  34. /**
  35. * Shows context menu at given location.
  36. *
  37. * @param left
  38. * @param top
  39. */
  40. public void showAt(int left, int top) {
  41. this.left = left;
  42. this.top = top;
  43. menu.clearItems();
  44. final Action[] actions = actionOwner.getActions();
  45. for (int i = 0; i < actions.length; i++) {
  46. final Action a = actions[i];
  47. menu.addItem(new MenuItem(a.getHTML(), true, a));
  48. }
  49. setPopupPositionAndShow(new PositionCallback() {
  50. public void setPosition(int offsetWidth, int offsetHeight) {
  51. // mac FF gets bad width due GWT popups overflow hacks,
  52. // re-determine width
  53. offsetWidth = menu.getOffsetWidth();
  54. int left = IContextMenu.this.left;
  55. int top = IContextMenu.this.top;
  56. if (offsetWidth + left > Window.getClientWidth()) {
  57. left = left - offsetWidth;
  58. if (left < 0) {
  59. left = 0;
  60. }
  61. }
  62. if (offsetHeight + top > Window.getClientHeight()) {
  63. top = top - offsetHeight;
  64. if (top < 0) {
  65. top = 0;
  66. }
  67. }
  68. setPopupPosition(left, top);
  69. }
  70. });
  71. }
  72. public void showAt(ActionOwner ao, int left, int top) {
  73. setActionOwner(ao);
  74. showAt(left, top);
  75. }
  76. /**
  77. * Extend standard Gwt MenuBar to set proper settings and to override
  78. * onPopupClosed method so that PopupPanel gets closed.
  79. */
  80. class CMenuBar extends MenuBar {
  81. public CMenuBar() {
  82. super(true);
  83. }
  84. public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
  85. super.onPopupClosed(sender, autoClosed);
  86. hide();
  87. }
  88. /*
  89. * public void onBrowserEvent(Event event) { // Remove current selection
  90. * when mouse leaves if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
  91. * Element to = DOM.eventGetToElement(event); if
  92. * (!DOM.isOrHasChild(getElement(), to)) { DOM.setElementProperty(
  93. * super.getSelectedItem().getElement(), "className",
  94. * super.getSelectedItem().getStylePrimaryName()); } }
  95. *
  96. * super.onBrowserEvent(event); }
  97. */
  98. }
  99. }