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 java.util.HashMap;
28 import java.util.Map.Entry;
29
30 import org.utgenome.gwt.utgb.client.UTGBClientException;
31 import org.utgenome.gwt.utgb.client.db.datatype.DataType;
32 import org.utgenome.gwt.utgb.client.db.datatype.InputForm;
33
34 import com.google.gwt.core.client.GWT;
35 import com.google.gwt.json.client.JSONObject;
36 import com.google.gwt.user.client.ui.Composite;
37 import com.google.gwt.user.client.ui.FlexTable;
38 import com.google.gwt.user.client.ui.HorizontalPanel;
39 import com.google.gwt.user.client.ui.Label;
40
41
42
43
44
45
46
47
48 public class DynamicForm extends Composite
49 {
50 FlexTable _panel = new FlexTable();
51 Relation _relation = null;
52 HashMap<String, InputForm> _parameterNameToInputForm = new HashMap<String, InputForm>();
53
54 public DynamicForm()
55 {
56 _panel.setStyleName("form");
57 initWidget(_panel);
58 }
59
60 private boolean isValidateParameterName(String parameterName)
61 {
62 if (_parameterNameToInputForm.get(parameterName) == null)
63 {
64 GWT.log("no input form for the given parameter name, " + parameterName + ", found",
65 new UTGBClientException());
66 return false;
67 }
68 return true;
69 }
70
71 public void setValue(String parameterName, String value)
72 {
73 if (!isValidateParameterName(parameterName))
74 return;
75
76 InputForm inputForm = (InputForm) _parameterNameToInputForm.get(parameterName);
77 inputForm.setValue(value);
78 }
79
80 public String getValue(String parameterName)
81 {
82 if (!isValidateParameterName(parameterName))
83 return "";
84
85 InputForm inputForm = (InputForm) _parameterNameToInputForm.get(parameterName);
86 return inputForm.getUserInput();
87 }
88
89 public void setRelataion(Relation relation)
90 {
91 this._relation = relation;
92 reloadRelation();
93 }
94
95 public void reloadRelation()
96 {
97
98 _panel.clear();
99 _parameterNameToInputForm.clear();
100 for (DataType dt : _relation.getDataTypeList())
101 {
102 HorizontalPanel fieldPanel = new HorizontalPanel();
103
104 InputForm inputForm = dt.getInputForm();
105
106 Label label = new Label(dt.getName() + ":");
107 label.setStyleName("form-label");
108 inputForm.setStyleName("form-field");
109
110 int row = _panel.getRowCount();
111 _panel.setWidget(row, 0, label);
112 _panel.setWidget(row, 1, inputForm);
113 _parameterNameToInputForm.put(dt.getName(), inputForm);
114 }
115 }
116
117 public JSONObject getInputData()
118 {
119 JSONObject json = new JSONObject();
120 for (Entry<String, InputForm> entry : _parameterNameToInputForm.entrySet())
121 {
122 String parameterName = (String) entry.getKey();
123 InputForm inputField = (InputForm) entry.getValue();
124 json.put(parameterName, inputField.getJSONValue());
125 }
126
127 return json;
128 }
129
130 }