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.

EscalatorBasicClientFeaturesWidget.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. package com.vaadin.tests.widgetset.client.grid;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.google.gwt.core.client.Duration;
  5. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  6. import com.google.gwt.dom.client.TableCellElement;
  7. import com.google.gwt.user.client.ui.Composite;
  8. import com.google.gwt.user.client.ui.HTML;
  9. import com.vaadin.client.ui.grid.Escalator;
  10. import com.vaadin.client.ui.grid.EscalatorUpdater;
  11. import com.vaadin.client.ui.grid.FlyweightCell;
  12. import com.vaadin.client.ui.grid.Row;
  13. import com.vaadin.client.ui.grid.RowContainer;
  14. public class EscalatorBasicClientFeaturesWidget extends
  15. PureGWTTestApplication<Escalator> {
  16. public static class LogWidget extends Composite {
  17. private static final int MAX_LOG = 9;
  18. private final HTML html = new HTML();
  19. private final List<String> logs = new ArrayList<String>();
  20. private Escalator escalator;
  21. public LogWidget() {
  22. initWidget(html);
  23. getElement().setId("log");
  24. }
  25. public void setEscalator(Escalator escalator) {
  26. this.escalator = escalator;
  27. }
  28. public void updateDebugLabel() {
  29. int headers = escalator.getHeader().getRowCount();
  30. int bodys = escalator.getBody().getRowCount();
  31. int footers = escalator.getFooter().getRowCount();
  32. int columns = escalator.getColumnConfiguration().getColumnCount();
  33. while (logs.size() > MAX_LOG) {
  34. logs.remove(0);
  35. }
  36. String logString = "<hr>";
  37. for (String log : logs) {
  38. logString += log + "<br>";
  39. }
  40. html.setHTML("Columns: " + columns + "<br>" + //
  41. "Header rows: " + headers + "<br>" + //
  42. "Body rows: " + bodys + "<br>" + //
  43. "Footer rows: " + footers + "<br>" + //
  44. logString);
  45. }
  46. public void log(String string) {
  47. logs.add((Duration.currentTimeMillis() % 10000) + ": " + string);
  48. }
  49. }
  50. public static class UpdaterLifetimeWidget extends
  51. EscalatorBasicClientFeaturesWidget {
  52. private final EscalatorUpdater debugUpdater = new EscalatorUpdater() {
  53. @Override
  54. public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) {
  55. log("preAttach", cellsToAttach);
  56. }
  57. @Override
  58. public void postAttach(Row row,
  59. Iterable<FlyweightCell> attachedCells) {
  60. log("postAttach", attachedCells);
  61. }
  62. @Override
  63. public void update(Row row, Iterable<FlyweightCell> cellsToUpdate) {
  64. log("update", cellsToUpdate);
  65. }
  66. @Override
  67. public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) {
  68. log("preDetach", cellsToDetach);
  69. }
  70. @Override
  71. public void postDetach(Row row,
  72. Iterable<FlyweightCell> detachedCells) {
  73. log("postDetach", detachedCells);
  74. }
  75. private void log(String methodName, Iterable<FlyweightCell> cells) {
  76. if (!cells.iterator().hasNext()) {
  77. return;
  78. }
  79. TableCellElement cellElement = cells.iterator().next()
  80. .getElement();
  81. boolean isAttached = cellElement.getParentElement() != null
  82. && cellElement.getParentElement().getParentElement() != null;
  83. logWidget.log(methodName + ": elementIsAttached == "
  84. + isAttached);
  85. }
  86. };
  87. public UpdaterLifetimeWidget() {
  88. super();
  89. escalator.getHeader().setEscalatorUpdater(debugUpdater);
  90. escalator.getBody().setEscalatorUpdater(debugUpdater);
  91. escalator.getFooter().setEscalatorUpdater(debugUpdater);
  92. }
  93. }
  94. private static final String COLUMNS_AND_ROWS_MENU = "Columns and Rows";
  95. private static final String GENERAL_MENU = "General";
  96. private static final String FEATURES_MENU = "Features";
  97. private static abstract class TestEscalatorUpdater implements
  98. EscalatorUpdater {
  99. @Override
  100. public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) {
  101. // noop
  102. }
  103. @Override
  104. public void postAttach(Row row, Iterable<FlyweightCell> attachedCells) {
  105. // noop
  106. }
  107. @Override
  108. public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) {
  109. // noop
  110. }
  111. @Override
  112. public void postDetach(Row row, Iterable<FlyweightCell> detachedCells) {
  113. // noop
  114. }
  115. }
  116. private class Data {
  117. private int columnCounter = 0;
  118. private int rowCounter = 0;
  119. private final List<Integer> columns = new ArrayList<Integer>();
  120. private final List<Integer> rows = new ArrayList<Integer>();
  121. @SuppressWarnings("boxing")
  122. public void insertRows(final int offset, final int amount) {
  123. final List<Integer> newRows = new ArrayList<Integer>();
  124. for (int i = 0; i < amount; i++) {
  125. newRows.add(rowCounter++);
  126. }
  127. rows.addAll(offset, newRows);
  128. }
  129. @SuppressWarnings("boxing")
  130. public void insertColumns(final int offset, final int amount) {
  131. final List<Integer> newColumns = new ArrayList<Integer>();
  132. for (int i = 0; i < amount; i++) {
  133. newColumns.add(columnCounter++);
  134. }
  135. columns.addAll(offset, newColumns);
  136. }
  137. public EscalatorUpdater createHeaderUpdater() {
  138. return new TestEscalatorUpdater() {
  139. @Override
  140. public void update(final Row row,
  141. final Iterable<FlyweightCell> cellsToUpdate) {
  142. for (final FlyweightCell cell : cellsToUpdate) {
  143. final Integer columnName = columns
  144. .get(cell.getColumn());
  145. cell.getElement().setInnerText("Header " + columnName);
  146. if (colspan == Colspan.NORMAL) {
  147. if (cell.getColumn() % 2 == 0) {
  148. cell.setColSpan(2);
  149. }
  150. } else if (colspan == Colspan.CRAZY) {
  151. if (cell.getColumn() % 3 == 0) {
  152. cell.setColSpan(2);
  153. }
  154. }
  155. }
  156. }
  157. };
  158. }
  159. public EscalatorUpdater createFooterUpdater() {
  160. return new TestEscalatorUpdater() {
  161. @Override
  162. public void update(final Row row,
  163. final Iterable<FlyweightCell> cellsToUpdate) {
  164. for (final FlyweightCell cell : cellsToUpdate) {
  165. final Integer columnName = columns
  166. .get(cell.getColumn());
  167. cell.getElement().setInnerText("Footer " + columnName);
  168. if (colspan == Colspan.NORMAL) {
  169. if (cell.getColumn() % 2 == 0) {
  170. cell.setColSpan(2);
  171. }
  172. } else if (colspan == Colspan.CRAZY) {
  173. if (cell.getColumn() % 3 == 1) {
  174. cell.setColSpan(2);
  175. }
  176. }
  177. }
  178. }
  179. };
  180. }
  181. public EscalatorUpdater createBodyUpdater() {
  182. return new TestEscalatorUpdater() {
  183. public void renderCell(final FlyweightCell cell) {
  184. final Integer columnName = columns.get(cell.getColumn());
  185. final Integer rowName = rows.get(cell.getRow());
  186. String cellInfo = columnName + "," + rowName;
  187. if (cell.getColumn() > 0) {
  188. cell.getElement().setInnerText("Cell: " + cellInfo);
  189. } else {
  190. cell.getElement().setInnerText(
  191. "Row " + cell.getRow() + ": " + cellInfo);
  192. }
  193. if (colspan == Colspan.NORMAL) {
  194. if (cell.getColumn() % 2 == 0) {
  195. cell.setColSpan(2);
  196. }
  197. } else if (colspan == Colspan.CRAZY) {
  198. if (cell.getColumn() % 3 == cell.getRow() % 3) {
  199. cell.setColSpan(2);
  200. }
  201. }
  202. }
  203. @Override
  204. public void update(final Row row,
  205. final Iterable<FlyweightCell> cellsToUpdate) {
  206. for (final FlyweightCell cell : cellsToUpdate) {
  207. renderCell(cell);
  208. }
  209. }
  210. };
  211. }
  212. public void removeRows(final int offset, final int amount) {
  213. for (int i = 0; i < amount; i++) {
  214. rows.remove(offset);
  215. }
  216. }
  217. public void removeColumns(final int offset, final int amount) {
  218. for (int i = 0; i < amount; i++) {
  219. columns.remove(offset);
  220. }
  221. }
  222. }
  223. protected final Escalator escalator;
  224. private final Data data = new Data();
  225. private enum Colspan {
  226. NONE, NORMAL, CRAZY;
  227. }
  228. private Colspan colspan = Colspan.NONE;
  229. protected final LogWidget logWidget = new LogWidget();
  230. public EscalatorBasicClientFeaturesWidget() {
  231. super(new EscalatorProxy());
  232. escalator = getTestedWidget();
  233. logWidget.setEscalator(escalator);
  234. ((EscalatorProxy) escalator).setLogWidget(logWidget);
  235. addNorth(logWidget, 200);
  236. final RowContainer header = escalator.getHeader();
  237. header.setEscalatorUpdater(data.createHeaderUpdater());
  238. final RowContainer footer = escalator.getFooter();
  239. footer.setEscalatorUpdater(data.createFooterUpdater());
  240. escalator.getBody().setEscalatorUpdater(data.createBodyUpdater());
  241. setWidth("500px");
  242. setHeight("500px");
  243. escalator.getElement().getStyle().setZIndex(0);
  244. addNorth(escalator, 500);
  245. createGeneralMenu();
  246. createColumnMenu();
  247. createHeaderRowsMenu();
  248. createBodyRowsMenu();
  249. createFooterRowsMenu();
  250. createColumnsAndRowsMenu();
  251. createFrozenMenu();
  252. createColspanMenu();
  253. }
  254. private void createFrozenMenu() {
  255. String[] menupath = { FEATURES_MENU, "Frozen columns" };
  256. addMenuCommand("Freeze 1 column", new ScheduledCommand() {
  257. @Override
  258. public void execute() {
  259. escalator.getColumnConfiguration().setFrozenColumnCount(1);
  260. }
  261. }, menupath);
  262. addMenuCommand("Freeze 0 columns", new ScheduledCommand() {
  263. @Override
  264. public void execute() {
  265. escalator.getColumnConfiguration().setFrozenColumnCount(0);
  266. }
  267. }, menupath);
  268. }
  269. private void createColspanMenu() {
  270. String[] menupath = { FEATURES_MENU, "Column spanning" };
  271. addMenuCommand("Apply normal colspan", new ScheduledCommand() {
  272. @Override
  273. public void execute() {
  274. colspan = Colspan.NORMAL;
  275. refreshEscalator();
  276. }
  277. }, menupath);
  278. addMenuCommand("Apply crazy colspan", new ScheduledCommand() {
  279. @Override
  280. public void execute() {
  281. colspan = Colspan.CRAZY;
  282. refreshEscalator();
  283. }
  284. }, menupath);
  285. addMenuCommand("Apply no colspan", new ScheduledCommand() {
  286. @Override
  287. public void execute() {
  288. colspan = Colspan.NONE;
  289. refreshEscalator();
  290. }
  291. }, menupath);
  292. }
  293. private void createColumnsAndRowsMenu() {
  294. String[] menupath = { COLUMNS_AND_ROWS_MENU };
  295. addMenuCommand("Add one of each row", new ScheduledCommand() {
  296. @Override
  297. public void execute() {
  298. insertRows(escalator.getHeader(), 0, 1);
  299. insertRows(escalator.getBody(), 0, 1);
  300. insertRows(escalator.getFooter(), 0, 1);
  301. }
  302. }, menupath);
  303. addMenuCommand("Remove one of each row", new ScheduledCommand() {
  304. @Override
  305. public void execute() {
  306. removeRows(escalator.getHeader(), 0, 1);
  307. removeRows(escalator.getBody(), 0, 1);
  308. removeRows(escalator.getFooter(), 0, 1);
  309. }
  310. }, menupath);
  311. }
  312. private void createGeneralMenu() {
  313. String[] menupath = { GENERAL_MENU };
  314. addMenuCommand("Detach Escalator", new ScheduledCommand() {
  315. @Override
  316. public void execute() {
  317. escalator.removeFromParent();
  318. }
  319. }, menupath);
  320. addMenuCommand("Attach Escalator", new ScheduledCommand() {
  321. @Override
  322. public void execute() {
  323. if (!escalator.isAttached()) {
  324. addNorth(escalator, 500);
  325. }
  326. }
  327. }, menupath);
  328. addMenuCommand("Clear (columns, then rows)", new ScheduledCommand() {
  329. @Override
  330. public void execute() {
  331. resetColRow();
  332. }
  333. }, menupath);
  334. addMenuCommand("Clear (rows, then columns)", new ScheduledCommand() {
  335. @Override
  336. public void execute() {
  337. resetRowCol();
  338. }
  339. }, menupath);
  340. addMenuCommand("Populate Escalator (columns, then rows)",
  341. new ScheduledCommand() {
  342. @Override
  343. public void execute() {
  344. resetColRow();
  345. insertColumns(0, 10);
  346. insertRows(escalator.getHeader(), 0, 1);
  347. insertRows(escalator.getBody(), 0, 100);
  348. insertRows(escalator.getFooter(), 0, 1);
  349. }
  350. }, menupath);
  351. addMenuCommand("Populate Escalator (rows, then columns)",
  352. new ScheduledCommand() {
  353. @Override
  354. public void execute() {
  355. resetColRow();
  356. insertRows(escalator.getHeader(), 0, 1);
  357. insertRows(escalator.getBody(), 0, 100);
  358. insertRows(escalator.getFooter(), 0, 1);
  359. insertColumns(0, 10);
  360. }
  361. }, menupath);
  362. }
  363. private void createColumnMenu() {
  364. String[] menupath = { COLUMNS_AND_ROWS_MENU, "Columns" };
  365. addMenuCommand("Add one column to beginning", new ScheduledCommand() {
  366. @Override
  367. public void execute() {
  368. insertColumns(0, 1);
  369. }
  370. }, menupath);
  371. addMenuCommand("Add one column to end", new ScheduledCommand() {
  372. @Override
  373. public void execute() {
  374. insertColumns(escalator.getColumnConfiguration()
  375. .getColumnCount(), 1);
  376. }
  377. }, menupath);
  378. addMenuCommand("Add ten columns", new ScheduledCommand() {
  379. @Override
  380. public void execute() {
  381. insertColumns(0, 10);
  382. }
  383. }, menupath);
  384. addMenuCommand("Remove one column from beginning",
  385. new ScheduledCommand() {
  386. @Override
  387. public void execute() {
  388. removeColumns(0, 1);
  389. }
  390. }, menupath);
  391. addMenuCommand("Remove one column from end", new ScheduledCommand() {
  392. @Override
  393. public void execute() {
  394. removeColumns(escalator.getColumnConfiguration()
  395. .getColumnCount() - 1, 1);
  396. }
  397. }, menupath);
  398. addMenuCommand("Refresh first column", new ScheduledCommand() {
  399. @Override
  400. public void execute() {
  401. escalator.getColumnConfiguration().refreshColumns(0, 1);
  402. }
  403. }, menupath);
  404. addMenuCommand("Resize first column to max width",
  405. new ScheduledCommand() {
  406. @Override
  407. public void execute() {
  408. escalator.getColumnConfiguration()
  409. .setColumnWidthToContent(0);
  410. }
  411. }, menupath);
  412. }
  413. private void createHeaderRowsMenu() {
  414. String[] menupath = { COLUMNS_AND_ROWS_MENU, "Header Rows" };
  415. createRowsMenu(escalator.getHeader(), menupath);
  416. }
  417. private void createFooterRowsMenu() {
  418. String[] menupath = { COLUMNS_AND_ROWS_MENU, "Footer Rows" };
  419. createRowsMenu(escalator.getFooter(), menupath);
  420. }
  421. private void createBodyRowsMenu() {
  422. String[] menupath = { COLUMNS_AND_ROWS_MENU, "Body Rows" };
  423. createRowsMenu(escalator.getBody(), menupath);
  424. addMenuCommand("Add 5 rows to top", new ScheduledCommand() {
  425. @Override
  426. public void execute() {
  427. insertRows(escalator.getBody(), 0, 5);
  428. }
  429. }, menupath);
  430. addMenuCommand("Add 50 rows to top", new ScheduledCommand() {
  431. @Override
  432. public void execute() {
  433. insertRows(escalator.getBody(), 0, 50);
  434. }
  435. }, menupath);
  436. addMenuCommand("Remove 5 rows from bottom", new ScheduledCommand() {
  437. @Override
  438. public void execute() {
  439. removeRows(escalator.getBody(), escalator.getBody()
  440. .getRowCount() - 5, 5);
  441. }
  442. }, menupath);
  443. addMenuCommand("Remove 50 rows from bottom", new ScheduledCommand() {
  444. @Override
  445. public void execute() {
  446. removeRows(escalator.getBody(), escalator.getBody()
  447. .getRowCount() - 50, 50);
  448. }
  449. }, menupath);
  450. addMenuCommand("Remove 50 rows from almost bottom",
  451. new ScheduledCommand() {
  452. @Override
  453. public void execute() {
  454. removeRows(escalator.getBody(), escalator.getBody()
  455. .getRowCount() - 60, 50);
  456. }
  457. }, menupath);
  458. addMenuCommand("Remove all, insert 30 and scroll 40px",
  459. new ScheduledCommand() {
  460. @Override
  461. public void execute() {
  462. removeRows(escalator.getBody(), 0, escalator.getBody()
  463. .getRowCount());
  464. insertRows(escalator.getBody(), 0, 30);
  465. escalator.setScrollTop(40);
  466. }
  467. }, menupath);
  468. }
  469. private void createRowsMenu(final RowContainer container, String[] menupath) {
  470. addMenuCommand("Add one row to beginning", new ScheduledCommand() {
  471. @Override
  472. public void execute() {
  473. int offset = 0;
  474. int number = 1;
  475. insertRows(container, offset, number);
  476. }
  477. }, menupath);
  478. addMenuCommand("Add one row to end", new ScheduledCommand() {
  479. @Override
  480. public void execute() {
  481. int offset = container.getRowCount();
  482. int number = 1;
  483. insertRows(container, offset, number);
  484. }
  485. }, menupath);
  486. addMenuCommand("Remove one row from beginning", new ScheduledCommand() {
  487. @Override
  488. public void execute() {
  489. int offset = 0;
  490. int number = 1;
  491. removeRows(container, offset, number);
  492. }
  493. }, menupath);
  494. addMenuCommand("Remove one row from end", new ScheduledCommand() {
  495. @Override
  496. public void execute() {
  497. int offset = container.getRowCount() - 1;
  498. int number = 1;
  499. removeRows(container, offset, number);
  500. }
  501. }, menupath);
  502. }
  503. private void insertRows(final RowContainer container, int offset, int number) {
  504. if (container == escalator.getBody()) {
  505. data.insertRows(offset, number);
  506. escalator.getBody().insertRows(offset, number);
  507. } else {
  508. container.insertRows(offset, number);
  509. }
  510. }
  511. private void removeRows(final RowContainer container, int offset, int number) {
  512. if (container == escalator.getBody()) {
  513. data.removeRows(offset, number);
  514. escalator.getBody().removeRows(offset, number);
  515. } else {
  516. container.removeRows(offset, number);
  517. }
  518. }
  519. private void insertColumns(final int offset, final int number) {
  520. data.insertColumns(offset, number);
  521. escalator.getColumnConfiguration().insertColumns(offset, number);
  522. }
  523. private void removeColumns(final int offset, final int number) {
  524. data.removeColumns(offset, number);
  525. escalator.getColumnConfiguration().removeColumns(offset, number);
  526. }
  527. private void resetColRow() {
  528. if (escalator.getColumnConfiguration().getColumnCount() > 0) {
  529. removeColumns(0, escalator.getColumnConfiguration()
  530. .getColumnCount());
  531. }
  532. if (escalator.getFooter().getRowCount() > 0) {
  533. removeRows(escalator.getFooter(), 0, escalator.getFooter()
  534. .getRowCount());
  535. }
  536. if (escalator.getBody().getRowCount() > 0) {
  537. removeRows(escalator.getBody(), 0, escalator.getBody()
  538. .getRowCount());
  539. }
  540. if (escalator.getHeader().getRowCount() > 0) {
  541. removeRows(escalator.getHeader(), 0, escalator.getHeader()
  542. .getRowCount());
  543. }
  544. }
  545. private void resetRowCol() {
  546. if (escalator.getFooter().getRowCount() > 0) {
  547. removeRows(escalator.getFooter(), 0, escalator.getFooter()
  548. .getRowCount());
  549. }
  550. if (escalator.getBody().getRowCount() > 0) {
  551. removeRows(escalator.getBody(), 0, escalator.getBody()
  552. .getRowCount());
  553. }
  554. if (escalator.getHeader().getRowCount() > 0) {
  555. removeRows(escalator.getHeader(), 0, escalator.getHeader()
  556. .getRowCount());
  557. }
  558. if (escalator.getColumnConfiguration().getColumnCount() > 0) {
  559. removeColumns(0, escalator.getColumnConfiguration()
  560. .getColumnCount());
  561. }
  562. }
  563. private void refreshEscalator() {
  564. if (escalator.getHeader().getRowCount() > 0) {
  565. escalator.getHeader().refreshRows(0,
  566. escalator.getHeader().getRowCount());
  567. }
  568. if (escalator.getBody().getRowCount() > 0) {
  569. escalator.getBody().refreshRows(0,
  570. escalator.getBody().getRowCount());
  571. }
  572. if (escalator.getFooter().getRowCount() > 0) {
  573. escalator.getFooter().refreshRows(0,
  574. escalator.getFooter().getRowCount());
  575. }
  576. }
  577. }