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

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