2 * Copyright 2000-2016 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.tests.widgetset.client.v7.grid;
18 import java.util.ArrayList;
19 import java.util.Comparator;
20 import java.util.Date;
21 import java.util.List;
23 import com.google.gwt.dom.client.Document;
24 import com.google.gwt.dom.client.Element;
25 import com.google.gwt.event.dom.client.ClickEvent;
26 import com.google.gwt.event.dom.client.ClickHandler;
27 import com.google.gwt.user.client.Timer;
28 import com.google.gwt.user.client.Window.Location;
29 import com.google.gwt.user.client.ui.Button;
30 import com.google.gwt.user.client.ui.HasWidgets;
31 import com.vaadin.client.data.DataChangeHandler;
32 import com.vaadin.client.data.DataSource;
33 import com.vaadin.client.ui.AbstractComponentConnector;
34 import com.vaadin.shared.Registration;
35 import com.vaadin.shared.ui.Connect;
36 import com.vaadin.tests.widgetset.server.v7.grid.GridClientColumnRenderers;
37 import com.vaadin.v7.client.renderers.ComplexRenderer;
38 import com.vaadin.v7.client.renderers.DateRenderer;
39 import com.vaadin.v7.client.renderers.HtmlRenderer;
40 import com.vaadin.v7.client.renderers.NumberRenderer;
41 import com.vaadin.v7.client.renderers.Renderer;
42 import com.vaadin.v7.client.renderers.TextRenderer;
43 import com.vaadin.v7.client.renderers.WidgetRenderer;
44 import com.vaadin.v7.client.widget.grid.CellReference;
45 import com.vaadin.v7.client.widget.grid.RendererCellReference;
46 import com.vaadin.v7.client.widget.grid.datasources.ListDataSource;
47 import com.vaadin.v7.client.widget.grid.datasources.ListSorter;
48 import com.vaadin.v7.client.widget.grid.sort.Sort;
49 import com.vaadin.v7.client.widget.grid.sort.SortEvent;
50 import com.vaadin.v7.client.widget.grid.sort.SortHandler;
51 import com.vaadin.v7.client.widget.grid.sort.SortOrder;
52 import com.vaadin.v7.client.widgets.Grid;
54 @Connect(GridClientColumnRenderers.GridController.class)
55 public class GridClientColumnRendererConnector
56 extends AbstractComponentConnector {
58 public static enum Renderers {
59 TEXT_RENDERER, WIDGET_RENDERER, HTML_RENDERER, NUMBER_RENDERER, DATE_RENDERER, CPLX_RENDERER;
63 * Datasource for simulating network latency
65 private class DelayedDataSource implements DataSource<String> {
67 private DataSource<String> ds;
68 private int firstRowIndex = -1;
69 private int numberOfRows;
70 private DataChangeHandler dataChangeHandler;
73 public DelayedDataSource(DataSource<String> ds, int latency) {
75 this.latency = latency;
79 public void ensureAvailability(final int firstRowIndex,
80 final int numberOfRows) {
85 DelayedDataSource.this.firstRowIndex = firstRowIndex;
86 DelayedDataSource.this.numberOfRows = numberOfRows;
87 dataChangeHandler.dataUpdated(firstRowIndex, numberOfRows);
88 dataChangeHandler.dataAvailable(firstRowIndex,
95 public String getRow(int rowIndex) {
96 if (rowIndex >= firstRowIndex
97 && rowIndex <= firstRowIndex + numberOfRows) {
98 return ds.getRow(rowIndex);
109 public Registration addDataChangeHandler(
110 DataChangeHandler dataChangeHandler) {
111 this.dataChangeHandler = dataChangeHandler;
116 public RowHandle<String> getHandle(String row) {
117 // TODO Auto-generated method stub (henrik paul: 17.6.)
123 protected void init() {
124 Grid<String> grid = getWidget();
125 grid.setSelectionMode(Grid.SelectionMode.NONE);
127 // Generated some column data
128 List<String> columnData = new ArrayList<String>();
129 for (int i = 0; i < 100; i++) {
130 columnData.add(String.valueOf(i));
133 // Provide data as data source
134 if (Location.getParameter("latency") != null) {
135 grid.setDataSource(new DelayedDataSource(
136 new ListDataSource<String>(columnData),
137 Integer.parseInt(Location.getParameter("latency"))));
139 grid.setDataSource(new ListDataSource<String>(columnData));
142 // Add a column to display the data in
143 Grid.Column<String, String> c = createColumnWithRenderer(
144 Renderers.TEXT_RENDERER);
146 grid.getDefaultHeaderRow().getCell(c).setText("Column 1");
148 // Add another column with a custom complex renderer
149 c = createColumnWithRenderer(Renderers.CPLX_RENDERER);
151 grid.getDefaultHeaderRow().getCell(c).setText("Column 2");
153 // Add method for testing sort event firing
154 grid.addSortHandler(new SortHandler<String>() {
156 public void sort(SortEvent<String> event) {
157 Element console = Document.get()
158 .getElementById("testDebugConsole");
159 String text = "Client-side sort event received<br>"
160 + "Columns: " + event.getOrder().size() + ", order: ";
161 for (SortOrder order : event.getOrder()) {
162 String columnHeader = getWidget().getDefaultHeaderRow()
163 .getCell(order.getColumn()).getText();
164 text += columnHeader + ": "
165 + order.getDirection().toString();
167 console.setInnerHTML(text);
172 registerRpc(GridClientColumnRendererRpc.class,
173 new GridClientColumnRendererRpc() {
176 public void addColumn(Renderers renderer) {
178 Grid.Column<?, String> column;
179 if (renderer == Renderers.NUMBER_RENDERER) {
180 column = createNumberColumnWithRenderer(renderer);
181 } else if (renderer == Renderers.DATE_RENDERER) {
182 column = createDateColumnWithRenderer(renderer);
184 column = createColumnWithRenderer(renderer);
186 getWidget().addColumn(column);
188 getWidget().getDefaultHeaderRow().getCell(column)
189 .setText("Column " + String.valueOf(
190 getWidget().getColumnCount() + 1));
194 public void detachAttach() {
197 HasWidgets parent = (HasWidgets) getWidget()
199 parent.remove(getWidget());
202 parent.add(getWidget());
206 public void triggerClientSorting() {
207 getWidget().sort(Sort.by(getWidget().getColumn(0)));
211 @SuppressWarnings("unchecked")
212 public void triggerClientSortingTest() {
213 Grid<String> grid = getWidget();
214 ListSorter<String> sorter = new ListSorter<String>(
217 // Make sorter sort the numbers in natural order
218 sorter.setComparator(
219 (Grid.Column<String, String>) grid.getColumn(0),
220 new Comparator<String>() {
222 public int compare(String o1, String o2) {
223 return Integer.parseInt(o1)
224 - Integer.parseInt(o2);
228 // Sort along column 0 in ascending order
229 grid.sort(grid.getColumn(0));
231 // Remove the sorter once we're done
232 sorter.removeFromGrid();
236 @SuppressWarnings("unchecked")
237 public void shuffle() {
238 Grid<String> grid = getWidget();
239 ListSorter<String> shuffler = new ListSorter<String>(
242 // Make shuffler return random order
243 shuffler.setComparator(
244 (Grid.Column<String, String>) grid.getColumn(0),
245 new Comparator<String>() {
247 public int compare(String o1, String o2) {
248 return com.google.gwt.user.client.Random
253 // "Sort" (actually shuffle) along column 0
254 grid.sort(grid.getColumn(0));
256 // Remove the shuffler when we're done so that it
257 // doesn't interfere with further operations
258 shuffler.removeFromGrid();
264 * Creates a a renderer for a {@link Renderers}
266 private Renderer createRenderer(Renderers renderer) {
269 return new TextRenderer();
271 case WIDGET_RENDERER:
272 return new WidgetRenderer<String, Button>() {
275 public Button createWidget() {
276 final Button button = new Button("");
277 button.addClickHandler(new ClickHandler() {
280 public void onClick(ClickEvent event) {
281 button.setText("Clicked");
288 public void render(RendererCellReference cell, String data,
290 button.setHTML(data);
295 return new HtmlRenderer() {
298 public void render(RendererCellReference cell,
300 super.render(cell, "<b>" + htmlString + "</b>");
304 case NUMBER_RENDERER:
305 return new NumberRenderer();
308 return new DateRenderer();
311 return new ComplexRenderer<String>() {
314 public void init(RendererCellReference cell) {
318 public void render(RendererCellReference cell, String data) {
319 cell.getElement().setInnerHTML("<span>" + data + "</span>");
320 cell.getElement().getStyle().clearBackgroundColor();
324 public void setContentVisible(RendererCellReference cell,
327 // Visualize content visible property
328 cell.getElement().getStyle()
329 .setBackgroundColor(hasData ? "green" : "red");
331 super.setContentVisible(cell, hasData);
335 public boolean onActivate(CellReference<?> cell) {
336 cell.getElement().setInnerHTML("<span>Activated!</span>");
342 return new TextRenderer();
346 private Grid.Column<String, String> createColumnWithRenderer(
347 Renderers renderer) {
348 return new Grid.Column<String, String>(createRenderer(renderer)) {
351 public String getValue(String row) {
357 private Grid.Column<Number, String> createNumberColumnWithRenderer(
358 Renderers renderer) {
359 return new Grid.Column<Number, String>(createRenderer(renderer)) {
362 public Number getValue(String row) {
363 return Long.parseLong(row);
368 private Grid.Column<Date, String> createDateColumnWithRenderer(
369 Renderers renderer) {
370 return new Grid.Column<Date, String>(createRenderer(renderer)) {
373 public Date getValue(String row) {
380 public Grid<String> getWidget() {
381 return (Grid<String>) super.getWidget();