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.

ClickableRenderer.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.renderers;
  17. import com.google.gwt.dom.client.BrowserEvents;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.dom.client.EventTarget;
  20. import com.google.gwt.event.dom.client.ClickEvent;
  21. import com.google.gwt.event.dom.client.ClickHandler;
  22. import com.google.gwt.event.dom.client.DomEvent;
  23. import com.google.gwt.event.dom.client.MouseEvent;
  24. import com.google.gwt.event.shared.EventHandler;
  25. import com.google.gwt.event.shared.HandlerManager;
  26. import com.google.gwt.event.shared.HandlerRegistration;
  27. import com.google.gwt.user.client.ui.Composite;
  28. import com.google.gwt.user.client.ui.Widget;
  29. import com.vaadin.client.WidgetUtil;
  30. import com.vaadin.client.widget.escalator.Cell;
  31. import com.vaadin.client.widget.escalator.RowContainer;
  32. import com.vaadin.client.widget.grid.CellReference;
  33. import com.vaadin.client.widget.grid.EventCellReference;
  34. import com.vaadin.client.widgets.Escalator;
  35. import com.vaadin.client.widgets.Grid;
  36. import com.vaadin.shared.ui.grid.GridConstants.Section;
  37. /**
  38. * An abstract superclass for renderers that render clickable widgets. Click
  39. * handlers can be added to a renderer to listen to click events emitted by all
  40. * widgets rendered by the renderer.
  41. *
  42. * @param <T>
  43. * the presentation (column) type
  44. * @param <W>
  45. * the widget type
  46. *
  47. * @since 7.4
  48. * @author Vaadin Ltd
  49. */
  50. public abstract class ClickableRenderer<T, W extends Widget>
  51. extends WidgetRenderer<T, W> implements ClickHandler {
  52. /**
  53. * A handler for {@link RendererClickEvent renderer click events}.
  54. *
  55. * @param <R>
  56. * the row type of the containing Grid
  57. *
  58. * @see ButtonRenderer#addClickHandler(RendererClickHandler)
  59. */
  60. public interface RendererClickHandler<R> extends EventHandler {
  61. /**
  62. * Called when a rendered button is clicked.
  63. *
  64. * @param event
  65. * the event representing the click
  66. */
  67. void onClick(RendererClickEvent<R> event);
  68. }
  69. /**
  70. * An event fired when a widget rendered by a ClickableWidgetRenderer
  71. * subclass is clicked.
  72. *
  73. * @param <R>
  74. * the row type of the containing Grid
  75. */
  76. @SuppressWarnings("rawtypes")
  77. public static class RendererClickEvent<R>
  78. extends MouseEvent<RendererClickHandler> {
  79. @SuppressWarnings("unchecked")
  80. static final Type<RendererClickHandler> TYPE = new Type<RendererClickHandler>(
  81. BrowserEvents.CLICK, new RendererClickEvent());
  82. private CellReference<R> cell;
  83. private R row;
  84. private RendererClickEvent() {
  85. }
  86. /**
  87. * Returns the cell of the clicked button.
  88. *
  89. * @return the cell
  90. */
  91. public CellReference<R> getCell() {
  92. return cell;
  93. }
  94. /**
  95. * Returns the data object corresponding to the row of the clicked
  96. * button.
  97. *
  98. * @return the row data object
  99. */
  100. public R getRow() {
  101. return row;
  102. }
  103. @Override
  104. public Type<RendererClickHandler> getAssociatedType() {
  105. return TYPE;
  106. }
  107. @Override
  108. @SuppressWarnings("unchecked")
  109. protected void dispatch(RendererClickHandler handler) {
  110. EventTarget target = getNativeEvent().getEventTarget();
  111. if (!Element.is(target)) {
  112. return;
  113. }
  114. Element e = Element.as(target);
  115. Grid<R> grid = (Grid<R>) findClosestParentGrid(e);
  116. cell = findCell(grid, e);
  117. row = cell.getRow();
  118. handler.onClick(this);
  119. }
  120. /**
  121. * Returns the cell the given element belongs to.
  122. *
  123. * @param grid
  124. * the grid instance that is queried
  125. * @param e
  126. * a cell element or the descendant of one
  127. * @return the cell or null if the element is not a grid cell or a
  128. * descendant of one
  129. */
  130. private static <T> CellReference<T> findCell(Grid<T> grid, Element e) {
  131. RowContainer container = getEscalator(grid).findRowContainer(e);
  132. if (container == null) {
  133. return null;
  134. }
  135. Cell cell = container.getCell(e);
  136. EventCellReference<T> cellReference = new EventCellReference<>(
  137. grid);
  138. // FIXME: Section is currently always body. Might be useful for the
  139. // future to have an actual check.
  140. cellReference.set(cell, Section.BODY);
  141. return cellReference;
  142. }
  143. private native static Escalator getEscalator(Grid<?> grid)
  144. /*-{
  145. return grid.@com.vaadin.client.widgets.Grid::escalator;
  146. }-*/;
  147. /**
  148. * Returns the Grid instance containing the given element, if any.
  149. * <p>
  150. * <strong>Note:</strong> This method may not work reliably if the grid
  151. * in question is wrapped in a {@link Composite} <em>unless</em> the
  152. * element is inside another widget that is a child of the wrapped grid;
  153. * please refer to the note in
  154. * {@link WidgetUtil#findWidget(Element, Class) Util.findWidget} for
  155. * details.
  156. *
  157. * @param e
  158. * the element whose parent grid to find
  159. * @return the parent grid or null if none found.
  160. */
  161. private static Grid<?> findClosestParentGrid(Element e) {
  162. Widget w = WidgetUtil.findWidget(e, null);
  163. while (w != null && !(w instanceof Grid)) {
  164. w = w.getParent();
  165. }
  166. return (Grid<?>) w;
  167. }
  168. }
  169. private HandlerManager handlerManager;
  170. /**
  171. * {@inheritDoc}
  172. * <p>
  173. * <em>Implementation note:</em> It is the implementing method's
  174. * responsibility to add {@code this} as a click handler of the returned
  175. * widget, or a widget nested therein, in order to make click events
  176. * propagate properly to handlers registered via
  177. * {@link #addClickHandler(RendererClickHandler) addClickHandler}.
  178. */
  179. @Override
  180. public abstract W createWidget();
  181. /**
  182. * Adds a click handler to this button renderer. The handler is invoked
  183. * every time one of the widgets rendered by this renderer is clicked.
  184. * <p>
  185. * Note that the row type of the click handler must match the row type of
  186. * the containing Grid.
  187. *
  188. * @param handler
  189. * the click handler to be added
  190. */
  191. public HandlerRegistration addClickHandler(
  192. RendererClickHandler<?> handler) {
  193. if (handlerManager == null) {
  194. handlerManager = new HandlerManager(this);
  195. }
  196. return handlerManager.addHandler(RendererClickEvent.TYPE, handler);
  197. }
  198. @Override
  199. public void onClick(ClickEvent event) {
  200. /*
  201. * The handler manager is lazily instantiated so it's null iff
  202. * addClickHandler is never called.
  203. */
  204. if (handlerManager != null) {
  205. DomEvent.fireNativeEvent(event.getNativeEvent(), handlerManager);
  206. }
  207. }
  208. }