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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.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. * @since 7.4
  31. * @author Vaadin Ltd
  32. */
  33. public class EventCellReference<T> extends CellReference<T> {
  34. private Section section;
  35. private TableCellElement element;
  36. public EventCellReference(Grid<T> grid) {
  37. super(new RowReference<>(grid));
  38. }
  39. /**
  40. * Sets the RowReference and CellReference to point to given Cell.
  41. *
  42. * @param targetCell
  43. * cell to point to
  44. */
  45. public void set(Cell targetCell, Section section) {
  46. Grid<T> grid = getGrid();
  47. int columnIndexDOM = targetCell.getColumn();
  48. Column<?, T> column = null;
  49. if (columnIndexDOM >= 0
  50. && columnIndexDOM < grid.getVisibleColumns().size()) {
  51. column = grid.getVisibleColumns().get(columnIndexDOM);
  52. }
  53. int row = targetCell.getRow();
  54. // Row objects only make sense for body section of Grid.
  55. T rowObject;
  56. if (section == Section.BODY && row >= 0
  57. && row < grid.getDataSource().size()) {
  58. rowObject = grid.getDataSource().getRow(row);
  59. } else {
  60. rowObject = null;
  61. }
  62. // At least for now we don't need to have the actual TableRowElement
  63. // available.
  64. getRowReference().set(row, rowObject, null);
  65. int columnIndex = grid.getColumns().indexOf(column);
  66. set(columnIndexDOM, columnIndex, column);
  67. this.element = targetCell.getElement();
  68. this.section = section;
  69. }
  70. @Override
  71. public TableCellElement getElement() {
  72. return element;
  73. }
  74. /**
  75. * Is the cell reference for a cell in the header of the Grid.
  76. *
  77. * @since 7.5
  78. * @return <code>true</true> if referenced cell is in the header,
  79. * <code>false</code> if not
  80. */
  81. public boolean isHeader() {
  82. return section == Section.HEADER;
  83. }
  84. /**
  85. * Is the cell reference for a cell in the body of the Grid.
  86. *
  87. * @since 7.5
  88. * @return <code>true</true> if referenced cell is in the body,
  89. * <code>false</code> if not
  90. */
  91. public boolean isBody() {
  92. return section == Section.BODY;
  93. }
  94. /**
  95. * Is the cell reference for a cell in the footer of the Grid.
  96. *
  97. * @since 7.5
  98. * @return <code>true</true> if referenced cell is in the footer,
  99. * <code>false</code> if not
  100. */
  101. public boolean isFooter() {
  102. return section == Section.FOOTER;
  103. }
  104. /**
  105. * Gets the Grid section where the referenced cell is.
  106. *
  107. * @since 7.5
  108. * @return grid section
  109. */
  110. public Section getSection() {
  111. return section;
  112. }
  113. }