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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.v7.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.util.ReflectTools;
  22. import com.vaadin.v7.shared.ui.grid.renderers.RendererClickRpc;
  23. import com.vaadin.v7.ui.Grid;
  24. import com.vaadin.v7.ui.Grid.AbstractRenderer;
  25. import com.vaadin.v7.ui.Grid.Column;
  26. /**
  27. * An abstract superclass for Renderers that render clickable items. Click
  28. * listeners can be added to a renderer to be notified when any of the rendered
  29. * items is clicked.
  30. *
  31. * @param <T>
  32. * the type presented by the renderer
  33. *
  34. * @since 7.4
  35. * @author Vaadin Ltd
  36. */
  37. public class ClickableRenderer<T> extends AbstractRenderer<T> {
  38. /**
  39. * An interface for listening to {@link RendererClickEvent renderer click
  40. * events}.
  41. *
  42. * @see {@link ButtonRenderer#addClickListener(RendererClickListener)}
  43. */
  44. public interface RendererClickListener extends ConnectorEventListener {
  45. static final Method CLICK_METHOD = ReflectTools.findMethod(
  46. RendererClickListener.class, "click", RendererClickEvent.class);
  47. /**
  48. * Called when a rendered button is clicked.
  49. *
  50. * @param event
  51. * the event representing the click
  52. */
  53. void click(RendererClickEvent event);
  54. }
  55. /**
  56. * An event fired when a button rendered by a ButtonRenderer is clicked.
  57. */
  58. public static class RendererClickEvent extends ClickEvent {
  59. private Object itemId;
  60. private Column column;
  61. protected RendererClickEvent(Grid source, Object itemId, Column column,
  62. MouseEventDetails mouseEventDetails) {
  63. super(source, mouseEventDetails);
  64. this.itemId = itemId;
  65. this.column = column;
  66. }
  67. /**
  68. * Returns the item ID of the row where the click event originated.
  69. *
  70. * @return the item ID of the clicked row
  71. */
  72. public Object getItemId() {
  73. return itemId;
  74. }
  75. /**
  76. * Returns the {@link Column} where the click event originated.
  77. *
  78. * @return the column of the click event
  79. */
  80. public Column getColumn() {
  81. return column;
  82. }
  83. /**
  84. * Returns the property ID where the click event originated.
  85. *
  86. * @return the property ID of the clicked cell
  87. */
  88. public Object getPropertyId() {
  89. return column.getPropertyId();
  90. }
  91. }
  92. protected ClickableRenderer(Class<T> presentationType) {
  93. this(presentationType, null);
  94. }
  95. protected ClickableRenderer(Class<T> presentationType,
  96. String nullRepresentation) {
  97. super(presentationType, nullRepresentation);
  98. registerRpc(new RendererClickRpc() {
  99. @Override
  100. public void click(String rowKey, String columnId,
  101. MouseEventDetails mouseDetails) {
  102. fireEvent(new RendererClickEvent(getParentGrid(),
  103. getItemId(rowKey), getColumn(columnId), mouseDetails));
  104. }
  105. });
  106. }
  107. /**
  108. * Adds a click listener to this button renderer. The listener is invoked
  109. * every time one of the buttons rendered by this renderer is clicked.
  110. *
  111. * @param listener
  112. * the click listener to be added
  113. */
  114. public void addClickListener(RendererClickListener listener) {
  115. addListener(RendererClickEvent.class, listener,
  116. RendererClickListener.CLICK_METHOD);
  117. }
  118. /**
  119. * Removes the given click listener from this renderer.
  120. *
  121. * @param listener
  122. * the click listener to be removed
  123. */
  124. public void removeClickListener(RendererClickListener listener) {
  125. removeListener(RendererClickEvent.class, listener);
  126. }
  127. }