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 67KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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.v7.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.HashMap;
  23. import java.util.Iterator;
  24. import java.util.LinkedHashMap;
  25. import java.util.List;
  26. import java.util.Locale;
  27. import java.util.Map;
  28. import java.util.Random;
  29. import com.vaadin.annotations.Theme;
  30. import com.vaadin.shared.data.sort.SortDirection;
  31. import com.vaadin.tests.components.AbstractComponentTest;
  32. import com.vaadin.ui.Button;
  33. import com.vaadin.ui.Button.ClickEvent;
  34. import com.vaadin.ui.Button.ClickListener;
  35. import com.vaadin.ui.Component;
  36. import com.vaadin.ui.CssLayout;
  37. import com.vaadin.ui.Label;
  38. import com.vaadin.ui.Notification;
  39. import com.vaadin.ui.Panel;
  40. import com.vaadin.v7.data.Container.Filter;
  41. import com.vaadin.v7.data.Item;
  42. import com.vaadin.v7.data.Property;
  43. import com.vaadin.v7.data.Property.ValueChangeEvent;
  44. import com.vaadin.v7.data.Property.ValueChangeListener;
  45. import com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException;
  46. import com.vaadin.v7.data.sort.Sort;
  47. import com.vaadin.v7.data.sort.SortOrder;
  48. import com.vaadin.v7.data.util.IndexedContainer;
  49. import com.vaadin.v7.event.ItemClickEvent;
  50. import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
  51. import com.vaadin.v7.event.SelectionEvent;
  52. import com.vaadin.v7.event.SelectionEvent.SelectionListener;
  53. import com.vaadin.v7.event.SortEvent;
  54. import com.vaadin.v7.shared.ui.grid.ColumnResizeMode;
  55. import com.vaadin.v7.shared.ui.grid.GridStaticCellType;
  56. import com.vaadin.v7.shared.ui.grid.HeightMode;
  57. import com.vaadin.v7.ui.Field;
  58. import com.vaadin.v7.ui.Grid;
  59. import com.vaadin.v7.ui.Grid.CellDescriptionGenerator;
  60. import com.vaadin.v7.ui.Grid.CellReference;
  61. import com.vaadin.v7.ui.Grid.CellStyleGenerator;
  62. import com.vaadin.v7.ui.Grid.Column;
  63. import com.vaadin.v7.ui.Grid.ColumnReorderEvent;
  64. import com.vaadin.v7.ui.Grid.ColumnReorderListener;
  65. import com.vaadin.v7.ui.Grid.ColumnResizeEvent;
  66. import com.vaadin.v7.ui.Grid.ColumnResizeListener;
  67. import com.vaadin.v7.ui.Grid.ColumnVisibilityChangeEvent;
  68. import com.vaadin.v7.ui.Grid.ColumnVisibilityChangeListener;
  69. import com.vaadin.v7.ui.Grid.DetailsGenerator;
  70. import com.vaadin.v7.ui.Grid.FooterCell;
  71. import com.vaadin.v7.ui.Grid.HeaderCell;
  72. import com.vaadin.v7.ui.Grid.HeaderRow;
  73. import com.vaadin.v7.ui.Grid.MultiSelectionModel;
  74. import com.vaadin.v7.ui.Grid.RowDescriptionGenerator;
  75. import com.vaadin.v7.ui.Grid.RowReference;
  76. import com.vaadin.v7.ui.Grid.RowStyleGenerator;
  77. import com.vaadin.v7.ui.Grid.SelectionMode;
  78. import com.vaadin.v7.ui.Grid.SelectionModel;
  79. import com.vaadin.v7.ui.renderers.DateRenderer;
  80. import com.vaadin.v7.ui.renderers.HtmlRenderer;
  81. import com.vaadin.v7.ui.renderers.NumberRenderer;
  82. /**
  83. * Tests the basic features like columns, footers and headers
  84. *
  85. * @since
  86. * @author Vaadin Ltd
  87. */
  88. @Theme("valo")
  89. public class GridBasicFeatures extends AbstractComponentTest<Grid> {
  90. public static final String ROW_STYLE_GENERATOR_ROW_NUMBERS_FOR_3_OF_4 = "Row numbers for 3/4";
  91. public static final String ROW_STYLE_GENERATOR_NONE = "None";
  92. public static final String ROW_STYLE_GENERATOR_ROW_NUMBERS = "Row numbers";
  93. public static final String ROW_STYLE_GENERATOR_EMPTY = "Empty string";
  94. public static final String ROW_STYLE_GENERATOR_NULL = "Null";
  95. public static final String CELL_STYLE_GENERATOR_NONE = "None";
  96. public static final String CELL_STYLE_GENERATOR_PROPERTY_TO_STRING = "Property to string";
  97. public static final String CELL_STYLE_GENERATOR_SPECIAL = "Special for 1/4 Column 1";
  98. public static final String CELL_STYLE_GENERATOR_EMPTY = "Empty string";
  99. public static final String CELL_STYLE_GENERATOR_NULL = "Null";
  100. private static final int MANUALLY_FORMATTED_COLUMNS = 5;
  101. public static final int COLUMNS = 12;
  102. public static final int EDITABLE_COLUMNS = COLUMNS - 1;
  103. public static final int ROWS = 1000;
  104. private int containerDelay = 0;
  105. private boolean singleSelectAllowDeselect = true;
  106. private IndexedContainer ds;
  107. private Grid grid;
  108. private SelectionListener selectionListener = new SelectionListener() {
  109. @Override
  110. public void select(SelectionEvent event) {
  111. Iterator<Object> iter = event.getAdded().iterator();
  112. Object addedRow = iter.hasNext() ? iter.next() : "none";
  113. iter = event.getRemoved().iterator();
  114. Object removedRow = iter.hasNext() ? iter.next() : "none";
  115. log("SelectionEvent: Added " + addedRow + ", Removed "
  116. + removedRow);
  117. }
  118. };
  119. private ItemClickListener itemClickListener = new ItemClickListener() {
  120. @Override
  121. public void itemClick(ItemClickEvent event) {
  122. log("Item " + (event.isDoubleClick() ? "double " : "") + "click on "
  123. + event.getPropertyId() + ", item " + event.getItemId());
  124. }
  125. };
  126. private RowDescriptionGenerator rowDescriptionGenerator = new RowDescriptionGenerator() {
  127. @Override
  128. public String getDescription(RowReference row) {
  129. return "Row tooltip for row " + row.getItemId();
  130. }
  131. };
  132. private CellDescriptionGenerator cellDescriptionGenerator = new CellDescriptionGenerator() {
  133. @Override
  134. public String getDescription(CellReference cell) {
  135. if ("Column 0".equals(cell.getPropertyId())) {
  136. return "Cell tooltip for row " + cell.getItemId()
  137. + ", column 0";
  138. } else {
  139. return null;
  140. }
  141. }
  142. };
  143. private ItemClickListener editorOpeningItemClickListener = new ItemClickListener() {
  144. @Override
  145. public void itemClick(ItemClickEvent event) {
  146. grid.editItem(event.getItemId());
  147. }
  148. };
  149. private ValueChangeListener reactiveValueChanger = new ValueChangeListener() {
  150. @Override
  151. @SuppressWarnings("unchecked")
  152. public void valueChange(ValueChangeEvent event) {
  153. Object id = grid.getEditedItemId();
  154. grid.getContainerDataSource().getContainerProperty(id, "Column 2")
  155. .setValue("Modified");
  156. }
  157. };
  158. private ColumnReorderListener columnReorderListener = new ColumnReorderListener() {
  159. @Override
  160. public void columnReorder(ColumnReorderEvent event) {
  161. log("Columns reordered, userOriginated: "
  162. + event.isUserOriginated());
  163. }
  164. };
  165. private ColumnVisibilityChangeListener columnVisibilityListener = new ColumnVisibilityChangeListener() {
  166. @Override
  167. public void columnVisibilityChanged(ColumnVisibilityChangeEvent event) {
  168. log("Visibility changed: "//
  169. + "propertyId: " + event.getColumn().getPropertyId() //
  170. + ", isHidden: " + event.getColumn().isHidden() //
  171. + ", userOriginated: " + event.isUserOriginated());
  172. }
  173. };
  174. private final DetailsGenerator detailedDetailsGenerator = new DetailsGenerator() {
  175. @Override
  176. public Component getDetails(final RowReference rowReference) {
  177. CssLayout cssLayout = new CssLayout();
  178. cssLayout.setHeight("200px");
  179. cssLayout.setWidth("100%");
  180. Item item = rowReference.getItem();
  181. for (Object propertyId : item.getItemPropertyIds()) {
  182. Property<?> prop = item.getItemProperty(propertyId);
  183. String string = prop.getValue().toString();
  184. cssLayout.addComponent(new Label(string));
  185. }
  186. final int rowIndex = grid.getContainerDataSource()
  187. .indexOfId(rowReference.getItemId());
  188. ClickListener clickListener = new ClickListener() {
  189. @Override
  190. public void buttonClick(ClickEvent event) {
  191. Notification.show("You clicked on the "
  192. + "button in the details for " + "row " + rowIndex);
  193. }
  194. };
  195. cssLayout.addComponent(new Button("Press me", clickListener));
  196. return cssLayout;
  197. }
  198. };
  199. private final DetailsGenerator watchingDetailsGenerator = new DetailsGenerator() {
  200. private int id = 0;
  201. @Override
  202. public Component getDetails(RowReference rowReference) {
  203. return new Label("You are watching item id "
  204. + rowReference.getItemId() + " (" + id++ + ")");
  205. }
  206. };
  207. private Map<Object, Panel> detailsMap = new HashMap<>();
  208. private final DetailsGenerator persistingDetailsGenerator = new DetailsGenerator() {
  209. @Override
  210. public Component getDetails(RowReference rowReference) {
  211. Object itemId = rowReference.getItemId();
  212. if (!detailsMap.containsKey(itemId)) {
  213. Panel panel = new Panel();
  214. panel.setContent(new Label("One"));
  215. detailsMap.put(itemId, panel);
  216. }
  217. return detailsMap.get(itemId);
  218. }
  219. };
  220. @Override
  221. @SuppressWarnings("unchecked")
  222. protected Grid constructComponent() {
  223. // Build data source
  224. ds = new IndexedContainer() {
  225. @Override
  226. public List<Object> getItemIds(int startIndex, int numberOfIds) {
  227. log("Requested items " + startIndex + " - "
  228. + (startIndex + numberOfIds));
  229. if (containerDelay > 0) {
  230. try {
  231. Thread.sleep(containerDelay);
  232. } catch (InterruptedException e) {
  233. e.printStackTrace();
  234. }
  235. }
  236. return super.getItemIds(startIndex, numberOfIds);
  237. }
  238. };
  239. {
  240. int col = 0;
  241. for (; col < COLUMNS - MANUALLY_FORMATTED_COLUMNS; col++) {
  242. ds.addContainerProperty(getColumnProperty(col), String.class,
  243. "");
  244. }
  245. ds.addContainerProperty(getColumnProperty(col++), Integer.class,
  246. Integer.valueOf(0));
  247. ds.addContainerProperty(getColumnProperty(col++), Date.class,
  248. new Date());
  249. ds.addContainerProperty(getColumnProperty(col++), String.class, "");
  250. // Random numbers
  251. ds.addContainerProperty(getColumnProperty(col++), Integer.class, 0);
  252. ds.addContainerProperty(getColumnProperty(col++), Integer.class, 0);
  253. }
  254. {
  255. Random rand = new Random();
  256. rand.setSeed(13334);
  257. long timestamp = 0;
  258. for (int row = 0; row < ROWS; row++) {
  259. Item item = ds.addItem(Integer.valueOf(row));
  260. int col = 0;
  261. for (; col < COLUMNS - MANUALLY_FORMATTED_COLUMNS; col++) {
  262. item.getItemProperty(getColumnProperty(col))
  263. .setValue("(" + row + ", " + col + ")");
  264. }
  265. item.getItemProperty(getColumnProperty(1)).setReadOnly(true);
  266. item.getItemProperty(getColumnProperty(col++))
  267. .setValue(Integer.valueOf(row));
  268. item.getItemProperty(getColumnProperty(col++))
  269. .setValue(new Date(timestamp));
  270. timestamp += 91250000; // a bit over a day, just to get
  271. // variation
  272. item.getItemProperty(getColumnProperty(col++))
  273. .setValue("<b>" + row + "</b>");
  274. // Random numbers
  275. item.getItemProperty(getColumnProperty(col++))
  276. .setValue(rand.nextInt());
  277. // Random between 0 - 5 to test multisorting
  278. item.getItemProperty(getColumnProperty(col++))
  279. .setValue(rand.nextInt(5));
  280. }
  281. }
  282. // Create grid
  283. Grid grid = new Grid(ds);
  284. {
  285. int col = grid.getContainerDataSource().getContainerPropertyIds()
  286. .size() - MANUALLY_FORMATTED_COLUMNS;
  287. grid.getColumn(getColumnProperty(col++))
  288. .setRenderer(new NumberRenderer(
  289. new DecimalFormat("0,000.00", DecimalFormatSymbols
  290. .getInstance(new Locale("fi", "FI")))));
  291. grid.getColumn(getColumnProperty(col++)).setRenderer(
  292. new DateRenderer(new SimpleDateFormat("dd.MM.yy HH:mm")));
  293. grid.getColumn(getColumnProperty(col++))
  294. .setRenderer(new HtmlRenderer());
  295. grid.getColumn(getColumnProperty(col++))
  296. .setRenderer(new NumberRenderer());
  297. grid.getColumn(getColumnProperty(col++))
  298. .setRenderer(new NumberRenderer());
  299. }
  300. // Create footer
  301. grid.appendFooterRow();
  302. grid.setFooterVisible(false);
  303. // Add footer values (header values are automatically created)
  304. for (int col = 0; col < COLUMNS; col++) {
  305. grid.getFooterRow(0).getCell(getColumnProperty(col))
  306. .setText("Footer " + col);
  307. }
  308. // Set varying column widths
  309. for (int col = 0; col < COLUMNS; col++) {
  310. Column column = grid.getColumn(getColumnProperty(col));
  311. column.setWidth(100 + col * 50);
  312. column.setHidable(isColumnHidableByDefault(col));
  313. }
  314. grid.addSortListener(new SortEvent.SortListener() {
  315. @Override
  316. public void sort(SortEvent event) {
  317. log("SortEvent: isUserOriginated? " + event.isUserOriginated());
  318. }
  319. });
  320. grid.addColumnResizeListener(new ColumnResizeListener() {
  321. @Override
  322. public void columnResize(ColumnResizeEvent event) {
  323. log("ColumnResizeEvent: isUserOriginated? "
  324. + event.isUserOriginated());
  325. }
  326. });
  327. grid.setSelectionMode(SelectionMode.NONE);
  328. grid.getColumn(getColumnProperty(2)).getEditorField().setReadOnly(true);
  329. grid.getColumn(getColumnProperty(3)).setEditable(false);
  330. createGridActions();
  331. createColumnActions();
  332. createPropertyActions();
  333. createHeaderActions();
  334. createFooterActions();
  335. createRowActions();
  336. createEditorActions();
  337. addHeightActions();
  338. addFilterActions();
  339. addInternalActions();
  340. createDetailsActions();
  341. this.grid = grid;
  342. return grid;
  343. }
  344. protected boolean isColumnHidableByDefault(int col) {
  345. return false;
  346. }
  347. protected boolean isColumnHiddenByDefault(int col) {
  348. return false;
  349. }
  350. private void addInternalActions() {
  351. createClickAction("Update column order without updating client",
  352. "Internals", new Command<Grid, Void>() {
  353. @Override
  354. public void execute(Grid grid, Void value, Object data) {
  355. List<Column> columns = grid.getColumns();
  356. grid.setColumnOrder(columns.get(1).getPropertyId(),
  357. columns.get(0).getPropertyId());
  358. grid.getUI().getConnectorTracker().markClean(grid);
  359. }
  360. }, null);
  361. }
  362. private void addFilterActions() {
  363. createBooleanAction("Column 1 starts with \"(23\"", "Filter", false,
  364. new Command<Grid, Boolean>() {
  365. Filter filter = new Filter() {
  366. @Override
  367. public boolean passesFilter(Object itemId, Item item) {
  368. return item.getItemProperty("Column 1").getValue()
  369. .toString().startsWith("(23");
  370. }
  371. @Override
  372. public boolean appliesToProperty(Object propertyId) {
  373. return propertyId.equals("Column 1");
  374. }
  375. };
  376. @Override
  377. public void execute(Grid grid, Boolean value, Object data) {
  378. if (value) {
  379. ds.addContainerFilter(filter);
  380. } else {
  381. ds.removeContainerFilter(filter);
  382. }
  383. }
  384. });
  385. createBooleanAction("Impassable filter", "Filter", false,
  386. new Command<Grid, Boolean>() {
  387. Filter filter = new Filter() {
  388. @Override
  389. public boolean passesFilter(Object itemId, Item item) {
  390. return false;
  391. }
  392. @Override
  393. public boolean appliesToProperty(Object propertyId) {
  394. return true;
  395. }
  396. };
  397. @Override
  398. public void execute(Grid c, Boolean value, Object data) {
  399. if (value) {
  400. ds.addContainerFilter(filter);
  401. } else {
  402. ds.removeContainerFilter(filter);
  403. }
  404. }
  405. });
  406. }
  407. protected void createGridActions() {
  408. LinkedHashMap<String, String> primaryStyleNames = new LinkedHashMap<>();
  409. primaryStyleNames.put("v-grid", "v-grid");
  410. primaryStyleNames.put("v-escalator", "v-escalator");
  411. primaryStyleNames.put("my-grid", "my-grid");
  412. createMultiClickAction("Primary style name", "State", primaryStyleNames,
  413. new Command<Grid, String>() {
  414. @Override
  415. public void execute(Grid grid, String value, Object data) {
  416. grid.setPrimaryStyleName(value);
  417. }
  418. }, primaryStyleNames.get("v-grid"));
  419. LinkedHashMap<String, SelectionMode> selectionModes = new LinkedHashMap<>();
  420. selectionModes.put("single", SelectionMode.SINGLE);
  421. selectionModes.put("multi", SelectionMode.MULTI);
  422. selectionModes.put("none", SelectionMode.NONE);
  423. createSelectAction("Selection mode", "State", selectionModes, "none",
  424. new Command<Grid, Grid.SelectionMode>() {
  425. @Override
  426. public void execute(Grid grid, SelectionMode selectionMode,
  427. Object data) {
  428. grid.setSelectionMode(selectionMode);
  429. if (selectionMode == SelectionMode.SINGLE) {
  430. grid.addSelectionListener(selectionListener);
  431. ((SelectionModel.Single) grid.getSelectionModel())
  432. .setDeselectAllowed(
  433. singleSelectAllowDeselect);
  434. } else {
  435. grid.removeSelectionListener(selectionListener);
  436. }
  437. }
  438. });
  439. LinkedHashMap<String, Integer> selectionLimits = new LinkedHashMap<>();
  440. selectionLimits.put("2", Integer.valueOf(2));
  441. selectionLimits.put("1000", Integer.valueOf(1000));
  442. selectionLimits.put("Integer.MAX_VALUE",
  443. Integer.valueOf(Integer.MAX_VALUE));
  444. createSelectAction("Selection limit", "State", selectionLimits, "1000",
  445. new Command<Grid, Integer>() {
  446. @Override
  447. public void execute(Grid grid, Integer limit, Object data) {
  448. if (!(grid
  449. .getSelectionModel() instanceof MultiSelectionModel)) {
  450. grid.setSelectionMode(SelectionMode.MULTI);
  451. }
  452. ((MultiSelectionModel) grid.getSelectionModel())
  453. .setSelectionLimit(limit.intValue());
  454. }
  455. });
  456. LinkedHashMap<String, List<SortOrder>> sortableProperties = new LinkedHashMap<>();
  457. for (Object propertyId : ds.getSortableContainerPropertyIds()) {
  458. sortableProperties.put(propertyId + ", ASC",
  459. Sort.by(propertyId).build());
  460. sortableProperties.put(propertyId + ", DESC",
  461. Sort.by(propertyId, SortDirection.DESCENDING).build());
  462. }
  463. createSelectAction("Sort by column", "State", sortableProperties,
  464. "Column 9, ascending", new Command<Grid, List<SortOrder>>() {
  465. @Override
  466. public void execute(Grid grid, List<SortOrder> sortOrder,
  467. Object data) {
  468. grid.setSortOrder(sortOrder);
  469. }
  470. });
  471. createBooleanAction("Reverse Grid Columns", "State", false,
  472. new Command<Grid, Boolean>() {
  473. @Override
  474. public void execute(Grid c, Boolean value, Object data) {
  475. List<Object> ids = new ArrayList<>();
  476. ids.addAll(ds.getContainerPropertyIds());
  477. if (!value) {
  478. c.setColumnOrder(ids.toArray());
  479. } else {
  480. Object[] idsArray = new Object[ids.size()];
  481. for (int i = 0; i < ids.size(); ++i) {
  482. idsArray[i] = ids.get(ids.size() - 1 - i);
  483. }
  484. c.setColumnOrder(idsArray);
  485. }
  486. }
  487. });
  488. LinkedHashMap<String, CellStyleGenerator> cellStyleGenerators = new LinkedHashMap<>();
  489. LinkedHashMap<String, RowStyleGenerator> rowStyleGenerators = new LinkedHashMap<>();
  490. rowStyleGenerators.put(ROW_STYLE_GENERATOR_NONE, null);
  491. rowStyleGenerators.put(ROW_STYLE_GENERATOR_ROW_NUMBERS,
  492. new RowStyleGenerator() {
  493. @Override
  494. public String getStyle(RowReference rowReference) {
  495. return "row" + rowReference.getItemId();
  496. }
  497. });
  498. rowStyleGenerators.put(ROW_STYLE_GENERATOR_ROW_NUMBERS_FOR_3_OF_4,
  499. new RowStyleGenerator() {
  500. @Override
  501. public String getStyle(RowReference rowReference) {
  502. int rowIndex = ((Integer) rowReference.getItemId())
  503. .intValue();
  504. if (rowIndex % 4 == 0) {
  505. return null;
  506. } else {
  507. return "row" + rowReference.getItemId();
  508. }
  509. }
  510. });
  511. rowStyleGenerators.put(ROW_STYLE_GENERATOR_EMPTY,
  512. new RowStyleGenerator() {
  513. @Override
  514. public String getStyle(RowReference rowReference) {
  515. return "";
  516. }
  517. });
  518. rowStyleGenerators.put(ROW_STYLE_GENERATOR_NULL,
  519. new RowStyleGenerator() {
  520. @Override
  521. public String getStyle(RowReference rowReference) {
  522. return null;
  523. }
  524. });
  525. cellStyleGenerators.put(CELL_STYLE_GENERATOR_NONE, null);
  526. cellStyleGenerators.put(CELL_STYLE_GENERATOR_PROPERTY_TO_STRING,
  527. new CellStyleGenerator() {
  528. @Override
  529. public String getStyle(CellReference cellReference) {
  530. return cellReference.getPropertyId().toString()
  531. .replace(' ', '-');
  532. }
  533. });
  534. cellStyleGenerators.put(CELL_STYLE_GENERATOR_SPECIAL,
  535. new CellStyleGenerator() {
  536. @Override
  537. public String getStyle(CellReference cellReference) {
  538. int rowIndex = ((Integer) cellReference.getItemId())
  539. .intValue();
  540. Object propertyId = cellReference.getPropertyId();
  541. if (rowIndex % 4 == 1) {
  542. return null;
  543. } else if (rowIndex % 4 == 3
  544. && "Column 1".equals(propertyId)) {
  545. return null;
  546. }
  547. return propertyId.toString().replace(' ', '_');
  548. }
  549. });
  550. cellStyleGenerators.put(CELL_STYLE_GENERATOR_EMPTY,
  551. new CellStyleGenerator() {
  552. @Override
  553. public String getStyle(CellReference cellReference) {
  554. return "";
  555. }
  556. });
  557. cellStyleGenerators.put(CELL_STYLE_GENERATOR_NULL,
  558. new CellStyleGenerator() {
  559. @Override
  560. public String getStyle(CellReference cellReference) {
  561. return null;
  562. }
  563. });
  564. createSelectAction("Row style generator", "State", rowStyleGenerators,
  565. CELL_STYLE_GENERATOR_NONE,
  566. new Command<Grid, RowStyleGenerator>() {
  567. @Override
  568. public void execute(Grid grid, RowStyleGenerator generator,
  569. Object data) {
  570. grid.setRowStyleGenerator(generator);
  571. }
  572. });
  573. createSelectAction("Cell style generator", "State", cellStyleGenerators,
  574. CELL_STYLE_GENERATOR_NONE,
  575. new Command<Grid, CellStyleGenerator>() {
  576. @Override
  577. public void execute(Grid grid, CellStyleGenerator generator,
  578. Object data) {
  579. grid.setCellStyleGenerator(generator);
  580. }
  581. });
  582. createBooleanAction("Row description generator", "State", false,
  583. new Command<Grid, Boolean>() {
  584. @Override
  585. public void execute(Grid c, Boolean value, Object data) {
  586. c.setRowDescriptionGenerator(
  587. value ? rowDescriptionGenerator : null);
  588. }
  589. });
  590. createBooleanAction("Cell description generator", "State", false,
  591. new Command<Grid, Boolean>() {
  592. @Override
  593. public void execute(Grid c, Boolean value, Object data) {
  594. c.setCellDescriptionGenerator(
  595. value ? cellDescriptionGenerator : null);
  596. }
  597. });
  598. LinkedHashMap<String, Integer> frozenOptions = new LinkedHashMap<>();
  599. for (int i = -1; i <= COLUMNS; i++) {
  600. frozenOptions.put(String.valueOf(i), Integer.valueOf(i));
  601. }
  602. /*
  603. * This line below is a workaround for a FF24 bug regarding submenu
  604. * handling - it makes the sub menu wider.
  605. */
  606. frozenOptions.put("-1 for unfreezing selection column", -1);
  607. createSelectAction("Frozen column count", "State", frozenOptions, "0",
  608. new Command<Grid, Integer>() {
  609. @Override
  610. public void execute(Grid c, Integer value, Object data) {
  611. c.setFrozenColumnCount(value.intValue());
  612. }
  613. });
  614. LinkedHashMap<String, Integer> containerDelayValues = new LinkedHashMap<>();
  615. for (int delay : new int[] { 0, 500, 2000, 10000 }) {
  616. containerDelayValues.put(String.valueOf(delay),
  617. Integer.valueOf(delay));
  618. }
  619. createSelectAction("Container delay", "State", containerDelayValues,
  620. "0", new Command<Grid, Integer>() {
  621. @Override
  622. public void execute(Grid grid, Integer delay, Object data) {
  623. containerDelay = delay.intValue();
  624. }
  625. });
  626. createBooleanAction("ItemClickListener", "State", false,
  627. new Command<Grid, Boolean>() {
  628. @Override
  629. public void execute(Grid c, Boolean value, Object data) {
  630. if (!value) {
  631. c.removeItemClickListener(itemClickListener);
  632. } else {
  633. c.addItemClickListener(itemClickListener);
  634. }
  635. }
  636. });
  637. createBooleanAction("EditorOpeningItemClickListener", "State", false,
  638. new Command<Grid, Boolean>() {
  639. @Override
  640. public void execute(Grid c, Boolean value, Object data) {
  641. if (!value) {
  642. c.removeItemClickListener(
  643. editorOpeningItemClickListener);
  644. } else {
  645. c.addItemClickListener(
  646. editorOpeningItemClickListener);
  647. }
  648. }
  649. });
  650. createBooleanAction("ReactiveValueChanger", "State", false,
  651. new Command<Grid, Boolean>() {
  652. @Override
  653. public void execute(Grid c, Boolean value, Object data) {
  654. Field<?> targetField = grid.getEditorFieldGroup()
  655. .getField("Column 0");
  656. if (targetField != null) {
  657. if (!value) {
  658. targetField.removeValueChangeListener(
  659. reactiveValueChanger);
  660. } else {
  661. targetField.addValueChangeListener(
  662. reactiveValueChanger);
  663. }
  664. }
  665. }
  666. });
  667. createBooleanAction("ColumnReorderListener", "State", false,
  668. new Command<Grid, Boolean>() {
  669. @Override
  670. public void execute(Grid grid, Boolean value, Object data) {
  671. if (value) {
  672. grid.addColumnReorderListener(
  673. columnReorderListener);
  674. } else {
  675. grid.removeColumnReorderListener(
  676. columnReorderListener);
  677. }
  678. }
  679. });
  680. createBooleanAction("ColumnVisibilityChangeListener", "State", false,
  681. new Command<Grid, Boolean>() {
  682. @Override
  683. public void execute(Grid grid, Boolean value, Object data) {
  684. if (value) {
  685. grid.addColumnVisibilityChangeListener(
  686. columnVisibilityListener);
  687. } else {
  688. grid.removeColumnVisibilityChangeListener(
  689. columnVisibilityListener);
  690. }
  691. }
  692. });
  693. createBooleanAction("Single select allow deselect", "State",
  694. singleSelectAllowDeselect, new Command<Grid, Boolean>() {
  695. @Override
  696. public void execute(Grid c, Boolean value, Object data) {
  697. singleSelectAllowDeselect = value.booleanValue();
  698. SelectionModel model = c.getSelectionModel();
  699. if (model instanceof SelectionModel.Single) {
  700. ((SelectionModel.Single) model).setDeselectAllowed(
  701. singleSelectAllowDeselect);
  702. }
  703. }
  704. });
  705. createBooleanAction("Column Reordering Allowed", "State", false,
  706. new Command<Grid, Boolean>() {
  707. @Override
  708. public void execute(Grid c, Boolean value, Object data) {
  709. c.setColumnReorderingAllowed(value);
  710. }
  711. });
  712. createClickAction("Select all", "State", new Command<Grid, String>() {
  713. @Override
  714. public void execute(Grid c, String value, Object data) {
  715. SelectionModel selectionModel = c.getSelectionModel();
  716. if (selectionModel instanceof SelectionModel.Multi) {
  717. ((SelectionModel.Multi) selectionModel).selectAll();
  718. }
  719. }
  720. }, null);
  721. createClickAction("Select none", "State", new Command<Grid, String>() {
  722. @Override
  723. public void execute(Grid c, String value, Object data) {
  724. SelectionModel selectionModel = c.getSelectionModel();
  725. if (selectionModel instanceof SelectionModel.Multi) {
  726. ((SelectionModel.Multi) selectionModel).deselectAll();
  727. }
  728. }
  729. }, null);
  730. }
  731. protected void createHeaderActions() {
  732. createCategory("Header", null);
  733. createBooleanAction("Visible", "Header", true,
  734. new Command<Grid, Boolean>() {
  735. @Override
  736. public void execute(Grid grid, Boolean value, Object data) {
  737. grid.setHeaderVisible(value);
  738. }
  739. });
  740. LinkedHashMap<String, String> defaultRows = new LinkedHashMap<>();
  741. defaultRows.put("Top", "Top");
  742. defaultRows.put("Bottom", "Bottom");
  743. defaultRows.put("Unset", "Unset");
  744. createMultiClickAction("Default row", "Header", defaultRows,
  745. new Command<Grid, String>() {
  746. @Override
  747. public void execute(Grid grid, String value, Object data) {
  748. HeaderRow defaultRow = null;
  749. if (value.equals("Top")) {
  750. defaultRow = grid.getHeaderRow(0);
  751. } else if (value.equals("Bottom")) {
  752. defaultRow = grid
  753. .getHeaderRow(grid.getHeaderRowCount() - 1);
  754. }
  755. grid.setDefaultHeaderRow(defaultRow);
  756. }
  757. }, defaultRows.get("Top"));
  758. createClickAction("Prepend row", "Header", new Command<Grid, Object>() {
  759. @Override
  760. public void execute(Grid grid, Object value, Object data) {
  761. grid.prependHeaderRow();
  762. }
  763. }, null);
  764. createClickAction("Append row", "Header", new Command<Grid, Object>() {
  765. @Override
  766. public void execute(Grid grid, Object value, Object data) {
  767. grid.appendHeaderRow();
  768. }
  769. }, null);
  770. createClickAction("Remove top row", "Header",
  771. new Command<Grid, Object>() {
  772. @Override
  773. public void execute(Grid grid, Object value, Object data) {
  774. grid.removeHeaderRow(0);
  775. }
  776. }, null);
  777. createClickAction("Remove bottom row", "Header",
  778. new Command<Grid, Object>() {
  779. @Override
  780. public void execute(Grid grid, Object value, Object data) {
  781. grid.removeHeaderRow(grid.getHeaderRowCount() - 1);
  782. }
  783. }, null);
  784. }
  785. protected void createFooterActions() {
  786. createCategory("Footer", null);
  787. createBooleanAction("Visible", "Footer", false,
  788. new Command<Grid, Boolean>() {
  789. @Override
  790. public void execute(Grid grid, Boolean value, Object data) {
  791. grid.setFooterVisible(value);
  792. }
  793. });
  794. createClickAction("Prepend row", "Footer", new Command<Grid, Object>() {
  795. @Override
  796. public void execute(Grid grid, Object value, Object data) {
  797. grid.prependFooterRow();
  798. }
  799. }, null);
  800. createClickAction("Append row", "Footer", new Command<Grid, Object>() {
  801. @Override
  802. public void execute(Grid grid, Object value, Object data) {
  803. grid.appendFooterRow();
  804. }
  805. }, null);
  806. createClickAction("Remove top row", "Footer",
  807. new Command<Grid, Object>() {
  808. @Override
  809. public void execute(Grid grid, Object value, Object data) {
  810. grid.removeFooterRow(0);
  811. }
  812. }, null);
  813. createClickAction("Remove bottom row", "Footer",
  814. new Command<Grid, Object>() {
  815. @Override
  816. public void execute(Grid grid, Object value, Object data) {
  817. grid.removeFooterRow(grid.getFooterRowCount() - 1);
  818. }
  819. }, null);
  820. }
  821. @SuppressWarnings("boxing")
  822. protected void createColumnActions() {
  823. createCategory("Columns", null);
  824. for (int c = 0; c < COLUMNS; c++) {
  825. final int index = c;
  826. createCategory(getColumnProperty(c), "Columns");
  827. createClickAction("Add / Remove", getColumnProperty(c),
  828. new Command<Grid, String>() {
  829. boolean wasHidable;
  830. boolean wasHidden;
  831. String wasColumnHidingToggleCaption;
  832. @Override
  833. public void execute(Grid grid, String value,
  834. Object data) {
  835. String columnProperty = getColumnProperty(
  836. (Integer) data);
  837. Column column = grid.getColumn(columnProperty);
  838. if (column == null) {
  839. column = grid.addColumn(columnProperty);
  840. column.setHidable(wasHidable);
  841. column.setHidden(wasHidden);
  842. column.setHidingToggleCaption(
  843. wasColumnHidingToggleCaption);
  844. } else {
  845. wasHidable = column.isHidable();
  846. wasHidden = column.isHidden();
  847. wasColumnHidingToggleCaption = column
  848. .getHidingToggleCaption();
  849. grid.removeColumn(columnProperty);
  850. }
  851. }
  852. }, null, c);
  853. createClickAction("Move left", getColumnProperty(c),
  854. new Command<Grid, String>() {
  855. @Override
  856. public void execute(Grid grid, String value,
  857. Object data) {
  858. final String columnProperty = getColumnProperty(
  859. (Integer) data);
  860. List<Column> cols = grid.getColumns();
  861. List<Object> reordered = new ArrayList<>();
  862. boolean addAsLast = false;
  863. for (int i = 0; i < cols.size(); i++) {
  864. Column col = cols.get(i);
  865. if (col.getPropertyId()
  866. .equals(columnProperty)) {
  867. if (i == 0) {
  868. addAsLast = true;
  869. } else {
  870. reordered.add(i - 1, columnProperty);
  871. }
  872. } else {
  873. reordered.add(col.getPropertyId());
  874. }
  875. }
  876. if (addAsLast) {
  877. reordered.add(columnProperty);
  878. }
  879. grid.setColumnOrder(reordered.toArray());
  880. }
  881. }, null, c);
  882. createBooleanAction("Sortable", getColumnProperty(c), true,
  883. new Command<Grid, Boolean>() {
  884. @Override
  885. public void execute(Grid grid, Boolean value,
  886. Object columnIndex) {
  887. Object propertyId = getColumnProperty(
  888. (Integer) columnIndex);
  889. Column column = grid.getColumn(propertyId);
  890. column.setSortable(value);
  891. }
  892. }, c);
  893. createBooleanAction("Resizable", getColumnProperty(c), true,
  894. new Command<Grid, Boolean>() {
  895. @Override
  896. public void execute(Grid grid, Boolean value,
  897. Object columnIndex) {
  898. Object propertyId = getColumnProperty(
  899. (Integer) columnIndex);
  900. Column column = grid.getColumn(propertyId);
  901. column.setResizable(value);
  902. }
  903. }, c);
  904. createBooleanAction("Hidable", getColumnProperty(c),
  905. isColumnHidableByDefault(c), new Command<Grid, Boolean>() {
  906. @Override
  907. public void execute(Grid c, Boolean hidable,
  908. Object propertyId) {
  909. grid.getColumn(propertyId).setHidable(hidable);
  910. }
  911. }, getColumnProperty(c));
  912. createBooleanAction("Hidden", getColumnProperty(c),
  913. isColumnHiddenByDefault(c), new Command<Grid, Boolean>() {
  914. @Override
  915. public void execute(Grid c, Boolean hidden,
  916. Object propertyId) {
  917. grid.getColumn(propertyId).setHidden(hidden);
  918. }
  919. }, getColumnProperty(c));
  920. createClickAction("Change hiding toggle caption",
  921. getColumnProperty(c), new Command<Grid, String>() {
  922. int count = 0;
  923. @Override
  924. public void execute(Grid grid, String value,
  925. Object data) {
  926. final String columnProperty = getColumnProperty(
  927. (Integer) data);
  928. grid.getColumn(columnProperty)
  929. .setHidingToggleCaption(columnProperty
  930. + " caption " + count++);
  931. }
  932. }, null, c);
  933. createClickAction("Change header caption", getColumnProperty(c),
  934. new Command<Grid, String>() {
  935. int count = 0;
  936. @Override
  937. public void execute(Grid grid, String value,
  938. Object data) {
  939. final String columnProperty = getColumnProperty(
  940. (Integer) data);
  941. grid.getColumn(columnProperty).setHeaderCaption(
  942. columnProperty + " header " + count++);
  943. }
  944. }, null, c);
  945. createCategory("Column " + c + " Width", getColumnProperty(c));
  946. createClickAction("Auto", "Column " + c + " Width",
  947. new Command<Grid, Integer>() {
  948. @Override
  949. public void execute(Grid grid, Integer value,
  950. Object columnIndex) {
  951. Object propertyId = getColumnProperty(
  952. (Integer) columnIndex);
  953. Column column = grid.getColumn(propertyId);
  954. column.setWidthUndefined();
  955. }
  956. }, -1, c);
  957. createClickAction("25.5px", "Column " + c + " Width",
  958. new Command<Grid, Void>() {
  959. @Override
  960. public void execute(Grid grid, Void value,
  961. Object columnIndex) {
  962. grid.getColumns().get((Integer) columnIndex)
  963. .setWidth(25.5);
  964. }
  965. }, null, c);
  966. for (int w = 50; w < 300; w += 50) {
  967. createClickAction(w + "px", "Column " + c + " Width",
  968. new Command<Grid, Integer>() {
  969. @Override
  970. public void execute(Grid grid, Integer value,
  971. Object columnIndex) {
  972. Object propertyId = getColumnProperty(
  973. (Integer) columnIndex);
  974. Column column = grid.getColumn(propertyId);
  975. column.setWidth(value);
  976. }
  977. }, w, c);
  978. }
  979. LinkedHashMap<String, GridStaticCellType> defaultRows = new LinkedHashMap<>();
  980. defaultRows.put("Text Header", GridStaticCellType.TEXT);
  981. defaultRows.put("Html Header ", GridStaticCellType.HTML);
  982. defaultRows.put("Widget Header", GridStaticCellType.WIDGET);
  983. createMultiClickAction("Header Type", getColumnProperty(c),
  984. defaultRows, new Command<Grid, GridStaticCellType>() {
  985. @Override
  986. public void execute(Grid grid, GridStaticCellType value,
  987. Object columnIndex) {
  988. final Object propertyId = getColumnProperty(
  989. (Integer) columnIndex);
  990. final HeaderCell cell = grid.getDefaultHeaderRow()
  991. .getCell(propertyId);
  992. switch (value) {
  993. case TEXT:
  994. cell.setText("Text Header");
  995. break;
  996. case HTML:
  997. cell.setHtml("HTML Header");
  998. break;
  999. case WIDGET:
  1000. cell.setComponent(new Button("Button Header",
  1001. new ClickListener() {
  1002. @Override
  1003. public void buttonClick(
  1004. ClickEvent event) {
  1005. log("Button clicked!");
  1006. }
  1007. }));
  1008. default:
  1009. break;
  1010. }
  1011. }
  1012. }, c);
  1013. defaultRows = new LinkedHashMap<>();
  1014. defaultRows.put("Text Footer", GridStaticCellType.TEXT);
  1015. defaultRows.put("Html Footer", GridStaticCellType.HTML);
  1016. defaultRows.put("Widget Footer", GridStaticCellType.WIDGET);
  1017. createMultiClickAction("Footer Type", getColumnProperty(c),
  1018. defaultRows, new Command<Grid, GridStaticCellType>() {
  1019. @Override
  1020. public void execute(Grid grid, GridStaticCellType value,
  1021. Object columnIndex) {
  1022. final Object propertyId = getColumnProperty(
  1023. (Integer) columnIndex);
  1024. final FooterCell cell = grid.getFooterRow(0)
  1025. .getCell(propertyId);
  1026. switch (value) {
  1027. case TEXT:
  1028. cell.setText("Text Footer");
  1029. break;
  1030. case HTML:
  1031. cell.setHtml("HTML Footer");
  1032. break;
  1033. case WIDGET:
  1034. cell.setComponent(new Button("Button Footer",
  1035. new ClickListener() {
  1036. @Override
  1037. public void buttonClick(
  1038. ClickEvent event) {
  1039. log("Button clicked!");
  1040. }
  1041. }));
  1042. default:
  1043. break;
  1044. }
  1045. }
  1046. }, c);
  1047. }
  1048. createClickAction("All columns auto width", "Columns",
  1049. new Command<Grid, Boolean>() {
  1050. @Override
  1051. public void execute(Grid c, Boolean value, Object data) {
  1052. for (Column col : grid.getColumns()) {
  1053. col.setWidthUndefined();
  1054. }
  1055. }
  1056. }, null);
  1057. createBooleanAction("All columns hidable", "Columns", false,
  1058. new Command<Grid, Boolean>() {
  1059. @Override
  1060. public void execute(Grid c, Boolean value, Object data) {
  1061. for (Column col : grid.getColumns()) {
  1062. col.setHidable(value);
  1063. }
  1064. }
  1065. });
  1066. createBooleanAction("Toggle all column hidden state", "Columns", false,
  1067. new Command<Grid, Boolean>() {
  1068. @Override
  1069. public void execute(Grid c, Boolean value, Object data) {
  1070. for (Column col : grid.getColumns()) {
  1071. col.setHidden(!col.isHidden());
  1072. }
  1073. }
  1074. });
  1075. createBooleanAction("All columns resizable", "Columns", false,
  1076. new Command<Grid, Boolean>() {
  1077. @Override
  1078. public void execute(Grid c, Boolean value, Object data) {
  1079. for (Column col : grid.getColumns()) {
  1080. col.setResizable(value);
  1081. }
  1082. }
  1083. });
  1084. createClickAction("All columns expanding, Col 0 has max width of 30px",
  1085. "Columns", new Command<Grid, Boolean>() {
  1086. @Override
  1087. public void execute(Grid c, Boolean value, Object data) {
  1088. for (Column col : grid.getColumns()) {
  1089. col.setWidthUndefined();
  1090. }
  1091. grid.getColumns().get(0).setMaximumWidth(30);
  1092. }
  1093. }, null);
  1094. createBooleanAction("Simple resize mode", "Columns", false,
  1095. new Command<Grid, Boolean>() {
  1096. @Override
  1097. public void execute(Grid g, Boolean value, Object data) {
  1098. g.setColumnResizeMode(value ? ColumnResizeMode.SIMPLE
  1099. : ColumnResizeMode.ANIMATED);
  1100. }
  1101. });
  1102. }
  1103. private static String getColumnProperty(int c) {
  1104. return "Column " + c;
  1105. }
  1106. protected void createPropertyActions() {
  1107. createCategory("Properties", null);
  1108. createBooleanAction("Prepend property", "Properties", false,
  1109. new Command<Grid, Boolean>() {
  1110. private final Object propertyId = new Object();
  1111. @Override
  1112. public void execute(Grid c, Boolean enable, Object data) {
  1113. if (enable.booleanValue()) {
  1114. ds.addContainerProperty(propertyId, String.class,
  1115. "property value");
  1116. grid.getColumn(propertyId)
  1117. .setHeaderCaption("new property");
  1118. grid.setColumnOrder(propertyId);
  1119. } else {
  1120. ds.removeContainerProperty(propertyId);
  1121. }
  1122. }
  1123. }, null);
  1124. }
  1125. protected void createRowActions() {
  1126. createCategory("Body rows", null);
  1127. class NewRowCommand implements Command<Grid, String> {
  1128. private final int index;
  1129. public NewRowCommand() {
  1130. this(0);
  1131. }
  1132. public NewRowCommand(int index) {
  1133. this.index = index;
  1134. }
  1135. @Override
  1136. public void execute(Grid c, String value, Object data) {
  1137. Item item = ds.addItemAt(index, new Object());
  1138. for (int i = 0; i < COLUMNS; i++) {
  1139. Class<?> type = ds.getType(getColumnProperty(i));
  1140. if (String.class.isAssignableFrom(type)) {
  1141. Property<String> itemProperty = getProperty(item, i);
  1142. itemProperty.setValue("newcell: " + i);
  1143. } else if (Integer.class.isAssignableFrom(type)) {
  1144. Property<Integer> itemProperty = getProperty(item, i);
  1145. itemProperty.setValue(Integer.valueOf(i));
  1146. } else {
  1147. // let the default value be taken implicitly.
  1148. }
  1149. }
  1150. }
  1151. private <T extends Object> Property<T> getProperty(Item item,
  1152. int i) {
  1153. @SuppressWarnings("unchecked")
  1154. Property<T> itemProperty = item
  1155. .getItemProperty(getColumnProperty(i));
  1156. return itemProperty;
  1157. }
  1158. }
  1159. final NewRowCommand newRowCommand = new NewRowCommand();
  1160. createClickAction("Add 18 rows", "Body rows",
  1161. new Command<Grid, String>() {
  1162. @Override
  1163. public void execute(Grid c, String value, Object data) {
  1164. for (int i = 0; i < 18; i++) {
  1165. newRowCommand.execute(c, value, data);
  1166. }
  1167. }
  1168. }, null);
  1169. createClickAction("Add first row", "Body rows", newRowCommand, null);
  1170. createClickAction("Add third row", "Body rows", new NewRowCommand(2),
  1171. null);
  1172. createClickAction("Remove first row", "Body rows",
  1173. new Command<Grid, String>() {
  1174. @Override
  1175. public void execute(Grid c, String value, Object data) {
  1176. Object firstItemId = ds.getIdByIndex(0);
  1177. ds.removeItem(firstItemId);
  1178. }
  1179. }, null);
  1180. createClickAction("Remove 18 first rows", "Body rows",
  1181. new Command<Grid, String>() {
  1182. @Override
  1183. public void execute(Grid c, String value, Object data) {
  1184. for (int i = 0; i < 18; i++) {
  1185. Object firstItemId = ds.getIdByIndex(0);
  1186. ds.removeItem(firstItemId);
  1187. }
  1188. }
  1189. }, null);
  1190. createClickAction("Modify first row (getItemProperty)", "Body rows",
  1191. new Command<Grid, String>() {
  1192. @SuppressWarnings("unchecked")
  1193. @Override
  1194. public void execute(Grid c, String value, Object data) {
  1195. Object firstItemId = ds.getIdByIndex(0);
  1196. Item item = ds.getItem(firstItemId);
  1197. for (int i = 0; i < COLUMNS; i++) {
  1198. Property<?> property = item
  1199. .getItemProperty(getColumnProperty(i));
  1200. if (property.getType().equals(String.class)) {
  1201. ((Property<String>) property)
  1202. .setValue("modified: " + i);
  1203. }
  1204. }
  1205. }
  1206. }, null);
  1207. createClickAction("Modify first row (getContainerProperty)",
  1208. "Body rows", new Command<Grid, String>() {
  1209. @SuppressWarnings("unchecked")
  1210. @Override
  1211. public void execute(Grid c, String value, Object data) {
  1212. Object firstItemId = ds.getIdByIndex(0);
  1213. for (Object containerPropertyId : ds
  1214. .getContainerPropertyIds()) {
  1215. Property<?> property = ds.getContainerProperty(
  1216. firstItemId, containerPropertyId);
  1217. if (property.getType().equals(String.class)) {
  1218. ((Property<String>) property).setValue(
  1219. "modified: " + containerPropertyId);
  1220. }
  1221. }
  1222. }
  1223. }, null);
  1224. createBooleanAction("Select first row", "Body rows", false,
  1225. new Command<Grid, Boolean>() {
  1226. @Override
  1227. public void execute(Grid grid, Boolean select,
  1228. Object data) {
  1229. final Object firstItemId = grid.getContainerDataSource()
  1230. .firstItemId();
  1231. if (select.booleanValue()) {
  1232. grid.select(firstItemId);
  1233. } else {
  1234. grid.deselect(firstItemId);
  1235. }
  1236. }
  1237. });
  1238. createClickAction("Remove all rows", "Body rows",
  1239. new Command<Grid, String>() {
  1240. @SuppressWarnings("unchecked")
  1241. @Override
  1242. public void execute(Grid c, String value, Object data) {
  1243. ds.removeAllItems();
  1244. }
  1245. }, null);
  1246. createClickAction("Remove selected rows", "Body rows",
  1247. new Command<Grid, String>() {
  1248. @Override
  1249. public void execute(Grid c, String value, Object data) {
  1250. // Usually you'd deselect all the rows before removing
  1251. // them. It is done this way to test for #19152
  1252. for (Object itemId : c.getSelectedRows()) {
  1253. ds.removeItem(itemId);
  1254. }
  1255. c.select(null);
  1256. }
  1257. }, null);
  1258. }
  1259. protected void createEditorActions() {
  1260. createBooleanAction("Enabled", "Editor", false,
  1261. new Command<Grid, Boolean>() {
  1262. @Override
  1263. public void execute(Grid c, Boolean value, Object data) {
  1264. c.setEditorEnabled(value);
  1265. }
  1266. });
  1267. createBooleanAction("Buffered mode", "Editor", true,
  1268. new Command<Grid, Boolean>() {
  1269. @Override
  1270. public void execute(Grid c, Boolean value, Object data) {
  1271. c.setEditorBuffered(value);
  1272. }
  1273. });
  1274. createClickAction("Edit item 5", "Editor", new Command<Grid, String>() {
  1275. @Override
  1276. public void execute(Grid c, String value, Object data) {
  1277. c.editItem(5);
  1278. }
  1279. }, null);
  1280. createClickAction("Edit item 100", "Editor",
  1281. new Command<Grid, String>() {
  1282. @Override
  1283. public void execute(Grid c, String value, Object data) {
  1284. c.editItem(100);
  1285. }
  1286. }, null);
  1287. createClickAction("Save", "Editor", new Command<Grid, String>() {
  1288. @Override
  1289. public void execute(Grid c, String value, Object data) {
  1290. try {
  1291. c.saveEditor();
  1292. } catch (CommitException e) {
  1293. // TODO Auto-generated catch block
  1294. e.printStackTrace();
  1295. }
  1296. }
  1297. }, null);
  1298. createClickAction("Cancel edit", "Editor", new Command<Grid, String>() {
  1299. @Override
  1300. public void execute(Grid c, String value, Object data) {
  1301. c.cancelEditor();
  1302. }
  1303. }, null);
  1304. createClickAction("Change save caption", "Editor",
  1305. new Command<Grid, String>() {
  1306. @Override
  1307. public void execute(Grid c, String value, Object data) {
  1308. c.setEditorSaveCaption("ǝʌɐS");
  1309. }
  1310. }, null);
  1311. createClickAction("Change cancel caption", "Editor",
  1312. new Command<Grid, String>() {
  1313. @Override
  1314. public void execute(Grid c, String value, Object data) {
  1315. c.setEditorCancelCaption("ʃǝɔuɐↃ");
  1316. }
  1317. }, null);
  1318. }
  1319. @SuppressWarnings("boxing")
  1320. protected void addHeightActions() {
  1321. createCategory("Height by Rows", "Size");
  1322. createBooleanAction("HeightMode Row", "Size", false,
  1323. new Command<Grid, Boolean>() {
  1324. @Override
  1325. public void execute(Grid c, Boolean heightModeByRows,
  1326. Object data) {
  1327. c.setHeightMode(heightModeByRows ? HeightMode.ROW
  1328. : HeightMode.CSS);
  1329. }
  1330. }, null);
  1331. addActionForHeightByRows(1d / 3d);
  1332. addActionForHeightByRows(2d / 3d);
  1333. for (double i = 1; i < 5; i++) {
  1334. addActionForHeightByRows(i);
  1335. addActionForHeightByRows(i + 1d / 3d);
  1336. addActionForHeightByRows(i + 2d / 3d);
  1337. }
  1338. Command<Grid, String> sizeCommand = new Command<Grid, String>() {
  1339. @Override
  1340. public void execute(Grid grid, String height, Object data) {
  1341. grid.setHeight(height);
  1342. }
  1343. };
  1344. createCategory("Height", "Size");
  1345. // header 20px + scrollbar 16px = 36px baseline
  1346. createClickAction("86px (no drag scroll select)", "Height", sizeCommand,
  1347. "86px");
  1348. createClickAction("96px (drag scroll select limit)", "Height",
  1349. sizeCommand, "96px");
  1350. createClickAction("106px (drag scroll select enabled)", "Height",
  1351. sizeCommand, "106px");
  1352. }
  1353. private void addActionForHeightByRows(final Double i) {
  1354. DecimalFormat df = new DecimalFormat("0.00");
  1355. createClickAction(df.format(i) + " rows", "Height by Rows",
  1356. new Command<Grid, String>() {
  1357. @Override
  1358. public void execute(Grid c, String value, Object data) {
  1359. c.setHeightByRows(i);
  1360. }
  1361. }, null);
  1362. }
  1363. private void createDetailsActions() {
  1364. Command<Grid, DetailsGenerator> swapDetailsGenerator = new Command<Grid, DetailsGenerator>() {
  1365. @Override
  1366. public void execute(Grid c, DetailsGenerator generator,
  1367. Object data) {
  1368. grid.setDetailsGenerator(generator);
  1369. }
  1370. };
  1371. Command<Grid, Boolean> openOrCloseItemId = new Command<Grid, Boolean>() {
  1372. @Override
  1373. @SuppressWarnings("boxing")
  1374. public void execute(Grid g, Boolean visible, Object itemId) {
  1375. g.setDetailsVisible(itemId, visible);
  1376. }
  1377. };
  1378. createCategory("Generators", "Details");
  1379. createClickAction("NULL", "Generators", swapDetailsGenerator,
  1380. DetailsGenerator.NULL);
  1381. createClickAction("\"Watching\"", "Generators", swapDetailsGenerator,
  1382. watchingDetailsGenerator);
  1383. createClickAction("Detailed", "Generators", swapDetailsGenerator,
  1384. detailedDetailsGenerator);
  1385. createClickAction("Persisting", "Generators", swapDetailsGenerator,
  1386. persistingDetailsGenerator);
  1387. createClickAction("- Change Component", "Generators",
  1388. new Command<Grid, Void>() {
  1389. @Override
  1390. public void execute(Grid c, Void value, Object data) {
  1391. for (Object id : detailsMap.keySet()) {
  1392. Panel panel = detailsMap.get(id);
  1393. Label label = (Label) panel.getContent();
  1394. if (label.getValue().equals("One")) {
  1395. panel.setContent(new Label("Two"));
  1396. } else {
  1397. panel.setContent(new Label("One"));
  1398. }
  1399. }
  1400. }
  1401. }, null);
  1402. createClickAction("Toggle firstItemId", "Details",
  1403. new Command<Grid, Void>() {
  1404. @Override
  1405. public void execute(Grid g, Void value, Object data) {
  1406. Object firstItemId = g.getContainerDataSource()
  1407. .firstItemId();
  1408. boolean toggle = g.isDetailsVisible(firstItemId);
  1409. g.setDetailsVisible(firstItemId, !toggle);
  1410. g.setDetailsVisible(firstItemId, toggle);
  1411. }
  1412. }, null);
  1413. createBooleanAction("Open firstItemId", "Details", false,
  1414. openOrCloseItemId, ds.firstItemId());
  1415. createBooleanAction("Open 1", "Details", false, openOrCloseItemId,
  1416. ds.getIdByIndex(1));
  1417. createBooleanAction("Open 995", "Details", false, openOrCloseItemId,
  1418. ds.getIdByIndex(995));
  1419. }
  1420. @Override
  1421. protected Integer getTicketNumber() {
  1422. return 12829;
  1423. }
  1424. @Override
  1425. protected Class<Grid> getTestClass() {
  1426. return Grid.class;
  1427. }
  1428. }