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.

VContextMenu.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.dom.client.NodeList;
  6. import com.google.gwt.dom.client.TableRowElement;
  7. import com.google.gwt.dom.client.TableSectionElement;
  8. import com.google.gwt.user.client.Element;
  9. import com.google.gwt.user.client.Window;
  10. import com.google.gwt.user.client.ui.MenuBar;
  11. import com.google.gwt.user.client.ui.MenuItem;
  12. import com.google.gwt.user.client.ui.PopupPanel;
  13. public class VContextMenu extends VToolkitOverlay implements SubPartAware {
  14. private ActionOwner actionOwner;
  15. private final CMenuBar menu = new CMenuBar();
  16. private int left;
  17. private int top;
  18. /**
  19. * This method should be used only by Client object as only one per client
  20. * should exists. Request an instance via client.getContextMenu();
  21. *
  22. * @param cli
  23. * to be set as an owner of menu
  24. */
  25. public VContextMenu() {
  26. super(true, false, true);
  27. setWidget(menu);
  28. setStyleName("i-contextmenu");
  29. }
  30. /**
  31. * Sets the element from which to build menu
  32. *
  33. * @param ao
  34. */
  35. public void setActionOwner(ActionOwner ao) {
  36. actionOwner = ao;
  37. }
  38. /**
  39. * Shows context menu at given location.
  40. *
  41. * @param left
  42. * @param top
  43. */
  44. public void showAt(int left, int top) {
  45. this.left = left;
  46. this.top = top;
  47. menu.clearItems();
  48. final Action[] actions = actionOwner.getActions();
  49. for (int i = 0; i < actions.length; i++) {
  50. final Action a = actions[i];
  51. menu.addItem(new MenuItem(a.getHTML(), true, a));
  52. }
  53. setPopupPositionAndShow(new PositionCallback() {
  54. public void setPosition(int offsetWidth, int offsetHeight) {
  55. // mac FF gets bad width due GWT popups overflow hacks,
  56. // re-determine width
  57. offsetWidth = menu.getOffsetWidth();
  58. int left = VContextMenu.this.left;
  59. int top = VContextMenu.this.top;
  60. if (offsetWidth + left > Window.getClientWidth()) {
  61. left = left - offsetWidth;
  62. if (left < 0) {
  63. left = 0;
  64. }
  65. }
  66. if (offsetHeight + top > Window.getClientHeight()) {
  67. top = top - offsetHeight;
  68. if (top < 0) {
  69. top = 0;
  70. }
  71. }
  72. setPopupPosition(left, top);
  73. }
  74. });
  75. }
  76. public void showAt(ActionOwner ao, int left, int top) {
  77. setActionOwner(ao);
  78. showAt(left, top);
  79. }
  80. /**
  81. * Extend standard Gwt MenuBar to set proper settings and to override
  82. * onPopupClosed method so that PopupPanel gets closed.
  83. */
  84. class CMenuBar extends MenuBar {
  85. public CMenuBar() {
  86. super(true);
  87. }
  88. @Override
  89. public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
  90. super.onPopupClosed(sender, autoClosed);
  91. hide();
  92. }
  93. /*
  94. * public void onBrowserEvent(Event event) { // Remove current selection
  95. * when mouse leaves if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
  96. * Element to = DOM.eventGetToElement(event); if
  97. * (!DOM.isOrHasChild(getElement(), to)) { DOM.setElementProperty(
  98. * super.getSelectedItem().getElement(), "className",
  99. * super.getSelectedItem().getStylePrimaryName()); } }
  100. *
  101. * super.onBrowserEvent(event); }
  102. */
  103. }
  104. public Element getSubPartElement(String subPart) {
  105. int index = Integer.parseInt(subPart.substring(6));
  106. // ApplicationConnection.getConsole().log(
  107. // "Searching element for selection index " + index);
  108. Element wrapperdiv = menu.getElement();
  109. com.google.gwt.dom.client.TableSectionElement tBody = (TableSectionElement) wrapperdiv
  110. .getFirstChildElement().getFirstChildElement();
  111. TableRowElement item = tBody.getRows().getItem(index);
  112. com.google.gwt.dom.client.Element clickableDivElement = item
  113. .getFirstChildElement().getFirstChildElement();
  114. return clickableDivElement.cast();
  115. }
  116. public String getSubPartName(Element subElement) {
  117. if (getElement().isOrHasChild(subElement)) {
  118. com.google.gwt.dom.client.Element e = subElement;
  119. {
  120. while (e != null && !e.getTagName().toLowerCase().equals("tr")) {
  121. e = e.getParentElement();
  122. // ApplicationConnection.getConsole().log("Found row");
  123. }
  124. }
  125. com.google.gwt.dom.client.TableSectionElement parentElement = (TableSectionElement) e
  126. .getParentElement();
  127. NodeList<TableRowElement> rows = parentElement.getRows();
  128. for (int i = 0; i < rows.getLength(); i++) {
  129. if (rows.getItem(i) == e) {
  130. // ApplicationConnection.getConsole().log(
  131. // "Found index for row" + 1);
  132. return "option" + i;
  133. }
  134. }
  135. return null;
  136. } else {
  137. return null;
  138. }
  139. }
  140. }