1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
package com.itmill.toolkit.terminal.gwt.client.ui;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.itmill.toolkit.terminal.gwt.client.Client;
import com.itmill.toolkit.terminal.gwt.client.Paintable;
import com.itmill.toolkit.terminal.gwt.client.UIDL;
public class ITablePaging extends Composite implements ITable, Paintable, ClickListener {
private Grid tBody = new Grid();
private Button nextPage = new Button(">");
private Button prevPage = new Button("<");
private Button firstPage = new Button("<<");
private Button lastPage = new Button(">>");
private int pageLength = 15;
private boolean rowHeaders = false;
private Map columnOrder = new HashMap();
private Client client;
private String id;
private boolean immediate = false;
private int selectMode = ITable.SELECT_MODE_NONE;
private Vector selectedRowKeys = new Vector();
private int totalRows;
private HashMap columnWidths = new HashMap();
private HashMap visibleColumns = new HashMap();
private int rows;
private int firstRow;
private boolean sortAscending = true;
private HorizontalPanel pager;
public HashMap rowKeysToTableRows = new HashMap();
public ITablePaging() {
tBody.setStyleName("itable-tbody");
VerticalPanel panel = new VerticalPanel();
pager = new HorizontalPanel();
pager.add(firstPage);
firstPage.addClickListener(this);
pager.add(prevPage);
prevPage.addClickListener(this);
pager.add(nextPage);
nextPage.addClickListener(this);
pager.add(lastPage);
lastPage.addClickListener(this);
panel.add(pager);
panel.add(tBody);
initWidget(panel);
}
public void updateFromUIDL(UIDL uidl, Client client) {
if (client.updateComponent(this, uidl, true))
return;
this.client = client;
this.id = uidl.getStringAttribute("id");
this.immediate = uidl.getBooleanAttribute("immediate");
this.totalRows = uidl.getIntAttribute("totalrows");
this.pageLength = uidl.getIntAttribute("pagelength");
this.firstRow = uidl.getIntAttribute("firstrow");
this.rows = uidl.getIntAttribute("rows");
if(uidl.hasAttribute("selectmode")) {
if(uidl.getStringAttribute("selectmode").equals("multi"))
selectMode = ITable.SELECT_MODE_MULTI;
else
selectMode = ITable.SELECT_MODE_SINGLE;
if(uidl.hasAttribute("selected")) {
Set selectedKeys = uidl.getStringArrayVariableAsSet("selected");
selectedRowKeys.clear();
for(Iterator it = selectedKeys.iterator();it.hasNext();)
selectedRowKeys.add((String) it.next());
}
}
if(uidl.hasVariable("sortascending"))
this.sortAscending = uidl.getBooleanVariable("sortascending");
if(uidl.hasAttribute("rowheaders"))
rowHeaders = true;
UIDL columnInfo = null;
UIDL rowData = null;
for(Iterator it = uidl.getChildIterator(); it.hasNext();) {
UIDL c = (UIDL) it.next();
if(c.getTag().equals("cols"))
columnInfo = c;
else if(c.getTag().equals("rows"))
rowData = c;
else if(c.getTag().equals("actions"))
updateActionMap(c);
else if(c.getTag().equals("visiblecolumns"))
updateVisibleColumns(c);
}
tBody.resize(rows+1, columnInfo.getChidlCount() + (rowHeaders ? 1 : 0 ));
updateHeader(columnInfo);
updateBody(rowData);
updatePager();
}
private void updateVisibleColumns(UIDL c) {
Iterator it = c.getChildIterator();
int count = 0;
visibleColumns.clear();
while(it.hasNext()) {
count++;
UIDL col = (UIDL) it.next();
visibleColumns.put(col.getStringAttribute("cid"), col.getStringAttribute("caption"));
}
}
private void updateActionMap(UIDL c) {
// TODO Auto-generated method stub
}
private void updateHeader(UIDL uidl) {
if(uidl == null)
return;
int colIndex = (rowHeaders ? 1 : 0);
for(Iterator it = uidl.getChildIterator();it.hasNext();) {
UIDL col = (UIDL) it.next();
String cid = col.getStringAttribute("cid");
tBody.setWidget(0, colIndex, new HeaderCell(cid, col.getStringAttribute("caption")));
colIndex++;
}
}
/**
* Updates row data from uidl. UpdateFromUIDL delegates updating
* tBody to this method.
*
* Updates may be to different part of tBody, depending on update type.
* It can be initial row data, scroll up, scroll down...
*
* @param uidl which contains row data
*/
private void updateBody(UIDL uidl) {
Iterator it = uidl.getChildIterator();
int curRowIndex = 1;
while(it.hasNext()){
UIDL rowUidl = (UIDL) it.next();
TableRow row = new TableRow(
curRowIndex,
String.valueOf(rowUidl.getIntAttribute("key")),
rowUidl.hasAttribute("selected"));
int colIndex = 0;
if(rowHeaders) {
tBody.setWidget(curRowIndex, colIndex,
new BodyCell(row, rowUidl.getStringAttribute("caption")));
colIndex++;
}
Iterator cells = rowUidl.getChildIterator();
while(cells.hasNext()) {
Object cell = cells.next();
if (cell instanceof String) {
tBody.setWidget(curRowIndex, colIndex,
new BodyCell(row, (String) cell));
} else {
Widget cellContent = client.getWidget((UIDL) cell);
BodyCell bodyCell = new BodyCell(row);
bodyCell.setWidget(cellContent);
tBody.setWidget(curRowIndex, colIndex, bodyCell);
}
colIndex++;
}
curRowIndex++;
}
}
private void updatePager() {
if(pageLength == 0) {
pager.setVisible(false);
return;
}
if(isFirstPage()) {
firstPage.setEnabled(false);
prevPage.setEnabled(false);
} else {
firstPage.setEnabled(true);
prevPage.setEnabled(true);
}
if(hasNextPage()) {
nextPage.setEnabled(true);
lastPage.setEnabled(true);
} else {
nextPage.setEnabled(false);
lastPage.setEnabled(false);
}
}
private boolean hasNextPage() {
if(firstRow + rows + 1 > totalRows)
return false;
return true;
}
private boolean isFirstPage() {
if(firstRow == 0)
return true;
return false;
}
public void onClick(Widget sender) {
if (sender instanceof Button) {
if(sender == firstPage)
client.updateVariable(this.id, "firstvisible", 0, true);
else if(sender == nextPage)
client.updateVariable(this.id, "firstvisible", firstRow + pageLength, true);
else if(sender == prevPage) {
int newFirst = firstRow - pageLength;
if(newFirst < 0)
newFirst = 0;
client.updateVariable(this.id, "firstvisible", newFirst, true);
} else if (sender == lastPage) {
client.updateVariable(this.id, "firstvisible", totalRows - pageLength, true);
}
}
if (sender instanceof HeaderCell) {
HeaderCell hCell = (HeaderCell) sender;
client.updateVariable(this.id, "sortcolumn", hCell.getCid(), false);
client.updateVariable(this.id, "sortascending", ( sortAscending ? false : true ), true);
}
}
private class HeaderCell extends HTML {
private String cid;
public String getCid() {
return cid;
}
public void setCid(String pid) {
this.cid = pid;
}
HeaderCell(String pid, String caption) {
super();
this.cid = pid;
addClickListener(ITablePaging.this);
setText(caption);
// TODO remove debug color
DOM.setStyleAttribute(getElement(), "color", "brown");
DOM.setStyleAttribute(getElement(), "font-weight", "bold");
}
}
/**
* Abstraction of table cell content. In needs to know on which row it
* is in case of context click.
*
* @author mattitahvonen
*/
public class BodyCell extends SimplePanel {
private TableRow row;
public BodyCell(TableRow row) {
super();
this.sinkEvents(Event.BUTTON_LEFT | Event.BUTTON_RIGHT);
this.row = row;
}
public BodyCell(TableRow row2, String textContent) {
super();
this.sinkEvents(Event.BUTTON_LEFT | Event.BUTTON_RIGHT);
this.row = row2;
setWidget(new Label(textContent));
}
public void onBrowserEvent(Event event) {
System.out.println("CEll event: " + event.toString());
switch (DOM.eventGetType(event)) {
case Event.BUTTON_RIGHT:
row.showContextMenu(event);
Window.alert("context menu un-implemented");
DOM.eventCancelBubble(event, true);
break;
case Event.BUTTON_LEFT:
if(ITablePaging.this.selectMode > ITable.SELECT_MODE_NONE)
row.toggleSelected();
break;
default:
break;
}
super.onBrowserEvent(event);
}
}
private class TableRow {
private String key;
private int rowIndex;
private boolean selected = false;
public TableRow(int rowIndex, String rowKey, boolean selected) {
ITablePaging.this.rowKeysToTableRows.put(rowKey, this);
this.rowIndex = rowIndex;
this.key = rowKey;
setSelected(selected);
}
/**
* This method is used to set row status. Does not change value on server.
* @param selected
*/
public void setSelected(boolean sel) {
this.selected = sel;
if(selected) {
selectedRowKeys.add(key);
DOM.setStyleAttribute(
ITablePaging.this.tBody.getRowFormatter().getElement(rowIndex),
"background", "yellow");
} else {
selectedRowKeys.remove(key);
DOM.setStyleAttribute(
ITablePaging.this.tBody.getRowFormatter().getElement(rowIndex),
"background", "transparent");
}
}
public void setContextMenuOptions(HashMap options) {
}
/**
* Toggles rows select state. Also updates state to server according to tables immediate flag.
*
*/
public void toggleSelected() {
if(selected) {
setSelected(false);
} else {
if(ITablePaging.this.selectMode == ITable.SELECT_MODE_SINGLE) {
ITablePaging.this.deselectAll();
}
setSelected(true);
}
client.updateVariable(id, "selected", selectedRowKeys.toArray(), immediate);
}
/**
* Shows context menu for this row.
*
* @param event Event which triggered context menu. Correct place for context menu can be determined with it.
*/
public void showContextMenu(Event event) {
System.out.println("TODO: Show context menu");
}
}
public void deselectAll() {
Object[] keys = selectedRowKeys.toArray();
for (int i = 0; i < keys.length; i++) {
TableRow tableRow = (TableRow) rowKeysToTableRows.get(keys[i]);
if(tableRow != null)
tableRow.setSelected(false);
}
// still ensure all selects are removed from
selectedRowKeys.clear();
}
}
|