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.

GridSortIndicator.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.tests.components.grid;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import com.vaadin.data.sort.SortOrder;
  20. import com.vaadin.event.SortEvent;
  21. import com.vaadin.event.SortEvent.SortListener;
  22. import com.vaadin.server.VaadinRequest;
  23. import com.vaadin.shared.data.sort.SortDirection;
  24. import com.vaadin.tests.components.AbstractTestUI;
  25. import com.vaadin.ui.Grid;
  26. /*
  27. * Test UI for checking that sort indicators of a Grid are updated when the sort order is changed by a
  28. * SortListener.
  29. */
  30. public class GridSortIndicator extends AbstractTestUI {
  31. @Override
  32. protected void setup(VaadinRequest request) {
  33. final Grid g = getGrid();
  34. addComponent(g);
  35. g.addSortListener(new SortListener() {
  36. private SortDirection oldSortDirection = null;
  37. @Override
  38. public void sort(SortEvent event) {
  39. List<SortOrder> currentSortOrder = new ArrayList<SortOrder>(
  40. event.getSortOrder());
  41. if (currentSortOrder.size() == 1) {
  42. // If the name column was clicked, set a new sort order for
  43. // both columns. Otherwise, revert to oldSortDirection if it
  44. // is not null.
  45. List<SortOrder> newSortOrder = new ArrayList<SortOrder>();
  46. SortDirection newSortDirection = oldSortDirection;
  47. if (currentSortOrder.get(0).getPropertyId().equals("Name")) {
  48. newSortDirection = SortDirection.ASCENDING
  49. .equals(oldSortDirection) ? SortDirection.DESCENDING
  50. : SortDirection.ASCENDING;
  51. }
  52. if (newSortDirection != null) {
  53. newSortOrder
  54. .add(new SortOrder("Name", newSortDirection));
  55. newSortOrder.add(new SortOrder("Value",
  56. newSortDirection));
  57. g.setSortOrder(newSortOrder);
  58. }
  59. oldSortDirection = newSortDirection;
  60. }
  61. }
  62. });
  63. }
  64. private final Grid getGrid() {
  65. Grid g = new Grid();
  66. g.addColumn("Name");
  67. g.addColumn("Value", Integer.class);
  68. g.addRow(new Object[] { "a", 4 });
  69. g.addRow(new Object[] { "b", 5 });
  70. g.addRow(new Object[] { "c", 3 });
  71. g.addRow(new Object[] { "a", 6 });
  72. g.addRow(new Object[] { "a", 2 });
  73. g.addRow(new Object[] { "c", 7 });
  74. g.addRow(new Object[] { "b", 1 });
  75. return g;
  76. }
  77. @Override
  78. public String getTestDescription() {
  79. return "When the first column is the primary sort column, both columns should have "
  80. + "a sort indicator with the same sort direction. Clicking on the right column "
  81. + "in that state should have no effect.";
  82. }
  83. @Override
  84. public Integer getTicketNumber() {
  85. return 17440;
  86. }
  87. }