My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
/*
* Copyright 2011 stanislawbartkowski@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.ibm.sampledb.client;

import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.RowStyles;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.ibm.sampledb.shared.GetField;
import com.ibm.sampledb.shared.GetField.FieldType;
import com.ibm.sampledb.shared.GetField.FieldValue;
import com.ibm.sampledb.shared.GetRowsInfo;
import com.ibm.sampledb.shared.IResourceType;
import com.ibm.sampledb.shared.OneRecord;
import com.ibm.sampledb.shared.RowFieldInfo;

class RecordPresentation {

private GetRowsInfo rInfo;
private final CellTable<OneRecord> table;
private final SimplePager pager;
private final ListDataProvider<OneRecord> dataProvider = new ListDataProvider<OneRecord>();
private List<OneRecord> listToDraw = null;
private OneRecord selected;

public OneRecord getSelected() {
return selected;
}

public GetRowsInfo getrInfo() {
return rInfo;
}

public CellTable<OneRecord> getTable() {
return table;
}

public List<OneRecord> getList() {
return dataProvider.getList();
}

private static native void jsAlert() /*-{
jsAlert();
}-*/;

private static native void addScript(String s) /*-{
$wnd.addScript(s);
}-*/;

private static native void addStyle(String s) /*-{
$wnd.addStyle(s);
}-*/;

private static native String callJsStringFun(String jsonFun, String paramS) /*-{
return $wnd.eval(jsonFun + '(\'' + paramS + '\')');
}-*/;

RecordPresentation(CellTable<OneRecord> table, SimplePager pager,
boolean selectable) {
this.table = table;
this.pager = pager;
dataProvider.addDataDisplay(table);
pager.setDisplay(table);
pager.setPageSize(20);
pager.setPage(0);
selected = null;
if (selectable) {
final SingleSelectionModel<OneRecord> selectionModel = new SingleSelectionModel<OneRecord>();
table.setSelectionModel(selectionModel);
selectionModel
.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
selected = selectionModel.getSelectedObject();
}
});

}
}

private class EDateColumn extends Column<OneRecord, Date> {

private final String field;

EDateColumn(DateCell d, String keyName) {
super(d);
this.field = keyName;
}

@Override
public Date getValue(OneRecord object) {
FieldValue val = GetField.getValue(field, rInfo, object);
Date d = val.getdField();
return d;
}
}

private class ENumberColumn extends Column<OneRecord, Number> {

private final RowFieldInfo f;

ENumberColumn(NumberCell d, RowFieldInfo field) {
super(d);
this.f = field;
}

@Override
public Number getValue(OneRecord object) {
Number n;
FieldValue val = GetField.getValue(f, object);
if (f.getfType() == FieldType.INTEGER) {
n = val.getiField();
} else {
n = val.getnField();
}

return n;
}
}

private class AttachClass implements ActionCell.Delegate<OneRecord> {

@Override
public void execute(OneRecord object) {
AttachmentDialog a = new AttachmentDialog(object, rInfo);
PopupDraw p = new PopupDraw(a, false);
p.draw(100, 100);
}

}

private class ActionNumberCell extends ActionCell<OneRecord> {

private final RowFieldInfo f;

ActionNumberCell(RowFieldInfo f) {
super("", new AttachClass());
this.f = f;
}

@Override
public void render(Cell.Context context, OneRecord value,
SafeHtmlBuilder sb) {
FieldValue numb = GetField.getValue(f, value);
sb.appendHtmlConstant("<strong>");
sb.append(numb.getiField());
sb.appendHtmlConstant("</strong>");
}

}

private class ButtonColumn extends Column<OneRecord, OneRecord> {

public ButtonColumn(ActionNumberCell cell) {
super(cell);
}

@Override
public OneRecord getValue(OneRecord object) {
return object;
}
}

private Column<OneRecord, ?> constructDate(RowFieldInfo fi) {
DateTimeFormat f = DateTimeFormat.getFormat("yyyy/MM/dd");
DateCell d = new DateCell(f);
return new EDateColumn(d, fi.getfId());
}

private Column<OneRecord, ?> constructNumber(RowFieldInfo f) {
NumberCell d = new NumberCell();
return new ENumberColumn(d, f);
}

private Column<OneRecord, ?> constructInteger(RowFieldInfo f) {
NumberFormat fo = NumberFormat.getFormat("#####");
NumberCell d = new NumberCell(fo);
return new ENumberColumn(d, f);
}

private Column<OneRecord, ?> constructAction(RowFieldInfo f) {
ActionNumberCell ce = new ActionNumberCell(f);
return new ButtonColumn(ce);
}

private class ETextColumn extends TextColumn<OneRecord> {

private final String field;

ETextColumn(String keyName) {
this.field = keyName;
}

@Override
public String getValue(OneRecord e) {

FieldValue val = GetField.getValue(field, rInfo, e);
String vals = val.getsField();

return vals;
}

}

private Column<OneRecord, ?> constructTextColumn(RowFieldInfo f) {
return new ETextColumn(f.getfId());
}

private class AddStyle implements RowStyles<OneRecord> {

private final String jsFun;

AddStyle(String jsFun) {
this.jsFun = jsFun;
}

@Override
public String getStyleNames(OneRecord row, int rowIndex) {
CreateJson js = new CreateJson("Employee");
for (RowFieldInfo f : rInfo.getfList()) {
String fId = f.getfId();
FieldValue v = GetField.getValue(f, row);
String val = v.getString(f);
boolean number = true;
if ((f.getfType() == FieldType.STRING)
|| (f.getfType() == FieldType.DATE)) {
number = false;
}
js.addElem(fId, val, number);
}
String jsString = js.createJsonString();
return callJsStringFun(jsFun, jsString);
}

}

void setRInfo(GetRowsInfo rInfo) {
this.rInfo = rInfo;
table.setWidth(rInfo.getResource().getTableWidth(), true);
for (RowFieldInfo f : rInfo.getfList()) {
if (f.getfDescr() == null) {
continue;
}
Column<OneRecord, ?> co = null;
switch (f.getfType()) {
case NUMBER:
co = constructNumber(f);
break;
case DATE:
co = constructDate(f);
break;
case INTEGER:
if (f.getfId().equals(IResourceType.NOATTACH)) {
co = constructAction(f);
} else {
co = constructInteger(f);
}
break;
case STRING:
co = constructTextColumn(f);
break;
}
table.addColumn(co, f.getfDescr());
table.setColumnWidth(co, f.getcSize(), Unit.PCT);
}
if (rInfo.getResource().getCssS() != null) {
addStyle(rInfo.getResource().getCssS());
}
if (rInfo.getResource().getJavaS() != null) {
addScript(rInfo.getResource().getJavaS());
}
if (rInfo.getResource().isCustomRow()) {
table.setRowStyles(new AddStyle(rInfo.getResource()
.getJsAddRowFunc()));
}
drawResList();

}

private void drawResList() {
if (rInfo == null) {
return;
}
if (listToDraw == null) {
return;
}
List<OneRecord> result = dataProvider.getList();
table.setRowCount(listToDraw.size(), true);
result.clear();
result.addAll(listToDraw);

}

void drawList(List<OneRecord> list) {
listToDraw = (List<OneRecord>) list;
drawResList();
}

}

Change log

r407 by stanislawbartkowski on Jul 6, 2011   Diff
new version with attachments
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 8380 bytes, 327 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting