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.

WidgetRenderer.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.TableCellElement;
  18. import com.google.gwt.user.client.ui.Widget;
  19. import com.vaadin.client.WidgetUtil;
  20. import com.vaadin.client.widget.grid.RendererCellReference;
  21. /**
  22. * A renderer for rendering widgets into cells.
  23. *
  24. * @since 7.4
  25. * @author Vaadin Ltd
  26. * @param <T>
  27. * the row data type
  28. * @param <W>
  29. * the Widget type
  30. */
  31. public abstract class WidgetRenderer<T, W extends Widget> extends
  32. ComplexRenderer<T> {
  33. @Override
  34. public void init(RendererCellReference cell) {
  35. // Implement if needed
  36. }
  37. /**
  38. * Creates a widget to attach to a cell. The widgets will be attached to the
  39. * cell after the cell element has been attached to DOM.
  40. *
  41. * @return widget to attach to a cell. All returned instances should be new
  42. * widget instances without a parent.
  43. */
  44. public abstract W createWidget();
  45. @Override
  46. public void render(RendererCellReference cell, T data) {
  47. W w = getWidget(cell.getElement());
  48. assert w != null : "Widget not found in cell (" + cell.getColumn()
  49. + "," + cell.getRow() + ")";
  50. render(cell, data, w);
  51. }
  52. /**
  53. * Renders a cell with a widget. This provides a way to update any
  54. * information in the widget that is cell specific. Do not detach the Widget
  55. * here, it will be done automatically by the Grid when the widget is no
  56. * longer needed.
  57. *
  58. * @param cell
  59. * the cell to render
  60. * @param data
  61. * the data of the cell
  62. * @param widget
  63. * the widget embedded in the cell
  64. */
  65. public abstract void render(RendererCellReference cell, T data, W widget);
  66. /**
  67. * Returns the widget contained inside the given cell element. Cannot be
  68. * called for cells that do not contain a widget.
  69. *
  70. * @param e
  71. * the element inside which to find a widget
  72. * @return the widget inside the element
  73. */
  74. protected W getWidget(TableCellElement e) {
  75. W w = getWidget(e, null);
  76. assert w != null : "Widget not found inside cell";
  77. return w;
  78. }
  79. /**
  80. * Returns the widget contained inside the given cell element, or null if it
  81. * is not an instance of the given class. Cannot be called for cells that do
  82. * not contain a widget.
  83. *
  84. * @param e
  85. * the element inside to find a widget
  86. * @param klass
  87. * the type of the widget to find
  88. * @return the widget inside the element, or null if its type does not match
  89. */
  90. protected static <W extends Widget> W getWidget(TableCellElement e,
  91. Class<W> klass) {
  92. W w = WidgetUtil.findWidget(e.getFirstChildElement(), klass);
  93. assert w == null || w.getElement() == e.getFirstChildElement() : "Widget not found inside cell";
  94. return w;
  95. }
  96. }