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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright 2000-2018 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.Locale;
  18. import com.google.gwt.core.client.Scheduler;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.dom.client.NodeList;
  21. import com.google.gwt.dom.client.Style;
  22. import com.google.gwt.dom.client.TableRowElement;
  23. import com.google.gwt.dom.client.TableSectionElement;
  24. import com.google.gwt.event.dom.client.BlurEvent;
  25. import com.google.gwt.event.dom.client.BlurHandler;
  26. import com.google.gwt.event.dom.client.FocusEvent;
  27. import com.google.gwt.event.dom.client.FocusHandler;
  28. import com.google.gwt.event.dom.client.HasBlurHandlers;
  29. import com.google.gwt.event.dom.client.HasFocusHandlers;
  30. import com.google.gwt.event.dom.client.HasKeyDownHandlers;
  31. import com.google.gwt.event.dom.client.HasKeyPressHandlers;
  32. import com.google.gwt.event.dom.client.KeyCodes;
  33. import com.google.gwt.event.dom.client.KeyDownEvent;
  34. import com.google.gwt.event.dom.client.KeyDownHandler;
  35. import com.google.gwt.event.dom.client.KeyPressEvent;
  36. import com.google.gwt.event.dom.client.KeyPressHandler;
  37. import com.google.gwt.event.dom.client.KeyUpEvent;
  38. import com.google.gwt.event.dom.client.KeyUpHandler;
  39. import com.google.gwt.event.dom.client.LoadEvent;
  40. import com.google.gwt.event.dom.client.LoadHandler;
  41. import com.google.gwt.event.logical.shared.CloseEvent;
  42. import com.google.gwt.event.logical.shared.CloseHandler;
  43. import com.google.gwt.event.shared.HandlerRegistration;
  44. import com.google.gwt.user.client.DOM;
  45. import com.google.gwt.user.client.Window;
  46. import com.google.gwt.user.client.ui.MenuBar;
  47. import com.google.gwt.user.client.ui.MenuItem;
  48. import com.google.gwt.user.client.ui.PopupPanel;
  49. import com.google.gwt.user.client.ui.RootPanel;
  50. import com.google.gwt.user.client.ui.impl.FocusImpl;
  51. import com.vaadin.client.Focusable;
  52. import com.vaadin.client.WidgetUtil;
  53. public class VContextMenu extends VOverlay implements SubPartAware {
  54. private ActionOwner actionOwner;
  55. private final CMenuBar menu = new CMenuBar();
  56. private int left;
  57. private int top;
  58. private Element focusedElement;
  59. private VLazyExecutor delayedImageLoadExecutioner = new VLazyExecutor(100,
  60. () -> imagesLoaded());
  61. /**
  62. * This method should be used only by Client object as only one per client
  63. * should exists. Request an instance via client.getContextMenu();
  64. */
  65. public VContextMenu() {
  66. super(true, false);
  67. setWidget(menu);
  68. setStyleName("v-contextmenu");
  69. getElement().setId(DOM.createUniqueId());
  70. addCloseHandler(new CloseHandler<PopupPanel>() {
  71. @Override
  72. public void onClose(CloseEvent<PopupPanel> event) {
  73. Element currentFocus = WidgetUtil.getFocusedElement();
  74. if (focusedElement != null && (currentFocus == null
  75. || menu.getElement().isOrHasChild(currentFocus)
  76. || RootPanel.getBodyElement().equals(currentFocus))) {
  77. focusedElement.focus();
  78. focusedElement = null;
  79. }
  80. }
  81. });
  82. }
  83. protected void imagesLoaded() {
  84. if (isVisible()) {
  85. show();
  86. }
  87. }
  88. /**
  89. * Sets the element from which to build menu.
  90. *
  91. * @param ao
  92. */
  93. public void setActionOwner(ActionOwner ao) {
  94. actionOwner = ao;
  95. }
  96. /**
  97. * Shows context menu at given location IF it contain at least one item.
  98. *
  99. * @param left
  100. * @param top
  101. */
  102. public void showAt(int left, int top) {
  103. final Action[] actions = actionOwner.getActions();
  104. if (actions == null || actions.length == 0) {
  105. // Only show if there really are actions
  106. return;
  107. }
  108. this.left = left;
  109. this.top = top;
  110. menu.clearItems();
  111. for (final Action a : actions) {
  112. menu.addItem(new MenuItem(a.getHTML(), true, a));
  113. }
  114. // Attach onload listeners to all images
  115. WidgetUtil.sinkOnloadForImages(menu.getElement());
  116. // Store the currently focused element, which will be re-focused when
  117. // context menu is closed
  118. focusedElement = WidgetUtil.getFocusedElement();
  119. // reset height (if it has been previously set explicitly)
  120. setHeight("");
  121. setPopupPositionAndShow((offsetWidth, offsetHeight) -> {
  122. // mac FF gets bad width due GWT popups overflow hacks,
  123. // re-determine width
  124. offsetWidth = menu.getOffsetWidth();
  125. int menuLeft = VContextMenu.this.left;
  126. int menuTop = VContextMenu.this.top;
  127. if (offsetWidth + menuLeft > Window.getClientWidth()) {
  128. menuLeft = menuLeft - offsetWidth;
  129. if (menuLeft < 0) {
  130. menuLeft = 0;
  131. }
  132. }
  133. if (offsetHeight + menuTop > Window.getClientHeight()) {
  134. menuTop = Math.max(0, Window.getClientHeight() - offsetHeight);
  135. }
  136. if (menuTop == 0) {
  137. setHeight(Window.getClientHeight() + "px");
  138. }
  139. setPopupPosition(menuLeft, menuTop);
  140. getElement().getStyle().setPosition(Style.Position.FIXED);
  141. /*
  142. * Move keyboard focus to menu, deferring the focus setting so the
  143. * focus is certainly moved to the menu in all browser after the
  144. * positioning has been done.
  145. */
  146. Scheduler.get().scheduleDeferred(() -> {
  147. // Focus the menu.
  148. menu.setFocus(true);
  149. // Unselect previously selected items
  150. menu.selectItem(null);
  151. });
  152. });
  153. }
  154. public void showAt(ActionOwner ao, int left, int top) {
  155. setActionOwner(ao);
  156. showAt(left, top);
  157. }
  158. /**
  159. * Extend standard Gwt MenuBar to set proper settings and to override
  160. * onPopupClosed method so that PopupPanel gets closed.
  161. */
  162. class CMenuBar extends MenuBar
  163. implements HasFocusHandlers, HasBlurHandlers, HasKeyDownHandlers,
  164. HasKeyPressHandlers, Focusable, LoadHandler, KeyUpHandler {
  165. public CMenuBar() {
  166. super(true);
  167. addDomHandler(this, LoadEvent.getType());
  168. addDomHandler(this, KeyUpEvent.getType());
  169. }
  170. @Override
  171. public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
  172. super.onPopupClosed(sender, autoClosed);
  173. // make focusable, as we don't need access key magic we don't need
  174. // to
  175. // use FocusImpl.createFocusable
  176. getElement().setTabIndex(0);
  177. hide();
  178. }
  179. /*
  180. * public void onBrowserEvent(Event event) { // Remove current selection
  181. * when mouse leaves if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
  182. * Element to = DOM.eventGetToElement(event); if
  183. * (!DOM.isOrHasChild(getElement(), to)) { DOM.setElementProperty(
  184. * super.getSelectedItem().getElement(), "className",
  185. * super.getSelectedItem().getStylePrimaryName()); } }
  186. *
  187. * super.onBrowserEvent(event); }
  188. */
  189. private MenuItem getItem(int index) {
  190. return super.getItems().get(index);
  191. }
  192. @Override
  193. public HandlerRegistration addFocusHandler(FocusHandler handler) {
  194. return addDomHandler(handler, FocusEvent.getType());
  195. }
  196. @Override
  197. public HandlerRegistration addBlurHandler(BlurHandler handler) {
  198. return addDomHandler(handler, BlurEvent.getType());
  199. }
  200. @Override
  201. public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
  202. return addDomHandler(handler, KeyDownEvent.getType());
  203. }
  204. @Override
  205. public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
  206. return addDomHandler(handler, KeyPressEvent.getType());
  207. }
  208. public void setFocus(boolean focus) {
  209. if (focus) {
  210. FocusImpl.getFocusImplForPanel().focus(getElement());
  211. } else {
  212. FocusImpl.getFocusImplForPanel().blur(getElement());
  213. }
  214. }
  215. @Override
  216. public void focus() {
  217. setFocus(true);
  218. }
  219. @Override
  220. public void onLoad(LoadEvent event) {
  221. // Handle icon onload events to ensure shadow is resized correctly
  222. delayedImageLoadExecutioner.trigger();
  223. }
  224. @Override
  225. public void onKeyUp(KeyUpEvent event) {
  226. // Allow to close context menu with ESC
  227. if (event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) {
  228. hide();
  229. }
  230. }
  231. }
  232. @Override
  233. public com.google.gwt.user.client.Element getSubPartElement(
  234. String subPart) {
  235. int index = Integer.parseInt(subPart.substring(6));
  236. // ApplicationConnection.getConsole().log(
  237. // "Searching element for selection index " + index);
  238. MenuItem item = menu.getItem(index);
  239. // ApplicationConnection.getConsole().log("Item: " + item);
  240. // Item refers to the td, which is the parent of the clickable element
  241. return item.getElement().getFirstChildElement().cast();
  242. }
  243. @Override
  244. public String getSubPartName(
  245. com.google.gwt.user.client.Element subElement) {
  246. if (getElement().isOrHasChild(subElement)) {
  247. com.google.gwt.dom.client.Element e = subElement;
  248. while (e != null
  249. && !e.getTagName().toLowerCase(Locale.ROOT).equals("tr")) {
  250. e = e.getParentElement();
  251. // ApplicationConnection.getConsole().log("Found row");
  252. }
  253. com.google.gwt.dom.client.TableSectionElement parentElement = (TableSectionElement) e
  254. .getParentElement();
  255. NodeList<TableRowElement> rows = parentElement.getRows();
  256. for (int i = 0; i < rows.getLength(); i++) {
  257. if (rows.getItem(i) == e) {
  258. // ApplicationConnection.getConsole().log(
  259. // "Found index for row" + 1);
  260. return "option" + i;
  261. }
  262. }
  263. return null;
  264. } else {
  265. return null;
  266. }
  267. }
  268. /**
  269. * Hides context menu if it is currently shown by given action owner.
  270. *
  271. * @param actionOwner
  272. */
  273. public void ensureHidden(ActionOwner actionOwner) {
  274. if (this.actionOwner == actionOwner) {
  275. hide();
  276. }
  277. }
  278. }