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  // DynamicForm.java
20  // Since: 2007/03/28
21  //
22  // $URL$ 
23  // $Author$
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   * A DynamicForm class creates a data input form organized according to a given
43   * reltaion
44   * 
45   * @author leo
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          // update the panel
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 }