]> source.dussan.org Git - vaadin-framework.git/blob
b7d6e03596522a1f870e68f90acca56c5bd7d3ab
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2016 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.v7.grid;
17
18 import java.util.ArrayList;
19 import java.util.Comparator;
20 import java.util.Date;
21 import java.util.List;
22
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;
53
54 @Connect(GridClientColumnRenderers.GridController.class)
55 public class GridClientColumnRendererConnector
56         extends AbstractComponentConnector {
57
58     public static enum Renderers {
59         TEXT_RENDERER, WIDGET_RENDERER, HTML_RENDERER, NUMBER_RENDERER, DATE_RENDERER, CPLX_RENDERER;
60     }
61
62     /**
63      * Datasource for simulating network latency
64      */
65     private class DelayedDataSource implements DataSource<String> {
66
67         private DataSource<String> ds;
68         private int firstRowIndex = -1;
69         private int numberOfRows;
70         private DataChangeHandler dataChangeHandler;
71         private int latency;
72
73         public DelayedDataSource(DataSource<String> ds, int latency) {
74             this.ds = ds;
75             this.latency = latency;
76         }
77
78         @Override
79         public void ensureAvailability(final int firstRowIndex,
80                 final int numberOfRows) {
81             new Timer() {
82
83                 @Override
84                 public void run() {
85                     DelayedDataSource.this.firstRowIndex = firstRowIndex;
86                     DelayedDataSource.this.numberOfRows = numberOfRows;
87                     dataChangeHandler.dataUpdated(firstRowIndex, numberOfRows);
88                     dataChangeHandler.dataAvailable(firstRowIndex,
89                             numberOfRows);
90                 }
91             }.schedule(latency);
92         }
93
94         @Override
95         public String getRow(int rowIndex) {
96             if (rowIndex >= firstRowIndex
97                     && rowIndex <= firstRowIndex + numberOfRows) {
98                 return ds.getRow(rowIndex);
99             }
100             return null;
101         }
102
103         @Override
104         public int size() {
105             return ds.size();
106         }
107
108         @Override
109         public Registration addDataChangeHandler(
110                 DataChangeHandler dataChangeHandler) {
111             this.dataChangeHandler = dataChangeHandler;
112             return null;
113         }
114
115         @Override
116         public RowHandle<String> getHandle(String row) {
117             // TODO Auto-generated method stub (henrik paul: 17.6.)
118             return null;
119         }
120     }
121
122     @Override
123     protected void init() {
124         Grid<String> grid = getWidget();
125         grid.setSelectionMode(Grid.SelectionMode.NONE);
126
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));
131         }
132
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"))));
138         } else {
139             grid.setDataSource(new ListDataSource<String>(columnData));
140         }
141
142         // Add a column to display the data in
143         Grid.Column<String, String> c = createColumnWithRenderer(
144                 Renderers.TEXT_RENDERER);
145         grid.addColumn(c);
146         grid.getDefaultHeaderRow().getCell(c).setText("Column 1");
147
148         // Add another column with a custom complex renderer
149         c = createColumnWithRenderer(Renderers.CPLX_RENDERER);
150         grid.addColumn(c);
151         grid.getDefaultHeaderRow().getCell(c).setText("Column 2");
152
153         // Add method for testing sort event firing
154         grid.addSortHandler(new SortHandler<String>() {
155             @Override
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();
166                 }
167                 console.setInnerHTML(text);
168             }
169         });
170
171         // Handle RPC calls
172         registerRpc(GridClientColumnRendererRpc.class,
173                 new GridClientColumnRendererRpc() {
174
175                     @Override
176                     public void addColumn(Renderers renderer) {
177
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);
183                         } else {
184                             column = createColumnWithRenderer(renderer);
185                         }
186                         getWidget().addColumn(column);
187
188                         getWidget().getDefaultHeaderRow().getCell(column)
189                                 .setText("Column " + String.valueOf(
190                                         getWidget().getColumnCount() + 1));
191                     }
192
193                     @Override
194                     public void detachAttach() {
195
196                         // Detach
197                         HasWidgets parent = (HasWidgets) getWidget()
198                                 .getParent();
199                         parent.remove(getWidget());
200
201                         // Re-attach
202                         parent.add(getWidget());
203                     }
204
205                     @Override
206                     public void triggerClientSorting() {
207                         getWidget().sort(Sort.by(getWidget().getColumn(0)));
208                     }
209
210                     @Override
211                     @SuppressWarnings("unchecked")
212                     public void triggerClientSortingTest() {
213                         Grid<String> grid = getWidget();
214                         ListSorter<String> sorter = new ListSorter<String>(
215                                 grid);
216
217                         // Make sorter sort the numbers in natural order
218                         sorter.setComparator(
219                                 (Grid.Column<String, String>) grid.getColumn(0),
220                                 new Comparator<String>() {
221                                     @Override
222                                     public int compare(String o1, String o2) {
223                                         return Integer.parseInt(o1)
224                                                 - Integer.parseInt(o2);
225                                     }
226                                 });
227
228                         // Sort along column 0 in ascending order
229                         grid.sort(grid.getColumn(0));
230
231                         // Remove the sorter once we're done
232                         sorter.removeFromGrid();
233                     }
234
235                     @Override
236                     @SuppressWarnings("unchecked")
237                     public void shuffle() {
238                         Grid<String> grid = getWidget();
239                         ListSorter<String> shuffler = new ListSorter<String>(
240                                 grid);
241
242                         // Make shuffler return random order
243                         shuffler.setComparator(
244                                 (Grid.Column<String, String>) grid.getColumn(0),
245                                 new Comparator<String>() {
246                                     @Override
247                                     public int compare(String o1, String o2) {
248                                         return com.google.gwt.user.client.Random
249                                                 .nextInt(3) - 1;
250                                     }
251                                 });
252
253                         // "Sort" (actually shuffle) along column 0
254                         grid.sort(grid.getColumn(0));
255
256                         // Remove the shuffler when we're done so that it
257                         // doesn't interfere with further operations
258                         shuffler.removeFromGrid();
259                     }
260                 });
261     }
262
263     /**
264      * Creates a a renderer for a {@link Renderers}
265      */
266     private Renderer createRenderer(Renderers renderer) {
267         switch (renderer) {
268         case TEXT_RENDERER:
269             return new TextRenderer();
270
271         case WIDGET_RENDERER:
272             return new WidgetRenderer<String, Button>() {
273
274                 @Override
275                 public Button createWidget() {
276                     final Button button = new Button("");
277                     button.addClickHandler(new ClickHandler() {
278
279                         @Override
280                         public void onClick(ClickEvent event) {
281                             button.setText("Clicked");
282                         }
283                     });
284                     return button;
285                 }
286
287                 @Override
288                 public void render(RendererCellReference cell, String data,
289                         Button button) {
290                     button.setHTML(data);
291                 }
292             };
293
294         case HTML_RENDERER:
295             return new HtmlRenderer() {
296
297                 @Override
298                 public void render(RendererCellReference cell,
299                         String htmlString) {
300                     super.render(cell, "<b>" + htmlString + "</b>");
301                 }
302             };
303
304         case NUMBER_RENDERER:
305             return new NumberRenderer();
306
307         case DATE_RENDERER:
308             return new DateRenderer();
309
310         case CPLX_RENDERER:
311             return new ComplexRenderer<String>() {
312
313                 @Override
314                 public void init(RendererCellReference cell) {
315                 }
316
317                 @Override
318                 public void render(RendererCellReference cell, String data) {
319                     cell.getElement().setInnerHTML("<span>" + data + "</span>");
320                     cell.getElement().getStyle().clearBackgroundColor();
321                 }
322
323                 @Override
324                 public void setContentVisible(RendererCellReference cell,
325                         boolean hasData) {
326
327                     // Visualize content visible property
328                     cell.getElement().getStyle()
329                             .setBackgroundColor(hasData ? "green" : "red");
330
331                     super.setContentVisible(cell, hasData);
332                 }
333
334                 @Override
335                 public boolean onActivate(CellReference<?> cell) {
336                     cell.getElement().setInnerHTML("<span>Activated!</span>");
337                     return true;
338                 }
339             };
340
341         default:
342             return new TextRenderer();
343         }
344     }
345
346     private Grid.Column<String, String> createColumnWithRenderer(
347             Renderers renderer) {
348         return new Grid.Column<String, String>(createRenderer(renderer)) {
349
350             @Override
351             public String getValue(String row) {
352                 return row;
353             }
354         };
355     }
356
357     private Grid.Column<Number, String> createNumberColumnWithRenderer(
358             Renderers renderer) {
359         return new Grid.Column<Number, String>(createRenderer(renderer)) {
360
361             @Override
362             public Number getValue(String row) {
363                 return Long.parseLong(row);
364             }
365         };
366     }
367
368     private Grid.Column<Date, String> createDateColumnWithRenderer(
369             Renderers renderer) {
370         return new Grid.Column<Date, String>(createRenderer(renderer)) {
371
372             @Override
373             public Date getValue(String row) {
374                 return new Date();
375             }
376         };
377     }
378
379     @Override
380     public Grid<String> getWidget() {
381         return (Grid<String>) super.getWidget();
382     }
383 }