View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of 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,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // BugReport Project
18  //
19  // DatabaseTable.java
20  // Since: 2007/03/28
21  //
22  // $URL$ 
23  // $Author$
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   * A DatabaseTable makes easier to display table data, the schema of which is given by a Relation object, and its raw
43   * data are described with the JSON format.
44   * 
45   * For example, a JSON string
46   * 
47   * <code>String s = "{\"data\" : [{\"name\" : \"leo\", \"id\" : 1 }, {\"name\" : \"ahsan\", \"id\" : 2 }]}"
48   * 
49   * holds two persons' data : (leo, 1) and (ahsan, 2).
50   * 
51   * Thus, 
52   * <code>
53   * DatabaseTable t(r);  //r = (name:string, id:integer)
54   * t.setTableData(s);
55   * </code>
56   * 
57   * will provide the following table: ----------------- | name | id | ----------------- | leo | 1 | | ahsan | 2 |
58   * -----------------
59   * 
60   * @author leo
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  		// set table labels
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  		// clear the row data
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 }