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

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