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

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