Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

VContextMenu.java 11KB

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