Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VContextMenu.java 12KB

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