1 package com.vaadin.tests.widgetset.client.v7.grid;
3 import java.util.ArrayList;
6 import com.google.gwt.core.client.Duration;
7 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
8 import com.google.gwt.dom.client.TableCellElement;
9 import com.google.gwt.user.client.DOM;
10 import com.google.gwt.user.client.ui.Composite;
11 import com.google.gwt.user.client.ui.HTML;
12 import com.vaadin.v7.client.widget.escalator.EscalatorUpdater;
13 import com.vaadin.v7.client.widget.escalator.FlyweightCell;
14 import com.vaadin.v7.client.widget.escalator.Row;
15 import com.vaadin.v7.client.widget.escalator.RowContainer;
16 import com.vaadin.v7.client.widget.escalator.RowContainer.BodyRowContainer;
17 import com.vaadin.v7.client.widget.escalator.Spacer;
18 import com.vaadin.v7.client.widget.escalator.SpacerUpdater;
19 import com.vaadin.v7.client.widgets.Escalator;
20 import com.vaadin.v7.shared.ui.grid.ScrollDestination;
22 public class EscalatorBasicClientFeaturesWidget
23 extends PureGWTTestApplication<Escalator> {
25 public static class LogWidget extends Composite {
27 private static final int MAX_LOG = 9;
29 private final HTML html = new HTML();
30 private final List<String> logs = new ArrayList<String>();
31 private Escalator escalator;
35 getElement().setId("log");
38 public void setEscalator(Escalator escalator) {
39 this.escalator = escalator;
42 public void updateDebugLabel() {
43 int headers = escalator.getHeader().getRowCount();
44 int bodys = escalator.getBody().getRowCount();
45 int footers = escalator.getFooter().getRowCount();
46 int columns = escalator.getColumnConfiguration().getColumnCount();
48 while (logs.size() > MAX_LOG) {
52 String logString = "<hr>";
53 for (String log : logs) {
54 logString += log + "<br>";
57 html.setHTML("Columns: " + columns + "<br>" + //
58 "Header rows: " + headers + "<br>" + //
59 "Body rows: " + bodys + "<br>" + //
60 "Footer rows: " + footers + "<br>" + //
64 public void log(String string) {
65 logs.add((Duration.currentTimeMillis() % 10000) + ": " + string);
69 public static class UpdaterLifetimeWidget
70 extends EscalatorBasicClientFeaturesWidget {
72 private final EscalatorUpdater debugUpdater = new EscalatorUpdater() {
74 public void preAttach(Row row,
75 Iterable<FlyweightCell> cellsToAttach) {
76 log("preAttach", cellsToAttach);
80 public void postAttach(Row row,
81 Iterable<FlyweightCell> attachedCells) {
82 log("postAttach", attachedCells);
86 public void update(Row row, Iterable<FlyweightCell> cellsToUpdate) {
87 log("update", cellsToUpdate);
91 public void preDetach(Row row,
92 Iterable<FlyweightCell> cellsToDetach) {
93 log("preDetach", cellsToDetach);
97 public void postDetach(Row row,
98 Iterable<FlyweightCell> detachedCells) {
99 log("postDetach", detachedCells);
102 private void log(String methodName, Iterable<FlyweightCell> cells) {
103 if (!cells.iterator().hasNext()) {
107 TableCellElement cellElement = cells.iterator().next()
109 boolean isAttached = cellElement.getParentElement() != null
110 && cellElement.getParentElement()
111 .getParentElement() != null;
113 methodName + ": elementIsAttached == " + isAttached);
117 public UpdaterLifetimeWidget() {
119 escalator.getHeader().setEscalatorUpdater(debugUpdater);
120 escalator.getBody().setEscalatorUpdater(debugUpdater);
121 escalator.getFooter().setEscalatorUpdater(debugUpdater);
125 private static final String COLUMNS_AND_ROWS_MENU = "Columns and Rows";
126 private static final String GENERAL_MENU = "General";
127 private static final String FEATURES_MENU = "Features";
129 private static abstract class TestEscalatorUpdater
130 implements EscalatorUpdater {
133 public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) {
138 public void postAttach(Row row, Iterable<FlyweightCell> attachedCells) {
143 public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) {
148 public void postDetach(Row row, Iterable<FlyweightCell> detachedCells) {
154 private int columnCounter = 0;
155 private int rowCounter = 0;
156 private final List<Integer> columns = new ArrayList<Integer>();
157 private final List<Integer> rows = new ArrayList<Integer>();
159 @SuppressWarnings("boxing")
160 public void insertRows(final int offset, final int amount) {
161 final List<Integer> newRows = new ArrayList<Integer>();
162 for (int i = 0; i < amount; i++) {
163 newRows.add(rowCounter++);
165 rows.addAll(offset, newRows);
168 @SuppressWarnings("boxing")
169 public void insertColumns(final int offset, final int amount) {
170 final List<Integer> newColumns = new ArrayList<Integer>();
171 for (int i = 0; i < amount; i++) {
172 newColumns.add(columnCounter++);
174 columns.addAll(offset, newColumns);
177 public EscalatorUpdater createHeaderUpdater() {
178 return new TestEscalatorUpdater() {
180 public void update(final Row row,
181 final Iterable<FlyweightCell> cellsToUpdate) {
182 for (final FlyweightCell cell : cellsToUpdate) {
183 final Integer columnName = columns
184 .get(cell.getColumn());
185 cell.getElement().setInnerText("Header " + columnName);
187 if (colspan == Colspan.NORMAL) {
188 if (cell.getColumn() % 2 == 0) {
191 } else if (colspan == Colspan.CRAZY) {
192 if (cell.getColumn() % 3 == 0) {
201 public EscalatorUpdater createFooterUpdater() {
202 return new TestEscalatorUpdater() {
204 public void update(final Row row,
205 final Iterable<FlyweightCell> cellsToUpdate) {
206 for (final FlyweightCell cell : cellsToUpdate) {
207 final Integer columnName = columns
208 .get(cell.getColumn());
209 cell.getElement().setInnerText("Footer " + columnName);
211 if (colspan == Colspan.NORMAL) {
212 if (cell.getColumn() % 2 == 0) {
215 } else if (colspan == Colspan.CRAZY) {
216 if (cell.getColumn() % 3 == 1) {
225 public EscalatorUpdater createBodyUpdater() {
226 return new TestEscalatorUpdater() {
228 public void renderCell(final FlyweightCell cell) {
229 final Integer columnName = columns.get(cell.getColumn());
230 final Integer rowName = rows.get(cell.getRow());
231 String cellInfo = columnName + "," + rowName;
233 if (cell.getColumn() > 0) {
234 cell.getElement().setInnerText("Cell: " + cellInfo);
236 cell.getElement().setInnerText(
237 "Row " + cell.getRow() + ": " + cellInfo);
240 if (colspan == Colspan.NORMAL) {
241 if (cell.getColumn() % 2 == 0) {
244 } else if (colspan == Colspan.CRAZY) {
245 if (cell.getColumn() % 3 == cell.getRow() % 3) {
252 public void update(final Row row,
253 final Iterable<FlyweightCell> cellsToUpdate) {
254 for (final FlyweightCell cell : cellsToUpdate) {
261 public void removeRows(final int offset, final int amount) {
262 for (int i = 0; i < amount; i++) {
267 public void removeColumns(final int offset, final int amount) {
268 for (int i = 0; i < amount; i++) {
269 columns.remove(offset);
274 protected final Escalator escalator;
275 private final Data data = new Data();
277 private enum Colspan {
281 private Colspan colspan = Colspan.NONE;
282 protected final LogWidget logWidget = new LogWidget();
284 public EscalatorBasicClientFeaturesWidget() {
285 super(new EscalatorProxy());
286 escalator = getTestedWidget();
287 logWidget.setEscalator(escalator);
289 ((EscalatorProxy) escalator).setLogWidget(logWidget);
290 addNorth(logWidget, 200);
292 final RowContainer header = escalator.getHeader();
293 header.setEscalatorUpdater(data.createHeaderUpdater());
295 final RowContainer footer = escalator.getFooter();
296 footer.setEscalatorUpdater(data.createFooterUpdater());
298 escalator.getBody().setEscalatorUpdater(data.createBodyUpdater());
303 escalator.getElement().getStyle().setZIndex(0);
304 addNorth(escalator, 500);
308 createHeaderRowsMenu();
309 createBodyRowsMenu();
310 createFooterRowsMenu();
311 createColumnsAndRowsMenu();
317 private void createFrozenMenu() {
318 String[] menupath = { FEATURES_MENU, "Frozen columns" };
319 addMenuCommand("Freeze 1 column", new ScheduledCommand() {
321 public void execute() {
322 escalator.getColumnConfiguration().setFrozenColumnCount(1);
325 addMenuCommand("Freeze 0 columns", new ScheduledCommand() {
327 public void execute() {
328 escalator.getColumnConfiguration().setFrozenColumnCount(0);
333 private void createColspanMenu() {
334 String[] menupath = { FEATURES_MENU, "Column spanning" };
335 addMenuCommand("Apply normal colspan", new ScheduledCommand() {
337 public void execute() {
338 colspan = Colspan.NORMAL;
342 addMenuCommand("Apply crazy colspan", new ScheduledCommand() {
344 public void execute() {
345 colspan = Colspan.CRAZY;
349 addMenuCommand("Apply no colspan", new ScheduledCommand() {
351 public void execute() {
352 colspan = Colspan.NONE;
358 private void createColumnsAndRowsMenu() {
359 String[] menupath = { COLUMNS_AND_ROWS_MENU };
360 addMenuCommand("Add one of each row", new ScheduledCommand() {
362 public void execute() {
363 insertRows(escalator.getHeader(), 0, 1);
364 insertRows(escalator.getBody(), 0, 1);
365 insertRows(escalator.getFooter(), 0, 1);
368 addMenuCommand("Remove one of each row", new ScheduledCommand() {
370 public void execute() {
371 removeRows(escalator.getHeader(), 0, 1);
372 removeRows(escalator.getBody(), 0, 1);
373 removeRows(escalator.getFooter(), 0, 1);
378 private void createGeneralMenu() {
379 String[] menupath = { GENERAL_MENU };
381 addMenuCommand("Detach Escalator", new ScheduledCommand() {
383 public void execute() {
384 escalator.removeFromParent();
388 addMenuCommand("Attach Escalator", new ScheduledCommand() {
390 public void execute() {
391 if (!escalator.isAttached()) {
392 addNorth(escalator, 500);
397 addMenuCommand("Clear (columns, then rows)", new ScheduledCommand() {
399 public void execute() {
403 addMenuCommand("Clear (rows, then columns)", new ScheduledCommand() {
405 public void execute() {
409 addMenuCommand("Populate Escalator (columns, then rows)",
410 new ScheduledCommand() {
412 public void execute() {
414 insertColumns(0, 10);
415 insertRows(escalator.getHeader(), 0, 1);
416 insertRows(escalator.getBody(), 0, 100);
417 insertRows(escalator.getFooter(), 0, 1);
420 addMenuCommand("Populate Escalator (rows, then columns)",
421 new ScheduledCommand() {
423 public void execute() {
425 insertRows(escalator.getHeader(), 0, 1);
426 insertRows(escalator.getBody(), 0, 100);
427 insertRows(escalator.getFooter(), 0, 1);
428 insertColumns(0, 10);
435 private void createSizeMenu() {
436 String[] menupath = new String[] { "General", "Size" };
438 addSizeMenuItem(null, "height", menupath);
439 addSizeMenuItem("200px", "height", menupath);
440 addSizeMenuItem("400px", "height", menupath);
441 addSizeMenuItem(null, "width", menupath);
442 addSizeMenuItem("200px", "width", menupath);
443 addSizeMenuItem("400px", "width", menupath);
446 private void addSizeMenuItem(final String size, final String direction,
448 final String title = (size != null ? size : "undefined");
449 addMenuCommand(title + " " + direction, new ScheduledCommand() {
451 public void execute() {
452 if (direction.equals("height")) {
453 escalator.setHeight(size);
455 escalator.setWidth(size);
461 private void createColumnMenu() {
462 String[] menupath = { COLUMNS_AND_ROWS_MENU, "Columns" };
463 addMenuCommand("Add one column to beginning", new ScheduledCommand() {
465 public void execute() {
469 addMenuCommand("Add one column to end", new ScheduledCommand() {
471 public void execute() {
473 escalator.getColumnConfiguration().getColumnCount(), 1);
476 addMenuCommand("Add ten columns", new ScheduledCommand() {
478 public void execute() {
479 insertColumns(0, 10);
482 addMenuCommand("Remove one column from beginning",
483 new ScheduledCommand() {
485 public void execute() {
489 addMenuCommand("Remove one column from end", new ScheduledCommand() {
491 public void execute() {
493 escalator.getColumnConfiguration().getColumnCount() - 1,
498 addMenuCommand("Refresh first column", new ScheduledCommand() {
500 public void execute() {
501 escalator.getColumnConfiguration().refreshColumns(0, 1);
505 addMenuCommand("Resize first column to max width",
506 new ScheduledCommand() {
508 public void execute() {
509 escalator.getColumnConfiguration().setColumnWidth(0,
514 addMenuCommand("Resize first column to 100 px", new ScheduledCommand() {
516 public void execute() {
517 escalator.getColumnConfiguration().setColumnWidth(0, 100);
522 private void createHeaderRowsMenu() {
523 String[] menupath = { COLUMNS_AND_ROWS_MENU, "Header Rows" };
524 createRowsMenu(escalator.getHeader(), menupath);
527 private void createFooterRowsMenu() {
528 String[] menupath = { COLUMNS_AND_ROWS_MENU, "Footer Rows" };
529 createRowsMenu(escalator.getFooter(), menupath);
532 private void createBodyRowsMenu() {
533 String[] menupath = { COLUMNS_AND_ROWS_MENU, "Body Rows" };
534 createRowsMenu(escalator.getBody(), menupath);
536 addMenuCommand("Add 5 rows to top", new ScheduledCommand() {
538 public void execute() {
539 insertRows(escalator.getBody(), 0, 5);
542 addMenuCommand("Add 50 rows to top", new ScheduledCommand() {
544 public void execute() {
545 insertRows(escalator.getBody(), 0, 50);
548 addMenuCommand("Remove 5 rows from bottom", new ScheduledCommand() {
550 public void execute() {
551 removeRows(escalator.getBody(),
552 escalator.getBody().getRowCount() - 5, 5);
555 addMenuCommand("Remove 50 rows from bottom", new ScheduledCommand() {
557 public void execute() {
558 removeRows(escalator.getBody(),
559 escalator.getBody().getRowCount() - 50, 50);
562 addMenuCommand("Remove 50 rows from almost bottom",
563 new ScheduledCommand() {
565 public void execute() {
566 removeRows(escalator.getBody(),
567 escalator.getBody().getRowCount() - 60, 50);
570 addMenuCommand("Remove all, insert 30 and scroll 40px",
571 new ScheduledCommand() {
573 public void execute() {
574 removeRows(escalator.getBody(), 0,
575 escalator.getBody().getRowCount());
576 insertRows(escalator.getBody(), 0, 30);
577 escalator.setScrollTop(40);
581 String[] scrollToRowMenuPath = new String[menupath.length + 1];
582 System.arraycopy(menupath, 0, scrollToRowMenuPath, 0, menupath.length);
583 scrollToRowMenuPath[scrollToRowMenuPath.length - 1] = "Scroll to...";
584 for (int i = 0; i < 100; i += 25) {
585 final int rowIndex = i;
586 addMenuCommand("Row " + i, new ScheduledCommand() {
588 public void execute() {
589 escalator.scrollToRow(rowIndex, ScrollDestination.ANY, 0);
591 }, scrollToRowMenuPath);
594 addMenuCommand("Set 20px default height", new ScheduledCommand() {
596 public void execute() {
597 escalator.getBody().setDefaultRowHeight(20);
602 private void createRowsMenu(final RowContainer container,
604 addMenuCommand("Add one row to beginning", new ScheduledCommand() {
606 public void execute() {
609 insertRows(container, offset, number);
612 addMenuCommand("Add one row to end", new ScheduledCommand() {
614 public void execute() {
615 int offset = container.getRowCount();
617 insertRows(container, offset, number);
620 addMenuCommand("Remove one row from beginning", new ScheduledCommand() {
622 public void execute() {
625 removeRows(container, offset, number);
628 addMenuCommand("Remove one row from end", new ScheduledCommand() {
630 public void execute() {
631 int offset = container.getRowCount() - 1;
633 removeRows(container, offset, number);
636 addMenuCommand("Remove all rows", new ScheduledCommand() {
638 public void execute() {
639 if (container.getRowCount() > 0) {
640 removeRows(container, 0, container.getRowCount());
646 private void createSpacerMenu() {
647 String[] menupath = { "Features", "Spacers" };
649 addMenuCommand("Swap Spacer Updater", new ScheduledCommand() {
650 private final SpacerUpdater CUSTOM = new SpacerUpdater() {
652 public void destroy(Spacer spacer) {
653 spacer.getElement().setInnerText("");
657 public void init(Spacer spacer) {
659 .setInnerText("Spacer for row " + spacer.getRow());
664 public void execute() {
665 BodyRowContainer body = escalator.getBody();
667 if (SpacerUpdater.NULL.equals(body.getSpacerUpdater())) {
668 body.setSpacerUpdater(CUSTOM);
670 body.setSpacerUpdater(SpacerUpdater.NULL);
675 addMenuCommand("Focusable Updater", new ScheduledCommand() {
677 public void execute() {
678 escalator.getBody().setSpacerUpdater(new SpacerUpdater() {
680 public void init(Spacer spacer) {
681 spacer.getElement().appendChild(DOM.createInputText());
685 public void destroy(Spacer spacer) {
686 spacer.getElement().removeAllChildren();
692 createSpacersMenuForRow(-1, menupath);
693 createSpacersMenuForRow(1, menupath);
694 createSpacersMenuForRow(50, menupath);
695 createSpacersMenuForRow(99, menupath);
698 private void createSpacersMenuForRow(final int rowIndex,
700 menupath = new String[] { menupath[0], menupath[1], "Row " + rowIndex };
701 addMenuCommand("Set 100px", new ScheduledCommand() {
703 public void execute() {
704 escalator.getBody().setSpacer(rowIndex, 100);
707 addMenuCommand("Set 50px", new ScheduledCommand() {
709 public void execute() {
710 escalator.getBody().setSpacer(rowIndex, 50);
713 addMenuCommand("Remove", new ScheduledCommand() {
715 public void execute() {
716 escalator.getBody().setSpacer(rowIndex, -1);
719 addMenuCommand("Scroll here (ANY, 0)", new ScheduledCommand() {
721 public void execute() {
722 escalator.scrollToSpacer(rowIndex, ScrollDestination.ANY, 0);
725 addMenuCommand("Scroll here row+spacer below (ANY, 0)",
726 new ScheduledCommand() {
728 public void execute() {
729 escalator.scrollToRowAndSpacer(rowIndex,
730 ScrollDestination.ANY, 0);
735 private void insertRows(final RowContainer container, int offset,
737 if (container == escalator.getBody()) {
738 data.insertRows(offset, number);
739 escalator.getBody().insertRows(offset, number);
741 container.insertRows(offset, number);
745 private void removeRows(final RowContainer container, int offset,
747 if (container == escalator.getBody()) {
748 data.removeRows(offset, number);
749 escalator.getBody().removeRows(offset, number);
751 container.removeRows(offset, number);
755 private void insertColumns(final int offset, final int number) {
756 data.insertColumns(offset, number);
757 escalator.getColumnConfiguration().insertColumns(offset, number);
760 private void removeColumns(final int offset, final int number) {
761 data.removeColumns(offset, number);
762 escalator.getColumnConfiguration().removeColumns(offset, number);
765 private void resetColRow() {
766 if (escalator.getColumnConfiguration().getColumnCount() > 0) {
768 escalator.getColumnConfiguration().getColumnCount());
770 if (escalator.getFooter().getRowCount() > 0) {
771 removeRows(escalator.getFooter(), 0,
772 escalator.getFooter().getRowCount());
775 if (escalator.getBody().getRowCount() > 0) {
776 removeRows(escalator.getBody(), 0,
777 escalator.getBody().getRowCount());
780 if (escalator.getHeader().getRowCount() > 0) {
781 removeRows(escalator.getHeader(), 0,
782 escalator.getHeader().getRowCount());
786 private void resetRowCol() {
787 if (escalator.getFooter().getRowCount() > 0) {
788 removeRows(escalator.getFooter(), 0,
789 escalator.getFooter().getRowCount());
792 if (escalator.getBody().getRowCount() > 0) {
793 removeRows(escalator.getBody(), 0,
794 escalator.getBody().getRowCount());
797 if (escalator.getHeader().getRowCount() > 0) {
798 removeRows(escalator.getHeader(), 0,
799 escalator.getHeader().getRowCount());
802 if (escalator.getColumnConfiguration().getColumnCount() > 0) {
804 escalator.getColumnConfiguration().getColumnCount());
808 private void refreshEscalator() {
809 if (escalator.getHeader().getRowCount() > 0) {
810 escalator.getHeader().refreshRows(0,
811 escalator.getHeader().getRowCount());
814 if (escalator.getBody().getRowCount() > 0) {
815 escalator.getBody().refreshRows(0,
816 escalator.getBody().getRowCount());
819 if (escalator.getFooter().getRowCount() > 0) {
820 escalator.getFooter().refreshRows(0,
821 escalator.getFooter().getRowCount());