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.

GridBasicFeatures.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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.basicfeatures;
  17. import java.text.DecimalFormat;
  18. import java.text.DecimalFormatSymbols;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.LinkedHashMap;
  23. import java.util.List;
  24. import java.util.Locale;
  25. import java.util.Random;
  26. import com.vaadin.data.Container.Filter;
  27. import com.vaadin.data.Item;
  28. import com.vaadin.data.Property;
  29. import com.vaadin.data.util.IndexedContainer;
  30. import com.vaadin.shared.ui.grid.GridStaticCellType;
  31. import com.vaadin.shared.ui.grid.HeightMode;
  32. import com.vaadin.shared.ui.grid.SortDirection;
  33. import com.vaadin.tests.components.AbstractComponentTest;
  34. import com.vaadin.ui.Button;
  35. import com.vaadin.ui.Button.ClickEvent;
  36. import com.vaadin.ui.Button.ClickListener;
  37. import com.vaadin.ui.components.grid.Grid;
  38. import com.vaadin.ui.components.grid.Grid.SelectionMode;
  39. import com.vaadin.ui.components.grid.GridColumn;
  40. import com.vaadin.ui.components.grid.GridFooter;
  41. import com.vaadin.ui.components.grid.GridFooter.FooterCell;
  42. import com.vaadin.ui.components.grid.GridHeader;
  43. import com.vaadin.ui.components.grid.GridHeader.HeaderCell;
  44. import com.vaadin.ui.components.grid.GridHeader.HeaderRow;
  45. import com.vaadin.ui.components.grid.SortOrderChangeEvent;
  46. import com.vaadin.ui.components.grid.SortOrderChangeListener;
  47. import com.vaadin.ui.components.grid.renderers.DateRenderer;
  48. import com.vaadin.ui.components.grid.renderers.HtmlRenderer;
  49. import com.vaadin.ui.components.grid.renderers.NumberRenderer;
  50. import com.vaadin.ui.components.grid.sort.Sort;
  51. import com.vaadin.ui.components.grid.sort.SortOrder;
  52. /**
  53. * Tests the basic features like columns, footers and headers
  54. *
  55. * @since
  56. * @author Vaadin Ltd
  57. */
  58. public class GridBasicFeatures extends AbstractComponentTest<Grid> {
  59. private static final int MANUALLY_FORMATTED_COLUMNS = 5;
  60. public static final int COLUMNS = 12;
  61. public static final int ROWS = 1000;
  62. private int columnGroupRows = 0;
  63. private IndexedContainer ds;
  64. @Override
  65. @SuppressWarnings("unchecked")
  66. protected Grid constructComponent() {
  67. // Build data source
  68. ds = new IndexedContainer() {
  69. @Override
  70. public List<Object> getItemIds(int startIndex, int numberOfIds) {
  71. log("Requested items " + startIndex + " - "
  72. + (startIndex + numberOfIds));
  73. return super.getItemIds(startIndex, numberOfIds);
  74. }
  75. };
  76. {
  77. int col = 0;
  78. for (; col < COLUMNS - MANUALLY_FORMATTED_COLUMNS; col++) {
  79. ds.addContainerProperty(getColumnProperty(col), String.class,
  80. "");
  81. }
  82. ds.addContainerProperty(getColumnProperty(col++), Integer.class,
  83. Integer.valueOf(0));
  84. ds.addContainerProperty(getColumnProperty(col++), Date.class,
  85. new Date());
  86. ds.addContainerProperty(getColumnProperty(col++), String.class, "");
  87. // Random numbers
  88. ds.addContainerProperty(getColumnProperty(col++), Integer.class, 0);
  89. ds.addContainerProperty(getColumnProperty(col++), Integer.class, 0);
  90. }
  91. {
  92. Random rand = new Random();
  93. rand.setSeed(13334);
  94. long timestamp = 0;
  95. for (int row = 0; row < ROWS; row++) {
  96. Item item = ds.addItem(Integer.valueOf(row));
  97. int col = 0;
  98. for (; col < COLUMNS - MANUALLY_FORMATTED_COLUMNS; col++) {
  99. item.getItemProperty(getColumnProperty(col)).setValue(
  100. "(" + row + ", " + col + ")");
  101. }
  102. item.getItemProperty(getColumnProperty(1)).setReadOnly(true);
  103. item.getItemProperty(getColumnProperty(col++)).setValue(
  104. Integer.valueOf(row));
  105. item.getItemProperty(getColumnProperty(col++)).setValue(
  106. new Date(timestamp));
  107. timestamp += 91250000; // a bit over a day, just to get
  108. // variation
  109. item.getItemProperty(getColumnProperty(col++)).setValue(
  110. "<b>" + row + "</b>");
  111. // Random numbers
  112. item.getItemProperty(getColumnProperty(col++)).setValue(
  113. rand.nextInt());
  114. // Random between 0 - 5 to test multisorting
  115. item.getItemProperty(getColumnProperty(col++)).setValue(
  116. rand.nextInt(5));
  117. }
  118. }
  119. // Create grid
  120. Grid grid = new Grid(ds);
  121. {
  122. int col = grid.getContainerDatasource().getContainerPropertyIds()
  123. .size()
  124. - MANUALLY_FORMATTED_COLUMNS;
  125. grid.getColumn(getColumnProperty(col++)).setRenderer(
  126. new NumberRenderer(new DecimalFormat("0,000.00",
  127. DecimalFormatSymbols.getInstance(new Locale("fi",
  128. "FI")))));
  129. grid.getColumn(getColumnProperty(col++)).setRenderer(
  130. new DateRenderer(new SimpleDateFormat("dd.MM.yy HH:mm")));
  131. grid.getColumn(getColumnProperty(col++)).setRenderer(
  132. new HtmlRenderer());
  133. grid.getColumn(getColumnProperty(col++)).setRenderer(
  134. new NumberRenderer());
  135. grid.getColumn(getColumnProperty(col++)).setRenderer(
  136. new NumberRenderer());
  137. }
  138. // Create footer
  139. GridFooter footer = grid.getFooter();
  140. footer.appendRow();
  141. footer.setVisible(false);
  142. // Add footer values (header values are automatically created)
  143. for (int col = 0; col < COLUMNS; col++) {
  144. footer.getRow(0).getCell(getColumnProperty(col))
  145. .setText("Footer " + col);
  146. }
  147. // Set varying column widths
  148. for (int col = 0; col < COLUMNS; col++) {
  149. grid.getColumn(getColumnProperty(col)).setWidth(100 + col * 50);
  150. }
  151. grid.addSortOrderChangeListener(new SortOrderChangeListener() {
  152. @Override
  153. public void sortOrderChange(SortOrderChangeEvent event) {
  154. String origin;
  155. switch (event.getOriginator()) {
  156. case API:
  157. origin = "API";
  158. break;
  159. case INTERNAL:
  160. origin = "INTERNAL";
  161. break;
  162. case USER:
  163. origin = "USER";
  164. break;
  165. default:
  166. origin = "!!! ERROR !!!";
  167. break;
  168. }
  169. log("Sort order: " + event.getSortOrder() + " by " + origin);
  170. }
  171. });
  172. grid.setSelectionMode(SelectionMode.NONE);
  173. grid.getEditorRow().setPropertyEditable(getColumnProperty(3), false);
  174. createGridActions();
  175. createColumnActions();
  176. createHeaderActions();
  177. createFooterActions();
  178. createRowActions();
  179. createEditorRowActions();
  180. addHeightActions();
  181. createClickAction("Column 1 starts with \"(23\"", "Filter",
  182. new Command<Grid, Void>() {
  183. @Override
  184. public void execute(Grid grid, Void value, Object data) {
  185. ds.addContainerFilter(new Filter() {
  186. @Override
  187. public boolean passesFilter(Object itemId, Item item)
  188. throws UnsupportedOperationException {
  189. return item.getItemProperty("Column 1")
  190. .getValue().toString()
  191. .startsWith("(23");
  192. }
  193. @Override
  194. public boolean appliesToProperty(Object propertyId) {
  195. return propertyId.equals("Column 1");
  196. }
  197. });
  198. }
  199. }, null);
  200. return grid;
  201. }
  202. protected void createGridActions() {
  203. LinkedHashMap<String, String> primaryStyleNames = new LinkedHashMap<String, String>();
  204. primaryStyleNames.put("v-grid", "v-grid");
  205. primaryStyleNames.put("v-escalator", "v-escalator");
  206. primaryStyleNames.put("my-grid", "my-grid");
  207. createMultiClickAction("Primary style name", "State",
  208. primaryStyleNames, new Command<Grid, String>() {
  209. @Override
  210. public void execute(Grid grid, String value, Object data) {
  211. grid.setPrimaryStyleName(value);
  212. }
  213. }, primaryStyleNames.get("v-grid"));
  214. LinkedHashMap<String, SelectionMode> selectionModes = new LinkedHashMap<String, Grid.SelectionMode>();
  215. selectionModes.put("single", SelectionMode.SINGLE);
  216. selectionModes.put("multi", SelectionMode.MULTI);
  217. selectionModes.put("none", SelectionMode.NONE);
  218. createSelectAction("Selection mode", "State", selectionModes, "none",
  219. new Command<Grid, Grid.SelectionMode>() {
  220. @Override
  221. public void execute(Grid grid, SelectionMode selectionMode,
  222. Object data) {
  223. grid.setSelectionMode(selectionMode);
  224. }
  225. });
  226. LinkedHashMap<String, List<SortOrder>> sortableProperties = new LinkedHashMap<String, List<SortOrder>>();
  227. for (Object propertyId : ds.getSortableContainerPropertyIds()) {
  228. sortableProperties.put(propertyId + ", ASC", Sort.by(propertyId)
  229. .build());
  230. sortableProperties.put(propertyId + ", DESC",
  231. Sort.by(propertyId, SortDirection.DESCENDING).build());
  232. }
  233. createSelectAction("Sort by column", "State", sortableProperties,
  234. "Column 9, ascending", new Command<Grid, List<SortOrder>>() {
  235. @Override
  236. public void execute(Grid grid, List<SortOrder> sortOrder,
  237. Object data) {
  238. grid.setSortOrder(sortOrder);
  239. }
  240. });
  241. createBooleanAction("Reverse Grid Columns", "State", false,
  242. new Command<Grid, Boolean>() {
  243. @Override
  244. public void execute(Grid c, Boolean value, Object data) {
  245. List<Object> ids = new ArrayList<Object>();
  246. ids.addAll(ds.getContainerPropertyIds());
  247. if (!value) {
  248. c.setColumnOrder(ids.toArray());
  249. } else {
  250. Object[] idsArray = new Object[ids.size()];
  251. for (int i = 0; i < ids.size(); ++i) {
  252. idsArray[i] = ids.get((ids.size() - 1) - i);
  253. }
  254. c.setColumnOrder(idsArray);
  255. }
  256. }
  257. });
  258. }
  259. protected void createHeaderActions() {
  260. createCategory("Header", null);
  261. createBooleanAction("Visible", "Header", true,
  262. new Command<Grid, Boolean>() {
  263. @Override
  264. public void execute(Grid grid, Boolean value, Object data) {
  265. grid.getHeader().setVisible(value);
  266. }
  267. });
  268. LinkedHashMap<String, String> defaultRows = new LinkedHashMap<String, String>();
  269. defaultRows.put("Top", "Top");
  270. defaultRows.put("Bottom", "Bottom");
  271. defaultRows.put("Unset", "Unset");
  272. createMultiClickAction("Default row", "Header", defaultRows,
  273. new Command<Grid, String>() {
  274. @Override
  275. public void execute(Grid grid, String value, Object data) {
  276. HeaderRow defaultRow = null;
  277. GridHeader header = grid.getHeader();
  278. if (value.equals("Top")) {
  279. defaultRow = header.getRow(0);
  280. } else if (value.equals("Bottom")) {
  281. defaultRow = header.getRow(header.getRowCount() - 1);
  282. }
  283. header.setDefaultRow(defaultRow);
  284. }
  285. }, defaultRows.get("Top"));
  286. createClickAction("Prepend row", "Header", new Command<Grid, Object>() {
  287. @Override
  288. public void execute(Grid grid, Object value, Object data) {
  289. grid.getHeader().prependRow();
  290. }
  291. }, null);
  292. createClickAction("Append row", "Header", new Command<Grid, Object>() {
  293. @Override
  294. public void execute(Grid grid, Object value, Object data) {
  295. grid.getHeader().appendRow();
  296. }
  297. }, null);
  298. createClickAction("Remove top row", "Header",
  299. new Command<Grid, Object>() {
  300. @Override
  301. public void execute(Grid grid, Object value, Object data) {
  302. grid.getHeader().removeRow(0);
  303. }
  304. }, null);
  305. createClickAction("Remove bottom row", "Header",
  306. new Command<Grid, Object>() {
  307. @Override
  308. public void execute(Grid grid, Object value, Object data) {
  309. grid.getHeader().removeRow(
  310. grid.getHeader().getRowCount() - 1);
  311. }
  312. }, null);
  313. }
  314. protected void createFooterActions() {
  315. createCategory("Footer", null);
  316. createBooleanAction("Visible", "Footer", false,
  317. new Command<Grid, Boolean>() {
  318. @Override
  319. public void execute(Grid grid, Boolean value, Object data) {
  320. grid.getFooter().setVisible(value);
  321. }
  322. });
  323. createClickAction("Prepend row", "Footer", new Command<Grid, Object>() {
  324. @Override
  325. public void execute(Grid grid, Object value, Object data) {
  326. grid.getFooter().prependRow();
  327. }
  328. }, null);
  329. createClickAction("Append row", "Footer", new Command<Grid, Object>() {
  330. @Override
  331. public void execute(Grid grid, Object value, Object data) {
  332. grid.getFooter().appendRow();
  333. }
  334. }, null);
  335. createClickAction("Remove top row", "Footer",
  336. new Command<Grid, Object>() {
  337. @Override
  338. public void execute(Grid grid, Object value, Object data) {
  339. grid.getFooter().removeRow(0);
  340. }
  341. }, null);
  342. createClickAction("Remove bottom row", "Footer",
  343. new Command<Grid, Object>() {
  344. @Override
  345. public void execute(Grid grid, Object value, Object data) {
  346. grid.getFooter().removeRow(
  347. grid.getFooter().getRowCount() - 1);
  348. }
  349. }, null);
  350. }
  351. protected void createColumnActions() {
  352. createCategory("Columns", null);
  353. for (int c = 0; c < COLUMNS; c++) {
  354. createCategory(getColumnProperty(c), "Columns");
  355. createBooleanAction("Visible", getColumnProperty(c), true,
  356. new Command<Grid, Boolean>() {
  357. @Override
  358. public void execute(Grid grid, Boolean value,
  359. Object columnIndex) {
  360. Object propertyId = getColumnProperty((Integer) columnIndex);
  361. GridColumn column = grid.getColumn(propertyId);
  362. column.setVisible(!column.isVisible());
  363. }
  364. }, c);
  365. createClickAction("Remove", getColumnProperty(c),
  366. new Command<Grid, String>() {
  367. @Override
  368. public void execute(Grid grid, String value, Object data) {
  369. grid.getContainerDatasource()
  370. .removeContainerProperty(
  371. getColumnProperty((Integer) data));
  372. removeCategory("Column " + data);
  373. }
  374. }, null, c);
  375. createClickAction("Freeze", getColumnProperty(c),
  376. new Command<Grid, String>() {
  377. @Override
  378. public void execute(Grid grid, String value, Object data) {
  379. grid.setLastFrozenPropertyId(getColumnProperty((Integer) data));
  380. }
  381. }, null, c);
  382. createBooleanAction("Sortable", getColumnProperty(c), true,
  383. new Command<Grid, Boolean>() {
  384. @Override
  385. public void execute(Grid grid, Boolean value,
  386. Object columnIndex) {
  387. Object propertyId = getColumnProperty((Integer) columnIndex);
  388. GridColumn column = grid.getColumn(propertyId);
  389. column.setSortable(value);
  390. }
  391. }, c);
  392. createCategory("Column " + c + " Width", getColumnProperty(c));
  393. createClickAction("Auto", "Column " + c + " Width",
  394. new Command<Grid, Integer>() {
  395. @Override
  396. public void execute(Grid grid, Integer value,
  397. Object columnIndex) {
  398. Object propertyId = getColumnProperty((Integer) columnIndex);
  399. GridColumn column = grid.getColumn(propertyId);
  400. column.setWidthUndefined();
  401. }
  402. }, -1, c);
  403. for (int w = 50; w < 300; w += 50) {
  404. createClickAction(w + "px", "Column " + c + " Width",
  405. new Command<Grid, Integer>() {
  406. @Override
  407. public void execute(Grid grid, Integer value,
  408. Object columnIndex) {
  409. Object propertyId = getColumnProperty((Integer) columnIndex);
  410. GridColumn column = grid.getColumn(propertyId);
  411. column.setWidth(value);
  412. }
  413. }, w, c);
  414. }
  415. LinkedHashMap<String, GridStaticCellType> defaultRows = new LinkedHashMap<String, GridStaticCellType>();
  416. defaultRows.put("Text Header", GridStaticCellType.TEXT);
  417. defaultRows.put("Html Header ", GridStaticCellType.HTML);
  418. defaultRows.put("Widget Header", GridStaticCellType.WIDGET);
  419. createMultiClickAction("Header Type", getColumnProperty(c),
  420. defaultRows, new Command<Grid, GridStaticCellType>() {
  421. @Override
  422. public void execute(Grid grid,
  423. GridStaticCellType value, Object columnIndex) {
  424. final Object propertyId = getColumnProperty((Integer) columnIndex);
  425. final HeaderCell cell = grid.getHeader()
  426. .getDefaultRow().getCell(propertyId);
  427. switch (value) {
  428. case TEXT:
  429. cell.setText("Text Header");
  430. break;
  431. case HTML:
  432. cell.setHtml("HTML Header");
  433. break;
  434. case WIDGET:
  435. cell.setComponent(new Button("Button Header",
  436. new ClickListener() {
  437. @Override
  438. public void buttonClick(
  439. ClickEvent event) {
  440. log("Button clicked!");
  441. }
  442. }));
  443. default:
  444. break;
  445. }
  446. }
  447. }, c);
  448. defaultRows = new LinkedHashMap<String, GridStaticCellType>();
  449. defaultRows.put("Text Footer", GridStaticCellType.TEXT);
  450. defaultRows.put("Html Footer", GridStaticCellType.HTML);
  451. defaultRows.put("Widget Footer", GridStaticCellType.WIDGET);
  452. createMultiClickAction("Footer Type", getColumnProperty(c),
  453. defaultRows, new Command<Grid, GridStaticCellType>() {
  454. @Override
  455. public void execute(Grid grid,
  456. GridStaticCellType value, Object columnIndex) {
  457. final Object propertyId = getColumnProperty((Integer) columnIndex);
  458. final FooterCell cell = grid.getFooter().getRow(0)
  459. .getCell(propertyId);
  460. switch (value) {
  461. case TEXT:
  462. cell.setText("Text Footer");
  463. break;
  464. case HTML:
  465. cell.setHtml("HTML Footer");
  466. break;
  467. case WIDGET:
  468. cell.setComponent(new Button("Button Footer",
  469. new ClickListener() {
  470. @Override
  471. public void buttonClick(
  472. ClickEvent event) {
  473. log("Button clicked!");
  474. }
  475. }));
  476. default:
  477. break;
  478. }
  479. }
  480. }, c);
  481. }
  482. }
  483. private static String getColumnProperty(int c) {
  484. return "Column " + c;
  485. }
  486. protected void createRowActions() {
  487. createCategory("Body rows", null);
  488. final Command<Grid, String> newRowCommand = new Command<Grid, String>() {
  489. @Override
  490. public void execute(Grid c, String value, Object data) {
  491. Item item = ds.addItemAt(0, new Object());
  492. for (int i = 0; i < COLUMNS; i++) {
  493. Class<?> type = ds.getType(getColumnProperty(i));
  494. if (String.class.isAssignableFrom(type)) {
  495. Property<String> itemProperty = getProperty(item, i);
  496. itemProperty.setValue("newcell: " + i);
  497. } else if (Integer.class.isAssignableFrom(type)) {
  498. Property<Integer> itemProperty = getProperty(item, i);
  499. itemProperty.setValue(Integer.valueOf(i));
  500. } else {
  501. // let the default value be taken implicitly.
  502. }
  503. }
  504. }
  505. private <T extends Object> Property<T> getProperty(Item item, int i) {
  506. @SuppressWarnings("unchecked")
  507. Property<T> itemProperty = item
  508. .getItemProperty(getColumnProperty(i));
  509. return itemProperty;
  510. }
  511. };
  512. createClickAction("Add 18 rows", "Body rows",
  513. new Command<Grid, String>() {
  514. @Override
  515. public void execute(Grid c, String value, Object data) {
  516. for (int i = 0; i < 18; i++) {
  517. newRowCommand.execute(c, value, data);
  518. }
  519. }
  520. }, null);
  521. createClickAction("Add first row", "Body rows", newRowCommand, null);
  522. createClickAction("Remove first row", "Body rows",
  523. new Command<Grid, String>() {
  524. @Override
  525. public void execute(Grid c, String value, Object data) {
  526. Object firstItemId = ds.getIdByIndex(0);
  527. ds.removeItem(firstItemId);
  528. }
  529. }, null);
  530. createClickAction("Modify first row (getItemProperty)", "Body rows",
  531. new Command<Grid, String>() {
  532. @SuppressWarnings("unchecked")
  533. @Override
  534. public void execute(Grid c, String value, Object data) {
  535. Object firstItemId = ds.getIdByIndex(0);
  536. Item item = ds.getItem(firstItemId);
  537. for (int i = 0; i < COLUMNS; i++) {
  538. Property<?> property = item
  539. .getItemProperty(getColumnProperty(i));
  540. if (property.getType().equals(String.class)) {
  541. ((Property<String>) property)
  542. .setValue("modified: " + i);
  543. }
  544. }
  545. }
  546. }, null);
  547. createClickAction("Modify first row (getContainerProperty)",
  548. "Body rows", new Command<Grid, String>() {
  549. @SuppressWarnings("unchecked")
  550. @Override
  551. public void execute(Grid c, String value, Object data) {
  552. Object firstItemId = ds.getIdByIndex(0);
  553. for (Object containerPropertyId : ds
  554. .getContainerPropertyIds()) {
  555. Property<?> property = ds.getContainerProperty(
  556. firstItemId, containerPropertyId);
  557. if (property.getType().equals(String.class)) {
  558. ((Property<String>) property)
  559. .setValue("modified: "
  560. + containerPropertyId);
  561. }
  562. }
  563. }
  564. }, null);
  565. createBooleanAction("Select first row", "Body rows", false,
  566. new Command<Grid, Boolean>() {
  567. @Override
  568. public void execute(Grid grid, Boolean select, Object data) {
  569. final Object firstItemId = grid
  570. .getContainerDatasource().firstItemId();
  571. if (select.booleanValue()) {
  572. grid.select(firstItemId);
  573. } else {
  574. grid.deselect(firstItemId);
  575. }
  576. }
  577. });
  578. createClickAction("Remove all rows", "Body rows",
  579. new Command<Grid, String>() {
  580. @SuppressWarnings("unchecked")
  581. @Override
  582. public void execute(Grid c, String value, Object data) {
  583. ds.removeAllItems();
  584. }
  585. }, null);
  586. }
  587. protected void createEditorRowActions() {
  588. createBooleanAction("Enabled", "Editor row", false,
  589. new Command<Grid, Boolean>() {
  590. @Override
  591. public void execute(Grid c, Boolean value, Object data) {
  592. c.getEditorRow().setEnabled(value);
  593. }
  594. });
  595. createClickAction("Edit item 5", "Editor row",
  596. new Command<Grid, String>() {
  597. @Override
  598. public void execute(Grid c, String value, Object data) {
  599. c.getEditorRow().editItem(5);
  600. }
  601. }, null);
  602. createClickAction("Edit item 100", "Editor row",
  603. new Command<Grid, String>() {
  604. @Override
  605. public void execute(Grid c, String value, Object data) {
  606. c.getEditorRow().editItem(100);
  607. }
  608. }, null);
  609. createClickAction("Cancel edit", "Editor row",
  610. new Command<Grid, String>() {
  611. @Override
  612. public void execute(Grid c, String value, Object data) {
  613. c.getEditorRow().cancel();
  614. }
  615. }, null);
  616. }
  617. @SuppressWarnings("boxing")
  618. protected void addHeightActions() {
  619. createCategory("Height by Rows", "Size");
  620. createBooleanAction("HeightMode Row", "Size", false,
  621. new Command<Grid, Boolean>() {
  622. @Override
  623. public void execute(Grid c, Boolean heightModeByRows,
  624. Object data) {
  625. c.setHeightMode(heightModeByRows ? HeightMode.ROW
  626. : HeightMode.CSS);
  627. }
  628. }, null);
  629. addActionForHeightByRows(1d / 3d);
  630. addActionForHeightByRows(2d / 3d);
  631. for (double i = 1; i < 5; i++) {
  632. addActionForHeightByRows(i);
  633. addActionForHeightByRows(i + 1d / 3d);
  634. addActionForHeightByRows(i + 2d / 3d);
  635. }
  636. Command<Grid, String> sizeCommand = new Command<Grid, String>() {
  637. @Override
  638. public void execute(Grid grid, String height, Object data) {
  639. grid.setHeight(height);
  640. }
  641. };
  642. createCategory("Height", "Size");
  643. // header 20px + scrollbar 16px = 36px baseline
  644. createClickAction("86px (no drag scroll select)", "Height",
  645. sizeCommand, "86px");
  646. createClickAction("96px (drag scroll select limit)", "Height",
  647. sizeCommand, "96px");
  648. createClickAction("106px (drag scroll select enabled)", "Height",
  649. sizeCommand, "106px");
  650. }
  651. private void addActionForHeightByRows(final Double i) {
  652. DecimalFormat df = new DecimalFormat("0.00");
  653. createClickAction(df.format(i) + " rows", "Height by Rows",
  654. new Command<Grid, String>() {
  655. @Override
  656. public void execute(Grid c, String value, Object data) {
  657. c.setHeightByRows(i);
  658. }
  659. }, null);
  660. }
  661. @Override
  662. protected Integer getTicketNumber() {
  663. return 12829;
  664. }
  665. @Override
  666. protected Class<Grid> getTestClass() {
  667. return Grid.class;
  668. }
  669. }