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.

GridBasicClientFeaturesWidget.java 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.widgetset.client.grid;
  17. import java.util.ArrayList;
  18. import java.util.Date;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.Random;
  23. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  24. import com.google.gwt.dom.client.Style.Unit;
  25. import com.google.gwt.event.dom.client.ClickEvent;
  26. import com.google.gwt.event.dom.client.ClickHandler;
  27. import com.google.gwt.event.shared.HandlerRegistration;
  28. import com.google.gwt.user.client.Timer;
  29. import com.google.gwt.user.client.Window;
  30. import com.google.gwt.user.client.ui.Button;
  31. import com.google.gwt.user.client.ui.Composite;
  32. import com.google.gwt.user.client.ui.HTML;
  33. import com.google.gwt.user.client.ui.Label;
  34. import com.google.gwt.user.client.ui.TextBox;
  35. import com.vaadin.client.data.DataSource;
  36. import com.vaadin.client.data.DataSource.RowHandle;
  37. import com.vaadin.client.ui.VLabel;
  38. import com.vaadin.client.ui.grid.Cell;
  39. import com.vaadin.client.ui.grid.EditorRowHandler;
  40. import com.vaadin.client.ui.grid.FlyweightCell;
  41. import com.vaadin.client.ui.grid.Grid;
  42. import com.vaadin.client.ui.grid.Grid.SelectionMode;
  43. import com.vaadin.client.ui.grid.GridColumn;
  44. import com.vaadin.client.ui.grid.GridFooter;
  45. import com.vaadin.client.ui.grid.GridFooter.FooterRow;
  46. import com.vaadin.client.ui.grid.GridHeader;
  47. import com.vaadin.client.ui.grid.GridHeader.HeaderRow;
  48. import com.vaadin.client.ui.grid.Renderer;
  49. import com.vaadin.client.ui.grid.datasources.ListDataSource;
  50. import com.vaadin.client.ui.grid.datasources.ListSorter;
  51. import com.vaadin.client.ui.grid.events.BodyKeyDownHandler;
  52. import com.vaadin.client.ui.grid.events.BodyKeyPressHandler;
  53. import com.vaadin.client.ui.grid.events.BodyKeyUpHandler;
  54. import com.vaadin.client.ui.grid.events.FooterKeyDownHandler;
  55. import com.vaadin.client.ui.grid.events.FooterKeyPressHandler;
  56. import com.vaadin.client.ui.grid.events.FooterKeyUpHandler;
  57. import com.vaadin.client.ui.grid.events.GridKeyDownEvent;
  58. import com.vaadin.client.ui.grid.events.GridKeyPressEvent;
  59. import com.vaadin.client.ui.grid.events.GridKeyUpEvent;
  60. import com.vaadin.client.ui.grid.events.HeaderKeyDownHandler;
  61. import com.vaadin.client.ui.grid.events.HeaderKeyPressHandler;
  62. import com.vaadin.client.ui.grid.events.HeaderKeyUpHandler;
  63. import com.vaadin.client.ui.grid.events.ScrollEvent;
  64. import com.vaadin.client.ui.grid.events.ScrollHandler;
  65. import com.vaadin.client.ui.grid.renderers.DateRenderer;
  66. import com.vaadin.client.ui.grid.renderers.HtmlRenderer;
  67. import com.vaadin.client.ui.grid.renderers.NumberRenderer;
  68. import com.vaadin.client.ui.grid.renderers.TextRenderer;
  69. import com.vaadin.client.ui.grid.selection.SelectionModel.None;
  70. import com.vaadin.tests.widgetset.client.grid.GridBasicClientFeaturesWidget.Data;
  71. /**
  72. * Grid basic client features test application.
  73. *
  74. * @since
  75. * @author Vaadin Ltd
  76. */
  77. public class GridBasicClientFeaturesWidget extends
  78. PureGWTTestApplication<Grid<List<Data>>> {
  79. public static enum Renderers {
  80. TEXT_RENDERER, HTML_RENDERER, NUMBER_RENDERER, DATE_RENDERER;
  81. }
  82. private class TestEditorRowHandler implements EditorRowHandler<List<Data>> {
  83. private Map<GridColumn<?, ?>, TextBox> widgets = new HashMap<GridColumn<?, ?>, TextBox>();
  84. private Label log = new Label();
  85. {
  86. log.addStyleName("editor-row-log");
  87. addSouth(log, 20);
  88. }
  89. @Override
  90. public void bind(EditorRowRequest<List<Data>> request) {
  91. List<Data> rowData = ds.getRow(request.getRowIndex());
  92. boolean hasSelectionColumn = !(grid.getSelectionModel() instanceof None);
  93. for (int i = 0; i < rowData.size(); i++) {
  94. int columnIndex = hasSelectionColumn ? i + 1 : i;
  95. getWidget(columnIndex).setText(rowData.get(i).value.toString());
  96. }
  97. request.invokeCallback();
  98. }
  99. @Override
  100. public void cancel(EditorRowRequest<List<Data>> request) {
  101. log.setText("Row " + request.getRowIndex() + " edit cancelled");
  102. request.invokeCallback();
  103. }
  104. @Override
  105. public void commit(EditorRowRequest<List<Data>> request) {
  106. log.setText("Row " + request.getRowIndex() + " edit committed");
  107. List<Data> rowData = ds.getRow(request.getRowIndex());
  108. int i = 0;
  109. for (; i < COLUMNS - MANUALLY_FORMATTED_COLUMNS; i++) {
  110. rowData.get(i).value = getWidget(i).getText();
  111. }
  112. rowData.get(i).value = Integer.valueOf(getWidget(i++).getText());
  113. rowData.get(i).value = new Date(getWidget(i++).getText());
  114. rowData.get(i).value = getWidget(i++).getText();
  115. rowData.get(i).value = Integer.valueOf(getWidget(i++).getText());
  116. rowData.get(i).value = Integer.valueOf(getWidget(i++).getText());
  117. // notify data source of changes
  118. ds.asList().set(request.getRowIndex(), rowData);
  119. request.invokeCallback();
  120. }
  121. @Override
  122. public void discard(EditorRowRequest<List<Data>> request) {
  123. bind(request);
  124. }
  125. @Override
  126. public TextBox getWidget(GridColumn<?, List<Data>> column) {
  127. if (grid.getColumns().indexOf(column) == 0
  128. && !(grid.getSelectionModel() instanceof None)) {
  129. return null;
  130. }
  131. TextBox w = widgets.get(column);
  132. if (w == null) {
  133. w = new TextBox();
  134. w.getElement().getStyle().setMargin(0, Unit.PX);
  135. widgets.put(column, w);
  136. }
  137. return w;
  138. }
  139. private TextBox getWidget(int i) {
  140. return getWidget(grid.getColumn(i));
  141. }
  142. }
  143. private static final int MANUALLY_FORMATTED_COLUMNS = 5;
  144. public static final int COLUMNS = 12;
  145. public static final int ROWS = 1000;
  146. private final Grid<List<Data>> grid;
  147. private final List<List<Data>> data;
  148. private final ListDataSource<List<Data>> ds;
  149. private final ListSorter<List<Data>> sorter;
  150. /**
  151. * Our basic data object
  152. */
  153. public final static class Data {
  154. Object value;
  155. }
  156. /**
  157. * Convenience method for creating a list of Data objects to be used as a
  158. * Row in the data source
  159. *
  160. * @param cols
  161. * number of columns (items) to include in the row
  162. * @return
  163. */
  164. private List<Data> createDataRow(int cols) {
  165. List<Data> list = new ArrayList<Data>(cols);
  166. for (int i = 0; i < cols; ++i) {
  167. list.add(new Data());
  168. }
  169. data.add(list);
  170. return list;
  171. }
  172. @SuppressWarnings("unchecked")
  173. public GridBasicClientFeaturesWidget() {
  174. super(new Grid<List<Data>>());
  175. // Initialize data source
  176. data = new ArrayList<List<Data>>();
  177. {
  178. Random rand = new Random();
  179. rand.setSeed(13334);
  180. long timestamp = 0;
  181. for (int row = 0; row < ROWS; row++) {
  182. List<Data> datarow = createDataRow(COLUMNS);
  183. Data d;
  184. int col = 0;
  185. for (; col < COLUMNS - MANUALLY_FORMATTED_COLUMNS; ++col) {
  186. d = datarow.get(col);
  187. d.value = "(" + row + ", " + col + ")";
  188. }
  189. d = datarow.get(col++);
  190. d.value = Integer.valueOf(row);
  191. d = datarow.get(col++);
  192. d.value = new Date(timestamp);
  193. timestamp += 91250000; // a bit over a day, just to get
  194. // variation
  195. d = datarow.get(col++);
  196. d.value = "<b>" + row + "</b>";
  197. d = datarow.get(col++);
  198. d.value = Integer.valueOf(rand.nextInt());
  199. d = datarow.get(col++);
  200. d.value = Integer.valueOf(rand.nextInt(5));
  201. }
  202. }
  203. ds = new ListDataSource<List<Data>>(data);
  204. grid = getTestedWidget();
  205. grid.getElement().setId("testComponent");
  206. grid.setDataSource(ds);
  207. grid.setSelectionMode(SelectionMode.NONE);
  208. grid.getEditorRow().setHandler(new TestEditorRowHandler());
  209. sorter = new ListSorter<List<Data>>(grid);
  210. // Create a bunch of grid columns
  211. // Data source layout:
  212. // text (String) * (COLUMNS - MANUALLY_FORMATTED_COLUMNS + 1) |
  213. // rownumber (Integer) | some date (Date) | row number as HTML (String)
  214. // | random value (Integer)
  215. int col = 0;
  216. // Text times COLUMNS - MANUALLY_FORMATTED_COLUMNS
  217. for (col = 0; col < COLUMNS - MANUALLY_FORMATTED_COLUMNS; ++col) {
  218. final int c = col;
  219. GridColumn<String, List<Data>> column = new GridColumn<String, List<Data>>(
  220. createRenderer(Renderers.TEXT_RENDERER)) {
  221. @Override
  222. public String getValue(List<Data> row) {
  223. return (String) row.get(c).value;
  224. }
  225. };
  226. column.setWidth(50 + c * 25);
  227. column.setHeaderText("Header (0," + c + ")");
  228. grid.addColumn(column);
  229. }
  230. // Integer row number
  231. {
  232. final int c = col++;
  233. GridColumn<Integer, List<Data>> column = new GridColumn<Integer, List<Data>>(
  234. createRenderer(Renderers.NUMBER_RENDERER)) {
  235. @Override
  236. public Integer getValue(List<Data> row) {
  237. return (Integer) row.get(c).value;
  238. }
  239. };
  240. grid.addColumn(column);
  241. column.setHeaderText("Header (0," + c + ")");
  242. }
  243. // Some date
  244. {
  245. final int c = col++;
  246. GridColumn<Date, List<Data>> column = new GridColumn<Date, List<Data>>(
  247. createRenderer(Renderers.DATE_RENDERER)) {
  248. @Override
  249. public Date getValue(List<Data> row) {
  250. return (Date) row.get(c).value;
  251. }
  252. };
  253. grid.addColumn(column);
  254. column.setHeaderText("Header (0," + c + ")");
  255. }
  256. // Row number as a HTML string
  257. {
  258. final int c = col++;
  259. GridColumn<String, List<Data>> column = new GridColumn<String, List<Data>>(
  260. createRenderer(Renderers.HTML_RENDERER)) {
  261. @Override
  262. public String getValue(List<Data> row) {
  263. return (String) row.get(c).value;
  264. }
  265. };
  266. grid.addColumn(column);
  267. column.setHeaderText("Header (0," + c + ")");
  268. }
  269. // Random integer value
  270. {
  271. final int c = col++;
  272. GridColumn<Integer, List<Data>> column = new GridColumn<Integer, List<Data>>(
  273. createRenderer(Renderers.NUMBER_RENDERER)) {
  274. @Override
  275. public Integer getValue(List<Data> row) {
  276. return (Integer) row.get(c).value;
  277. }
  278. };
  279. grid.addColumn(column);
  280. column.setHeaderText("Header (0," + c + ")");
  281. }
  282. // Random integer value between 0 and 5
  283. {
  284. final int c = col++;
  285. GridColumn<Integer, List<Data>> column = new GridColumn<Integer, List<Data>>(
  286. createRenderer(Renderers.NUMBER_RENDERER)) {
  287. @Override
  288. public Integer getValue(List<Data> row) {
  289. return (Integer) row.get(c).value;
  290. }
  291. };
  292. grid.addColumn(column);
  293. column.setHeaderText("Header (0," + c + ")");
  294. }
  295. HeaderRow row = grid.getHeader().getDefaultRow();
  296. for (int i = 0; i < col; ++i) {
  297. String caption = "Header (0," + i + ")";
  298. GridColumn<?, ?> column = grid.getColumn(i);
  299. // Lets use some different cell types
  300. if (i % 3 == 0) {
  301. // No-op
  302. } else if (i % 2 == 0) {
  303. row.getCell(column).setHtml("<b>" + caption + "</b>");
  304. } else {
  305. row.getCell(column).setWidget(new HTML(caption));
  306. }
  307. }
  308. ++headerCounter;
  309. //
  310. // Populate the menu
  311. //
  312. createStateMenu();
  313. createColumnsMenu();
  314. createHeaderMenu();
  315. createFooterMenu();
  316. createEditorRowMenu();
  317. createInternalsMenu();
  318. grid.getElement().getStyle().setZIndex(0);
  319. //
  320. // Composite wrapping for grid.
  321. //
  322. boolean isComposite = Window.Location.getParameter("composite") != null;
  323. if (isComposite) {
  324. addNorth(new Composite() {
  325. {
  326. initWidget(grid);
  327. }
  328. }, 400);
  329. } else {
  330. addNorth(grid, 400);
  331. }
  332. createKeyHandlers();
  333. }
  334. private void createInternalsMenu() {
  335. String[] listenersPath = { "Component", "Internals", "Listeners" };
  336. final Label label = new Label();
  337. addSouth(label, 20);
  338. addMenuCommand("Add scroll listener", new ScheduledCommand() {
  339. private HandlerRegistration scrollHandler = null;
  340. @Override
  341. public void execute() {
  342. if (scrollHandler != null) {
  343. return;
  344. }
  345. scrollHandler = grid.addScrollHandler(new ScrollHandler() {
  346. @Override
  347. public void onScroll(ScrollEvent event) {
  348. @SuppressWarnings("hiding")
  349. final Grid<?> grid = (Grid<?>) event.getSource();
  350. label.setText("scrollTop: " + grid.getScrollTop()
  351. + ", scrollLeft: " + grid.getScrollLeft());
  352. }
  353. });
  354. }
  355. }, listenersPath);
  356. }
  357. private void createStateMenu() {
  358. String[] selectionModePath = { "Component", "State", "Selection mode" };
  359. String[] primaryStyleNamePath = { "Component", "State",
  360. "Primary Stylename" };
  361. addMenuCommand("multi", new ScheduledCommand() {
  362. @Override
  363. public void execute() {
  364. grid.setSelectionMode(SelectionMode.MULTI);
  365. }
  366. }, selectionModePath);
  367. addMenuCommand("single", new ScheduledCommand() {
  368. @Override
  369. public void execute() {
  370. grid.setSelectionMode(SelectionMode.SINGLE);
  371. }
  372. }, selectionModePath);
  373. addMenuCommand("none", new ScheduledCommand() {
  374. @Override
  375. public void execute() {
  376. grid.setSelectionMode(SelectionMode.NONE);
  377. }
  378. }, selectionModePath);
  379. addMenuCommand("v-grid", new ScheduledCommand() {
  380. @Override
  381. public void execute() {
  382. grid.setStylePrimaryName("v-grid");
  383. }
  384. }, primaryStyleNamePath);
  385. addMenuCommand("v-escalator", new ScheduledCommand() {
  386. @Override
  387. public void execute() {
  388. grid.setStylePrimaryName("v-escalator");
  389. }
  390. }, primaryStyleNamePath);
  391. addMenuCommand("v-custom-style", new ScheduledCommand() {
  392. @Override
  393. public void execute() {
  394. grid.setStylePrimaryName("v-custom-style");
  395. }
  396. }, primaryStyleNamePath);
  397. addMenuCommand("Edit and refresh Row 0", new ScheduledCommand() {
  398. @Override
  399. public void execute() {
  400. DataSource<List<Data>> ds = grid.getDataSource();
  401. RowHandle<List<Data>> rowHandle = ds.getHandle(ds.getRow(0));
  402. rowHandle.getRow().get(0).value = "Foo";
  403. rowHandle.updateRow();
  404. }
  405. }, "Component", "State");
  406. addMenuCommand("Delayed edit of Row 0", new ScheduledCommand() {
  407. @Override
  408. public void execute() {
  409. DataSource<List<Data>> ds = grid.getDataSource();
  410. final RowHandle<List<Data>> rowHandle = ds.getHandle(ds
  411. .getRow(0));
  412. new Timer() {
  413. @Override
  414. public void run() {
  415. rowHandle.getRow().get(0).value = "Bar";
  416. rowHandle.updateRow();
  417. }
  418. }.schedule(1500);
  419. }
  420. }, "Component", "State");
  421. }
  422. private void createColumnsMenu() {
  423. for (int i = 0; i < COLUMNS; i++) {
  424. final int index = i;
  425. final GridColumn<?, List<Data>> column = grid.getColumn(index);
  426. addMenuCommand("Visible", new ScheduledCommand() {
  427. @Override
  428. public void execute() {
  429. column.setVisible(!column.isVisible());
  430. }
  431. }, "Component", "Columns", "Column " + i);
  432. addMenuCommand("Sortable", new ScheduledCommand() {
  433. @Override
  434. public void execute() {
  435. column.setSortable(!column.isSortable());
  436. }
  437. }, "Component", "Columns", "Column " + i);
  438. addMenuCommand("Frozen", new ScheduledCommand() {
  439. @Override
  440. public void execute() {
  441. if (column.equals(grid.getLastFrozenColumn())) {
  442. grid.setLastFrozenColumn(null);
  443. } else {
  444. grid.setLastFrozenColumn(column);
  445. }
  446. }
  447. }, "Component", "Columns", "Column " + i);
  448. addMenuCommand("auto", new ScheduledCommand() {
  449. @Override
  450. public void execute() {
  451. column.setWidth(-1);
  452. }
  453. }, "Component", "Columns", "Column " + i, "Width");
  454. addMenuCommand("50px", new ScheduledCommand() {
  455. @Override
  456. public void execute() {
  457. column.setWidth(50);
  458. }
  459. }, "Component", "Columns", "Column " + i, "Width");
  460. addMenuCommand("200px", new ScheduledCommand() {
  461. @Override
  462. public void execute() {
  463. column.setWidth(200);
  464. }
  465. }, "Component", "Columns", "Column " + i, "Width");
  466. // Header types
  467. addMenuCommand("Text Header", new ScheduledCommand() {
  468. @Override
  469. public void execute() {
  470. column.setHeaderText("Text Header");
  471. }
  472. }, "Component", "Columns", "Column " + i, "Header Type");
  473. addMenuCommand("HTML Header", new ScheduledCommand() {
  474. @Override
  475. public void execute() {
  476. grid.getHeader().getRow(0).getCell(column)
  477. .setHtml("<b>HTML Header</b>");
  478. }
  479. }, "Component", "Columns", "Column " + i, "Header Type");
  480. addMenuCommand("Widget Header", new ScheduledCommand() {
  481. @Override
  482. public void execute() {
  483. final Button button = new Button("Button Header");
  484. button.addClickHandler(new ClickHandler() {
  485. @Override
  486. public void onClick(ClickEvent event) {
  487. button.setText("Clicked");
  488. }
  489. });
  490. grid.getHeader().getRow(0).getCell(column)
  491. .setWidget(button);
  492. }
  493. }, "Component", "Columns", "Column " + i, "Header Type");
  494. // Footer types
  495. addMenuCommand("Text Footer", new ScheduledCommand() {
  496. @Override
  497. public void execute() {
  498. grid.getFooter().getRow(0).getCell(column)
  499. .setText("Text Footer");
  500. }
  501. }, "Component", "Columns", "Column " + i, "Footer Type");
  502. addMenuCommand("HTML Footer", new ScheduledCommand() {
  503. @Override
  504. public void execute() {
  505. grid.getFooter().getRow(0).getCell(column)
  506. .setHtml("<b>HTML Footer</b>");
  507. }
  508. }, "Component", "Columns", "Column " + i, "Footer Type");
  509. addMenuCommand("Widget Footer", new ScheduledCommand() {
  510. @Override
  511. public void execute() {
  512. final Button button = new Button("Button Footer");
  513. button.addClickHandler(new ClickHandler() {
  514. @Override
  515. public void onClick(ClickEvent event) {
  516. button.setText("Clicked");
  517. }
  518. });
  519. grid.getFooter().getRow(0).getCell(column)
  520. .setWidget(button);
  521. }
  522. }, "Component", "Columns", "Column " + i, "Footer Type");
  523. }
  524. }
  525. private int headerCounter = 0;
  526. private int footerCounter = 0;
  527. private void setHeaderTexts(HeaderRow row) {
  528. for (int i = 0; i < COLUMNS; ++i) {
  529. String caption = "Header (" + headerCounter + "," + i + ")";
  530. // Lets use some different cell types
  531. if (i % 3 == 0) {
  532. row.getCell(grid.getColumn(i)).setText(caption);
  533. } else if (i % 2 == 0) {
  534. row.getCell(grid.getColumn(i))
  535. .setHtml("<b>" + caption + "</b>");
  536. } else {
  537. row.getCell(grid.getColumn(i)).setWidget(new HTML(caption));
  538. }
  539. }
  540. headerCounter++;
  541. }
  542. private void setFooterTexts(FooterRow row) {
  543. for (int i = 0; i < COLUMNS; ++i) {
  544. String caption = "Footer (" + footerCounter + "," + i + ")";
  545. // Lets use some different cell types
  546. if (i % 3 == 0) {
  547. row.getCell(grid.getColumn(i)).setText(caption);
  548. } else if (i % 2 == 0) {
  549. row.getCell(grid.getColumn(i))
  550. .setHtml("<b>" + caption + "</b>");
  551. } else {
  552. row.getCell(grid.getColumn(i)).setWidget(new HTML(caption));
  553. }
  554. }
  555. footerCounter++;
  556. }
  557. private void createHeaderMenu() {
  558. final GridHeader header = grid.getHeader();
  559. final String[] menuPath = { "Component", "Header" };
  560. addMenuCommand("Visible", new ScheduledCommand() {
  561. @Override
  562. public void execute() {
  563. header.setVisible(!header.isVisible());
  564. }
  565. }, menuPath);
  566. addMenuCommand("Top", new ScheduledCommand() {
  567. @Override
  568. public void execute() {
  569. header.setDefaultRow(header.getRow(0));
  570. }
  571. }, "Component", "Header", "Default row");
  572. addMenuCommand("Bottom", new ScheduledCommand() {
  573. @Override
  574. public void execute() {
  575. header.setDefaultRow(header.getRow(header.getRowCount() - 1));
  576. }
  577. }, "Component", "Header", "Default row");
  578. addMenuCommand("Unset", new ScheduledCommand() {
  579. @Override
  580. public void execute() {
  581. header.setDefaultRow(null);
  582. }
  583. }, "Component", "Header", "Default row");
  584. addMenuCommand("Prepend row", new ScheduledCommand() {
  585. @Override
  586. public void execute() {
  587. configureHeaderRow(header.prependRow());
  588. }
  589. }, menuPath);
  590. addMenuCommand("Append row", new ScheduledCommand() {
  591. @Override
  592. public void execute() {
  593. configureHeaderRow(header.appendRow());
  594. }
  595. }, menuPath);
  596. addMenuCommand("Remove top row", new ScheduledCommand() {
  597. @Override
  598. public void execute() {
  599. header.removeRow(0);
  600. }
  601. }, menuPath);
  602. addMenuCommand("Remove bottom row", new ScheduledCommand() {
  603. @Override
  604. public void execute() {
  605. header.removeRow(header.getRowCount() - 1);
  606. }
  607. }, menuPath);
  608. }
  609. private void configureHeaderRow(final HeaderRow row) {
  610. final GridHeader header = grid.getHeader();
  611. setHeaderTexts(row);
  612. String rowTitle = "Row " + header.getRowCount();
  613. final String[] menuPath = { "Component", "Header", rowTitle };
  614. addMenuCommand("Join column cells 0, 1", new ScheduledCommand() {
  615. @Override
  616. public void execute() {
  617. row.join(row.getCell(grid.getColumn(0)),
  618. row.getCell(grid.getColumn(1)));
  619. }
  620. }, menuPath);
  621. addMenuCommand("Join columns 1, 2", new ScheduledCommand() {
  622. @Override
  623. public void execute() {
  624. row.join(grid.getColumn(1), grid.getColumn(2));
  625. }
  626. }, menuPath);
  627. addMenuCommand("Join columns 3, 4, 5", new ScheduledCommand() {
  628. @Override
  629. public void execute() {
  630. row.join(grid.getColumn(3), grid.getColumn(4),
  631. grid.getColumn(5));
  632. }
  633. }, menuPath);
  634. addMenuCommand("Join all columns", new ScheduledCommand() {
  635. @Override
  636. public void execute() {
  637. row.join(grid.getColumns().toArray(
  638. new GridColumn[grid.getColumnCount()]));
  639. }
  640. }, menuPath);
  641. }
  642. private void createFooterMenu() {
  643. final GridFooter footer = grid.getFooter();
  644. final String[] menuPath = { "Component", "Footer" };
  645. addMenuCommand("Visible", new ScheduledCommand() {
  646. @Override
  647. public void execute() {
  648. footer.setVisible(!footer.isVisible());
  649. }
  650. }, menuPath);
  651. addMenuCommand("Prepend row", new ScheduledCommand() {
  652. @Override
  653. public void execute() {
  654. configureFooterRow(footer.prependRow());
  655. }
  656. }, menuPath);
  657. addMenuCommand("Append row", new ScheduledCommand() {
  658. @Override
  659. public void execute() {
  660. configureFooterRow(footer.appendRow());
  661. }
  662. }, menuPath);
  663. addMenuCommand("Remove top row", new ScheduledCommand() {
  664. @Override
  665. public void execute() {
  666. footer.removeRow(0);
  667. }
  668. }, menuPath);
  669. addMenuCommand("Remove bottom row", new ScheduledCommand() {
  670. @Override
  671. public void execute() {
  672. assert footer.getRowCount() > 0;
  673. footer.removeRow(footer.getRowCount() - 1);
  674. }
  675. }, menuPath);
  676. }
  677. private void createEditorRowMenu() {
  678. addMenuCommand("Enabled", new ScheduledCommand() {
  679. @Override
  680. public void execute() {
  681. grid.getEditorRow()
  682. .setEnabled(!grid.getEditorRow().isEnabled());
  683. }
  684. }, "Component", "Editor row");
  685. addMenuCommand("Edit row 5", new ScheduledCommand() {
  686. @Override
  687. public void execute() {
  688. grid.getEditorRow().editRow(5);
  689. }
  690. }, "Component", "Editor row");
  691. addMenuCommand("Edit row 100", new ScheduledCommand() {
  692. @Override
  693. public void execute() {
  694. grid.getEditorRow().editRow(100);
  695. }
  696. }, "Component", "Editor row");
  697. addMenuCommand("Commit", new ScheduledCommand() {
  698. @Override
  699. public void execute() {
  700. grid.getEditorRow().commit();
  701. }
  702. }, "Component", "Editor row");
  703. addMenuCommand("Discard", new ScheduledCommand() {
  704. @Override
  705. public void execute() {
  706. grid.getEditorRow().discard();
  707. }
  708. }, "Component", "Editor row");
  709. addMenuCommand("Cancel edit", new ScheduledCommand() {
  710. @Override
  711. public void execute() {
  712. grid.getEditorRow().cancel();
  713. }
  714. }, "Component", "Editor row");
  715. }
  716. private void configureFooterRow(final FooterRow row) {
  717. final GridFooter footer = grid.getFooter();
  718. setFooterTexts(row);
  719. String rowTitle = "Row " + footer.getRowCount();
  720. final String[] menuPath = { "Component", "Footer", rowTitle };
  721. addMenuCommand("Join column cells 0, 1", new ScheduledCommand() {
  722. @Override
  723. public void execute() {
  724. row.join(row.getCell(grid.getColumn(0)),
  725. row.getCell(grid.getColumn(1)));
  726. }
  727. }, menuPath);
  728. addMenuCommand("Join columns 1, 2", new ScheduledCommand() {
  729. @Override
  730. public void execute() {
  731. row.join(grid.getColumn(1), grid.getColumn(2));
  732. }
  733. }, menuPath);
  734. addMenuCommand("Join all columns", new ScheduledCommand() {
  735. @Override
  736. public void execute() {
  737. row.join(grid.getColumns().toArray(
  738. new GridColumn[grid.getColumnCount()]));
  739. }
  740. }, menuPath);
  741. }
  742. /**
  743. * Creates a renderer for a {@link Renderers}
  744. */
  745. @SuppressWarnings("rawtypes")
  746. private final Renderer createRenderer(Renderers renderer) {
  747. switch (renderer) {
  748. case TEXT_RENDERER:
  749. return new TextRenderer();
  750. case HTML_RENDERER:
  751. return new HtmlRenderer() {
  752. @Override
  753. public void render(FlyweightCell cell, String htmlString) {
  754. super.render(cell, "<b>" + htmlString + "</b>");
  755. }
  756. };
  757. case NUMBER_RENDERER:
  758. return new NumberRenderer();
  759. case DATE_RENDERER:
  760. return new DateRenderer();
  761. default:
  762. return new TextRenderer();
  763. }
  764. }
  765. /**
  766. * Creates a collection of handlers for all the grid key events
  767. */
  768. private void createKeyHandlers() {
  769. final List<VLabel> labels = new ArrayList<VLabel>();
  770. for (int i = 0; i < 9; ++i) {
  771. VLabel tmp = new VLabel();
  772. addNorth(tmp, 20);
  773. labels.add(tmp);
  774. }
  775. // Key Down Events
  776. grid.addBodyKeyDownHandler(new BodyKeyDownHandler() {
  777. private final VLabel label = labels.get(0);
  778. @Override
  779. public void onKeyDown(GridKeyDownEvent event) {
  780. Cell focused = event.getFocusedCell();
  781. updateLabel(label, event.toDebugString(), focused.getRow(),
  782. focused.getColumn());
  783. }
  784. });
  785. grid.addHeaderKeyDownHandler(new HeaderKeyDownHandler() {
  786. private final VLabel label = labels.get(1);
  787. @Override
  788. public void onKeyDown(GridKeyDownEvent event) {
  789. Cell focused = event.getFocusedCell();
  790. updateLabel(label, event.toDebugString(), focused.getRow(),
  791. focused.getColumn());
  792. }
  793. });
  794. grid.addFooterKeyDownHandler(new FooterKeyDownHandler() {
  795. private final VLabel label = labels.get(2);
  796. @Override
  797. public void onKeyDown(GridKeyDownEvent event) {
  798. Cell focused = event.getFocusedCell();
  799. updateLabel(label, event.toDebugString(), focused.getRow(),
  800. focused.getColumn());
  801. }
  802. });
  803. // Key Up Events
  804. grid.addBodyKeyUpHandler(new BodyKeyUpHandler() {
  805. private final VLabel label = labels.get(3);
  806. @Override
  807. public void onKeyUp(GridKeyUpEvent event) {
  808. Cell focused = event.getFocusedCell();
  809. updateLabel(label, event.toDebugString(), focused.getRow(),
  810. focused.getColumn());
  811. }
  812. });
  813. grid.addHeaderKeyUpHandler(new HeaderKeyUpHandler() {
  814. private final VLabel label = labels.get(4);
  815. @Override
  816. public void onKeyUp(GridKeyUpEvent event) {
  817. Cell focused = event.getFocusedCell();
  818. updateLabel(label, event.toDebugString(), focused.getRow(),
  819. focused.getColumn());
  820. }
  821. });
  822. grid.addFooterKeyUpHandler(new FooterKeyUpHandler() {
  823. private final VLabel label = labels.get(5);
  824. @Override
  825. public void onKeyUp(GridKeyUpEvent event) {
  826. Cell focused = event.getFocusedCell();
  827. updateLabel(label, event.toDebugString(), focused.getRow(),
  828. focused.getColumn());
  829. }
  830. });
  831. // Key Press Events
  832. grid.addBodyKeyPressHandler(new BodyKeyPressHandler() {
  833. private final VLabel label = labels.get(6);
  834. @Override
  835. public void onKeyPress(GridKeyPressEvent event) {
  836. Cell focused = event.getFocusedCell();
  837. updateLabel(label, event.toDebugString(), focused.getRow(),
  838. focused.getColumn());
  839. }
  840. });
  841. grid.addHeaderKeyPressHandler(new HeaderKeyPressHandler() {
  842. private final VLabel label = labels.get(7);
  843. @Override
  844. public void onKeyPress(GridKeyPressEvent event) {
  845. Cell focused = event.getFocusedCell();
  846. updateLabel(label, event.toDebugString(), focused.getRow(),
  847. focused.getColumn());
  848. }
  849. });
  850. grid.addFooterKeyPressHandler(new FooterKeyPressHandler() {
  851. private final VLabel label = labels.get(8);
  852. @Override
  853. public void onKeyPress(GridKeyPressEvent event) {
  854. Cell focused = event.getFocusedCell();
  855. updateLabel(label, event.toDebugString(), focused.getRow(),
  856. focused.getColumn());
  857. }
  858. });
  859. }
  860. private void updateLabel(VLabel label, String output, int row, int col) {
  861. String coords = "(" + row + ", " + col + ")";
  862. label.setText(coords + " " + output);
  863. }
  864. }