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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.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<T>(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 row = targetCell.getRow();
  48. int columnIndexDOM = targetCell.getColumn();
  49. Column<?, T> column = grid.getVisibleColumns().get(columnIndexDOM);
  50. // Row objects only make sense for body section of Grid.
  51. T rowObject;
  52. if (section == Section.BODY) {
  53. rowObject = grid.getDataSource().getRow(row);
  54. } else {
  55. rowObject = null;
  56. }
  57. // At least for now we don't need to have the actual TableRowElement
  58. // available.
  59. getRowReference().set(row, rowObject, null);
  60. int columnIndex = grid.getColumns().indexOf(column);
  61. set(columnIndexDOM, columnIndex, column);
  62. this.element = targetCell.getElement();
  63. this.section = section;
  64. }
  65. @Override
  66. public TableCellElement getElement() {
  67. return element;
  68. }
  69. /**
  70. * Is the cell reference for a cell in the header of the Grid.
  71. *
  72. * @since 7.5
  73. * @return <code>true</true> if referenced cell is in the header,
  74. * <code>false</code> if not
  75. */
  76. public boolean isHeader() {
  77. return section == Section.HEADER;
  78. }
  79. /**
  80. * Is the cell reference for a cell in the body of the Grid.
  81. *
  82. * @since 7.5
  83. * @return <code>true</true> if referenced cell is in the body,
  84. * <code>false</code> if not
  85. */
  86. public boolean isBody() {
  87. return section == Section.BODY;
  88. }
  89. /**
  90. * Is the cell reference for a cell in the footer of the Grid.
  91. *
  92. * @since 7.5
  93. * @return <code>true</true> if referenced cell is in the footer,
  94. * <code>false</code> if not
  95. */
  96. public boolean isFooter() {
  97. return section == Section.FOOTER;
  98. }
  99. /**
  100. * Gets the Grid section where the referenced cell is.
  101. *
  102. * @since 7.5
  103. * @return grid section
  104. */
  105. public Section getSection() {
  106. return section;
  107. }
  108. }