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.

VTestGrid.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.vaadin.tests.widgetset.client.grid;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.google.gwt.user.client.ui.Composite;
  5. import com.vaadin.client.ui.grid.Cell;
  6. import com.vaadin.client.ui.grid.ColumnConfiguration;
  7. import com.vaadin.client.ui.grid.Escalator;
  8. import com.vaadin.client.ui.grid.EscalatorUpdater;
  9. import com.vaadin.client.ui.grid.Row;
  10. import com.vaadin.client.ui.grid.RowContainer;
  11. import com.vaadin.shared.ui.grid.ScrollDestination;
  12. public class VTestGrid extends Composite {
  13. private static class Data {
  14. private int columnCounter = 0;
  15. private int rowCounter = 0;
  16. private final List<Integer> columns = new ArrayList<Integer>();
  17. private final List<Integer> rows = new ArrayList<Integer>();
  18. @SuppressWarnings("boxing")
  19. public void insertRows(final int offset, final int amount) {
  20. final List<Integer> newRows = new ArrayList<Integer>();
  21. for (int i = 0; i < amount; i++) {
  22. newRows.add(rowCounter++);
  23. }
  24. rows.addAll(offset, newRows);
  25. }
  26. @SuppressWarnings("boxing")
  27. public void insertColumns(final int offset, final int amount) {
  28. final List<Integer> newColumns = new ArrayList<Integer>();
  29. for (int i = 0; i < amount; i++) {
  30. newColumns.add(columnCounter++);
  31. }
  32. columns.addAll(offset, newColumns);
  33. }
  34. public EscalatorUpdater createHeaderUpdater() {
  35. return new EscalatorUpdater() {
  36. @Override
  37. public void updateCells(final Row row,
  38. final Iterable<Cell> cellsToUpdate) {
  39. for (final Cell cell : cellsToUpdate) {
  40. if (cell.getColumn() % 3 == 0) {
  41. cell.setColSpan(2);
  42. }
  43. final Integer columnName = columns
  44. .get(cell.getColumn());
  45. cell.getElement().setInnerText("Header " + columnName);
  46. }
  47. }
  48. };
  49. }
  50. public EscalatorUpdater createFooterUpdater() {
  51. return new EscalatorUpdater() {
  52. @Override
  53. public void updateCells(final Row row,
  54. final Iterable<Cell> cellsToUpdate) {
  55. for (final Cell cell : cellsToUpdate) {
  56. if (cell.getColumn() % 3 == 1) {
  57. cell.setColSpan(2);
  58. }
  59. final Integer columnName = columns
  60. .get(cell.getColumn());
  61. cell.getElement().setInnerText("Footer " + columnName);
  62. }
  63. }
  64. };
  65. }
  66. public EscalatorUpdater createBodyUpdater() {
  67. return new EscalatorUpdater() {
  68. private int i = 0;
  69. public void renderCell(final Cell cell) {
  70. final Integer columnName = columns.get(cell.getColumn());
  71. final Integer rowName = rows.get(cell.getRow());
  72. final String cellInfo = columnName + "," + rowName + " ("
  73. + i + ")";
  74. if (cell.getColumn() > 0) {
  75. cell.getElement().setInnerText("Cell: " + cellInfo);
  76. } else {
  77. cell.getElement().setInnerText(
  78. "Row " + cell.getRow() + ": " + cellInfo);
  79. }
  80. if (cell.getColumn() % 3 == cell.getRow() % 3) {
  81. cell.setColSpan(3);
  82. }
  83. final double c = i * .1;
  84. final int r = (int) ((Math.cos(c) + 1) * 128);
  85. final int g = (int) ((Math.cos(c / Math.PI) + 1) * 128);
  86. final int b = (int) ((Math.cos(c / (Math.PI * 2)) + 1) * 128);
  87. cell.getElement()
  88. .getStyle()
  89. .setBackgroundColor(
  90. "rgb(" + r + "," + g + "," + b + ")");
  91. if ((r * .8 + g * 1.3 + b * .9) / 3 < 127) {
  92. cell.getElement().getStyle().setColor("white");
  93. } else {
  94. cell.getElement().getStyle().clearColor();
  95. }
  96. i++;
  97. }
  98. @Override
  99. public void updateCells(final Row row,
  100. final Iterable<Cell> cellsToUpdate) {
  101. for (final Cell cell : cellsToUpdate) {
  102. renderCell(cell);
  103. }
  104. }
  105. };
  106. }
  107. public void removeRows(final int offset, final int amount) {
  108. for (int i = 0; i < amount; i++) {
  109. rows.remove(offset);
  110. }
  111. }
  112. public void removeColumns(final int offset, final int amount) {
  113. for (int i = 0; i < amount; i++) {
  114. columns.remove(offset);
  115. }
  116. }
  117. }
  118. private final Escalator escalator = new Escalator();
  119. private final Data data = new Data();
  120. public VTestGrid() {
  121. initWidget(escalator);
  122. final RowContainer header = escalator.getHeader();
  123. header.setEscalatorUpdater(data.createHeaderUpdater());
  124. header.insertRows(0, 1);
  125. final RowContainer footer = escalator.getFooter();
  126. footer.setEscalatorUpdater(data.createFooterUpdater());
  127. footer.insertRows(0, 1);
  128. escalator.getBody().setEscalatorUpdater(data.createBodyUpdater());
  129. insertRows(0, 100);
  130. insertColumns(0, 10);
  131. setWidth(TestGridState.DEFAULT_WIDTH);
  132. setHeight(TestGridState.DEFAULT_HEIGHT);
  133. }
  134. public void insertRows(final int offset, final int number) {
  135. data.insertRows(offset, number);
  136. escalator.getBody().insertRows(offset, number);
  137. }
  138. public void insertColumns(final int offset, final int number) {
  139. data.insertColumns(offset, number);
  140. escalator.getColumnConfiguration().insertColumns(offset, number);
  141. }
  142. public ColumnConfiguration getColumnConfiguration() {
  143. return escalator.getColumnConfiguration();
  144. }
  145. public void scrollToRow(final int index,
  146. final ScrollDestination destination, final int padding) {
  147. escalator.scrollToRow(index, destination, padding);
  148. }
  149. public void scrollToColumn(final int index,
  150. final ScrollDestination destination, final int padding) {
  151. escalator.scrollToColumn(index, destination, padding);
  152. }
  153. public void removeRows(final int offset, final int amount) {
  154. data.removeRows(offset, amount);
  155. escalator.getBody().removeRows(offset, amount);
  156. }
  157. public void removeColumns(final int offset, final int amount) {
  158. data.removeColumns(offset, amount);
  159. escalator.getColumnConfiguration().removeColumns(offset, amount);
  160. }
  161. @Override
  162. public void setWidth(String width) {
  163. escalator.setWidth(width);
  164. }
  165. @Override
  166. public void setHeight(String height) {
  167. escalator.setHeight(height);
  168. }
  169. public RowContainer getHeader() {
  170. return escalator.getHeader();
  171. }
  172. public RowContainer getBody() {
  173. return escalator.getBody();
  174. }
  175. public RowContainer getFooter() {
  176. return escalator.getFooter();
  177. }
  178. public void calculateColumnWidths() {
  179. escalator.calculateColumnWidths();
  180. }
  181. }