您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GridDetailsDetach.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.tests.components.grid;
  17. import com.vaadin.data.util.BeanItemContainer;
  18. import com.vaadin.event.ItemClickEvent;
  19. import com.vaadin.event.ItemClickEvent.ItemClickListener;
  20. import com.vaadin.server.VaadinRequest;
  21. import com.vaadin.tests.components.AbstractTestUI;
  22. import com.vaadin.ui.Button;
  23. import com.vaadin.ui.Button.ClickEvent;
  24. import com.vaadin.ui.Component;
  25. import com.vaadin.ui.Label;
  26. import com.vaadin.ui.LegacyGrid;
  27. import com.vaadin.ui.LegacyGrid.DetailsGenerator;
  28. import com.vaadin.ui.LegacyGrid.RowReference;
  29. import com.vaadin.ui.LegacyGrid.SelectionMode;
  30. import com.vaadin.ui.VerticalLayout;
  31. public class GridDetailsDetach extends AbstractTestUI {
  32. private LegacyGrid currentGrid;
  33. @Override
  34. protected void setup(VaadinRequest request) {
  35. VerticalLayout layout = new VerticalLayout();
  36. layout.setSizeFull();
  37. Button button = new Button("Test");
  38. layout.addComponent(button);
  39. layout.setExpandRatio(button, 0f);
  40. currentGrid = generateGrid();
  41. final VerticalLayout gridContainer = new VerticalLayout();
  42. gridContainer.addComponent(currentGrid);
  43. button.addClickListener(new Button.ClickListener() {
  44. @Override
  45. public void buttonClick(ClickEvent event) {
  46. gridContainer.replaceComponent(currentGrid, new Label("Foo"));
  47. }
  48. });
  49. layout.addComponent(
  50. new Button("Reattach Grid", new Button.ClickListener() {
  51. @Override
  52. public void buttonClick(ClickEvent event) {
  53. gridContainer.removeAllComponents();
  54. gridContainer.addComponent(currentGrid);
  55. }
  56. }));
  57. layout.addComponent(gridContainer);
  58. layout.setExpandRatio(gridContainer, 1f);
  59. addComponent(layout);
  60. }
  61. private LegacyGrid generateGrid() {
  62. BeanItemContainer<GridExampleBean> container = new BeanItemContainer<GridExampleBean>(
  63. GridExampleBean.class);
  64. for (int i = 0; i < 1000; i++) {
  65. container.addItem(new GridExampleBean("Bean " + i, i * i, i / 10d));
  66. }
  67. final LegacyGrid grid = new LegacyGrid(container);
  68. grid.setColumnOrder("name", "amount", "count");
  69. grid.setSizeFull();
  70. grid.setSelectionMode(SelectionMode.NONE);
  71. grid.setDetailsGenerator(new DetailsGenerator() {
  72. @Override
  73. public Component getDetails(RowReference rowReference) {
  74. final GridExampleBean bean = (GridExampleBean) rowReference
  75. .getItemId();
  76. VerticalLayout layout = new VerticalLayout(
  77. new Label("Extra data for " + bean.getName()));
  78. layout.setMargin(true);
  79. return layout;
  80. }
  81. });
  82. grid.addItemClickListener(new ItemClickListener() {
  83. @Override
  84. public void itemClick(ItemClickEvent event) {
  85. Object itemId = event.getItemId();
  86. grid.setDetailsVisible(itemId, !grid.isDetailsVisible(itemId));
  87. }
  88. });
  89. return grid;
  90. }
  91. public class GridExampleBean {
  92. private String name;
  93. private int count;
  94. private double amount;
  95. public GridExampleBean() {
  96. }
  97. public GridExampleBean(String name, int count, double amount) {
  98. this.name = name;
  99. this.count = count;
  100. this.amount = amount;
  101. }
  102. public String getName() {
  103. return name;
  104. }
  105. public int getCount() {
  106. return count;
  107. }
  108. public double getAmount() {
  109. return amount;
  110. }
  111. public void setName(String name) {
  112. this.name = name;
  113. }
  114. public void setCount(int count) {
  115. this.count = count;
  116. }
  117. public void setAmount(double amount) {
  118. this.amount = amount;
  119. }
  120. }
  121. }