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.

EventCellReference.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2000-2021 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.widget.grid;
  17. import com.google.gwt.dom.client.TableCellElement;
  18. import com.vaadin.client.widget.escalator.Cell;
  19. import com.vaadin.client.widgets.Grid;
  20. import com.vaadin.client.widgets.Grid.Column;
  21. import com.vaadin.shared.ui.grid.GridConstants.Section;
  22. /**
  23. * A data class which contains information which identifies a cell being the
  24. * target of an event from {@link Grid}.
  25. * <p>
  26. * Since this class follows the <code>Flyweight</code>-pattern any instance of
  27. * this object is subject to change without the user knowing it and so should
  28. * not be stored anywhere outside of the method providing these instances.
  29. *
  30. * @param <T>
  31. * The row type of the grid. The row type is the POJO type from where
  32. * the data is retrieved into the column cells.
  33. *
  34. * @since 7.4
  35. * @author Vaadin Ltd
  36. */
  37. public class EventCellReference<T> extends CellReference<T> {
  38. private Section section;
  39. private TableCellElement element;
  40. /**
  41. * Constructs a cell reference for an event targeting a grid cell. Needs to
  42. * be populated using {@link #set(Cell, Section)}.
  43. *
  44. * @param grid
  45. * the grid the event originates from
  46. *
  47. * @see EventCellReference
  48. */
  49. public EventCellReference(Grid<T> grid) {
  50. super(new RowReference<>(grid));
  51. }
  52. /**
  53. * Configures this CellReference and its internal RowReference to point to
  54. * the given Cell.
  55. *
  56. * @param targetCell
  57. * the cell to point to
  58. * @param section
  59. * the section the cell belongs to
  60. */
  61. public void set(Cell targetCell, Section section) {
  62. Grid<T> grid = getGrid();
  63. int columnIndexDOM = targetCell.getColumn();
  64. Column<?, T> column = null;
  65. if (columnIndexDOM >= 0
  66. && columnIndexDOM < grid.getVisibleColumns().size()) {
  67. column = grid.getVisibleColumns().get(columnIndexDOM);
  68. }
  69. int row = targetCell.getRow();
  70. // Row objects only make sense for body section of Grid.
  71. T rowObject;
  72. if (section == Section.BODY && row >= 0
  73. && row < grid.getDataSource().size()) {
  74. rowObject = grid.getDataSource().getRow(row);
  75. } else {
  76. rowObject = null;
  77. }
  78. // At least for now we don't need to have the actual TableRowElement
  79. // available.
  80. getRowReference().set(row, rowObject, null);
  81. int columnIndex = grid.getColumns().indexOf(column);
  82. set(columnIndexDOM, columnIndex, column);
  83. this.element = targetCell.getElement();
  84. this.section = section;
  85. }
  86. @Override
  87. public TableCellElement getElement() {
  88. return element;
  89. }
  90. /**
  91. * Is the cell reference for a cell in the header of the Grid.
  92. *
  93. * @since 7.5
  94. * @return <code>true</code> if referenced cell is in the header,
  95. * <code>false</code> if not
  96. */
  97. public boolean isHeader() {
  98. return section == Section.HEADER;
  99. }
  100. /**
  101. * Is the cell reference for a cell in the body of the Grid.
  102. *
  103. * @since 7.5
  104. * @return <code>true</code> if referenced cell is in the body,
  105. * <code>false</code> if not
  106. */
  107. public boolean isBody() {
  108. return section == Section.BODY;
  109. }
  110. /**
  111. * Is the cell reference for a cell in the footer of the Grid.
  112. *
  113. * @since 7.5
  114. * @return <code>true</code> if referenced cell is in the footer,
  115. * <code>false</code> if not
  116. */
  117. public boolean isFooter() {
  118. return section == Section.FOOTER;
  119. }
  120. /**
  121. * Gets the Grid section where the referenced cell is.
  122. *
  123. * @since 7.5
  124. * @return grid section
  125. */
  126. public Section getSection() {
  127. return section;
  128. }
  129. }