Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VContextMenu.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.TableRowElement;
  22. import com.google.gwt.dom.client.TableSectionElement;
  23. import com.google.gwt.event.dom.client.BlurEvent;
  24. import com.google.gwt.event.dom.client.BlurHandler;
  25. import com.google.gwt.event.dom.client.FocusEvent;
  26. import com.google.gwt.event.dom.client.FocusHandler;
  27. import com.google.gwt.event.dom.client.HasBlurHandlers;
  28. import com.google.gwt.event.dom.client.HasFocusHandlers;
  29. import com.google.gwt.event.dom.client.HasKeyDownHandlers;
  30. import com.google.gwt.event.dom.client.HasKeyPressHandlers;
  31. import com.google.gwt.event.dom.client.KeyCodes;
  32. import com.google.gwt.event.dom.client.KeyDownEvent;
  33. import com.google.gwt.event.dom.client.KeyDownHandler;
  34. import com.google.gwt.event.dom.client.KeyPressEvent;
  35. import com.google.gwt.event.dom.client.KeyPressHandler;
  36. import com.google.gwt.event.dom.client.KeyUpEvent;
  37. import com.google.gwt.event.dom.client.KeyUpHandler;
  38. import com.google.gwt.event.dom.client.LoadEvent;
  39. import com.google.gwt.event.dom.client.LoadHandler;
  40. import com.google.gwt.event.logical.shared.CloseEvent;
  41. import com.google.gwt.event.logical.shared.CloseHandler;
  42. import com.google.gwt.event.shared.HandlerRegistration;
  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((offsetWidth, offsetHeight) -> {
  121. // mac FF gets bad width due GWT popups overflow hacks,
  122. // re-determine width
  123. offsetWidth = menu.getOffsetWidth();
  124. int menuLeft = VContextMenu.this.left;
  125. int menuTop = VContextMenu.this.top;
  126. if (offsetWidth + menuLeft > Window.getClientWidth()) {
  127. menuLeft = menuLeft - offsetWidth;
  128. if (menuLeft < 0) {
  129. menuLeft = 0;
  130. }
  131. }
  132. if (offsetHeight + menuTop > Window.getClientHeight()) {
  133. menuTop = Math.max(0, Window.getClientHeight() - offsetHeight);
  134. }
  135. if (menuTop == 0) {
  136. setHeight(Window.getClientHeight() + "px");
  137. }
  138. setPopupPosition(menuLeft, menuTop);
  139. /*
  140. * Move keyboard focus to menu, deferring the focus setting so the
  141. * focus is certainly moved to the menu in all browser after the
  142. * positioning has been done.
  143. */
  144. Scheduler.get().scheduleDeferred(() -> {
  145. // Focus the menu.
  146. menu.setFocus(true);
  147. // Unselect previously selected items
  148. menu.selectItem(null);
  149. });
  150. });
  151. }
  152. public void showAt(ActionOwner ao, int left, int top) {
  153. setActionOwner(ao);
  154. showAt(left, top);
  155. }
  156. /**
  157. * Extend standard Gwt MenuBar to set proper settings and to override
  158. * onPopupClosed method so that PopupPanel gets closed.
  159. */
  160. class CMenuBar extends MenuBar
  161. implements HasFocusHandlers, HasBlurHandlers, HasKeyDownHandlers,
  162. HasKeyPressHandlers, Focusable, LoadHandler, KeyUpHandler {
  163. public CMenuBar() {
  164. super(true);
  165. addDomHandler(this, LoadEvent.getType());
  166. addDomHandler(this, KeyUpEvent.getType());
  167. }
  168. @Override
  169. public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
  170. super.onPopupClosed(sender, autoClosed);
  171. // make focusable, as we don't need access key magic we don't need
  172. // to
  173. // use FocusImpl.createFocusable
  174. getElement().setTabIndex(0);
  175. hide();
  176. }
  177. /*
  178. * public void onBrowserEvent(Event event) { // Remove current selection
  179. * when mouse leaves if (DOM.eventGetType(event) == Event.ONMOUSEOUT) {
  180. * Element to = DOM.eventGetToElement(event); if
  181. * (!DOM.isOrHasChild(getElement(), to)) { DOM.setElementProperty(
  182. * super.getSelectedItem().getElement(), "className",
  183. * super.getSelectedItem().getStylePrimaryName()); } }
  184. *
  185. * super.onBrowserEvent(event); }
  186. */
  187. private MenuItem getItem(int index) {
  188. return super.getItems().get(index);
  189. }
  190. @Override
  191. public HandlerRegistration addFocusHandler(FocusHandler handler) {
  192. return addDomHandler(handler, FocusEvent.getType());
  193. }
  194. @Override
  195. public HandlerRegistration addBlurHandler(BlurHandler handler) {
  196. return addDomHandler(handler, BlurEvent.getType());
  197. }
  198. @Override
  199. public HandlerRegistration addKeyDownHandler(KeyDownHandler handler) {
  200. return addDomHandler(handler, KeyDownEvent.getType());
  201. }
  202. @Override
  203. public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
  204. return addDomHandler(handler, KeyPressEvent.getType());
  205. }
  206. public void setFocus(boolean focus) {
  207. if (focus) {
  208. FocusImpl.getFocusImplForPanel().focus(getElement());
  209. } else {
  210. FocusImpl.getFocusImplForPanel().blur(getElement());
  211. }
  212. }
  213. @Override
  214. public void focus() {
  215. setFocus(true);
  216. }
  217. @Override
  218. public void onLoad(LoadEvent event) {
  219. // Handle icon onload events to ensure shadow is resized correctly
  220. delayedImageLoadExecutioner.trigger();
  221. }
  222. @Override
  223. public void onKeyUp(KeyUpEvent event) {
  224. // Allow to close context menu with ESC
  225. if (event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) {
  226. hide();
  227. }
  228. }
  229. }
  230. @Override
  231. public com.google.gwt.user.client.Element getSubPartElement(
  232. String subPart) {
  233. int index = Integer.parseInt(subPart.substring(6));
  234. // ApplicationConnection.getConsole().log(
  235. // "Searching element for selection index " + index);
  236. MenuItem item = menu.getItem(index);
  237. // ApplicationConnection.getConsole().log("Item: " + item);
  238. // Item refers to the td, which is the parent of the clickable element
  239. return item.getElement().getFirstChildElement().cast();
  240. }
  241. @Override
  242. public String getSubPartName(
  243. com.google.gwt.user.client.Element subElement) {
  244. if (getElement().isOrHasChild(subElement)) {
  245. com.google.gwt.dom.client.Element e = subElement;
  246. while (e != null
  247. && !e.getTagName().toLowerCase(Locale.ROOT).equals("tr")) {
  248. e = e.getParentElement();
  249. // ApplicationConnection.getConsole().log("Found row");
  250. }
  251. com.google.gwt.dom.client.TableSectionElement parentElement = (TableSectionElement) e
  252. .getParentElement();
  253. NodeList<TableRowElement> rows = parentElement.getRows();
  254. for (int i = 0; i < rows.getLength(); i++) {
  255. if (rows.getItem(i) == e) {
  256. // ApplicationConnection.getConsole().log(
  257. // "Found index for row" + 1);
  258. return "option" + i;
  259. }
  260. }
  261. return null;
  262. } else {
  263. return null;
  264. }
  265. }
  266. /**
  267. * Hides context menu if it is currently shown by given action owner.
  268. *
  269. * @param actionOwner
  270. */
  271. public void ensureHidden(ActionOwner actionOwner) {
  272. if (this.actionOwner == actionOwner) {
  273. hide();
  274. }
  275. }
  276. }