Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ClickableRenderer.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.ui.renderers;
  17. import java.lang.reflect.Method;
  18. import com.vaadin.event.ConnectorEventListener;
  19. import com.vaadin.event.MouseEvents.ClickEvent;
  20. import com.vaadin.shared.MouseEventDetails;
  21. import com.vaadin.shared.Registration;
  22. import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;
  23. import com.vaadin.ui.Grid;
  24. import com.vaadin.ui.Grid.Column;
  25. import com.vaadin.util.ReflectTools;
  26. /**
  27. * An abstract superclass for {@link Renderer}s that render clickable items.
  28. * Click listeners can be added to a renderer to be notified when any of the
  29. * rendered items is clicked.
  30. *
  31. * @param <T>
  32. * the type of the parent {@link Grid}
  33. * @param <V>
  34. * the type presented by the renderer
  35. *
  36. * @since 7.4
  37. * @author Vaadin Ltd
  38. */
  39. public abstract class ClickableRenderer<T, V> extends AbstractRenderer<T, V> {
  40. /**
  41. * An interface for listening to {@link RendererClickEvent renderer click
  42. * events}.
  43. *
  44. * @see ButtonRenderer#addClickListener(RendererClickListener)
  45. */
  46. public interface RendererClickListener<T> extends ConnectorEventListener {
  47. static final Method CLICK_METHOD = ReflectTools.findMethod(
  48. RendererClickListener.class, "click", RendererClickEvent.class);
  49. /**
  50. * Called when a rendered button is clicked.
  51. *
  52. * @param event
  53. * the event representing the click
  54. */
  55. void click(RendererClickEvent<T> event);
  56. }
  57. /**
  58. * An event fired when a clickable widget rendered by a ClickableRenderer is
  59. * clicked.
  60. *
  61. * @param <T>
  62. * the item type associated with this click event
  63. */
  64. public static class RendererClickEvent<T> extends ClickEvent {
  65. private final T item;
  66. private final Column column;
  67. protected RendererClickEvent(Grid<T> source, T item, Column column,
  68. MouseEventDetails mouseEventDetails) {
  69. super(source, mouseEventDetails);
  70. this.item = item;
  71. this.column = column;
  72. }
  73. /**
  74. * Returns the item of the row where the click event originated.
  75. *
  76. * @return the item of the clicked row
  77. */
  78. public T getItem() {
  79. return item;
  80. }
  81. /**
  82. * Returns the {@link Column} where the click event originated.
  83. *
  84. * @return the column of the click event
  85. */
  86. public Column getColumn() {
  87. return column;
  88. }
  89. }
  90. /**
  91. * Creates a new clickable renderer with the given presentation type. No
  92. * null representation will be used.
  93. *
  94. * @param presentationType
  95. * the data type that this renderer displays, not
  96. * <code>null</code>
  97. */
  98. protected ClickableRenderer(Class<V> presentationType) {
  99. this(presentationType, null);
  100. }
  101. /**
  102. * Creates a new clickable renderer with the given presentation type and
  103. * null representation.
  104. *
  105. * @param presentationType
  106. * the data type that this renderer displays, not
  107. * <code>null</code>
  108. * @param nullRepresentation
  109. * a string that will be sent to the client instead of a regular
  110. * value in case the actual cell value is <code>null</code>. May
  111. * be <code>null</code>.
  112. */
  113. protected ClickableRenderer(Class<V> presentationType,
  114. String nullRepresentation) {
  115. super(presentationType, nullRepresentation);
  116. registerRpc((RendererClickRpc) (String rowKey, String columnId,
  117. MouseEventDetails mouseDetails) -> {
  118. Grid<T> grid = getParentGrid();
  119. T item = grid.getDataCommunicator().getKeyMapper().get(rowKey);
  120. Column column = grid.getColumn(columnId);
  121. fireEvent(
  122. new RendererClickEvent<>(grid, item, column, mouseDetails));
  123. });
  124. }
  125. /**
  126. * Adds a click listener to this button renderer. The listener is invoked
  127. * every time one of the buttons rendered by this renderer is clicked.
  128. *
  129. * @param listener
  130. * the click listener to be added, not null
  131. */
  132. public Registration addClickListener(RendererClickListener<T> listener) {
  133. return addListener(RendererClickEvent.class, listener,
  134. RendererClickListener.CLICK_METHOD);
  135. }
  136. /**
  137. * Removes the given click listener from this renderer.
  138. *
  139. * @param listener
  140. * the click listener to be removed
  141. */
  142. @Deprecated
  143. public void removeClickListener(RendererClickListener<T> listener) {
  144. removeListener(RendererClickEvent.class, listener);
  145. }
  146. }