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 package org.utgenome.gwt.utgb.client.db;
26
27 import org.utgenome.gwt.utgb.client.UTGBClientException;
28 import org.utgenome.gwt.utgb.client.db.datatype.DataType;
29
30 import com.google.gwt.core.client.GWT;
31 import com.google.gwt.json.client.JSONArray;
32 import com.google.gwt.json.client.JSONBoolean;
33 import com.google.gwt.json.client.JSONException;
34 import com.google.gwt.json.client.JSONObject;
35 import com.google.gwt.json.client.JSONParser;
36 import com.google.gwt.json.client.JSONValue;
37 import com.google.gwt.user.client.ui.CheckBox;
38 import com.google.gwt.user.client.ui.Composite;
39 import com.google.gwt.user.client.ui.FlexTable;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class DatabaseTable extends Composite {
64 Relation _relation;
65 FlexTable _table = new FlexTable();
66 public static final int LABEL_ROW = 0;
67
68 public DatabaseTable() {
69 _table.setStyleName("table");
70 initWidget(_table);
71 }
72
73 public DatabaseTable(Relation relation) {
74 setRelation(relation);
75 _table.setStyleName("table");
76 initWidget(_table);
77 }
78
79 public void setRelation(Relation relation) {
80 this._relation = relation;
81
82
83 int columnIndex = 0;
84 for (DataType dt : _relation.getDataTypeList()) {
85 _table.setText(LABEL_ROW, columnIndex, dt.getName());
86 columnIndex++;
87 }
88 _table.getRowFormatter().setStyleName(LABEL_ROW, "table-label");
89 }
90
91 public void setTableData(String jsonData) {
92
93 while (_table.getRowCount() > 1)
94 _table.removeRow(_table.getRowCount() - 1);
95
96 try {
97 JSONValue json = JSONParser.parse(jsonData);
98 JSONObject root;
99 if ((root = json.isObject()) != null) {
100 JSONValue array = root.get("data");
101 if (array == null)
102 return;
103 JSONArray rowArray;
104 if ((rowArray = array.isArray()) != null) {
105 for (int i = 0; i < rowArray.size(); i++) {
106 addRow(rowArray.get(i));
107 }
108 }
109 }
110 }
111 catch (JSONException e) {
112 GWT.log("JSON error", e);
113 }
114 }
115
116 public void addRow(String jsonData) {
117 try {
118 JSONValue json = JSONParser.parse(jsonData);
119 addRow(json);
120 }
121 catch (JSONException e) {
122 GWT.log("JSON error", e);
123 }
124 }
125
126 public void addRow(JSONValue rowValue) {
127 JSONObject rowData;
128
129 int row = _table.getRowCount();
130 if ((rowData = rowValue.isObject()) != null) {
131 int columnIndex = 0;
132 for (DataType dt : _relation.getDataTypeList()) {
133 JSONValue jsonValue = rowData.get(dt.getName());
134 setValue(row, columnIndex, jsonValue);
135 columnIndex++;
136 }
137 _table.getRowFormatter().setStyleName(row, "table-data");
138 }
139 else {
140 GWT.log("invalid json data", new UTGBClientException());
141 }
142 }
143
144 public void setValue(int row, int column, JSONValue value) {
145 if (value == null)
146 return;
147
148 DataType dataType = _relation.getDataType(column);
149
150 JSONBoolean bool;
151 if ((bool = value.isBoolean()) != null) {
152 CheckBox cb = new CheckBox();
153 cb.setValue(bool.booleanValue());
154 _table.setWidget(row, column, cb);
155 }
156 else {
157 _table.setText(row, column, dataType.toString(value));
158 }
159 }
160
161 }