123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659 |
- package com.vaadin.tests.widgetset.client.grid;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import com.google.gwt.core.client.Duration;
- import com.google.gwt.core.client.Scheduler.ScheduledCommand;
- import com.google.gwt.dom.client.TableCellElement;
- import com.google.gwt.user.client.ui.Composite;
- import com.google.gwt.user.client.ui.HTML;
- import com.vaadin.client.ui.grid.Escalator;
- import com.vaadin.client.ui.grid.EscalatorUpdater;
- import com.vaadin.client.ui.grid.FlyweightCell;
- import com.vaadin.client.ui.grid.Row;
- import com.vaadin.client.ui.grid.RowContainer;
-
- public class EscalatorBasicClientFeaturesWidget extends
- PureGWTTestApplication<Escalator> {
-
- public static class LogWidget extends Composite {
-
- private static final int MAX_LOG = 9;
-
- private final HTML html = new HTML();
- private final List<String> logs = new ArrayList<String>();
- private Escalator escalator;
-
- public LogWidget() {
- initWidget(html);
- getElement().setId("log");
- }
-
- public void setEscalator(Escalator escalator) {
- this.escalator = escalator;
- }
-
- public void updateDebugLabel() {
- int headers = escalator.getHeader().getRowCount();
- int bodys = escalator.getBody().getRowCount();
- int footers = escalator.getFooter().getRowCount();
- int columns = escalator.getColumnConfiguration().getColumnCount();
-
- while (logs.size() > MAX_LOG) {
- logs.remove(0);
- }
-
- String logString = "<hr>";
- for (String log : logs) {
- logString += log + "<br>";
- }
-
- html.setHTML("Columns: " + columns + "<br>" + //
- "Header rows: " + headers + "<br>" + //
- "Body rows: " + bodys + "<br>" + //
- "Footer rows: " + footers + "<br>" + //
- logString);
- }
-
- public void log(String string) {
- logs.add((Duration.currentTimeMillis() % 10000) + ": " + string);
- }
- }
-
- public static class UpdaterLifetimeWidget extends
- EscalatorBasicClientFeaturesWidget {
-
- private final EscalatorUpdater debugUpdater = new EscalatorUpdater() {
- @Override
- public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) {
- log("preAttach", cellsToAttach);
- }
-
- @Override
- public void postAttach(Row row,
- Iterable<FlyweightCell> attachedCells) {
- log("postAttach", attachedCells);
- }
-
- @Override
- public void update(Row row, Iterable<FlyweightCell> cellsToUpdate) {
- log("update", cellsToUpdate);
- }
-
- @Override
- public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) {
- log("preDetach", cellsToDetach);
- }
-
- @Override
- public void postDetach(Row row,
- Iterable<FlyweightCell> detachedCells) {
- log("postDetach", detachedCells);
- }
-
- private void log(String methodName, Iterable<FlyweightCell> cells) {
- if (!cells.iterator().hasNext()) {
- return;
- }
-
- TableCellElement cellElement = cells.iterator().next()
- .getElement();
- boolean isAttached = cellElement.getParentElement() != null
- && cellElement.getParentElement().getParentElement() != null;
- logWidget.log(methodName + ": elementIsAttached == "
- + isAttached);
- }
- };
-
- public UpdaterLifetimeWidget() {
- super();
- escalator.getHeader().setEscalatorUpdater(debugUpdater);
- escalator.getBody().setEscalatorUpdater(debugUpdater);
- escalator.getFooter().setEscalatorUpdater(debugUpdater);
- }
- }
-
- private static final String COLUMNS_AND_ROWS_MENU = "Columns and Rows";
- private static final String GENERAL_MENU = "General";
- private static final String FEATURES_MENU = "Features";
-
- private static abstract class TestEscalatorUpdater implements
- EscalatorUpdater {
-
- @Override
- public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) {
- // noop
- }
-
- @Override
- public void postAttach(Row row, Iterable<FlyweightCell> attachedCells) {
- // noop
- }
-
- @Override
- public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) {
- // noop
- }
-
- @Override
- public void postDetach(Row row, Iterable<FlyweightCell> detachedCells) {
- // noop
- }
- }
-
- private class Data {
- private int columnCounter = 0;
- private int rowCounter = 0;
- private final List<Integer> columns = new ArrayList<Integer>();
- private final List<Integer> rows = new ArrayList<Integer>();
-
- @SuppressWarnings("boxing")
- public void insertRows(final int offset, final int amount) {
- final List<Integer> newRows = new ArrayList<Integer>();
- for (int i = 0; i < amount; i++) {
- newRows.add(rowCounter++);
- }
- rows.addAll(offset, newRows);
- }
-
- @SuppressWarnings("boxing")
- public void insertColumns(final int offset, final int amount) {
- final List<Integer> newColumns = new ArrayList<Integer>();
- for (int i = 0; i < amount; i++) {
- newColumns.add(columnCounter++);
- }
- columns.addAll(offset, newColumns);
- }
-
- public EscalatorUpdater createHeaderUpdater() {
- return new TestEscalatorUpdater() {
- @Override
- public void update(final Row row,
- final Iterable<FlyweightCell> cellsToUpdate) {
- for (final FlyweightCell cell : cellsToUpdate) {
- final Integer columnName = columns
- .get(cell.getColumn());
- cell.getElement().setInnerText("Header " + columnName);
-
- if (colspan == Colspan.NORMAL) {
- if (cell.getColumn() % 2 == 0) {
- cell.setColSpan(2);
- }
- } else if (colspan == Colspan.CRAZY) {
- if (cell.getColumn() % 3 == 0) {
- cell.setColSpan(2);
- }
- }
- }
- }
- };
- }
-
- public EscalatorUpdater createFooterUpdater() {
- return new TestEscalatorUpdater() {
- @Override
- public void update(final Row row,
- final Iterable<FlyweightCell> cellsToUpdate) {
- for (final FlyweightCell cell : cellsToUpdate) {
- final Integer columnName = columns
- .get(cell.getColumn());
- cell.getElement().setInnerText("Footer " + columnName);
-
- if (colspan == Colspan.NORMAL) {
- if (cell.getColumn() % 2 == 0) {
- cell.setColSpan(2);
- }
- } else if (colspan == Colspan.CRAZY) {
- if (cell.getColumn() % 3 == 1) {
- cell.setColSpan(2);
- }
- }
- }
- }
- };
- }
-
- public EscalatorUpdater createBodyUpdater() {
- return new TestEscalatorUpdater() {
-
- public void renderCell(final FlyweightCell cell) {
- final Integer columnName = columns.get(cell.getColumn());
- final Integer rowName = rows.get(cell.getRow());
- String cellInfo = columnName + "," + rowName;
-
- if (cell.getColumn() > 0) {
- cell.getElement().setInnerText("Cell: " + cellInfo);
- } else {
- cell.getElement().setInnerText(
- "Row " + cell.getRow() + ": " + cellInfo);
- }
-
- if (colspan == Colspan.NORMAL) {
- if (cell.getColumn() % 2 == 0) {
- cell.setColSpan(2);
- }
- } else if (colspan == Colspan.CRAZY) {
- if (cell.getColumn() % 3 == cell.getRow() % 3) {
- cell.setColSpan(2);
- }
- }
- }
-
- @Override
- public void update(final Row row,
- final Iterable<FlyweightCell> cellsToUpdate) {
- for (final FlyweightCell cell : cellsToUpdate) {
- renderCell(cell);
- }
- }
- };
- }
-
- public void removeRows(final int offset, final int amount) {
- for (int i = 0; i < amount; i++) {
- rows.remove(offset);
- }
- }
-
- public void removeColumns(final int offset, final int amount) {
- for (int i = 0; i < amount; i++) {
- columns.remove(offset);
- }
- }
- }
-
- protected final Escalator escalator;
- private final Data data = new Data();
-
- private enum Colspan {
- NONE, NORMAL, CRAZY;
- }
-
- private Colspan colspan = Colspan.NONE;
- protected final LogWidget logWidget = new LogWidget();
-
- public EscalatorBasicClientFeaturesWidget() {
- super(new EscalatorProxy());
- escalator = getTestedWidget();
- logWidget.setEscalator(escalator);
-
- ((EscalatorProxy) escalator).setLogWidget(logWidget);
- addNorth(logWidget, 200);
-
- final RowContainer header = escalator.getHeader();
- header.setEscalatorUpdater(data.createHeaderUpdater());
-
- final RowContainer footer = escalator.getFooter();
- footer.setEscalatorUpdater(data.createFooterUpdater());
-
- escalator.getBody().setEscalatorUpdater(data.createBodyUpdater());
-
- setWidth("500px");
- setHeight("500px");
-
- escalator.getElement().getStyle().setZIndex(0);
- addNorth(escalator, 500);
-
- createGeneralMenu();
- createColumnMenu();
- createHeaderRowsMenu();
- createBodyRowsMenu();
- createFooterRowsMenu();
- createColumnsAndRowsMenu();
- createFrozenMenu();
- createColspanMenu();
- }
-
- private void createFrozenMenu() {
- String[] menupath = { FEATURES_MENU, "Frozen columns" };
- addMenuCommand("Freeze 1 column", new ScheduledCommand() {
- @Override
- public void execute() {
- escalator.getColumnConfiguration().setFrozenColumnCount(1);
- }
- }, menupath);
- addMenuCommand("Freeze 0 columns", new ScheduledCommand() {
- @Override
- public void execute() {
- escalator.getColumnConfiguration().setFrozenColumnCount(0);
- }
- }, menupath);
- }
-
- private void createColspanMenu() {
- String[] menupath = { FEATURES_MENU, "Column spanning" };
- addMenuCommand("Apply normal colspan", new ScheduledCommand() {
- @Override
- public void execute() {
- colspan = Colspan.NORMAL;
- refreshEscalator();
- }
- }, menupath);
- addMenuCommand("Apply crazy colspan", new ScheduledCommand() {
- @Override
- public void execute() {
- colspan = Colspan.CRAZY;
- refreshEscalator();
- }
- }, menupath);
- addMenuCommand("Apply no colspan", new ScheduledCommand() {
- @Override
- public void execute() {
- colspan = Colspan.NONE;
- refreshEscalator();
- }
- }, menupath);
- }
-
- private void createColumnsAndRowsMenu() {
- String[] menupath = { COLUMNS_AND_ROWS_MENU };
- addMenuCommand("Add one of each row", new ScheduledCommand() {
- @Override
- public void execute() {
- insertRows(escalator.getHeader(), 0, 1);
- insertRows(escalator.getBody(), 0, 1);
- insertRows(escalator.getFooter(), 0, 1);
- }
- }, menupath);
- addMenuCommand("Remove one of each row", new ScheduledCommand() {
- @Override
- public void execute() {
- removeRows(escalator.getHeader(), 0, 1);
- removeRows(escalator.getBody(), 0, 1);
- removeRows(escalator.getFooter(), 0, 1);
- }
- }, menupath);
- }
-
- private void createGeneralMenu() {
- String[] menupath = { GENERAL_MENU };
-
- addMenuCommand("Detach Escalator", new ScheduledCommand() {
- @Override
- public void execute() {
- escalator.removeFromParent();
- }
- }, menupath);
-
- addMenuCommand("Attach Escalator", new ScheduledCommand() {
- @Override
- public void execute() {
- if (!escalator.isAttached()) {
- addNorth(escalator, 500);
- }
- }
- }, menupath);
-
- addMenuCommand("Clear (columns, then rows)", new ScheduledCommand() {
- @Override
- public void execute() {
- resetColRow();
- }
- }, menupath);
- addMenuCommand("Clear (rows, then columns)", new ScheduledCommand() {
- @Override
- public void execute() {
- resetRowCol();
- }
- }, menupath);
- addMenuCommand("Populate Escalator (columns, then rows)",
- new ScheduledCommand() {
- @Override
- public void execute() {
- resetColRow();
- insertColumns(0, 10);
- insertRows(escalator.getHeader(), 0, 1);
- insertRows(escalator.getBody(), 0, 100);
- insertRows(escalator.getFooter(), 0, 1);
- }
- }, menupath);
- addMenuCommand("Populate Escalator (rows, then columns)",
- new ScheduledCommand() {
- @Override
- public void execute() {
- resetColRow();
- insertRows(escalator.getHeader(), 0, 1);
- insertRows(escalator.getBody(), 0, 100);
- insertRows(escalator.getFooter(), 0, 1);
- insertColumns(0, 10);
- }
- }, menupath);
- }
-
- private void createColumnMenu() {
- String[] menupath = { COLUMNS_AND_ROWS_MENU, "Columns" };
- addMenuCommand("Add one column to beginning", new ScheduledCommand() {
- @Override
- public void execute() {
- insertColumns(0, 1);
- }
- }, menupath);
- addMenuCommand("Add one column to end", new ScheduledCommand() {
- @Override
- public void execute() {
- insertColumns(escalator.getColumnConfiguration()
- .getColumnCount(), 1);
- }
- }, menupath);
- addMenuCommand("Add ten columns", new ScheduledCommand() {
- @Override
- public void execute() {
- insertColumns(0, 10);
- }
- }, menupath);
- addMenuCommand("Remove one column from beginning",
- new ScheduledCommand() {
- @Override
- public void execute() {
- removeColumns(0, 1);
- }
- }, menupath);
- addMenuCommand("Remove one column from end", new ScheduledCommand() {
- @Override
- public void execute() {
- removeColumns(escalator.getColumnConfiguration()
- .getColumnCount() - 1, 1);
- }
- }, menupath);
-
- addMenuCommand("Refresh first column", new ScheduledCommand() {
- @Override
- public void execute() {
- escalator.getColumnConfiguration().refreshColumns(0, 1);
- }
- }, menupath);
-
- addMenuCommand("Resize first column to max width",
- new ScheduledCommand() {
- @Override
- public void execute() {
- escalator.getColumnConfiguration()
- .setColumnWidthToContent(0);
- }
- }, menupath);
- }
-
- private void createHeaderRowsMenu() {
- String[] menupath = { COLUMNS_AND_ROWS_MENU, "Header Rows" };
- createRowsMenu(escalator.getHeader(), menupath);
- }
-
- private void createFooterRowsMenu() {
- String[] menupath = { COLUMNS_AND_ROWS_MENU, "Footer Rows" };
- createRowsMenu(escalator.getFooter(), menupath);
- }
-
- private void createBodyRowsMenu() {
- String[] menupath = { COLUMNS_AND_ROWS_MENU, "Body Rows" };
- createRowsMenu(escalator.getBody(), menupath);
-
- addMenuCommand("Add 5 rows to top", new ScheduledCommand() {
- @Override
- public void execute() {
- insertRows(escalator.getBody(), 0, 5);
- }
- }, menupath);
- addMenuCommand("Add 50 rows to top", new ScheduledCommand() {
- @Override
- public void execute() {
- insertRows(escalator.getBody(), 0, 50);
- }
- }, menupath);
- addMenuCommand("Remove 5 rows from bottom", new ScheduledCommand() {
- @Override
- public void execute() {
- removeRows(escalator.getBody(), escalator.getBody()
- .getRowCount() - 5, 5);
- }
- }, menupath);
- addMenuCommand("Remove 50 rows from bottom", new ScheduledCommand() {
- @Override
- public void execute() {
- removeRows(escalator.getBody(), escalator.getBody()
- .getRowCount() - 50, 50);
- }
- }, menupath);
- addMenuCommand("Remove 50 rows from almost bottom",
- new ScheduledCommand() {
- @Override
- public void execute() {
- removeRows(escalator.getBody(), escalator.getBody()
- .getRowCount() - 60, 50);
- }
- }, menupath);
- addMenuCommand("Remove all, insert 30 and scroll 40px",
- new ScheduledCommand() {
- @Override
- public void execute() {
- removeRows(escalator.getBody(), 0, escalator.getBody()
- .getRowCount());
- insertRows(escalator.getBody(), 0, 30);
- escalator.setScrollTop(40);
- }
- }, menupath);
- }
-
- private void createRowsMenu(final RowContainer container, String[] menupath) {
- addMenuCommand("Add one row to beginning", new ScheduledCommand() {
- @Override
- public void execute() {
- int offset = 0;
- int number = 1;
- insertRows(container, offset, number);
- }
- }, menupath);
- addMenuCommand("Add one row to end", new ScheduledCommand() {
- @Override
- public void execute() {
- int offset = container.getRowCount();
- int number = 1;
- insertRows(container, offset, number);
- }
- }, menupath);
- addMenuCommand("Remove one row from beginning", new ScheduledCommand() {
- @Override
- public void execute() {
- int offset = 0;
- int number = 1;
- removeRows(container, offset, number);
- }
- }, menupath);
- addMenuCommand("Remove one row from end", new ScheduledCommand() {
- @Override
- public void execute() {
- int offset = container.getRowCount() - 1;
- int number = 1;
- removeRows(container, offset, number);
- }
- }, menupath);
- }
-
- private void insertRows(final RowContainer container, int offset, int number) {
- if (container == escalator.getBody()) {
- data.insertRows(offset, number);
- escalator.getBody().insertRows(offset, number);
- } else {
- container.insertRows(offset, number);
- }
- }
-
- private void removeRows(final RowContainer container, int offset, int number) {
- if (container == escalator.getBody()) {
- data.removeRows(offset, number);
- escalator.getBody().removeRows(offset, number);
- } else {
- container.removeRows(offset, number);
- }
- }
-
- private void insertColumns(final int offset, final int number) {
- data.insertColumns(offset, number);
- escalator.getColumnConfiguration().insertColumns(offset, number);
- }
-
- private void removeColumns(final int offset, final int number) {
- data.removeColumns(offset, number);
- escalator.getColumnConfiguration().removeColumns(offset, number);
- }
-
- private void resetColRow() {
- if (escalator.getColumnConfiguration().getColumnCount() > 0) {
- removeColumns(0, escalator.getColumnConfiguration()
- .getColumnCount());
- }
- if (escalator.getFooter().getRowCount() > 0) {
- removeRows(escalator.getFooter(), 0, escalator.getFooter()
- .getRowCount());
- }
-
- if (escalator.getBody().getRowCount() > 0) {
- removeRows(escalator.getBody(), 0, escalator.getBody()
- .getRowCount());
- }
-
- if (escalator.getHeader().getRowCount() > 0) {
- removeRows(escalator.getHeader(), 0, escalator.getHeader()
- .getRowCount());
- }
- }
-
- private void resetRowCol() {
- if (escalator.getFooter().getRowCount() > 0) {
- removeRows(escalator.getFooter(), 0, escalator.getFooter()
- .getRowCount());
- }
-
- if (escalator.getBody().getRowCount() > 0) {
- removeRows(escalator.getBody(), 0, escalator.getBody()
- .getRowCount());
- }
-
- if (escalator.getHeader().getRowCount() > 0) {
- removeRows(escalator.getHeader(), 0, escalator.getHeader()
- .getRowCount());
- }
-
- if (escalator.getColumnConfiguration().getColumnCount() > 0) {
- removeColumns(0, escalator.getColumnConfiguration()
- .getColumnCount());
- }
- }
-
- private void refreshEscalator() {
- if (escalator.getHeader().getRowCount() > 0) {
- escalator.getHeader().refreshRows(0,
- escalator.getHeader().getRowCount());
- }
-
- if (escalator.getBody().getRowCount() > 0) {
- escalator.getBody().refreshRows(0,
- escalator.getBody().getRowCount());
- }
-
- if (escalator.getFooter().getRowCount() > 0) {
- escalator.getFooter().refreshRows(0,
- escalator.getFooter().getRowCount());
- }
- }
- }
|