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.4KB

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